Sorry for bumping this, but for anyone who comes across this and it seems like magic…
img.contiguous() makes sure the tensor is in a contiguous chunk of memory for efficient access.
.view(img.size(0), -1) resizes from [c, x, y, z…etc] to [c, n] where n = xyz…
.std(-1) takes the standard deviation when you collapse the last channel so you end up with the channel mean.
If the last command is confusing, it’s because pytorch dims can be confusing, here is a nice explanation
Understanding dimensions in pytorch 6