ConfusionMatrix metrics don't work for semantic segmentation

When using the Precision or Recall metrics (which derive from ConfusionMatrix), I get the follow exception:

  File "/opt/conda/lib/python3.6/site-packages/fastai/metrics.py", line 147, in on_batch_end
    cm = ((preds==self.x[:, None]) & (targs==self.x[:, None, None])).sum(dim=2, dtype=torch.float32)
RuntimeError: The size of tensor a (2100) must match the size of tensor b (300) at non-singleton dimension 3

This is because https://github.com/fastai/fastai/blob/master/fastai/metrics.py#L142 and https://github.com/fastai/fastai/blob/master/fastai/metrics.py#L145 assume that the last dimension is the one over classes. However, for semantic segmentation (using the builtin unet_learner), the class dimension has index 1 (in a tensor with four dimensions). To fix this, I propose adding a class_ind field to ConfusionMatrix which defaults to -1. Does this sound like a good way to fix the problem? If so, I can make a PR.

Thanks!

1 Like

Sure. Make sure a version with the right clas_idx works properly in you case though.

I have a problem with calculating confusion matrix for semantic image segmentation. I got this error
RuntimeError: The size of tensor a (360) must match the size of tensor b (5) at non-singleton dimension 1
I have 5 classes (4 classes + 1 Void class) could you please help me how I can use interp.ConfusionMatrix() in this case?

Thank you

can you post your solution? I am looking for the solution for this problem