I don’t see anything obviously wrong with this immediately, but I also don’t recall the exact data layout and format used here. The best practice would be to write some predictable test cases if you aren’t sure about the correctness (e.g., try generating synthetic model outputs and labels like an entire image with just one class, half and half). You could also check that random outputs get roughly the accuracy of random guessing, etc…