Skip to content

Instantly share code, notes, and snippets.

@wbuchwalter
Created December 13, 2021 18:49
Show Gist options
  • Save wbuchwalter/a08a6a24bef9c5370ba312cf340ea14a to your computer and use it in GitHub Desktop.
Save wbuchwalter/a08a6a24bef9c5370ba312cf340ea14a to your computer and use it in GitHub Desktop.
import numpy as np
from os import listdir
from os.path import isfile, isdir, join
import os
import json
import random
import re
cwd = os.getcwd()
cwd = os.getcwd()
datadir = cwd.split('filelists')[0]
data_path = join(datadir,'Datasets/ILSVRC/Data/CLS-LOC/')
savedir = './'
dataset_list = ['base', 'val', 'novel']
cl = -1
folderlist = []
datasetmap = {'base':'train','val':'val','novel':'test'};
filelists = {'base':{},'val':{},'novel':{} }
filelists_flat = {'base':[],'val':[],'novel':[] }
labellists_flat = {'base':[],'val':[],'novel':[] }
for dataset in dataset_list:
with open(datasetmap[dataset] + ".csv", "r") as lines:
for i, line in enumerate(lines):
if i == 0:
continue
fid, _ , label = re.split(',|\.', line)
label = label.replace('\n','')
if not label in filelists[dataset]:
folderlist.append(label)
filelists[dataset][label] = []
fnames = listdir( join(data_path, datasetmap[dataset], label) )
fname_number = [ int(re.split('_|\.', fname)[0][1:]) for fname in fnames]
sorted_fnames = list(zip( *sorted( zip(fnames, fname_number), key = lambda f_tuple: f_tuple[1] )))[0]
print('sorting')
# import pdb; pdb.set_trace()
#fid2 = int(fid[-5:])-1
s = sorted_fnames[i%600-1]
if fid+'.jpg' != s:
print('uneq')
import pdb; pdb.set_trace()
fname = join( data_path,label, sorted_fnames[i%600-1] )
filelists[dataset][label].append(fname)
for key, filelist in filelists[dataset].items():
cl += 1
random.shuffle(filelist)
filelists_flat[dataset] += filelist
labellists_flat[dataset] += np.repeat(cl, len(filelist)).tolist()
for dataset in dataset_list:
fo = open(savedir + dataset + ".json", "w")
fo.write('{"label_names": [')
fo.writelines(['"%s",' % item for item in folderlist])
fo.seek(0, os.SEEK_END)
fo.seek(fo.tell()-1, os.SEEK_SET)
fo.write('],')
fo.write('"image_names": [')
fo.writelines(['"%s",' % item for item in filelists_flat[dataset]])
fo.seek(0, os.SEEK_END)
fo.seek(fo.tell()-1, os.SEEK_SET)
fo.write('],')
fo.write('"image_labels": [')
fo.writelines(['%d,' % item for item in labellists_flat[dataset]])
fo.seek(0, os.SEEK_END)
fo.seek(fo.tell()-1, os.SEEK_SET)
fo.write(']}')
fo.close()
print("%s -OK" %dataset)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment