Skip to content

Instantly share code, notes, and snippets.

@johncf
Last active November 15, 2023 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johncf/4f74f73217653fe34b2a8586a1ad50b6 to your computer and use it in GitHub Desktop.
Save johncf/4f74f73217653fe34b2a8586a1ad50b6 to your computer and use it in GitHub Desktop.
Extract validation set from IMAGENET into another zip file, with label-directories
import os.path
import gzip
import pickle
from zipfile import ZipFile
# from https://github.com/onnx/models/blob/8d50e3f/vision/classification/imagenet_val_maps.pklz
with gzip.open('imagenet_val_maps.pklz', 'rb') as f:
dirs, mappings = pickle.load(f)
src_dir = "ILSVRC/Data/CLS-LOC/val"
target_dir = "ILSVRC/val"
path_map = {}
for m in mappings:
path_map[os.path.join(src_dir, m[0])] = os.path.join(target_dir, m[1], m[0])
# from https://www.kaggle.com/competitions/imagenet-object-localization-challenge/data
with ZipFile('imagenet-val.zip', 'w') as ozf:
with ZipFile('imagenet.zip', 'r') as zf:
dirs = set()
for path in zf.namelist():
if path.startswith("ILSVRC/Data/CLS-LOC/val/"):
newpath = path_map[path]
ozf.writestr(newpath, zf.read(path))
elif path == "LOC_synset_mapping.txt":
ozf.writestr(f"ILSVRC/synset_mapping.txt", zf.read(path))
elif path == "LOC_val_solution.csv":
ozf.writestr(f"ILSVRC/val_loc.csv", zf.read(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment