Datasets:

Languages:
English
ArXiv:
License:

Building dataset tree helper for pytorch users

#17
by FalsoMoralista - opened

Train dataset

Untar the tarballs into the "train" and "val" directories then use the following:

import os
import shutil

train_images = os.listdir('train/')
for image in train_images:
    split = image.split('_')
    cls_name = split[0]

    if not os.path.exists('train/' + cls_name):
        #print('creating dir: ', 'train/' + cls_name)
        os.makedirs('train/' + cls_name, exist_ok=True)

    src = 'train/' + image
    destination = 'train/' + cls_name + '/'
    #print('moving')
    #print(src)
    #print(destination)
    
    shutil.move(src, destination)

Val dataset

A minor adjustment has to be performed here because the val directories are being named as "cls_name.JPG" which might cause issues

i haven't tried but perhaps doing > cls_name = cls_name.replace(".JPG", "") might solve the issue


import os
import shutil

val_images = os.listdir('val/')
for image in val_images:
   split = image.split('_')
   cls_name = split[3]

   if not os.path.exists('val/' + cls_name):
       #print('creating dir: ', 'val/' + cls_name)
       os.makedirs('val/' + cls_name, exist_ok=True)

   src = 'val/' + image
   destination = 'val/' + cls_name + '/'
   #print('moving')
   #print(src)
   #print(destination)
   
   shutil.move(src, destination)

Sign up or log in to comment