berniwal/swin-transformer-pytorch

fail to run the code

Closed this issue · 5 comments

Hi, i'm intereted in your code! But when i run the example of it,

Traceback (most recent call last):
File "D:/Code/Pytorch/swin-transformer-pytorch-0.4/example.py", line 16, in
logits = net(dummy_x) # (1,3)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 219, in forward
x = self.stage1(img)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 190, in forward
x = regular_block(x)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 149, in forward
x = self.attention_block(x)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 22, in forward
return self.fn(x, **kwargs) + x
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 32, in forward
return self.fn(self.norm(x), **kwargs)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 117, in forward
dots += self.pos_embedding[self.relative_indices[:, :, 0], self.relative_indices[:, :, 1]]
IndexError: tensors used as indices must be long, byte or bool tensors

And when i change the type to long, the code has another error.

Traceback (most recent call last):
File "D:/Code/Pytorch/swin-transformer-pytorch-0.4/example.py", line 16, in
logits = net(dummy_x) # (1,3)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 219, in forward
x = self.stage1(img)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 188, in forward
x = self.patch_partition(x)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Code\Pytorch\swin-transformer-pytorch-0.4\swin_transformer_pytorch\swin_transformer.py", line 164, in forward
x = self.patch_merge(x).view(b, -1, new_h, new_w).permute(0, 2, 3, 1)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\modules\fold.py", line 295, in forward
self.padding, self.stride)
File "D:\Softwares\Anaconda\envs\pytorch_18\lib\site-packages\torch\nn\functional.py", line 4313, in unfold
return torch._C._nn.im2col(input, _pair(kernel_size), _pair(dilation), _pair(padding), _pair(stride))
RuntimeError: "im2col_out_cpu" not implemented for 'Long'

Thanks for reporting the error, other people had the first issue as well and solved it by either upgrading the numpy version or changing to [self.relative_indices[:, :, 0].type(torch.long), self.relative_indices[:, :, 1].type(torch.long)]. In you case the second fix seems no to work, so could you tell me the Python, Numpy and Torch version you are using such that I can reproduce the error on my machine?

Thanks for reporting the error, other people had the first issue as well and solved it by either upgrading the numpy version or changing to [self.relative_indices[:, :, 0].type(torch.long), self.relative_indices[:, :, 1].type(torch.long)]. In you case the second fix seems no to work, so could you tell me the Python, Numpy and Torch version you are using such that I can reproduce the error on my machine?

Thank you for your reply, now I can successfully run the code. (Python=3.7, Numpy=1.20, Pytorch=1.8)

So upgrading to a newer numpy version fixed the issue? Is this the working configuration or the configuration you got the error with? In the first case could you also provide me the failing configuration settings?

So upgrading to a newer numpy version fixed the issue? Is this the working configuration or the configuration you got the error with? In the first case could you also provide me the failing configuration settings?

Maybe there is something wrong with my expression, before you reply I just changed the type of input data instead of model. When I changing to [self.relative_indices[:, :, 0].type(torch.long), self.relative_indices[:, :, 1].type(torch.long)], the code could be run successfully.

Instead of numpy version, changing line no 117 on swing_transformer.py with the following is best option, as mentioned previously.

dots += self.pos_embedding[self.relative_indices[:, :, 0].type(torch.long), self.relative_indices[:, :, 1].type(torch.long)]