IOU pytorch implementation

sank: <p>How to implement IOU for mutli-class image segmentation?</p>

kirtac83: <p>I have found this one working good <a href="https://www.kaggle.com/iezepov/fast-iou-scoring-metric-in-pytorch-and-numpy" rel="nofollow noopener">https://www.kaggle.com/iezepov/fast-iou-scoring-metric-in-pytorch-and-numpy</a></p>

tom: <p>I wrote an extensive IoU notebook for a talk on the JIT linked from <a href="https://lernapparat.de/pytorch-jit-android/" rel="nofollow noopener">https://lernapparat.de/pytorch-jit-android/</a> . The usual way is to do “class agnostic” IoU and a standard classification loss (eg cross entropy), so multiclass happens only in the second.<br>
I always recommend the SSD lecture from <a href="https://fast.ai/" rel="nofollow noopener">https://fast.ai/</a> 's second course.</p>
<p>Best regards</p>
<p>Thomas</p>

Kartik_Chopra: <p>Hi <a class="mention" href="/u/tom">@tom</a>, I want to calculate IoU where my labels are of dimension <code>[batch, class, h, w]</code> and I have 4 classes. Initially I had 4 masks per image and I stacked them together to form the above mentioned dimension. Now I’m having difficulty in calculating IoU per class. Can you help me with that ?</p>

tom: <p>I’m not sure I understand your labelling.</p>
<ul>
<li>I think you want 4 coordinates (x0, y0, h, w).</li>
<li>Are you saying that you have class as an array dimension or do you have class as a separate tensor for each area?</li>
<li>But then computing the IoU per item should be straightforward.</li>
<li>If you don’t have <code>class</code> as an array dimension, but have that as an extra label, using <code>index_add_</code> using the class as index should help you consolidate per-item IoU to something that has <code>class</code> as an array dimension.</li>
</ul>
<p>Best regards</p>
<p>Thomas</p>

Kartik_Chopra: <p>Hi Thomas, I’ll be more clear.</p>
<ul>
<li>So for 1 input image, I have 4 masks that correspond to 4 classes.</li>
<li>I converted each mask to binary (0/1)</li>
<li>I stacked these masks to have this dimension <code>[batch, 4, h, w]</code>
</li>
<li>Now I want to calculate IoU for each class.</li>
</ul>

tom: <ul>
<li>But w, h doesn’t have location information?</li>
<li>What’s the shape of your predictions? Both for area and class?</li>
</ul>
<p>Best regards</p>
<p>Thomas</p>

Kartik_Chopra: <p>My logits are of the shape <code>[batch, 4, h, w]</code> as well and then I’m doing<br>
<code>preds = torch.sigmoid(logits) &gt; 0.5</code> to get the predictions.</p>
<p>The shape of the predictions is also <code>[batch, 4, h, w]</code></p>

tom: <p>I’ll have to pass with these.</p>
<p>Best regards and all the best for your project</p>
<p>Thomas</p>

zzzf: <p>This only works for binary segmentation.</p>

moreshud: <p>Hi <a class="mention" href="/u/zzzf">@zzzf</a>,</p>
<p>Any idea of handling with multi-class?</p>

zzzf: <p>I think IOU, per-pixel mean-IOU and per-image mean-IOU are often mis-interpreted.</p>
<p>For training, the code above is OK, but for report per-pixel mean-IOU, you can have a look at: <a href="https://github.com/davidtvs/PyTorch-ENet/blob/a67d048ec837849eb79dfb8ec51b629a9738b362/metric/confusionmatrix.py" class="inline-onebox" rel="noopener nofollow ugc">PyTorch-ENet/confusionmatrix.py at a67d048ec837849eb79dfb8ec51b629a9738b362 · davidtvs/PyTorch-ENet · GitHub</a></p>

moreshud: <p>Thanks for the reply. I will go through the shared link</p>