Hi,
I am not sure about your code, but there is easier ways to handle it. You can define your custom Dataset class and have all data in each iteration just by setting batch_size=len(data) and you no longer have to change your code for different purposes. And also, you have to implement __len__ function too.
Here is conventional implementation of custom dataset class:

  
    
    
    Custom Dataset labeling from CSV vision
  
  
    Hi, 
You can define your own custom dataset class easily to handle this kind of situations. 
Here is the top-level structure of the class your can implement: 

class PlacesDataset(Dataset):
  def __init__():
     # initialize variables such is path to csv file and images and transforms
  def __len__():
    # here you just need to return a single integer number as the length of your dataset, in your 
    #  case, number of images in your train folder or lines in csv file
  def __getitem__(): 
   …
  


After creating such a Dataset, you can use it for different purposes like this:
train_dataset = PlacesDataset(txt_path=args.txt,
                              img_dir=args.img,
                              transform=custom_transforms)

train_loader = DataLoader(dataset=train_dataset,
                          batch_size=args.bs,
                          shuffle=True,
                          num_workers=args.nw,
                          pin_memory=pin_memory)


bests