Wrong with dataloader?

rui_zhou: <p>File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py”, line 224, in <strong>init</strong><br>
sampler = RandomSampler(dataset, generator=generator)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/utils/data/sampler.py”, line 96, in <strong>init</strong><br>
“value, but got num_samples={}”.format(self.num_samples))<br>
ValueError: num_samples should be a positive integer value, but got num_samples=0</p>

anujd9: <p>Hi. Can you please check that the path to dataset is correct ? I had a similar issue and providing the correct path to dataset solved that.</p>

rui_zhou: <p>How can I make the generator give the data I expect, can you give me some guidance?</p>

anujd9: <p>Hi. So, I don’t think the issue is related to the generator. The generator here is basically a PyTorch generator object that manages the state of the algorithm which produces pseudo random numbers like which device to use (cpu/gpu), state of the generator, setting the seed etc. <a href="https://pytorch.org/docs/stable/generated/torch.Generator.html" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.Generator.html</a><br>
For your problem, you just need to provide the right path to the dataset.<br>
Can you provide a bit more details here like what is the <code>generator</code> in your case and what kind of <code>dataset</code> are you using?</p>

rui_zhou: <p>I am using some pictures labeled as voc dataset (JPEGImages and SegmentationClass), and I want to use it in  another model(image and mask).I put it directly into the file.How does the image convert into mask? is there any format?</p>

rui_zhou: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/c/6c30c112437e3cb330e8c189a56151410d3e377c.jpeg" alt="9" data-base62-sha1="fr61ndfmOSsMPOW0uCggJFsPy6U" width="652" height="200"> <img src="https://discuss.pytorch.org/uploads/default/original/3X/3/3/330b496c3c8f4861e73c530cd555c372feda4c42.png" alt="sampledataset" data-base62-sha1="7hyx2l9ppZ5TfgZ9pLS08xwJdLQ" width="380" height="137"><br>
my dataset and the annotated road crack image database that used in the model</p>

anujd9: <p>Hi. So the images that you have in 1, 2, 3, 4 are basically the segmentation masks. The difference in their color represents different class/label.<br>
It looks like that the model that you are using right now does handle the case when the mask is rgb instead of grayscale.<br>
Just change the mask color from grayscale to rgb if your image has 3 channels.<br>
I hope this gives some clarity.</p>

rui_zhou: <p>I change maskcolormode to rgb but the image still not load in,<br>
to apply this model what else should I do, can you give me some guidance?<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">
      <a href="https://github.com/msminhas93/DeepLabv3FineTuning" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars3.githubusercontent.com/u/6465972?s=400&amp;v=4" class="thumbnail onebox-avatar" width="400" height="400">

<h3><a href="https://github.com/msminhas93/DeepLabv3FineTuning" target="_blank" rel="noopener nofollow ugc">msminhas93/DeepLabv3FineTuning</a></h3>

<p>Tutorial on fine tuning DeepLabv3 segmentation network for your own segmentation task in PyTorch. - msminhas93/DeepLabv3FineTuning</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


rui_zhou: <p>May the reason be that only 4 images can not be divided into the train and val dataset in the network?<br>
So I try part of Voc in this model, but the size of image is not the same, how should I make them the same?</p>

anujd9: <p>Hi. Yes the number of images could be a reason depending on how you are loading them. To make the images to the same size, you can use a custom transform something like this:</p>
<pre><code class="lang-auto"># Transforms for DataLoader
class Resize(object):
    """ Resize Image and/or Masks """

    def __init__(self, height, width):
        """

        Args:
            height (int): resize to height
            width (int): resize to width
        """

        self.height = height
        self.width = width

    def __call__(self, sample):
        """

        Args:
            sample (dict): input image/mask pair

        Returns:
            dict: image/mask pair as ndarray
        """

        image, mask = sample['image'], sample['mask']
        dim = (self.width, self.height)
        image = image.resize(dim)
        mask = mask.resize(dim)

        return {'image': image,
                'mask': mask}
