How to improve my model which predicts well but for the wrong reason?

thejoe: <p>Hi everyone,</p>
<p>I’m trying to develop a pytorch model to classify some synthetic images. The model should identify whether an image contains out-of-place bolts or not (in the attachment there are two examples of images from both classes). I got high accuracy and low loss (98% on the validation set). But if I look on the activation map I see that the model is predicting well but for the wrong reason.</p>
<p>I trained my model for 30 epochs (I saved the weights of the epoch with the best accuracy) with an Adam optimizer and a lr of 0.01 and used batches of 10 images.</p>
<p>IMAGES<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://simgbb.com/images/favicon.png" class="site-icon" width="300" height="300">
      <a href="https://ibb.co/6g0vfxM" target="_blank" rel="nofollow noopener">ImgBB</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:666/500;"><img src="https://i.ibb.co/MMk834v/clean.png" class="thumbnail" width="666" height="500"></div>

<h3><a href="https://ibb.co/6g0vfxM" target="_blank" rel="nofollow noopener">clean</a></h3>

<p>Image clean hosted in ImgBB</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://simgbb.com/images/favicon.png" class="site-icon" width="300" height="300">
      <a href="https://ibb.co/rtDNHjC" target="_blank" rel="nofollow noopener">ImgBB</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/487;"><img src="https://i.ibb.co/26GHd9D/with-bolts.png" class="thumbnail" width="690" height="487"></div>

<h3><a href="https://ibb.co/rtDNHjC" target="_blank" rel="nofollow noopener">with-bolts</a></h3>

<p>Image with-bolts hosted in ImgBB</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>Do you know how it could be possible to improve my model pushing it to give more weight to the out-of-place bolts?</p>

thejoe: <p>This is the activation map:  <a href="https://ibb.co/V9sq16x" rel="nofollow noopener">https://ibb.co/V9sq16x</a></p>

mowolf: <p>If you nevertheless get high validation accuracy what is the issue? I am not sure if an activation map can tell you a lot about how the network works? What exactly is your activation map showing?</p>
<p>If you think your networks learned the wrong parts, then maybe you can proprocess the image to only show the region of interest? As Low validation error means your network can classify your validation set very good, are you sure your validation images are correct?</p>

thejoe: <p>thanks for your reply. My activation map simply generates an heat map according to weights of the last layer of my resnet50 and then it is overlayed on the image. So, basically I would expect a red area around the out-of-place bolt.</p>
<p>Regarding my validation set, I selected similar images belonging to both classes (‘clean’, ‘with bolts’). I tried different shuffling and i obtained different results ( very few cases with low accuracy) but in this way I have a model which is dependent more on the ‘fortune’ of having the right images .</p>
<p>This is a screenshot of my training performances:<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://simgbb.com/images/favicon.png" class="site-icon" width="300" height="300">
      <a href="https://ibb.co/19WFLzN" target="_blank" rel="nofollow noopener">ImgBB</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/240;"><img src="https://i.ibb.co/D720trF/Screenshot-2020-04-10-at-11-57-05.png" class="thumbnail" width="690" height="240"></div>

<h3><a href="https://ibb.co/19WFLzN" target="_blank" rel="nofollow noopener">Screenshot-2020-04-10-at-11-57-05</a></h3>

<p>Image Screenshot-2020-04-10-at-11-57-05 hosted in ImgBB</p>


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

mowolf: <aside class="quote no-group" data-username="thejoe" data-post="4" data-topic="76150">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/thejoe/48/11809_2.png" class="avatar"> thejoe:</div>
<blockquote>
<p>So, basically I would expect a red area around the out-of-place bolt.</p>
</blockquote>
</aside>
<p>I assume you then have a layer with two outputs that classify your image? I think your resnet output does not need to match locally to the regions of your image.</p>
<aside class="quote no-group" data-username="thejoe" data-post="4" data-topic="76150">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/thejoe/48/11809_2.png" class="avatar"> thejoe:</div>
<blockquote>
<p>Regarding my validation set, I selected similar images belonging to both classes (‘clean’, ‘with bolts’). I tried different shuffling and i obtained different results ( very few cases with low accuracy) but in this way I have a model which is dependent more on the ‘fortune’ of having the right images .</p>
</blockquote>
</aside>
<p>You just overfitted your model. Get more data with more different bolt positions and viewing angles. And use a substantial amount of different images for validation (&gt;1k). As you have synthetic images, I assume you can generate more? How do your true/false pairs look like? Do you use the exactly same scene with and without a bolt? Also are both classes equaly distributed in your training and validation data?</p>

thejoe: <aside class="quote no-group" data-username="mowolf" data-post="5" data-topic="76150">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mowolf/48/22587_2.png" class="avatar"> mowolf:</div>
<blockquote>
<p>I assume you then have a layer with two outputs that classify your image? I think your resnet output does not need to match locally to the regions of your image.</p>
</blockquote>
</aside>
<p>The classifier is made as follow:</p>
<pre><code class="lang-auto">resnet.fc = nn.Sequential(
             nn.Linear(resnet.fc.in_features, 512),
             nn.ReLU(),
             nn.Dropout(p=0.5),
             nn.Linear(512, 2)
        )
</code></pre>
<p>I have a lot of synthetic images with more view angles and bolts position. I cannot generate more but I ha lots of data to play with. The only problem is that maybe it is difficult to retrieve images with the exactly same background with and without bolts.</p>
<p>Right now I’m proceeding by little steps. Adding some similar images at time and see if something happens. Consider that I trained the model with a training set composed by 250 images and a validation set of 150 images.</p>
<aside class="quote no-group" data-username="mowolf" data-post="5" data-topic="76150">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mowolf/48/22587_2.png" class="avatar"> mowolf:</div>
<blockquote>
<p>Also are both classes equaly distributed in your training and validation data?</p>
</blockquote>
</aside>
<p>Do you mean in relation to the dataset or to the specific batch?</p>

mowolf: <p>250 images are by any means not enough. Your model is heavily overfitting. You need at minimum 1k+ images but better would be 10k-100k.</p>
<aside class="quote no-group" data-username="thejoe" data-post="6" data-topic="76150">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/thejoe/48/11809_2.png" class="avatar"> thejoe:</div>
<blockquote>
<p>The only problem is that maybe it is difficult to retrieve images with the exactly same background with and without bolts.</p>
</blockquote>
</aside>
<p>I think this would be great for the network to learn however.</p>

thejoe: <p>thnak you! I will try with more images</p>