Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: The size of tensor a (1600) must match the size of tensor b (196) at non-singleton dimension 1 #1

Closed
SeyedAliRezMousavi opened this issue Sep 13, 2021 · 2 comments

Comments

@SeyedAliRezMousavi
Copy link

I tried to train model but got error:

UCTransNet
transformer head num: 4
transformer layers num: 4
transformer expand ratio: 4
Let's use 2 GPUs!
log dir:

========= Epoch [1/2001] =========
Test_session_09.13_13h31
Training with batch size : 4
Traceback (most recent call last):
  File ".\train.py", line 187, in <module>
    model = main_loop(model_type=config.model_name, tensorboard=True)
  File ".\train.py", line 135, in main_loop
    train_one_epoch(train_loader, model, criterion, optimizer, writer, epoch, None, model_type, logger)
  File "D:\Projects\Crack\UCTransNet\Train_one_epoch.py", line 70, in train_one_epoch
    preds = model(images)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\parallel\data_parallel.py", line 168, in forward
    outputs = self.parallel_apply(replicas, inputs, kwargs)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\parallel\data_parallel.py", line 178, in parallel_apply
    return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\parallel\parallel_apply.py", line 86, in parallel_apply
    output.reraise()
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\_utils.py", line 425, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in replica 0 on device 0.
Original Traceback (most recent call last):
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\parallel\parallel_apply.py", line 61, in _worker
    output = module(*input, **kwargs)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\Projects\Crack\UCTransNet\models\UCTransNet.py", line 123, in forward
    x1,x2,x3,x4,att_weights = self.mtc(x1,x2,x3,x4)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\Projects\Crack\UCTransNet\models\CTrans.py", line 348, in forward
    emb1 = self.embeddings_1(en1)
  File "C:\Users\techno v\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\Projects\Crack\UCTransNet\models\CTrans.py", line 43, in forward
    embeddings = x + self.position_embeddings
RuntimeError: The size of tensor a (1600) must match the size of tensor b (196) at non-singleton dimension 1
@McGregorWwww
Copy link
Owner

Hi, please check your image size, the size we used in the code is 224*224, so the number of patches is (224/16)^2=196. If you do not want to resize your images, you can change the "img_size" parameter in "Config.py". Hope this can help!

@SeyedAliRezMousavi
Copy link
Author

thanks.
https://github.com/McGregorWwww/UCTransNet/commit/c136ee5530e46fe40c250a029783387a39082546

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants