ValueError: num_samples should be a positive integer value, but got num_samples=0

username: ky_Pa
cooked: <p>May I ask what is the cause of the error and how can I correct it?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bc54d219c5ab2d4341ba072fd5715c5e181c5f48.png" data-download-href="https://discuss.pytorch.org/uploads/default/bc54d219c5ab2d4341ba072fd5715c5e181c5f48" title="微信图片_20200407102959"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bc54d219c5ab2d4341ba072fd5715c5e181c5f48.png" alt="微信图片_20200407102959" data-base62-sha1="qS3uv7Q26SkL0kREAu4Ipe9zVyM" width="690" height="228" data-dominant-color="312E28"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200407102959</span><span class="informations">1143×379 22.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/8/8882fc96169855b9e51b703bdd6aa59fcb59ad4a.png" data-download-href="https://discuss.pytorch.org/uploads/default/8882fc96169855b9e51b703bdd6aa59fcb59ad4a" title="微信图片_20200407103037"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/8/8882fc96169855b9e51b703bdd6aa59fcb59ad4a.png" alt="微信图片_20200407103037" data-base62-sha1="jtDzyYY3CEmYt5tIuaiZgDzzV2O" width="690" height="142" data-dominant-color="383933"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200407103037</span><span class="informations">1095×226 13.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>Could you create an instance of your <code>ImageDataset</code>s and check their length via <code>print(len(dataset))</code>?<br>
Based on the code I assume you might be passing the wrong path and thus no images are found.</p>

username: ky_Pa
cooked: <p>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?</p>
<pre><code class="lang-auto"># 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,
)
</code></pre>
<pre><code class="lang-auto">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))</code></pre>

username: ptrblck
cooked: <p>Just create an instance before passing it to the <code>DataLoader</code> and print the length:</p>
<pre><code class="lang-python">dataset = ImageDataset("F:/CycleGAN/datasets/datasets-master/horse2zebra/train/%s" % opt.dataset_name, transforms_=transforms_, unaligned=True)
print(len(dataset))
</code></pre>

username: ky_Pa
cooked: <p>The length seems to be 1.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/c/3c3db480dba266d34fb6d4c99b76aabf782ec21f.png" data-download-href="https://discuss.pytorch.org/uploads/default/3c3db480dba266d34fb6d4c99b76aabf782ec21f" title="微信图片_20200407164115"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/c/3c3db480dba266d34fb6d4c99b76aabf782ec21f.png" alt="微信图片_20200407164115" data-base62-sha1="8AUQKnxERzxDdXKyF4uuvkX5AIv" width="690" height="103" data-dominant-color="32332D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200407164115</span><span class="informations">1873×282 20.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>It seems the image files are not found properly.<br>
You could check all files via:</p>
<pre><code class="lang-python">print(dataset.files_A)
print(dataset.files_B)
</code></pre>
<p>and make sure that all necessary files are there.</p>

username: ky_Pa
cooked: <p>Is this just a simple path error? My data set picture is here.<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b8d48028ab335116ab6241c2f4d896ca6e3ceba.png" data-download-href="https://discuss.pytorch.org/uploads/default/5b8d48028ab335116ab6241c2f4d896ca6e3ceba" title="微信图片_20200407171404"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b8d48028ab335116ab6241c2f4d896ca6e3ceba.png" alt="微信图片_20200407171404" data-base62-sha1="d3U8TUclxcIpsoS2k96Uk7qAjQS" width="690" height="171" data-dominant-color="F1F3F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200407171404</span><span class="informations">872×217 10.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: mayurat22
cooked: <p>Did you manage to rectify this error ?</p>

username: mayurat22
cooked: <p>The Solution to the problem is that folder structure is expected as follows<br>
./dataset/horse2zebra<br>
/train/A<br>
/train/B<br>
/test/A<br>
/test/B<br>
where A and B folder names</p>

username: Divyanshu_Dimri
cooked: <p>run the <strong>len</strong>(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()</p>