I am preparing data for an RNN, the data point in the dataset is a list containing 11 RGB images, thus the dimension of “out” in the following is 11 * 3 * 224 * 224:
def getitem(self,index):
topk_s = self.tops[index]
if self.shuffle:
topk_s = random.sample(topk_s,len(topk_s))
out = []
for i in topk_s:
img = Image.open(os.path.join(self.slides, self.grid[i])).convert('RGB')
if self.mult != 1:
img = img.resize((224,224), Image.BILINEAR)
if self.transform is not None:
img = self.transform(img)
out.append(img)
return out
the dimension of dataset is:
(614, 11, 3, 224, 224)
But after making a dataloader, the batch size (128) is swapped to 2nd dimension, why would this happen?
Thank you! I learned a lot from your answer to simplify a tricky question!
class MyDataset(data.Dataset):
def __init__(self):
pass
def __len__(self):
return 614
def __getitem__(self, index):
out = []
for i in range(11):
out.append(torch.randn(3, 224, 224))
return out
dataset = MyDataset()
x = dataset[0]
print(len(x),len(x[0]),len(x[0][0]),len(x[0][0][0]))
loader = torch.utils.data.DataLoader(dataset, batch_size=128)
for i, data1 in enumerate(loader):
print(len(data1),len(data1[0]),len(data1[0][0]),len(data1[0][0][0]),len(data1[0][0][0][0]))
If you are returning a list from the __getitem__ PyTorch will keep it and x as well as data1 will both be lists containing the tensors. If you out = torch.stack(out) the list in __getitem__ the same output shapes seem in my code snippet will be returned.