Update:
Hi Again, So I went with the backup approach i.e. creating a text file with paths. I am sharing it here if others also end in a similar situation. Can someone tell me if there is any way to improve the efficiency. I am concerned with the fact that I have to open my text file each time i need to get a pic. So for each epoch it has to open (batch_size*number of batches)  times so in total number of samples * epochs, will this affect the computational time, if so how adversely. Can someone suggest any modification that can solve the problem? Thanks!
class Flowers(Dataset):

  def __init__(self,path,json_path=None,transform=None):

    self.path=path

    self.json_path=json_path

    self.transform=transform

    self.textmaker()


  def textmaker(self):

    f= open("train.txt","w+")

    for species in os.listdir(self.path):    

          spec_folder = os.path.join(self.path,species)

          for image in os.listdir(spec_folder):

            image_path=os.path.join(spec_folder,image)

            line=str(image_path)+','+str(species)+'\n'

            f.write(line)

    f.close()

  def __getitem__(self,idx):

    text = open("train.txt")

    lines=text.readlines()

    line=lines[idx]

    image_path=line.split(",")[0]

    label=line.split(",")[1][:-1]

    label=torch.tensor(int(label))

    image = cv2.imread(image_path)

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    text.close()

    if self.transform:

      image=self.transform(image=image)

    return image,label

  def __len__(self):

    N=0

    for dirpath,dirnames,filenames in os.walk(self.path):

      n=len(filenames)

      N+=n

    return N