Skip to content

Instantly share code, notes, and snippets.

@nstrodt
Created July 25, 2019 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nstrodt/bd270131160f02564f0165e888976471 to your computer and use it in GitHub Desktop.
Save nstrodt/bd270131160f02564f0165e888976471 to your computer and use it in GitHub Desktop.
Create the Tiny ImageNet Full-Sized (TIFS) Dataset used in http://arxiv.org/abs/1906.00735
from pathlib import Path
import argparse
import os
import random
import shutil
parser = argparse.ArgumentParser()
parser.add_argument('--loc', type=lambda loc: Path(loc), required=True,
help='Location of ILSVRC2012 dataset')
parser.add_argument('--out', type=lambda loc: Path(loc), required=True,
help='Location of newly generated TIFS dataset')
parser.add_argument('--seed', type=int, help='Seed for subset generation')
TINY_CLASSES = ['n03355925', 'n03992509', 'n02423022', 'n06596364',
'n07920052', 'n02437312', 'n01910747', 'n07614500',
'n09332890', 'n01882714', 'n02364673', 'n02190166',
'n12267677', 'n02917067', 'n03599486', 'n04099969',
'n07747607', 'n02410509', 'n02279972', 'n02094433',
'n02948072', 'n02124075', 'n02509815', 'n02123045',
'n01917289', 'n03649909', 'n07753592', 'n02699494',
'n02815834', 'n02906734', 'n04596742', 'n02233338',
'n04067472', 'n03447447', 'n04507155', 'n02666196',
'n07720875', 'n03393912', 'n02808440', 'n01742172',
'n04328186', 'n02927161', 'n02843684', 'n03837869',
'n03042490', 'n01644900', 'n04597913', 'n02395406',
'n02963159', 'n02226429', 'n03838899', 'n02132136',
'n02795169', 'n02481823', 'n03388043', 'n04285008',
'n03980874', 'n04532670', 'n02977058', 'n02480495',
'n02099712', 'n03854065', 'n02002724', 'n04265275',
'n01784675', 'n03733131', 'n03255030', 'n02403003',
'n02165456', 'n09193705', 'n02415577', 'n04399382',
'n04417672', 'n07749582', 'n03977966', 'n04501370',
'n09246464', 'n04023962', 'n03250847', 'n02999410',
'n02321529', 'n03983396', 'n07875152', 'n02769748',
'n01774750', 'n02074367', 'n01629819', 'n04008634',
'n04398044', 'n07871810', 'n07768694', 'n03444034',
'n02085620', 'n02788148', 'n02125311', 'n01641577',
'n07873807', 'n03976657', 'n02814860', 'n04366367',
'n02504458', 'n02988304', 'n02814533', 'n01770393',
'n03902125', 'n02802426', 'n04456115', 'n07579787',
'n03089624', 'n04532106', 'n02950826', 'n03930313',
'n04254777', 'n02056570', 'n01768244', 'n03662601',
'n02486410', 'n04070727', 'n02268443', 'n03100240',
'n01774384', 'n04275548', 'n01984695', 'n04149813',
'n01443537', 'n03404251', 'n03424325', 'n04560804',
'n03584254', 'n03796401', 'n04487081', 'n04486054',
'n02206856', 'n02129165', 'n07734744', 'n07711569',
'n02236044', 'n02730930', 'n04118538', 'n07715103',
'n03763968', 'n03670208', 'n01855672', 'n02113799',
'n03814639', 'n04251144', 'n04133789', 'n07615774',
'n02837789', 'n09256479', 'n04311004', 'n04179913',
'n03804744', 'n01944390', 'n03617480', 'n03770439',
'n02123394', 'n01698640', 'n02841315', 'n01983481',
'n03937543', 'n03891332', 'n03201208', 'n02106662',
'n04259630', 'n02099601', 'n03085013', 'n02892201',
'n02231487', 'n03400231', 'n03544143', 'n04465501',
'n01950731', 'n07695742', 'n03160309', 'n03014705',
'n03179701', 'n02058221', 'n04074963', 'n04371430',
'n02909870', 'n04562935', 'n02281406', 'n04356056',
'n03126707', 'n03970156', 'n02793495', 'n09428293',
'n02669723', 'n03706229', 'n02823428', 'n03026506',
'n04146614', 'n03637318', 'n02883205', 'n04376876',
'n07583066', 'n01945685', 'n04540053', 'n02791270']
if __name__ == '__main__':
args = parser.parse_args()
if args.seed is not None:
random.seed(args.seed)
args.loc = Path(args.loc)
for phase in ('train', 'val'):
for i, cls in enumerate(TINY_CLASSES):
path = args.loc / phase / cls
chosen_samples = random.sample(os.listdir(args.loc / phase / cls),
500 if phase == 'train' else 50)
out_path = args.out / phase /cls
os.makedirs(out_path, exist_ok=True)
for f in chosen_samples:
shutil.copy(path / f, out_path)
print(f'{phase}: ({i+1}/{len(TINY_CLASSES)}) classes generated',
end='\n' if i-1 == len(TINY_CLASSES) else '\r')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment