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!