Using MSELoss, how to calculate loss per voxel?

username: pyto
cooked: <p>Hey everyone,</p>
<p>I am still pretty new to neural networks and pytorch. Now that my Neural Net seems to be computing pretty great results, I am having a problem with the interpretation of the losses (training loss seems to be always higher than validation loss).</p>
<p>But first, here is the information about the data I have been working with:</p>
<ul>
<li>3d CT images (so only one channel)</li>
<li>always same sizes for height (512) and width (512)</li>
<li>varying size for depth</li>
</ul>
<p>Output should be denoised images.</p>
<p>Since the images of course are too big  to train with the whole image, I had to work with patches and took the same number of random sample patches from each image.</p>
<p>For training I am using the sample patches of 47 images and for validation of 12 images.</p>
<p>In order to be able to compare the losses obtained during training and validation to the testing loss of the complete images, I would like to have loss plots that show the average loss per voxel (this also makes sense to me since the images are not the same sizes in depth)</p>
<p>Now I already tried setting reduction=‘sum’ and adding all mini-batch losses which in the end of each epoch are devided by the whole number of training-patches or validation-patches and then devided by the number of voxels per patch.</p>
<p>But somehow this always gives me a higher loss for training than for validation. Especially for the first Epoch, the loss is easily 10 times higher for training.</p>
<p>I already tried a lot of loss calculations, but nothing seems right. Could someone please try to help me out?</p>
<p>Thank you in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ptrblck
cooked: <p>Could you post the loss calculation and in particular how the average loss per voxel is calculated?<br>
You can sometimes observe a higher training loss compared to the validation loss, if e.g. dropout is used and the training model has thus less capacity.<br>
However, a 10x increase seems too large.</p>

username: pyto
cooked: <p>Thanks for the fast reply!</p>
<p>Since I didn’t use any dropout, I guess something about my calculation is very off.</p>
<p>In the end I thought calculating how far off my resulting images are from the groundtruth by using L1 and L2 distance per Voxel as such example (in order to be able to compare statistics of different images, with different sizes, to each other):<br>
Image groundtruth g<br>
Image output o<br>
–&gt; images as numpy arrays:<br>
abs_difference = np.absolute(g-o)<br>
l1 = np.mean(abs_difference)<br>
l2 = np.sqrt(np.mean(abs_difference*abs_difference))<br>
If my idea is completely off, please tell me.</p>
<p>So now I switched to L1Loss, because I realized my mistake and MSELoss will not work per voxel if I don’t want to implement my own loss function (should have realized that earlier).<br>
Still with L1Loss and reduction=‘sum’, and summing all losses of one epoch, then dividing them by (‘number of training or validation patches’ times ‘number of voxels per patch’) gives me a loss twice as high for training than validation in the beginning. Is this problematic? It is becoming better fast though (similar values for training and validation).</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="pyto" data-post="3" data-topic="82709">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/f19dbf/48.png" class="avatar"> pyto:</div>
<blockquote>
<p>Still with L1Loss and reduction=‘sum’, and summing all losses of one epoch, then dividing them by (‘number of training or validation patches’ times ‘number of voxels per patch’) gives me a loss twice as high for training than validation in the beginning. Is this problematic? It is becoming better fast though (similar values for training and validation).</p>
</blockquote>
</aside>
<p>If you are accumulating the training loss for the complete epoch and calculate the average at the end of each epoch, the training loss might be higher, since your model was trained during the epoch.<br>
E.g. the first batch might yield a training loss of 100, while the last batch yields 10. While the validation loss would be calculated using the “trained” model, the training loss would be the average of the complete epoch.</p>
<p>To verify it, you could recalculate the training loss after each epoch and compare it to the validation loss.</p>

username: pyto
cooked: <p>Ahh. Thank you!! That makes sense.</p>