Get the right input type but my code still not work

All the tensor is ByteTensor, why this error occur?

I get this error ‘RuntimeError: expected Byte tensor (got Float tensor)’, but I print the input x showing it is Byte tensor.

How to fix this?

Part of code.

def forward(self, x1, x2, y):
    print(x1.data)
    x1 = self.conv1(x1)      <------- ERROR HERE

Traceback (most recent call last):
File “/Users/wzy/PycharmProjects/pytorch/resnet.py”, line 277, in
train(epoch)
File “/Users/wzy/PycharmProjects/pytorch/resnet.py”, line 241, in train
h_x1, h_x2, h_y = model(x1, x2, y)
File “/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “/Users/wzy/PycharmProjects/pytorch/resnet.py”, line 166, in forward
x1 = self.conv1(x1)
File “/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/module.py”, line 206, in call
result = self.forward(*input, **kwargs)
File “/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/conv.py”, line 237, in forward
self.padding, self.dilation, self.groups)
File “/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/functional.py”, line 40, in conv2d
return f(input, weight, bias)
RuntimeError: expected Byte tensor (got Float tensor)

(0 ,0 ,.,.) =
0 0 0 … 255 255 255
0 0 0 … 255 255 255
0 0 1 … 255 255 255
… ⋱ …
0 0 1 … 255 255 255
0 0 0 … 255 255 255
0 0 0 … 255 255 255

(0 ,1 ,.,.) =
0 0 0 … 255 255 255
0 0 0 … 255 255 255
0 0 1 … 255 255 255
… ⋱ …
0 0 1 … 255 255 255
0 0 0 … 255 255 255
0 0 0 … 255 255 255

(0 ,2 ,.,.) =
0 0 0 … 255 255 255
0 0 0 … 255 255 255
0 0 1 … 255 255 255
… ⋱ …
0 0 1 … 255 255 255
0 0 0 … 255 255 255
0 0 0 … 255 255 255
[torch.ByteTensor of size 1x3x28x28]

Cek your input. I think your input in form of Float Tensor. I always have same problem for the first time. Sometimes we convert from numpy give different tensor type. Anyway just for the shortcut, if your model wants Byte tensor, just cast your input tensor to byte tensor. For example your input tensor name is input , so you can use input.type(torch.ByteTensor) it will automatically convert your float tensor to byte tensor.

Thanks,however I write my custom dataset class with
imgx1 = torch.ByteTensor(imgx1).view(28, 28, 3).permute(2, 1, 0)
return imgx1
I think the input is ByteTensor when I get input.

If all tensors ByteTensor, to help ensure this try entering into code:

torch.set_default_tensor_type('torch.ByteTensor')

Thanks, I got the following error, is this a problem with the resnet model ?

Traceback (most recent call last):
  File "/Users/wzy/PycharmProjects/pytorch/resnet.py", line 211, in <module>
    model = resnet101()
  File "/Users/wzy/PycharmProjects/pytorch/resnet.py", line 205, in resnet101
    model = ResNet(Bottleneck, [3, 4, 23, 3], **kwargs)
  File "/Users/wzy/PycharmProjects/pytorch/resnet.py", line 127, in __init__
    bias=False)
  File "/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 233, in __init__
    False, _pair(0), groups, bias)
  File "/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 37, in __init__
    self.reset_parameters()
  File "/Users/wzy/anaconda/lib/python3.5/site-packages/torch/nn/modules/conv.py", line 44, in reset_parameters
    self.weight.data.uniform_(-stdv, stdv)
AttributeError: 'torch.ByteTensor' object has no attribute 'uniform_'

Did you get it to work? I got the same issue. Thanks

HI,this is a confusing error message and it will be fixed in the new version. You can see pytorch github issues for more details.

1 Like

Hello @xiao1228 Sorry for my late reply. Here I try in Pytorch version 0.2 but I think it also works in version 0.1.
image

1 Like