Showing predicted segmentation

MA_CASANDRA_QUILANG: <p>Hi. I am trying to compare the original image, the ground truth mask and the predicted mask by displaying them. However, I am entirely new to this and I would like to ask for anyone’s opinion if am I doing it right. Here is my prediction script:</p>
<pre><code class="lang-auto">def test(model):
  model.eval
  correct = 0
  total = 0

  with torch.no_grad():
      for i in tqdm(range(len(img_test))):

          
          real_class = mask_test[i]
          net_out = model(img_test[i].view(-1, 3, 448, 448))[0]
          _,predicted_class = torch.max(net_out, 0)
          
          prediction = predicted_class.eq(mask_test[i])

          
          plt.figure(figsize=(22,8))
          plt.subplot(1, 3, 1)
          plt.imshow(real_class.detach().cpu().squeeze())
          plt.title('original mask')

          plt.subplot(1, 3, 2)
          plt.imshow(prediction.detach().cpu().squeeze())
          plt.title('predicted mask')

          plt.subplot(1, 3, 3)
          plt.imshow(img_test[i].view(-1, 448, 448, 3).detach().cpu().squeeze())
          plt.title('original image')

          break
          

      
test(model)
</code></pre>
<p>I am asking this because the ground truth and my predicted mask is almost identical, I fear it was just inverting colors.</p>

MA_CASANDRA_QUILANG: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/e/ce819364cadbb1168d727d71350bc435187abb12.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ce819364cadbb1168d727d71350bc435187abb12" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ce819364cadbb1168d727d71350bc435187abb12_2_690x241.jpeg" alt="image" data-base62-sha1="tsPYmROni5zGHenNZavsVvJmlhw" width="690" height="241" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ce819364cadbb1168d727d71350bc435187abb12_2_690x241.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ce819364cadbb1168d727d71350bc435187abb12_2_1035x361.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/e/ce819364cadbb1168d727d71350bc435187abb12_2_1380x482.jpeg 2x" data-dominant-color="BBA88B"><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">image</span><span class="informations">1537×537 134 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>
by the way this are my results</p>

user_123454321: <p>The center image (predicted mask) shows that background is correctly segmented but the two objects are not since that part is equal to 0. Can you try plotting predicted_class directly ?</p>

MA_CASANDRA_QUILANG: <p>Hi! Thank you for replying. I dont understand why can you say that the two objects are not segmented correctly? is it because of the color?</p>

user_123454321: <p>Yes. I think the default cmap is viridis and when I print the values for 0 and 255 (note that pyplot rescales the array to have max become 255 and min become 0) I get</p>
<pre><code class="lang-auto">viridis = plt.cm.get_cmap('viridis', 12)
print(viridis(0), viridis(255))
(0.267004, 0.004874, 0.329415, 1.0) (0.993248, 0.906157, 0.143936, 1.0)
</code></pre>
<p>So, yellow is 255 and purple is 0, which could mean the purple part is wrongly segmented.</p>

MA_CASANDRA_QUILANG: <p>Thank you for your help. Can you help me pinpoint where I might did something wrong for the objects inside be wrongly segmented?</p>

user_123454321: <p>Can you plot the predicted_class using</p>
<pre><code class="lang-auto">plt.subplot(1, 3, 2)
plt.imshow(predicted_class.detach().cpu().squeeze())
plt.title('predicted mask')
</code></pre>

MA_CASANDRA_QUILANG: <p>I am saddened by this! <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/5/057a175420ca1ab82702c293040c57def459303d.png" data-download-href="https://discuss.pytorch.org/uploads/default/057a175420ca1ab82702c293040c57def459303d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/057a175420ca1ab82702c293040c57def459303d_2_503x500.png" alt="image" data-base62-sha1="MrXUMWaXtfvGbcG3b5XtkMe2e1" width="503" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/057a175420ca1ab82702c293040c57def459303d_2_503x500.png, https://discuss.pytorch.org/uploads/default/original/3X/0/5/057a175420ca1ab82702c293040c57def459303d.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/5/057a175420ca1ab82702c293040c57def459303d.png 2x" data-dominant-color="784983"><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">image</span><span class="informations">510×506 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>
<p>can you please help me figure out my mistake?</p>

user_123454321: <p>Hmmm…looks like the model has predicted everything as background. What kind of dice scores were you getting during training?</p>

MA_CASANDRA_QUILANG: <p>For a detailed explanation on the project I am working I am goin to put all the details. I am currently working to develop a model that can hopefully take an RGB image containing garbage and outputs a segmented image (i am hoping to segment it according to the 60 categories I defined). I used Adam as the optimizer and cross-entropy as the loss_function. During training, (I tried 10 epoch at most), the lowest loss value I got was 0.14. This is my training script:</p>
<pre><code class="lang-auto">BATCH_SIZE = 4
EPOCHS = 3

def train(model):
  model.train()
  for epoch in range(EPOCHS):
      for i in tqdm(range(0, len(img_train), BATCH_SIZE)): 
          batch_img_train = img_train[i:i+BATCH_SIZE].view(-1, 3, 448, 448)
          batch_mask_train = mask_train[i:i+BATCH_SIZE].view(-1, 1, 448, 448)
        
          model.zero_grad()

          outputs = model(batch_img_train)
        
          loss = loss_function(outputs, batch_mask_train.squeeze(1).long())
          loss.backward()
          optimizer.step()    # Does the update

      print(f"Epoch: {epoch}. Loss: {loss}")
  
  return batch_img_train, batch_mask_train, outputs

train(model)</code></pre>

user_123454321: <p>Hmmm, although the loss may seem small, but since the background class much more abundant, the model might have got away with predicting every pixel as background. Can you try using dice as both loss and as a metric ? Also, is the loss around 0.14 for validation too ?</p>

MA_CASANDRA_QUILANG: <p>I may sound so dumb but I don’t understand what you with “using dice as both loss and as a metric”. Can you please help me understand that?</p>

user_123454321: <p>There is something called dice loss (which is quite similar to F1-score but can also be soft to be differentiable) which is used regularly in semantic segmentation tasks. The dice score (or F1-score) can then also be used as a metric to evaluate one’s model on the validation set.</p>

MA_CASANDRA_QUILANG: <p>Thank your for your response. I’ve read about the problem that I currently have and according to the articles I’ve read, my network might have what they call “class imbalance”. The images in my dataset mostly contain small garbage only. How can I solve this kind of problem?</p>

user_123454321: <p>Dice loss should help I think. If not you can try to ‘oversample’ by selecting patches of the ‘small garbage’ more often.</p>

MA_CASANDRA_QUILANG: <p>is dice loss the same with what they call IoU? and how do I oversample?</p>

user_123454321: <p>It is slightly different, while dice score (hard) is</p>
<pre><code class="lang-auto">2 * TP / (2 * TP + FP +FN)
</code></pre>
<p>IOU is</p>
<pre><code class="lang-auto">TP / (TP + FP + FN)
</code></pre>

MA_CASANDRA_QUILANG: <p>What part of my code should I implement dice?</p>

user_123454321: <p>You can replace loss_function with cross_entropy plus dice loss.</p>

MA_CASANDRA_QUILANG: <p>My loss function is already cross entropy. How do I incorporate dice loss with it?</p>