Visualizing CNN predicted image

username: Yolkandwhite
cooked: <p>I want to visualize predicted output from the image.<br>
I set the dimension of original image data, ground truth mask data, and predicted data and plot it using matplotlib.<br>
but I don’t know why dimension error occurs.<br>
any idea?</p>
<pre><code class="lang-auto">r = random.randint(0, 3607)
gen = BasicDataset('/home/intern/Desktop/YH/Brain_MRI/BrainMRI_train/MRI/MRI/', '/home/intern/Desktop/YH/Brain_MRI/BrainMRI_train/mask/mask/')
x, y = gen.__getitem__(r)
img = x
mask = y["masks"]


img = np.transpose(img, (1, 2, 0))
mask = mask.numpy()
mask = np.transpose(mask, (1, 2, 0))

fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax = fig.add_subplot(1, 3, 1)
print("img shape : ", img.shape)   #shape just before imshow()
ax.imshow(img)
ax = fig.add_subplot(1, 3, 2)
print("mask shape: ", mask.shape)
ax.imshow(np.reshape(mask,(256, 256)), cmap = "gray")

img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
img = torch.cuda.FloatTensor(img)

#normalize
img = img/255
mask = mask/255

model.eval()
for param in model.parameters():
    param.requires_grad = False

test = model(img)
test = test.to("cpu")
test = test.numpy()
test = np.squeeze(test, 0)
test = np.transpose(test, (1, 2, 0))
print("test shape : ", test.shape)

ax = fig.add_subplot(1, 3, 3)
ax.imshow(np.reshape(test,(256, 256)), cmap = "gray")
ax.imshow(test)
</code></pre>
<h3><a name="out-1" class="anchor" href="#out-1"></a>out</h3>
<pre><code class="lang-auto">img shape :  (256, 256, 3)
mask shape:  (256, 256, 1)
test shape :  (256, 256, 1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-569-83fe8fe6e4e8&gt; in &lt;module&gt;()
     37 ax = fig.add_subplot(1, 3, 3)
     38 ax.imshow(np.reshape(test,(256, 256)), cmap = "gray")
---&gt; 39 ax.imshow(test)

/usr/local/lib/python3.5/dist-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-&gt; 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   5485                               resample=resample, **kwargs)
   5486 
-&gt; 5487         im.set_data(X)
   5488         im.set_alpha(alpha)
   5489         if im.get_clip_path() is None:

