May I ask what is the cause of the error and how can I correct it?
Could you create an instance of your ImageDataset
s and check their length via print(len(dataset))
?
Based on the code I assume you might be passing the wrong path and thus no images are found.
After modifying the path, I still get the same error. There are pictures under this path. In addition, where should I print the length of the data set?
# Training data loader
dataloader = DataLoader(
ImageDataset("F:/CycleGAN/datasets/datasets-master/horse2zebra/train/%s" % opt.dataset_name, transforms_=transforms_, unaligned=True),
batch_size=opt.batch_size,
shuffle=True,
num_workers=opt.n_cpu,
)
# Test data loader
val_dataloader = DataLoader(
ImageDataset("F:/CycleGAN/datasets/datasets-master/horse2zebra/test/%s" % opt.dataset_name, transforms_=transforms_, unaligned=True, mode="test"),
batch_size=5,
shuffle=True,
num_workers=1,
)
class ImageDataset(Dataset):
def __init__(self, root, transforms_=None, unaligned=False, mode="train"):
self.transform = transforms.Compose(transforms_)
self.unaligned = unaligned
self.files_A = sorted(glob.glob(os.path.join(root, "%s/A" % mode) + "/*.*"))
self.files_B = sorted(glob.glob(os.path.join(root, "%s/B" % mode) + "/*.*"))
def __getitem__(self, index):
image_A = Image.open(self.files_A[index % len(self.files_A)])
if self.unaligned:
image_B = Image.open(self.files_B[random.randint(0, len(self.files_B) - 1)])
else:
image_B = Image.open(self.files_B[index % len(self.files_B)])
# Convert grayscale images to rgb
if image_A.mode != "RGB":
image_A = to_rgb(image_A)
if image_B.mode != "RGB":
image_B = to_rgb(image_B)
item_A = self.transform(image_A)
item_B = self.transform(image_B)
return {"A": item_A, "B": item_B}
def __len__(self):
return max(len(self.files_A), len(self.files_B))
Just create an instance before passing it to the DataLoader
and print the length:
dataset = ImageDataset("F:/CycleGAN/datasets/datasets-master/horse2zebra/train/%s" % opt.dataset_name, transforms_=transforms_, unaligned=True)
print(len(dataset))
It seems the image files are not found properly.
You could check all files via:
print(dataset.files_A)
print(dataset.files_B)
and make sure that all necessary files are there.
Did you manage to rectify this error ?
The Solution to the problem is that folder structure is expected as follows
./dataset/horse2zebra
/train/A
/train/B
/test/A
/test/B
where A and B folder names
run the len(self) func. after creating an instance of the class. The problem is that it is unable to get the size of your sample dataset. Calling it before the dataloader() provide it with that. There are many ways to initiate the len() function of the class, you can also use indexing or simply call len()