Calculating dice coefficient

lavender99: <pre><code class="lang-auto">def dice_coeff(pred, target):
    smooth = 1.
    num = pred.size(0)
    m1 = pred.view(num, -1).float()  # Flatten
    m2 = target.view(num, -1).float()  # Flatten
    intersection = (m1 * m2).sum().float()

    return (2. * intersection + smooth) / (m1.sum() + m2.sum() + smooth)
</code></pre>
<p>in the code above i am trying to calculating  dice coefficient for segmetnation task<br>
but it resturn tensor value instead of the value of similrty<br>
train dice tensor(3.2344e-05, device=‘cuda:0’)</p>

Tony-Y: <p>Use <a href="https://pytorch.org/docs/stable/tensors.html#torch.Tensor.item" rel="nofollow noopener">item()</a> to get the value in a tensor.</p>

lavender99: <aside class="quote no-group" data-username="lavender99" data-post="1" data-topic="44154">
<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/lavender99/48/10608_2.png" class="avatar"> lavender99:</div>
<blockquote>
<p>def dice_coeff(pred, target): smooth = 1. num = pred.size(0) m1 = pred.view(num, -1).float() # Flatten m2 = target.view(num, -1).float() # Flatten intersection = (m1 * m2).sum().float() return (2. * intersection + smooth) / (m1.sum() + m2.sum() + smooth)</p>
</blockquote>
</aside>
<p>thanks<br>
i tried it works , but the value is 99 which impossible , do you have another function to measure dice similrty ?</p>

Tony-Y: <aside class="quote no-group" data-username="lavender99" data-post="1" data-topic="44154">
<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/lavender99/48/10608_2.png" class="avatar"> lavender99:</div>
<blockquote>
<p>num = pred.size(0)</p>
</blockquote>
</aside>
<p>What is <code>num</code>? I guess it is the size of mini-batch, the number of training examples, or the number of classes. If it is the size of mini-batch or the number of training examples, you can calculate per-example dice coefficients by using <code>sum(dim=1)</code> instead of <code>sum()</code>.</p>

lavender99: <p>still the same<br>
<a class="mention" href="/u/tony-y">@Tony-Y</a>  do you have any other functions to calculate the dice similarity</p>

Tony-Y: <aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/1249" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/rogertrullo" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/8496304?v=2&amp;s=96" class="thumbnail onebox-avatar" width="60" height="60">
</a>

<h4><a href="https://github.com/pytorch/pytorch/issues/1249" target="_blank" rel="nofollow noopener">Issue: Dice Loss PR</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/rogertrullo" target="_blank" rel="nofollow noopener">rogertrullo</a>
	on <a href="https://github.com/pytorch/pytorch/issues/1249" target="_blank" rel="nofollow noopener">2017-04-12</a>
	</div>
	<div class="user">
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">Hi, I have implemented a Dice loss function which is used in segmentation tasks, and sometimes even preferred over cross_entropy. More...</pre>

<div class="labels">
</div>

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

<p>This issue thread might help you.</p>

lavender99: <p>Thank you , actually it is an implementation of dice loss  not dice similarity</p>

Arun_Mohan: <p>Can I use the same dice coeff function in case of multiclass segmentation?</p>

ptrblck: <p>The linked GitHub issue provides a multi-class dice loss approach.<br>
Would that work for you or are you running into issues with it?</p>