</code></pre>
<p><strong>NOTE</strong> that the images in the above Resize transform are loaded using <code>PIL.Image</code>.</p>
<p>Don’t forget to convert the above images to tensor by either using the <code>ToTensor</code> transform as below:</p>
<pre><code class="lang-auto"># Convert Image from Numpy to Tensor
class ToTensor(object):
    """ Convert ndarray to Tensor """

    def __call__(self, sample):
        """

        Args:
            sample (dict): input image/mask pair

        Returns:
            torch Tensor: image/mask tensor
        """

        image, mask = sample['image'], sample['mask']

        image = np.asarray(image)
        mask = np.asarray(mask)

        if len(image.shape) == 3:
            image = image.transpose(2, 0, 1)

        if len(mask.shape) == 2:
            mask = mask.reshape((1,) + mask.shape)

        if len(image.shape) == 2:
            image = image.reshape((1,) + image.shape)

        return {'image': torch.from_numpy(np.array(image)),
                'mask': torch.from_numpy(np.array(mask))}
</code></pre>
<p>Then you can setup these transforms:</p>
<pre><code class="lang-auto">transforms.Compose([Resize(330, 422), ToTensor()])
</code></pre>
<p>The reason for writing these transforms instead of using the built in ones is due to image as feature and another image as label and we need to apply transforms to both so as to get a image/mask pair.</p>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

anujd9: <p>Hi. So, you need to change your dataloader from that GitHub repository as below to make it work with the VOC segmentation dataset.</p>
<pre><code class="lang-auto">import torch
import torchvision
from torch.utils.data import Dataset
from PIL import Image
import glob
import numpy as np

class SegmentationDataset(Dataset):
    """Segmentation Dataset"""

    def __init__(self, root_dir: str, image_dir: str, mask_dir: str,
                 transform=None, seed: int = None, fraction: float = None,
                 subset: str = None, imagecolormode: str = 'rgb',
                 maskcolormode: str = 'rgb'):
        """

        Args:
            root_dir (str): dataset dir path
            image_dir (str): input image dir name
            mask_dir (str): mask image dir name
            transform: PyTorch data transform
            seed (int): random seed for reproducibility
            fraction (float): dataset train/test split percentage
            subset (str): subset from existing dataset
            imagecolormode (str): input image color mode
            maskcolormode (str): input mask color mode

        """

        self.color_dict = {'rgb': 1, 'grayscale': 0}
        assert (imagecolormode in ['rgb', 'grayscale'])
        assert (maskcolormode in ['rgb', 'grayscale'])

        self.imagecolorflag = self.color_dict[imagecolormode]
        self.maskcolorflag = self.color_dict[maskcolormode]
        self.root_dir = root_dir
        self.transform = transform
        
        if not fraction:
            # UPDATE: Get the Segmentation Masks Before Images
            self.mask_names = sorted(
                glob.glob(os.path.join(self.root_dir, mask_dir, '*')))
            
            # UPDATE: Get images with the names in the mask_names list but with updated path and '.jpg' extension
            self.image_names = sorted(
                os.path.join(self.root_dir, image_dir, fname.split('/')[4].split('.png')[0] + '.jpg')
                for fname in self.mask_names)
        else:
            assert (subset in ['Train', 'Test'])
            self.fraction = fraction

            # UPDATE: Get the Segmentation Masks Before Images
            self.mask_list = np.array(
                sorted(glob.glob(os.path.join(self.root_dir, mask_dir, '*'))))
            
            # UPDATE: Get images with the names in the mask_names list but with updated path and '.jpg' extension
            self.image_list = np.array(
                sorted(os.path.join(self.root_dir, image_dir, fname.split('/')[4].split('.png')[0] + '.jpg')
                       for fname in self.mask_list))
            
            if seed:
                np.random.seed(seed)
                indices = np.arange(len(self.image_list))
                np.random.shuffle(indices)
                self.image_list = self.image_list[indices]
                self.mask_list = self.mask_list[indices]
            if subset == 'Train':
                self.image_names = self.image_list[:int(
                    np.ceil(len(self.image_list) * (1 - self.fraction)))]
                self.mask_names = self.mask_list[:int(
                    np.ceil(len(self.mask_list) * (1 - self.fraction)))]
            else:
                self.image_names = self.image_list[int(
                    np.ceil(len(self.image_list) * (1 - self.fraction))):]
                self.mask_names = self.mask_list[int(
                    np.ceil(len(self.mask_list) * (1 - self.fraction))):]

    def __getitem__(self, idx):
        """

        Args:
            idx (int): index of input image

        Returns:
            dict: image and mask image
        """

        img_name = self.image_names[idx]
        mask_name = self.mask_names[idx]

        image = Image.open(img_name)
        mask = Image.open(mask_name)

        sample = {'image': image, 'mask': mask}

        if self.transform:
            sample = self.transform(sample)

        return sample

    def __len__(self):
        """

        Returns: length of dataset
        """

        return len(self.image_names)
