RuntimeError when passing models.resnet50: expected scalar type Byte but found Float

Hi everyone, I am working on an encoder model based on resnet50. And whenever I pass the images into the encoder, the RuntimeError occurs:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-67-bfcd094ef931> in <module>()
     23         '''
     24         # input EncoderEps, train DF
---> 25         fL = EncoderEps(LRimg)
     26         fH = EncoderEps(HRimg)
     27         f_size = fL.size(0)

4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

<ipython-input-64-952baa4092a1> in forward(self, x)
     30 
     31     def forward(self,x):
---> 32         x = self.model.conv1(x)
     33         print("Success with conv1")
     34         x = self.model.bn1(x)

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--> 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    397 
    398     def forward(self, input: Tensor) -> Tensor:
--> 399         return self._conv_forward(input, self.weight, self.bias)
    400 
    401 class Conv3d(_ConvNd):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    394                             _pair(0), self.dilation, self.groups)
    395         return F.conv2d(input, weight, bias, self.stride,
--> 396                         self.padding, self.dilation, self.groups)
    397 
    398     def forward(self, input: Tensor) -> Tensor:

RuntimeError: expected scalar type Byte but found Float

It is shown that the error occur in the conv1 layer when it is calling the Conv2d and something related with the padding, dilation parameters. But I don’t know how to deal with it.
My encoder model code is like this:

def weights_init_kaiming(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') # For old pytorch, you may use kaiming_normal.
    elif classname.find('Linear') != -1:
        init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')
        init.constant_(m.bias.data, 0.0)
    elif classname.find('BatchNorm1d') != -1:
        init.normal_(m.weight.data, 1.0, 0.02)
        init.constant_(m.bias.data, 0.0)

def weights_init_normal(m):
    classname = m.__class__.__name__
    if classname.find('Linear') != -1:
        init.normal_(m.weight.data, std=0.001)
        init.constant_(m.bias.data, 0.0) 

class encoder(nn.Module):
    def __init__(self, reduced_dim=256):
        super(encoder, self).__init__()
        Resnet_50 = models.resnet50(pretrained=True)
        #base_model = Resnet_50
        base_model = nn.Sequential(*(list(Resnet_50.children())[:-2]))
        self.model = base_model

        add_block = nn.Sequential(nn.Conv2d(2048, reduced_dim, kernel_size=1, stride=1, padding=0),
                                  nn.BatchNorm2d(reduced_dim))
        add_block.apply(weights_init_kaiming) # 为什么在原resnet结构中添加模块需要初始化参数呢?
        self.add_block = add_block
    
    def forward(self,x):
        x = self.model(x)
        f = self.add_block(x)
        print("Success with addblock")
        return f

Maybe something is run with my data? my trainning dataloader is built with the transform like this:

trndata_trans = transforms.Compose([transforms.ToPILImage(),
                                    transforms.Resize((256,128)),
                                    transforms.RandomHorizontalFlip(),
                                    transforms.RandomVerticalFlip(),
                                    transforms.ToTensor(),
                                    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

Can anyone help me out with this plz?

1 Like

Did you figure it out? I have the same issue

I have the same issue as well

These (padding, dilation and groups) are already byte size…

RuntimeError: expected scalar type Byte but found Float

return F.conv2d(input, weight, bias, self.stride, self.padding, self.dilation, self.groups)