Dice Loss for a three Image 3D volume

username: aksg87
cooked: <p>Hello all,</p>
<p>I am creating a model that is performing well for the segmentation problem below:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/1/b12ba9f30c0dbf2e50e876040c473b00d22d2b02.png" alt="image" data-base62-sha1="phk3P3khTxpFktgMJiNcbyKuhX4" width="233" height="124"></p>
<p>My question is now I want to now give this model more 3D context and also include the image prior to and after the main image i.e. “neighbors”. Would it make sense to also take the dice loss of these neighbor images with their respective masks, perhaps weighting the loss less than the central image? My primary metric is optimizing the main image segmentation for any given volume. Any feedback is appreciated!</p>

username: ptrblck
cooked: <p>Your approach would thus mean to change the 2D use case to a 3D one, right?<br>
Instead of classifying a single slice you would now consider a small volume of slices and predict all segmentation masks for these slices.</p>
<p>That sounds reasonable, as the depth dimension should also yield useful information besides the spatial dimensions.</p>
<p>Or would you still use the 2D model and just pass multiple slices to the model?</p>

username: aksg87
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>:</p>
<p>Yes, one idea is making the 2D case a 3D one. I would have to then run inference across a stack of images for the patient, possibly shifting the model over one image at a time and averaging results. To do this, I think simply averaging the loss for each output mask would suffice?</p>
<p>I was also wondering if there was a good way to check consistency across the three image predictions as an input to the model. (I.e. the predictions should have a similar distribution and number of positive pixels should also be similar – the anatomy cannot shift rapidly across a single image).</p>
<p>Curious if you had any pointers or examples of people doing this in PyTorch!</p>

username: ptrblck
cooked: <p>That’s an interesting use case. However, would you like to use 3D modules for it (e.g. <code>nn.Conv3d</code>) or rather a windowed approach?</p>
<aside class="quote no-group" data-username="aksg87" data-post="3" data-topic="81216">
<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/aksg87/48/15602_2.png" class="avatar"> aksg87:</div>
<blockquote>
<p>I was also wondering if there was a good way to check consistency across the three image predictions as an input to the model. (I.e. the predictions should have a similar distribution and number of positive pixels should also be similar – the anatomy cannot shift rapidly across a single image).</p>
</blockquote>
</aside>
<p>Really just a wild idea, but for a windowed approach, you could use the predicted classes of the “middle” slice as an additional cross entropy loss for the neighboring slices.<br>
I have no idea, if this would work at all, but might be worth a try. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>