I think IOU, per-pixel mean-IOU and per-image mean-IOU are often mis-interpreted.
For training, the code above is OK, but for report per-pixel mean-IOU, you can have a look at: PyTorch-ENet/confusionmatrix.py at a67d048ec837849eb79dfb8ec51b629a9738b362 · davidtvs/PyTorch-ENet · GitHub 343