Answers.Body: <blockquote>
  <p>I am afraid that the model will infer from the background information that it shouldn't use to predict the plant diseases, what makes the problem worse is that some plant diseases only exist in one dataset and not the other</p>
  </blockquote>
  <blockquote>
  <p>I am afraid that when I use the model on real-life conditions (say someone capturing an image with their phone) the model will be heavily biased towards predicting labels in the second dataset (the one with actual background) since the background is similar and it somehow associated the background with the inference process.</p>
  </blockquote>
  <p>You are right to be concerned about this. However, you are not alone in having a neural network model that relies heavily on the background or other confounding factors for your classification task.</p>
  <p>A small paper that addresses a similar issue is <a href="https://arxiv.org/pdf/1712.01653.pdf" rel="nofollow noreferrer">Context Augmentation for Convolutional Neural Networks</a>. Table 1 from this paper shows how the model trained and tested on only the background performs <em>better</em> than model trained and tested on only the foreground. They address this by making a training set where the foreground objects are given many backgrounds. Given your imbalanced datasets, with some diseases only present in one dataset and not the other, I think context augmentation might work for you. The main barrier here is that you do not have segmentation maps for these. Are your datasets too large to segment yourself? You might consider automatic segmentation networks - especially for the images with white backgrounds.</p>
  <p>As for your interpretability methods, you should <a href="https://arxiv.org/abs/1810.03292" rel="nofollow noreferrer">not expect too much from them</a> though <a href="https://ieeexplore.ieee.org/abstract/document/8760405?casa_token=Xfnqz3RKMN4AAAAA:MQzbNdTrx_rytJ4o9DTp8MZWEZ-i85EDLGWQrXVhGTZR8PSdXFvHHoJvBMWgh7ntO69Xlz3eZ2A" rel="nofollow noreferrer">some groups are able to get good results with them</a> using GradCAM.</p>
  <p>Here is an alternative idea that may also solve your problem: If you are trying to make a system that can be used in the field, is it possible to have the technician or photographer put a white board under the diseased leaf?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-04T20:09:13.440Z
Answers.LastActivityDate: 2021-01-04T20:09:13.440Z
Answers.UserDisplayName: Alina Barnett
Body: <p>I have an interesting problem related to training the model on two different datasets for the target feature on images taken on different conditions, which might affect the model's ability to generalize.</p>
  <p>To explain I will give examples of images from the two different datasets.</p>
  <p>Dataset 1 sample image:</p>
  <p><a href="https://i.sstatic.net/lkduq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lkduq.png" alt="enter image description here" /></a></p>
  <p>Dataset 2 sample image:</p>
  <p><a href="https://i.sstatic.net/wy9LN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wy9LN.png" alt="[Dataset 2 sample image:2" /></a></p>
  <p>As you see the images are captured in two completely different conditions. I am afraid that the model will infer from the background information that it shouldn't use to predict the plant diseases, what makes the problem worse is that some plant diseases only exist in one dataset and not the other, if all the diseases are contained in both datasets then I wouldn't think there would be a problem.</p>
  <p>I am assuming I need a way to unify the background by somehow detecting the leaf pixels in the images and unifying the background in a way that makes the model focuses on the important features.</p>
  <p>I've tried some segmentation methods but the methods I tried don't always give desirable results for all the images.</p>
  <p>What is the recommended approach here? All help is appreciated</p>
  <p>Further explanation of the problem.</p>
  <p>Ok so I will explain one more thing, my model on the two datasets works fine when training and validating, It got a 94% accuracy.</p>
  <p>The problem is, even though the model performs well on the datasets I have, I am afraid that when I use the model on real-life conditions (say someone capturing an image with their phone) the model will be heavily biased towards predicting labels in the second dataset (the one with actual background) since the background is similar and it somehow associated the background with the inference process.</p>
  <p>I have tried downloading a leaf image of a label that is contained on the first dataset ( the one with the white background), where the image had a real-life background, the model as expected failed to predict the correct label and predicted a label contained in the second dataset, I am assuming it was due to the background. I have tried this experiment multiple times, and the model consistently failed in similar scenarios</p>
  <p>I used some Interpretability techniques as well to visualize the important pixels and it seems like the model is using the background for inference, but I am not an expert in interpreting these graphs so I am not 100% sure.</p>
  <p><a href="https://i.sstatic.net/ueZvC.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ueZvC.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/1To4R.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1To4R.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/IMIQw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/IMIQw.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.sstatic.net/vzNcl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vzNcl.png" alt="enter image description here" /></a></p>
  