</code></pre>
<p>Now you can call the above dataset class as follows:</p>
<pre><code class="lang-auto">dataset = SegmentationDataset(root_dir='./VOCdevkit/VOC2012/', image_dir='JPEGImages', mask_dir='SegmentationClass', seed=100, fraction=0.1, subset='Train')
</code></pre>
<p>and everything should work. You can test the image/mask pairs using the following code:</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt

for i,data in enumerate(dataset):
    image, mask = data['image'], data['mask']
    show_image_mask(image, mask)
    if i &gt; 5:
        break
</code></pre>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

rui_zhou: <p>So grateful for you help and awesome code!<br>
But the train not begin by running the command<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/d/7d8e3a6e19544e77f1b0e0d61324b5496dad3791.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/7d8e3a6e19544e77f1b0e0d61324b5496dad3791" title="8"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d8e3a6e19544e77f1b0e0d61324b5496dad3791_2_690x341.jpeg" alt="8" data-base62-sha1="hUIpceWJt6EbMopyPEL9AAzrOed" width="690" height="341" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d8e3a6e19544e77f1b0e0d61324b5496dad3791_2_690x341.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d8e3a6e19544e77f1b0e0d61324b5496dad3791_2_1035x511.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d8e3a6e19544e77f1b0e0d61324b5496dad3791_2_1380x682.jpeg 2x" data-dominant-color="2C3033"><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">8</span><span class="informations">1573×778 121 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><br>
May it be that the train and test dataset not splited successfully?</p>

ptrblck: <p><code>plt.imshow</code> might work, but currently you are passing an invalid image path, thus the image cannot be loaded.</p>

rui_zhou: <p>it not work within a loop when  the image path is right, it just give no feedback <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09" title="13"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09_2_690x232.jpeg" alt="13" data-base62-sha1="qIRncAtPz04aCEibKUuFglpaaYV" width="690" height="232" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09_2_690x232.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09_2_1035x348.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/b/bb4ab85e5dd81c856ce9828f0e787409ceb2eb09.jpeg 2x" data-dominant-color="262525"><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">13</span><span class="informations">1361×458 51.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>

anujd9: <p>Ok. So I instantiated the dataset line just to explain the concept and test it out, but you don’t need that. Just make changes to the <code>SegmentationDataset</code> class and that’s it. Nothing else needs to be changed in your code from that GitHub repository. It has the code to instantiate the dataset and split into train and test sets in the <code>get_dataloader_single_folder</code> function.</p>

anujd9: <p>Also up there, <code>plt.imshow()</code> takes only one image at a time. So if you just do <code>plt.imshow(image)</code>, that should work.</p>

