lightly-ai / lightly

A python library for self-supervised learning on images.
https://docs.lightly.ai/self-supervised-learning/
MIT License
2.92k stars 248 forks source link

RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x256 and 512x1024) #1589

Closed CharisWg closed 1 month ago

CharisWg commented 1 month ago

I encountered this error: Is self.prediction_head = BYOLPredictionHead(256, 1024, 256) incorrect? Can anyone help me?"

Traceback (most recent call last): File "/datashare3/charis/code2/SSL-yolo8/lightly-master/examples/pytorch/byol_Charis1.py", line 117, in p0 = byol(x0) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, *kwargs) File "/datashare3/charis/code2/SSL-yolo8/lightly-master/examples/pytorch/byol_Charis1.py", line 42, in forward z = self.projection_head(y) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(args, kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(*args, kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/lightly/models/modules/heads.py", line 65, in forward projection: Tensor = self.layers(x) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(args, kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/container.py", line 217, in forward input = module(input) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl return self._call_impl(*args, *kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1541, in _call_impl return forward_call(args, **kwargs) File "/datashare3/charis/anaconda/envs/LSSL/lib/python3.9/site-packages/torch/nn/modules/linear.py", line 116, in forward return F.linear(input, self.weight, self.bias) RuntimeError: mat1 and mat2 shapes cannot be multiplied (256x256 and 512x1024)

guarin commented 1 month ago

Hi! It looks like the input to the projection_head has dimension 256 instead of 512 (note that this is the projection head and not the prediction head!). If your backbone outputs features with dimension 256 you have to change the first layer of the projection head accordingly:


self.projection_head = BYOLProjectionHead(256, 1024, 256)