Tags: <deep-learning><computer-vision><image-recognition><image-segmentation><domain-adaptation>
Title: Training a classifier on different datasets with different image conditions for different labels causes the model to infer using the background
UserDisplayName: Mohammed Alkhrashi
ThreadText: # Training a classifier on different datasets with different image conditions for different labels causes the model to infer using the background
  
  (Asked by: username_0 on 2021-01-03)
  
  I have an interesting problem related to training the model on two different datasets for the target feature on images taken on different conditions, which might affect the model's ability to generalize.
  
  To explain I will give examples of images from the two different datasets.
  
  Dataset 1 sample image:
  
  [![enter image description here](https://i.sstatic.net/lkduq.png)](https://i.sstatic.net/lkduq.png)
  
  Dataset 2 sample image:
  
  [![[Dataset 2 sample image:2](https://i.sstatic.net/wy9LN.png)](https://i.sstatic.net/wy9LN.png)
  
  As you see the images are captured in two completely different conditions. I am afraid that the model will infer from the background information that it shouldn't use to predict the plant diseases, what makes the problem worse is that some plant diseases only exist in one dataset and not the other, if all the diseases are contained in both datasets then I wouldn't think there would be a problem.
  
  I am assuming I need a way to unify the background by somehow detecting the leaf pixels in the images and unifying the background in a way that makes the model focuses on the important features.
  
  I've tried some segmentation methods but the methods I tried don't always give desirable results for all the images.
  
  What is the recommended approach here? All help is appreciated
  
  Further explanation of the problem.
  
  Ok so I will explain one more thing, my model on the two datasets works fine when training and validating, It got a 94% accuracy.
  
  The problem is, even though the model performs well on the datasets I have, I am afraid that when I use the model on real-life conditions (say someone capturing an image with their phone) the model will be heavily biased towards predicting labels in the second dataset (the one with actual background) since the background is similar and it somehow associated the background with the inference process.
  
  I have tried downloading a leaf image of a label that is contained on the first dataset ( the one with the white background), where the image had a real-life background, the model as expected failed to predict the correct label and predicted a label contained in the second dataset, I am assuming it was due to the background. I have tried this experiment multiple times, and the model consistently failed in similar scenarios
  
  I used some Interpretability techniques as well to visualize the important pixels and it seems like the model is using the background for inference, but I am not an expert in interpreting these graphs so I am not 100% sure.
  
  [![enter image description here](https://i.sstatic.net/ueZvC.png)](https://i.sstatic.net/ueZvC.png)
  
  [![enter image description here](https://i.sstatic.net/1To4R.png)](https://i.sstatic.net/1To4R.png)
  
  [![enter image description here](https://i.sstatic.net/IMIQw.png)](https://i.sstatic.net/IMIQw.png)
  
  [![enter image description here](https://i.sstatic.net/vzNcl.png)](https://i.sstatic.net/vzNcl.png)
  
  **Tags:** deep-learning, computer-vision, image-recognition, image-segmentation, domain-adaptation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-01-04. Score: 1 | Accepted answer
  
  > I am afraid that the model will infer from the background information that it shouldn't use to predict the plant diseases, what makes the problem worse is that some plant diseases only exist in one dataset and not the other
  
  > I am afraid that when I use the model on real-life conditions (say someone capturing an image with their phone) the model will be heavily biased towards predicting labels in the second dataset (the one with actual background) since the background is similar and it somehow associated the background with the inference process.
  
  You are right to be concerned about this. However, you are not alone in having a neural network model that relies heavily on the background or other confounding factors for your classification task.
  
  A small paper that addresses a similar issue is [Context Augmentation for Convolutional Neural Networks](https://arxiv.org/pdf/1712.01653.pdf). Table 1 from this paper shows how the model trained and tested on only the background performs *better* than model trained and tested on only the foreground. They address this by making a training set where the foreground objects are given many backgrounds. Given your imbalanced datasets, with some diseases only present in one dataset and not the other, I think context augmentation might work for you. The main barrier here is that you do not have segmentation maps for these. Are your datasets too large to segment yourself? You might consider automatic segmentation networks - especially for the images with white backgrounds.
  
  As for your interpretability methods, you should [not expect too much from them](https://arxiv.org/abs/1810.03292) though [some groups are able to get good results with them](https://ieeexplore.ieee.org/abstract/document/8760405?casa_token=Xfnqz3RKMN4AAAAA:MQzbNdTrx_rytJ4o9DTp8MZWEZ-i85EDLGWQrXVhGTZR8PSdXFvHHoJvBMWgh7ntO69Xlz3eZ2A) using GradCAM.
  
  Here is an alternative idea that may also solve your problem: If you are trying to make a system that can be used in the field, is it possible to have the technician or photographer put a white board under the diseased leaf?
  
  
  ---
  
  