rui_zhou: <p>So grateful. But when I only change imageFolder to image_dir in SegDataset it gives me TypeError of <strong>init</strong>(), but if I change it to imageFolder , SegDataset can not works well alone, what cause the problem and how should I define them?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c278b696f4a1260de54df8d94225a5b8129fa00.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/2c278b696f4a1260de54df8d94225a5b8129fa00" title="25"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c278b696f4a1260de54df8d94225a5b8129fa00_2_690x38.jpeg" alt="25" data-base62-sha1="6iBJpZ5ImaFMlvlirJqNjzpBaPS" width="690" height="38" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c278b696f4a1260de54df8d94225a5b8129fa00_2_690x38.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c278b696f4a1260de54df8d94225a5b8129fa00.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c278b696f4a1260de54df8d94225a5b8129fa00.jpeg 2x" data-dominant-color="272829"><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">25</span><span class="informations">791×44 5.43 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><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/8/f813991cf327b99bb75613e599e5eb423cd561d6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f813991cf327b99bb75613e599e5eb423cd561d6" title="23"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/8/f813991cf327b99bb75613e599e5eb423cd561d6_2_690x275.jpeg" alt="23" data-base62-sha1="zoAoSZux5u4shywSLuU8res3lIO" width="690" height="275" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/8/f813991cf327b99bb75613e599e5eb423cd561d6_2_690x275.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/8/f813991cf327b99bb75613e599e5eb423cd561d6_2_1035x412.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/8/f813991cf327b99bb75613e599e5eb423cd561d6_2_1380x550.jpeg 2x" data-dominant-color="2C2B2B"><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">23</span><span class="informations">1419×567 46.4 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/d/1/d18a5bf9e9862dbbe4c90be196924a3bc056e697.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d18a5bf9e9862dbbe4c90be196924a3bc056e697" title="24"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d18a5bf9e9862dbbe4c90be196924a3bc056e697_2_690x252.jpeg" alt="24" data-base62-sha1="tTGdVOK0dDcf4PseKth3doNP7SL" width="690" height="252" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d18a5bf9e9862dbbe4c90be196924a3bc056e697_2_690x252.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d18a5bf9e9862dbbe4c90be196924a3bc056e697_2_1035x378.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/1/d18a5bf9e9862dbbe4c90be196924a3bc056e697.jpeg 2x" data-dominant-color="3D3D3D"><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">24</span><span class="informations">1323×484 74.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>

rui_zhou: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/6/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3" title="26"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3_2_690x144.jpeg" alt="26" data-base62-sha1="eAQ2SyidYcAFeNUdyGcdRk4yng7" width="690" height="144" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3_2_690x144.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/6/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3_2_1035x216.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/6/664881aa1ce4bd494b03265ce3cb9bec10b0eeb3.jpeg 2x" data-dominant-color="3B3B3B"><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">26</span><span class="informations">1241×260 42.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>

rui_zhou: <p>Thanks you very much to teach me the original function, I conduct a breakpoints debugging in colab and the image has been read, but something still wrong with the dataloader, can you give me some suggestions?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/4/64f08d6c897ef616abb8799afa22412a69830736.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/64f08d6c897ef616abb8799afa22412a69830736" title="32"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64f08d6c897ef616abb8799afa22412a69830736_2_690x397.jpeg" alt="32" data-base62-sha1="eoX7X3amZCQtIgQafhU8Dfc31Qi" width="690" height="397" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64f08d6c897ef616abb8799afa22412a69830736_2_690x397.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64f08d6c897ef616abb8799afa22412a69830736_2_1035x595.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/4/64f08d6c897ef616abb8799afa22412a69830736_2_1380x794.jpeg 2x" data-dominant-color="353535"><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">32</span><span class="informations">1419×817 116 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>

rui_zhou: <p>The original problem appears and this time for more specific…but I cannot understand this…<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/0/308d68f3b742fba40389f1a68d014a46cf7f008d.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/308d68f3b742fba40389f1a68d014a46cf7f008d" title="33"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/308d68f3b742fba40389f1a68d014a46cf7f008d_2_690x406.jpeg" alt="33" data-base62-sha1="6VvT3DnAuhHVpUw4TXkMr36Vg9D" width="690" height="406" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/308d68f3b742fba40389f1a68d014a46cf7f008d_2_690x406.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/0/308d68f3b742fba40389f1a68d014a46cf7f008d_2_1035x609.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/0/308d68f3b742fba40389f1a68d014a46cf7f008d.jpeg 2x" data-dominant-color="3D3E3F"><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">33</span><span class="informations">1362×802 118 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>