pytorch conv2d value cannot be converted to type uint8_t without overflow

廉价感情. 提交于 2020-07-09 16:15:41

问题


I'm passing a torch.Tensor with a dtype of torch.uint8 to an nn.Conv2d module and it is giving the error

RuntimeError: value cannot be converted to type uint8_t without overflow: -0.0344873

My conv2d is defined as self.conv1 = nn.Conv2d(3, 6, 5). The error comes in my forward method when I pass the tensor to the module like self.conv1(x). The tensor has shape (4, 3, 480, 640). I'm not sure how to fix this. Here is the stack trace

Traceback (most recent call last):

  File "cnn.py", line 54, in <module>

    outputs = net(inputs)

  File "/Users/my_repos/venv_projc/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__

    result = self.forward(*input, **kwargs)

  File "cnn.py", line 24, in forward

    test = self.conv1(x)

  File "/Users/my_repos/venv_projc/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__

    result = self.forward(*input, **kwargs)

  File "/Users/my_repos/venv_projc/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 345, in forward

    return self.conv2d_forward(input, self.weight)

  File "/Users/my_repos/venv_projc/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 342, in conv2d_forward

    self.padding, self.dilation, self.groups)

RuntimeError: value cannot be converted to type uint8_t without overflow: -0.0344873

回答1:


Converting the tensor to a float seemed to fix it self.conv1(x.float())



来源:https://stackoverflow.com/questions/60253449/pytorch-conv2d-value-cannot-be-converted-to-type-uint8-t-without-overflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!