@ptrblck It takes the current  [batch_size, nb_classes, height, width] target which is spread across 7 channels (8 now in order to add background) and then squeezes the nb_classes dimension from 8 to 1, since my pixels/classes are never overlapping (I’m dealing with documents).
Hence it iterates the  [batch_size, nb_classes, height, width] and transforms it to [batch_size, height, width] instead, whereas before each channel had a binary value for each pixel, it now has the equivalent long integer representing the class, as you suggested.
I’m pretty sure someone else (you most likely) can do the above in a simpler and more elegant way.
PS: b_size is batch size, and labels[batch] is the [nb_classes, height, width] tensor.