I do not think there is a way to do this using PyTorch’s dataloader, but you can always write a custom data loading function of your own which can do the same. A simple way I can think of to do this is shown below:
'''the loadBatch function below expects three arguments, list containing image paths, list of labels, and a start index, this index can be used to access the batches in the exact order you want, for example index [0:batchSize] can be batch 1, [batchSize:2*batchSize] can be batch 2, and so on'''
batchSize = 20 #assuming sample batch size of 20
def loadBatch(imagePath, imageLabels, batchID):
images = imagePath[batchID:batchID+batchSize]
labels = imageLabels[batchID:batchID+batchSize]
imageFiles = []
for image in images:
i = cv2.imread(image, i)
#insert augmentation and resizing functions here to make sure all images have same size
i = np.transpose(i, (2,0,1)) #converting HWC to CHW
imageFiles.append(i)
imageFiles = np.array(imageFiles)
imageFiles = torch.from_numpy(imageFiles)
labels = torch.from_numpy(labels)
return imageFiles, labels
The above function can now be used load the batches in any order you want given that you provide the batchID properly. For example lets take the following example:
batch 1: start index=0
batch 2: start index = 20 since we assumed batch size of 20
batch 3: start index = 40 and so on
And for the training part we wish to load the batches in the following order:
Epoch 1: batch 1, batch 3, batch 2, batch 4
Epoch 2: batch 3, batch 2, batch 4, batch 1
So we will create a list of lists which has the order of starting indices for these batches,
#you can create a custom order generator as per your own need to create the list
orderOfBatch = [[0, 40, 20, 60], [40, 20, 60, 0]]
for epoch in range(0,2):
batchindices = orderOfBatch[epoch]
for batchIndex in batchindices:
images, labels = loadBatch(imagePath, imageLabels, batchIndex)
#training code goes here
While this is not the most efficient way, it can surely get the task done.