解决PyTorch报错:RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatT
2020-07-20 04:21
369 查看
1. 问题描述
1.1 在使用pytorch0.4.1推理pyramidbox 或者SSD时,发生以下报错:
RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #2 'other'
在pytorch0.3上面应该时不报错的,改成0.4之后发生报错。
[code]Traceback (most recent call last): File "test.py", line 241, in <module> det0 = detect_face(image, shrink) File "test.py", line 60, in detect_face y = net(x) File "....../lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__ result = self.forward(*input, **kwargs) File "....../pyramidbox/pyramid.py", line 452, in forward self.priors.type(type(x.data)) # default boxes File "....../pyramidbox/layers/functions/detection.py", line 46, in forward decoded_boxes = decode(loc_data[i], prior_data, self.variance) File "....../pyramidbox/layers/box_utils.py", line 231, in decode priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:], RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #2 'other'
1.2 找到报错的box_utils.py
[code]def decode(loc, priors, variances): boxes = torch.cat(( priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:], priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1])), 1) boxes[:, :2] -= boxes[:, 2:] / 2 boxes[:, 2:] += boxes[:, :2] return boxes
2. 解决问题
修改了的变量应该是torch.cuda.FloatTensor,CUDA类型才对,因为版本问题,在0.4上被指派到了CPU上,所以要强行修改回cuda类型。修改如下:
[code]def decode(loc, priors, variances): boxes = torch.cat(( priors[:, :2].cuda() + loc[:, :2].cuda() * variances[0] * priors[:, 2:].cuda(), priors[:, 2:].cuda() * torch.exp(loc[:, 2:].cuda() * variances[1])), 1) boxes[:, :2] -= boxes[:, 2:] / 2 boxes[:, 2:] += boxes[:, :2] return boxes
至此,问题得到解决。
相关文章推荐
- RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for ar
- RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.DoubleTensor
- 使用nn.init自定义权重,总是报错RuntimeError: Expected object of scalar type Double but got scalar type Float for
- Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same(解决)
- Expected object of backend CPU but got backend CUDA for argument #2 'weight' 报错的问题和解决办法
- No qualifying bean of type is defined: expected single matching bean but found 2
- cli3.0+总是报错:Expected indentation of 0 spaces but found 2如何解决
- python3环境下 tensorflow环境中经常遇到'*' has type str, but expected one of: bytes问题的解决
- RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal
- 解决编译错误:Property 'value' not found on object of type '_strong id'
- Expected indentation of 0 spaces but found 4解决办法
- python3环境下 tensorflow环境中经常遇到'*' has type str, but expected one of: bytes问题的解决
- JAVA: 解决is expected to be of type but was actually of type com.sun.proxy.$Proxy的问题
- Bean named '...' is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法
- client found response content type of text/html charset=utf-8 but expected text/xml
- Spring的JUnit错误:NoSuchBeanDefinitionException: No unique bean of type,expected single matching bean but found 2 [dataSource1,data
- expected 'float **' but argument is of type 'float (*)[5]'
- Function MulBackward0 returned an invalid gradienttorch.FloatTensor but got torch.cuda.FloatTensor
- **Bean named 'XXX' is expected but was actually of type 'com.sun.proxy.$Proxy**'的两种解决方法**
- Property or field 'remark' cannot be found on object of type '....'问题解决