The size of tensor a (3) must match the size of tensor b (224) at non-singleton dimension 4

Hi,

I have an annoying issue that I can’t handle.

Some say that it is from batch size, others from the metric used for a non/binary segmentation, etc…
I tested different batch size, it is a binary segmentation, so I want to use IoU.

Quite lost, may you help me please ?


from catalyst.dl.callbacks import DiceCallback, IouCallback, \
  CriterionCallback

runner.train(
    model=model,
    criterion=criterion,
    optimizer=optimizer,
    scheduler=scheduler,
    
    # our dataloaders
    loaders=loaders,
    
    callbacks=[
        # Each criterion is calculated separately.
        CriterionCallback(
            input_key="mask",
            prefix="loss_iou",
            criterion_key="iou"
        ),
        
        # metrics
        IouCallback(input_key="mask"),
    ],
    # path to save logs
    logdir=logdir,
    
    num_epochs=num_epochs,
    
    # save our best checkpoint by IoU metric
    main_metric="iou",
    # IoU needs to be maximized.
    minimize_metric=False,
    
    # for FP16. It uses the variable from the very first cell
    fp16=fp16_params,
    
    # for external monitoring tools, like Alchemy
    monitoring_params=monitoring_params,
    
    # prints train logs
    verbose=True,
)

Based on the shapes in the error message it seems you are trying to apply an operation on a channel-first and another channel-last tensor.
Which line of code throws this error?

Here is the output, I agree it is about the shape but seeing this topic, I’m not so sure : https://forums.fast.ai/t/runtimeerror-the-size-of-tensor-a-115-must-match-the-size-of-tensor-b-64-at-non-singleton-dimension-1/50953/17

1/20 * Epoch (train):   0% 0/1 [00:00<?, ?it/s]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-78-330e12609d6c> in <module>()
     38 
     39     # prints train logs
---> 40     verbose=True,
     41 )

14 frames
/usr/local/lib/python3.6/dist-packages/catalyst/utils/criterion/iou.py in iou(outputs, targets, classes, eps, threshold, activation)
     42         _sum = torch.sum
     43 
---> 44     intersection = _sum(targets * outputs)
     45     union = _sum(targets) + _sum(outputs)
     46     # this looks a bit awkward but `eps * (union == 0)` term

RuntimeError: The size of tensor a (3) must match the size of tensor b (224) at non-singleton dimension 4

Could you print the shapes of targets and outputs, please?