Hi All,
I’m trying to create a dataloader with multiple image inputs (different resolutions related to each other). I’m not sure if I am going in the right direction. Essentially I have three related images, where they are stored in a data structure like this:
dataset — train
|------ 40x-----*png
|-------20x-----*png
|-------10x-----*png
However, I think perhaps I have an issue with indexing?
My code looks like this:
import os
import glob
import numpy as np
import random
from PIL import Image
from torch.utils import data
from torch.utils.data.dataset import Dataset
from torchvision import transforms as T
class Patches(data.Dataset):
def __init__(self, root, phase):
self.phase = phase
imgs_40x = []
imgs_20x = []
imgs_5x = []
if phase == 'train':
for path, subdirs, files in os.walk(root):
for sd in subdirs:
if sd == "20x":
twenty = os.path.join(path,sd)
for path, subdirs, files in os.walk(twenty):
for fn in files:
if fn.endswith(".png") == True:
imgs_20x.append(path + "/" + fn)
elif sd == "5x":
five = os.path.join(path,sd)
for path, subdirs, files in os.walk(five):
for fn in files:
if fn.endswith(".png") == True:
imgs_5x.append(path + "/" + fn)
elif sd == "40x":
forty = os.path.join(path,sd)
for path, subdirs, files in os.walk(forty):
for fn in files:
if fn.endswith(".png") == True:
imgs_40x.append(path + "/" + fn)
self.imgs_40x = imgs_40x
self.imgs_20x = imgs_20x
self.imgs_5x = imgs_5x
elif phase == 'val' or 'test':
for path, subdirs, files in os.walk(root):
for sd in subdirs:
if sd == "20x":
twenty = os.path.join(path,sd)
for path, subdirs, files in os.walk(twenty):
for fn in files:
if fn.endswith(".png") == True:
imgs_20x.append(path + "/" + fn)
elif sd == "5x":
five = os.path.join(path,sd)
for path, subdirs, files in os.walk(five):
for fn in files:
if fn.endswith(".png") == True:
imgs_5x.append(path + "/" + fn)
elif sd == "40x":
forty = os.path.join(path,sd)
for path, subdirs, files in os.walk(forty):
for fn in files:
if fn.endswith(".png") == True:
imgs_40x.append(path + "/" + fn)
self.imgs_40x = imgs_40x
self.imgs_20x = imgs_20x
self.imgs_5x = imgs_5x
if self.phase == 'train':
self.transforms = T.Compose([T.RandomResizedCrop(224),T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
else:
self.transforms = T.Compose([T.RandomResizedCrop(224),T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
def __getitem__(self, index):
if self.phase == 'train':
path_40x = self.imgs_40x[index]
path_20x = self.imgs_20x[index]
path_5x = self.imgs_5x[index]
data_40 = Image.open(path_40x).convert('RGB')
data_40 = self.transforms(data_40)
data_20 = Image.open(path_20x).convert('RGB')
data_20 = self.transforms(data_20)
data_5 = Image.open(path_5x).convert('RGB')
data_5 = self.transforms(data_5)
label = int(path_40x.split('/')[2])
elif self.phase == 'val' or 'test':
path_40x = self.imgs_40x[index]
path_20x = self.imgs_20x[index]
path_5x = self.imgs_5x[index]
data_40 = Image.open(path_40x).convert('RGB')
data_40 = self.transforms(data_40)
data_20 = Image.open(path_20x).convert('RGB')
data_20 = self.transforms(data_20)
data_5 = Image.open(path_5x).convert('RGB')
data_5 = self.transforms(data_5)
label = int(path_40x.split('/')[2])
return data_40, data_20, data_5, path_40x, path_20x, path_5x, label
def __len__(self):
return len(self.imgs_40x)
if __name__ == '__main__':
root = 'mults/'
train_dataset = Patches(root=root+ "train/", phase= 'train')
trainloader = data.DataLoader(train_dataset, batch_size=5)
print(len(trainloader))
for i, (data_40, data_20, data_5, path_40x, path_20x, path_5x, label) in enumerate(trainloader):
print(data_40)
Not really sure if I need to rewrite this entirely because it returns 0 for the length of the trainloader…