Skip to content

Instantly share code, notes, and snippets.

@johncf
Last active November 15, 2023 19:20
Show Gist options
  • Save johncf/4f74f73217653fe34b2a8586a1ad50b6 to your computer and use it in GitHub Desktop.
Save johncf/4f74f73217653fe34b2a8586a1ad50b6 to your computer and use it in GitHub Desktop.

Revisions

  1. johncf revised this gist Nov 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion imagenet-extract-val.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    import pickle
    from zipfile import ZipFile

    # from https://github.com/onnx/models/blob/main/vision/classification/imagenet_val_maps.pklz
    # 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"
  2. johncf revised this gist Nov 15, 2023. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions imagenet-extract-val.py
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,14 @@
    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.zip', 'r') as zf:
    with ZipFile('imagenet-val.zip', 'w') as ozf:
    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 == "ILSVRC/ImageSets/CLS-LOC/val.txt":
    ozf.writestr("ILSVRC/val.txt", 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))
  3. johncf created this gist Nov 15, 2023.
    24 changes: 24 additions & 0 deletions imagenet-extract-val.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import os.path
    import gzip
    import pickle
    from zipfile import ZipFile

    # from https://github.com/onnx/models/blob/main/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.zip', 'r') as zf:
    with ZipFile('imagenet-val.zip', 'w') as ozf:
    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 == "ILSVRC/ImageSets/CLS-LOC/val.txt":
    ozf.writestr("ILSVRC/val.txt", zf.read(path))