It seems you are permuting the spatial dimensions into the batch dimension of the input tensor, while the target tensor seems to use the original batch dimension, which will thus break.
Both, the model output and target, are expected to have the same batch dimension, which represents the number of samples in the current batch.
I’m not familiar with your use case, so don’t know why the reshaping is needed, so could you explain it a bit more?