CRF loss for semantic segmentation

username: HaziqRazali
cooked: <p>I am doing semantic segmentation and was wondering if there is a method in PyTorch that will allow me to compute the CRF loss shown below? I am not trying to do inference. I just want to compute the loss based on the unary and pairwise terms.</p>
<p>I could do it myself. Replicate the output 8 times, shift the pixels accordingly and compute the difference to determine if the labels are similar but I don’t know if that would be efficient.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/4/045e193f7d2250e394f04b473a65a8711c6d17b3.png" data-download-href="https://discuss.pytorch.org/uploads/default/045e193f7d2250e394f04b473a65a8711c6d17b3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/045e193f7d2250e394f04b473a65a8711c6d17b3_2_344x62.png" alt="image" data-base62-sha1="CDvWDX7ROGyovFuoMDDVHpiUuL" width="344" height="62" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/045e193f7d2250e394f04b473a65a8711c6d17b3_2_344x62.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/045e193f7d2250e394f04b473a65a8711c6d17b3_2_516x93.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/045e193f7d2250e394f04b473a65a8711c6d17b3_2_688x124.png 2x" data-dominant-color="F3F3F3"><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">image</span><span class="informations">1299×237 28 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>

username: tom
cooked: <p>I think you want to give more context than that. I assume that phi is your “ground truth loss” (x being the ground truth or some such information) and psi the consistency loss.<br>
Now psi is rather underspecified in the formula. From your description, I looks like that i,j iterates over all pairs of adjacent pixels (of which there would be 8 if you have axis-parallel and diagonal neighbors in 2D or 8 if you have axis-parallel only in 3D).<br>
Quite likely, it is not terribly efficient but neither terribly inefficient to do the slicing manually ( (y[:, :, 1:, 1:] - y[:, :, :-1, :-1]).abs().sum() and friends).</p>
<p>Best regards</p>
<p>Thomas</p>

username: HaziqRazali
cooked: <p>Sorry, here is a description of the terms used</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/9/99056ba18ee72609b89df1d344a886ad3beedcfe.png" data-download-href="https://discuss.pytorch.org/uploads/default/99056ba18ee72609b89df1d344a886ad3beedcfe" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99056ba18ee72609b89df1d344a886ad3beedcfe_2_443x500.png" alt="image" data-base62-sha1="lPGFiTgExB7x5yFPc925YffNrY2" width="443" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/9/99056ba18ee72609b89df1d344a886ad3beedcfe_2_443x500.png, https://discuss.pytorch.org/uploads/default/original/3X/9/9/99056ba18ee72609b89df1d344a886ad3beedcfe.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/9/99056ba18ee72609b89df1d344a886ad3beedcfe.png 2x" data-dominant-color="EFEFEF"><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">image</span><span class="informations">583×658 55.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>
<p>But you are right, and that is exactly what I am trying to do. And thank you, doing it your way for each of the 8 neighbours</p>
<blockquote>
<p>y[:, :, 1:, 1:] - y[:, :, :-1, :-1]).abs().sum()</p>
</blockquote>
<p>Looks cleaner and more efficient than my initial plan to use torch.roll for each neighbour.</p>