import os
data=[]
img_size = 48
# data = torch.empty((7000,img_size,img_size,3),dtype=torch.float32)
# label = torch.empty((7000))
def create_data():
    i = 0
    for item in (['abyssinian','american_bulldog','american_pit_bull_terrier','basset_hound',
                  'beagle','bengal','birman','bombay','boxer','british_shorthair','chihuahua',
                  'egyptian_mau','english_cocker_spaniel','english_setter','german_shorthaired',
                  'great_pyrenees','havanese','japanese_chin','keeshond','leonberger','maine_coon',
                  'miniature_pinscher','newfoundland','persian','pomeranian','pug','ragdoll','russian_blue',
                  'saint_bernard','samoyed','scottish_terrier','shiba_inu','siamese','sphynx','staffordshire_bull_terrier',
                  'wheaten_terrier','yorkshire_terrier']):
        path='drive/MyDrive/Animal_Breed/TRAIN' + item+"/"
        for img in os.listdir(path)[:]:       
            if i%1 == 0: print(i)
            new_img_array=cv2.imread(os.path.join(path,img))   
            new_img_array = cv2.cvtColor(new_img_array, cv2.COLOR_BGR2RGB)
#             print(new_img_array.shape)
            if item == 'abyssinian':
                data.append([new_img_array,0])
            elif item == 'american_bulldog':
                data.append([new_img_array,1])
            elif item == 'american_pit_bull_terrier' :
                data.append([new_img_array,2])
            elif item == 'basset_hound':
                data.append([new_img_array,3])
            elif item == 'beagle' :
                data.append([new_img_array,4])
            elif item == 'bengal':
                data.append([new_img_array,5])
            elif item == 'birman':
                data.append([new_img_array,6])
            elif item == 'bombay' :
                data.append([new_img_array,7])
            elif item == 'boxer':
                data.append([new_img_array,8])
            elif item == 'british_shorthair' :
                data.append([new_img_array,9])
            elif item == 'chihuahua':
                data.append([new_img_array,10])
            elif item == 'egyptian_mau':
                data.append([new_img_array,11])
            elif item == 'english_cocker_spaniel' :
                data.append([new_img_array,12])
            elif item == 'english_setter':
                data.append([new_img_array,13])
            elif item == 'german_shorthaired' :
                data.append([new_img_array,14])
            elif item == 'great_pyrenees':
                data.append([new_img_array,15])
            elif item == 'havanese':
                data.append([new_img_array,16])
            elif item == 'japanese_chin' :
                data.append([new_img_array,17])
            elif item == 'maine_coon':
                data.append([new_img_array,18])
            elif item == 'miniature_pinscher' :
                data.append([new_img_array,19])
            elif item == 'newfoundland':
                data.append([new_img_array,20])
            elif item == 'persian':
                data.append([new_img_array,21])
            elif item == 'pomeranian' :
                data.append([new_img_array,22])
            elif item == 'pug':
                data.append([new_img_array,23])
            elif item == 'ragdoll' :
                data.append([new_img_array,24])
            elif item == 'saint_bernard':
                data.append([new_img_array,25])
            elif item == 'keeshond':
                data.append([new_img_array,26])
            elif item == 'leonberger' :
                data.append([new_img_array,27])
            elif item == 'russian_blue':
                data.append([new_img_array,28])
            elif item == 'samoyed' :
                data.append([new_img_array,29])
            elif item == 'scottish_terrier':
                data.append([new_img_array,30])
            elif item == 'shiba_inu':
                data.append([new_img_array,31])
            elif item == 'siamese' :
                data.append([new_img_array,32])
            elif item == 'sphynx':
                data.append([new_img_array,33])
            elif item == 'staffordshire_bull_terrier' :
                data.append([new_img_array,34])
            elif item == 'wheaten_terrier':
                data.append([new_img_array,35])
            elif item == 'yorkshire_terrier':
                data.append([new_img_array,36])
            i+=1

I have done this and created whole DataLoader, It’s pretty faster, I think that’s network problem what happened in colab. But still, does ImageFolder read the data from the disk every epoch? I mean to say when I tried to train this (ImageFolder) on local machine there was lots of use of CPU and less of GPU (more time for CPU and less time for GPU), but when I created data as shown in the code above the GPU was almost fully occupied for the whole training phase.