/usr/local/lib/python3.5/dist-packages/matplotlib/image.py in set_data(self, A)
    651         if not (self._A.ndim == 2
    652                 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
--&gt; 653             raise TypeError("Invalid dimensions for image data")
    654 
    655         if self._A.ndim == 3:

TypeError: Invalid dimensions for image data
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/7/c7ed95143e8235b9c877c73dae7ecd0f8dad0979.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c7ed95143e8235b9c877c73dae7ecd0f8dad0979" title="brainnn"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7ed95143e8235b9c877c73dae7ecd0f8dad0979_2_517x164.jpeg" alt="brainnn" data-base62-sha1="swE28mXztiNZfCVHgkwhWfC5DXX" width="517" height="164" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7ed95143e8235b9c877c73dae7ecd0f8dad0979_2_517x164.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7ed95143e8235b9c877c73dae7ecd0f8dad0979_2_775x246.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/7/c7ed95143e8235b9c877c73dae7ecd0f8dad0979_2_1034x328.jpeg 2x" data-dominant-color="7F7F7F"><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">brainnn</span><span class="informations">1770×563 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>
<p>even though there was an error I could still get image.</p>

username: ptrblck
cooked: <p><code>matplotlib.pyplot</code> doesn’t like the single channel dimension, so you should remove it.</p>

username: Yolkandwhite
cooked: <p>there was no error when I just print images and masks.</p>
<p>error came out when I tried to print predicted images…</p>
<p>and they all have same dimension.</p>

username: ptrblck
cooked: <p>That’s strange, as it’s not working for me:</p>
<pre><code class="lang-python">plt.imshow(np.random.randn(24, 24, 1))
&gt; TypeError: Invalid shape (24, 24, 1) for image data
</code></pre>
<p>However, based in the stack trace, it seems that you are reshaping the image, so the error might be in some code part, which is not posted.</p>

username: Yolkandwhite
cooked: <p>I don’t think there is an problem with reshaping image.<br>
I printed out the shape just before the imshow() and It was 3 dimension.</p>
<pre><code class="lang-auto">r = random.randint(0, 3608)
gen = BasicDataset('/home/intern/Desktop/YH/Brain_MRI/BrainMRI_train/MRI/MRI/', '/home/intern/Desktop/YH/Brain_MRI/BrainMRI_train/mask/mask/')
x, y = gen.__getitem__(r)
img = x
mask = y["masks"]

img = np.transpose(img, (1, 2, 0)) #makes 256, 256, 3
mask = mask.numpy()
mask = np.transpose(mask, (1, 2, 0)) #makes 256, 256, 1

print("img shape :", img.shape, "\nmask shape :", mask.shape, "\n")
fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax = fig.add_subplot(1, 2, 1)
ax.imshow(img)
ax = fig.add_subplot(1, 2, 2)
ax.imshow(np.reshape(mask,(256, 256)), cmap = "gray")
</code></pre>
<h3><a name="out-1" class="anchor" href="#out-1"></a>out</h3>
<pre><code class="lang-auto">img shape : (256, 256, 3) 
mask shape : (256, 256, 1)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/1/3113e0e552f412666783612180adaf98a7cebf97.png" data-download-href="https://discuss.pytorch.org/uploads/default/3113e0e552f412666783612180adaf98a7cebf97" title="imagesds01"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/3113e0e552f412666783612180adaf98a7cebf97_2_690x315.png" alt="imagesds01" data-base62-sha1="709Z6pOabWRfgYCUxEaxK1YjLjV" width="690" height="315" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/3113e0e552f412666783612180adaf98a7cebf97_2_690x315.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/1/3113e0e552f412666783612180adaf98a7cebf97_2_1035x472.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/1/3113e0e552f412666783612180adaf98a7cebf97.png 2x" data-dominant-color="EBEBEB"><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">imagesds01</span><span class="informations">1044×478 113 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>mine is working good</p>

username: ptrblck
cooked: <p>You are reshaping one <code>test</code> array, but then try to plot it directly:</p>
<pre><code class="lang-python">ax.imshow(np.reshape(test,(256, 256)), cmap = "gray")
ax.imshow(test)
</code></pre>
<p>which will yield the mentioned error.</p>

username: Yolkandwhite
cooked: <p>it worked in this way.</p>
<pre><code class="lang-auto">test = np.reshape(test,(256, 256))
ax.imshow(test, cmap = "gray")
</code></pre>
<p>but I don’t know why it is printed with colors.<br>
cmap =“gray” is not working in this way</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/595be8188cba6dae11fe791d76eafb62fedccd9e.png" data-download-href="https://discuss.pytorch.org/uploads/default/595be8188cba6dae11fe791d76eafb62fedccd9e" title="ㅈㅂㄹㅈㅂㄹ"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595be8188cba6dae11fe791d76eafb62fedccd9e_2_517x365.png" alt="ㅈㅂㄹㅈㅂㄹ" data-base62-sha1="cKvp2BSuXfgyleRHps5Q7jwkZ7U" width="517" height="365" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595be8188cba6dae11fe791d76eafb62fedccd9e_2_517x365.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/9/595be8188cba6dae11fe791d76eafb62fedccd9e_2_775x547.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/9/595be8188cba6dae11fe791d76eafb62fedccd9e.png 2x" data-dominant-color="D3D7D7"><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">ㅈㅂㄹㅈㅂㄹ</span><span class="informations">797×563 156 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>Maybe you are overwriting it by calling <code>imshow</code> on the same <code>ax</code> object with a <code>cmap</code> and without?</p>

username: Yolkandwhite
cooked: <p>oh why did I not delete it … haha<br>
emberessing mistake</p>

username: Yolkandwhite
cooked: <p>anyways even though I trained about 50 epochs and the cost was 0.0085, my predicted image doesn’t look good at all. Most of the region of mask image is just black background. so It should have lots of black region, but my predicted mask looks so terrible…</p>
<p>when I tried to print the predicted mask,<br>
I assumed that predicted image comes out of model after training.<br>
so I put an image into the model and fixed some dimension to print it using matplotlib.<br>
is this not correct?</p>