Dataloader: Invalid Argument Error: Invalid Shape

Khalaf90: <p>visualize works only on my images, but on masks it gives Invalid shape () for image data , all masks have the same size 512x512 , what could be the reason ?</p>

ptrblck: <p>Could you post a code snippet showing this behavior?<br>
In case you are using <code>matplotlib</code>, it should allow you to visualize images containing only the spatial size as seen here:</p>
<pre><code class="lang-python">mask = torch.randint(0, 2, (244, 244))
plt.imshow(mask.numpy())
</code></pre>

Khalaf90: <p>I am using the code from <a href="https://github.com/qubvel/segmentation_models.pytorch/blob/master/examples/cars%20segmentation%20(camvid).ipynb" rel="noopener nofollow ugc">https://github.com/qubvel/segmentation_models.pytorch/blob/master/examples/cars%20segmentation%20(camvid).ipynb</a></p>
<p>when i reach:</p>
<pre><code>visualize(
image=image, 
cars_mask=mask.squeeze(),
) 
</code></pre>
<p>it gives:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/d/ad29836e0249177db9da78e523e56e97ad9fef55.png" data-download-href="https://discuss.pytorch.org/uploads/default/ad29836e0249177db9da78e523e56e97ad9fef55" title="l"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/ad29836e0249177db9da78e523e56e97ad9fef55_2_690x382.png" alt="l" data-base62-sha1="oHRxKNe5PeOiRiwzO1NIpvRq8uh" width="690" height="382" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/d/ad29836e0249177db9da78e523e56e97ad9fef55_2_690x382.png, https://discuss.pytorch.org/uploads/default/original/3X/a/d/ad29836e0249177db9da78e523e56e97ad9fef55.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/d/ad29836e0249177db9da78e523e56e97ad9fef55.png 2x" data-dominant-color="F5F5F6"><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">l</span><span class="informations">894×496 37.3 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>
<p>all my images and masks have same shape(512x512) but here it gives:</p>
<pre><code>  print(dataset[0][0].shape) gives (512, 512, 3)
  print(dataset[0][1].shape) gives (1,)
</code></pre>

ptrblck: <p>Your mask seems to have a single dimension, which is not a valid image format, so you would have to check the <code>Dataset</code> and make sure <code>mask</code> is created properly.</p>

Khalaf90: <pre><code> def __getitem__(self, i):
    image = cv2.imread(self.images_fps[i])
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    mask = cv2.imread(self.masks_fps[i], 0)
</code></pre>
<p>i think here are images and masks created, it works for images but not for masks -.- , they all have same size (512x512)</p>

ptrblck: <p>The posted code looks alright, but doesn’t match</p>
<pre><code class="lang-auto">print(dataset[0][1].shape) gives (1,)
</code></pre>
<p>so could you check what exactly is returned in the <code>__getitem__</code> method, as the second return value doesn’t seem to be the mask.</p>

Khalaf90: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/f/5fe6dc440864c1dda7733075774331a07cb29d34.png" data-download-href="https://discuss.pytorch.org/uploads/default/5fe6dc440864c1dda7733075774331a07cb29d34" title="sample14_54"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5fe6dc440864c1dda7733075774331a07cb29d34_2_375x500.png" alt="sample14_54" data-base62-sha1="dGnYv9yUkMmf7TVLfnmHtP1zT00" width="375" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5fe6dc440864c1dda7733075774331a07cb29d34_2_375x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5fe6dc440864c1dda7733075774331a07cb29d34_2_562x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/f/5fe6dc440864c1dda7733075774331a07cb29d34_2_750x1000.png 2x" data-dominant-color="151515"><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">sample14_54</span><span class="informations">3024×4032 157 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>

ptrblck: <p>Assuming the first image represents the image while the second one the mask, the data loading looks correct, so I’m still unsure why the previous shape is returned.<br>
Could you iterate the <code>Dataset</code> for an entire epoch and check all mask shapes?</p>