Project-MONAI / tutorials

MONAI Tutorials
https://monai.io/started.html
Apache License 2.0
1.79k stars 669 forks source link

Adapting self-supervision tutorial for 2D case - contrastive loss #582

Closed ptvu281 closed 2 years ago

ptvu281 commented 2 years ago

Discussed in https://github.com/Project-MONAI/tutorials/discussions/581

Originally posted by **ptvu281** February 27, 2022 I'm trying to adapt the [self-supervision tutorial](https://github.com/Project-MONAI/tutorials/blob/master/self_supervised_pretraining/ssl_script_train.py) for a 2D case. However, I keep getting a tensor shape mismatch error at the contrastive loss step. I've posted a screenshot of the error here: [https://imgur.com/a/AXmWKgB](https://imgur.com/a/AXmWKgB) Looking at the [source code](https://docs.monai.io/en/latest/_modules/monai/losses/contrastive.html), I see that the error could be caused by the shape mismatch between `negatives_mask` and `sim_matrix` but I'm not sure how to proceed. The things I have changed to adapt for 2D are: - At the flatten step during training, change `end_dim=4` to `end_dim=3` - Change `img_size` and `patch_size` to a 2-tuple instead of 3-tuple Any suggestion on how to proceed is appreciated!!
Nic-Ma commented 2 years ago

Hi @yiheng-wang-nv ,

Could you please help confirm this question and provide some comments?

Thanks in advance.

yiheng-wang-nv commented 2 years ago

Hi @ptvu281 ,

the error means: negatives_mask.shape[1] = 8, and torch.exp(sim_matrix / temperature_tensor).shape[1] = 4,

where negatives_mask.shape[1] equals to self.batch_size * 2, torch.exp(sim_matrix / temperature_tensor).shape[1] equals to input.shape[1] + target.shape[1], these two values should be the same.

As shown in this line, could you please try to print the shape of flat_out_v1 and flat_out_v2, and what is the batch size you set?