How to implement soft-IoU loss?

anil_batra: <p>I am trying to implement soft-mIoU loss for semantic segmentation as per the following equation. but loss is very low and I am not able to find the wrong step in the implementation.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/dae59a277c7d68a234de19be8b998af7bf45be09.png" data-download-href="https://discuss.pytorch.org/uploads/default/dae59a277c7d68a234de19be8b998af7bf45be09" title="soft-IoU"><img src="https://discuss.pytorch.org/uploads/default/original/2X/d/dae59a277c7d68a234de19be8b998af7bf45be09.png" alt="soft-IoU" data-base62-sha1="ves0yjJ0ESr79xVGJToX5Q7chuN" width="690" height="220" data-dominant-color="EDEDEE"><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">soft-IoU</span><span class="informations">725×232 17.1 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>
<pre><code class="lang-auto">def to_one_hot(tensor,nClasses):
    
    n,h,w = tensor.size()
    one_hot = torch.zeros(n,nClasses,h,w).scatter_(1,tensor.view(n,1,h,w),1)
    return one_hot

class mIoULoss(nn.Module):
    def __init__(self, weight=None, size_average=True, n_classes=2):
        super(mIoULoss, self).__init__()
        self.classes = n_classes

    def forward(self, inputs, target_oneHot):
    	# inputs =&gt; N x Classes x H x W
    	# target_oneHot =&gt; N x Classes x H x W

    	N = inputs.size()[0]

    	# predicted probabilities for each pixel along channel
    	inputs = F.softmax(inputs,dim=1)
    	
    	# Numerator Product
    	inter = inputs * target_oneHot
    	## Sum over all pixels N x C x H x W =&gt; N x C
    	inter = inter.view(N,self.classes,-1).sum(2)

    	#Denominator 
    	union= inputs + target_oneHot - (inputs*target_oneHot)
    	## Sum over all pixels N x C x H x W =&gt; N x C
    	union = union.view(N,self.classes,-1).sum(2)

    	loss = inter/union

    	## Return average loss over classes and batch
    	return loss.mean()
</code></pre>
<p>Can any one help me?<br>
Thanks</p>

11185: <p>Hi, I am wondering whether have you solved the problem?</p>

anil_batra: <p>Yes, there was a minor bug. Written code is correct except the return statement; in which returned loss need to be negative:</p>
<pre><code class="lang-auto">return -loss.mean()
</code></pre>
<p>Thanks</p>

TonyMaster: <blockquote>
<p>Hello !<br>
I’m confused by this loss, becaose ti seems that if the pixel value more corect, the loss become larger, and the value equal to label ,the loss would be 1?</p>
</blockquote>
<p>Sorry, I didn’t notice your last comment !</p>

exitNA: <p>hi, what is name of this paper?</p>

lyakaap: <p>I guess this paper. <a href="https://www.cs.umanitoba.ca/~ywang/papers/isvc16.pdf" rel="nofollow noopener">https://www.cs.umanitoba.ca/~ywang/papers/isvc16.pdf</a></p>

32700848c3814c7c8305: <p>I think this paper.<br>
DeepRoadMapper: Extracting Road Topology from Aerial Images<br>
<a href="http://openaccess.thecvf.com/content_ICCV_2017/papers/Mattyus_DeepRoadMapper_Extracting_Road_ICCV_2017_paper.pdf" class="onebox" target="_blank" rel="nofollow noopener">http://openaccess.thecvf.com/content_ICCV_2017/papers/Mattyus_DeepRoadMapper_Extracting_Road_ICCV_2017_paper.pdf</a></p>

harryhan618: <p>According to <a href="https://www.cs.umanitoba.ca/~ywang/papers/isvc16.pdf" rel="nofollow noopener">paper: Optimizing Intersection-Over-Union in Deep Neural Networks for Image Segmentation</a>, the loss should be <code>1 - soft_IOU</code>.</p>

Chaminda: <p>See this: <a href="https://github.com/anilbatra2185/road_connectivity/blob/master/utils/loss.py" class="inline-onebox" rel="noopener nofollow ugc">road_connectivity/loss.py at master · anilbatra2185/road_connectivity · GitHub</a></p>