Answers.Body: <p>The background being an unbalance class is a well known problem in image segmentation. Before digging into custom losses you should take a look to existing ones that address this specific issue like the <a href="https://pycad.co/the-difference-between-dice-and-dice-loss/" rel="nofollow noreferrer">Dice Loss</a> or <a href="https://medium.com/swlh/understanding-focal-loss-for-pixel-level-classification-in-convolutional-neural-networks-720f19f431b1" rel="nofollow noreferrer">Focal Loss</a>, the latter being more tunable having a extra hyper parameter that can be optimized. You can easily find on github  tensorflow implementations of both.<br />
  For a more detailed comparison and reference to other similar losses you can also check this <a href="https://arxiv.org/pdf/2006.14822.pdf" rel="nofollow noreferrer">paper</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-27T21:16:47.863Z
Answers.Text: Can you briefly summarize Dice Loss and Focal Loss to make your answer more self-contained?
Answers.UserDisplayName: Josiah Yoder
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-27T14:57:00.503Z
Answers.LastActivityDate: 2021-12-27T14:57:00.503Z
Answers.UserDisplayName: Edoardo Guerriero
Body: <p>I'm training a Tensorflow model that receives an image and segments the image into foreground and background. That is, if the input image is <code>w x h x 3</code>, then the neural network outputs a <code>w x h x 1</code> image of <code>0</code>'s and <code>1</code>'s, where <code>0</code> represents background and <code>1</code> represents foreground.</p>
  <p>I've computed that about 75% of the true mask is background, so the neural network simply trains a model that outputs all <code>0</code>'s and gets a 75% accuracy.</p>
  <p>To solve this, I'm thinking of implementing a custom loss function that checks if there are more than a certain percentage of <code>0</code>'s, and if so, to add a very large number to the loss to disincentivize the all <code>0</code>'s strategy.</p>
  <p>The issue is that this loss function becomes non-differentiable.</p>
  <p>Where should I go from here?</p>
  
Tags: <neural-networks><tensorflow><objective-functions><image-segmentation>
Title: Custom Tensorflow loss function that disincentivizes all black pixels
UserDisplayName: Sam Liu
ThreadText: # Custom Tensorflow loss function that disincentivizes all black pixels
  
  (Asked by: username_0 on 2021-12-27)
  
  I'm training a Tensorflow model that receives an image and segments the image into foreground and background. That is, if the input image is `w x h x 3`, then the neural network outputs a `w x h x 1` image of `0`'s and `1`'s, where `0` represents background and `1` represents foreground.
  
  I've computed that about 75% of the true mask is background, so the neural network simply trains a model that outputs all `0`'s and gets a 75% accuracy.
  
  To solve this, I'm thinking of implementing a custom loss function that checks if there are more than a certain percentage of `0`'s, and if so, to add a very large number to the loss to disincentivize the all `0`'s strategy.
  
  The issue is that this loss function becomes non-differentiable.
  
  Where should I go from here?
  
  **Tags:** neural-networks, tensorflow, objective-functions, image-segmentation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-12-27. Score: 2 | Accepted answer
  
  The background being an unbalance class is a well known problem in image segmentation. Before digging into custom losses you should take a look to existing ones that address this specific issue like the [Dice Loss](https://pycad.co/the-difference-between-dice-and-dice-loss/) or [Focal Loss](https://medium.com/swlh/understanding-focal-loss-for-pixel-level-classification-in-convolutional-neural-networks-720f19f431b1), the latter being more tunable having a extra hyper parameter that can be optimized. You can easily find on github tensorflow implementations of both.  
  For a more detailed comparison and reference to other similar losses you can also check this [paper](https://arxiv.org/pdf/2006.14822.pdf).
  
  > **Comments:**
  > **username_2**: Can you briefly summarize Dice Loss and Focal Loss to make your answer more self-contained?  
  
  ---
  
  