Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

在修改完网络并训练时报了RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same这个错误,记录一下错误原因以及解决方法。

首先确保所有数据都是全程在cuda上运行的没错,所以我在原先的main函数基础上,新增了将model和x、y都放在cuda上,结果运行之后还是报错。

if __name__ == '__main__':
    upscale = 4
    window_size = 8
    height, width = 224, 224
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = SwinTransformerFusion(img_size=224, patch_size=4, in_chans=64, num_classes=5,
                                  embed_dim=96, depths=[2, 2, 2, 2], depths_decoder=[1, 2, 2, 2],
                                  num_heads=[3, 6, 12, 24],
                                  window_size=7, mlp_ratio=4., qkv_bias=True, qk_scale=None,
                                  drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1,
                                  norm_layer=nn.LayerNorm, ape=False, patch_norm=True,
                                  use_checkpoint=False, final_upsample="expand_first")
    
    x = torch.randn((1, 64, height, width))
    y = torch.randn((1, 64, height, width))
    model = model.to(device)
    x, y = x.to(device, torch.float32), y.to(device, torch.float32)
    x = model(x, y)
    print(x.shape)

报错就出在我自己添加的x = conv_after_fusion(x)这个函数上,看下报错:

Traceback (most recent call last):
  File "F:\MyCode\SwinFusionUnet\models\SwinFUnet.py", line 1682, in <module>
    x = model(x, y)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "F:\MyCode\SwinFusionUnet\models\SwinFUnet.py", line 1647, in forward
    x, x_downsample, y, y_downsample = self.forward_features_Fusion(x, y)
  File "F:\MyCode\SwinFusionUnet\models\SwinFUnet.py", line 1562, in forward_features_Fusion
    x = self.conv_after_Fusion(x)
  File "F:\MyCode\SwinFusionUnet\models\SwinFUnet.py", line 1582, in conv_after_Fusion
    x = conv_after_fusion(x)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 463, in forward
    return self._conv_forward(input, self.weight, self.bias)
  File "D:\Anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 459, in _conv_forward
    return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

 这个错误出在我自己新增的函数x = conv_after_fusion(x)上,函数里有一个卷积操作:

conv_after_fusion = nn.Conv2d(in_channels=in_channels, out_channels=in_channels // 2, kernel_size=3, stride=1,
                                      padding=1)

问题就在这一行,卷积时参数不知为何不是cuda类型的,于是在这个函数后加上.cuda()

conv_after_fusion = nn.Conv2d(in_channels=in_channels, out_channels=in_channels // 2, kernel_size=3, stride=1,
                                      padding=1).cuda()

问题成功解决。总结一下如果这个卷积操作在SwinTransformerBlock初始化时就以定义,那应该就不会有问题,但由于我是在自己在类里写的函数里定义了卷积,那它的参数可能就默认在cpu上。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值