Last active
November 15, 2023 19:20
-
-
Save johncf/4f74f73217653fe34b2a8586a1ad50b6 to your computer and use it in GitHub Desktop.
Extract validation set from IMAGENET into another zip file, with label-directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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