(N, C, d1, d2, ...,dK) format and output size in (o1, o2, ...,oK) format.报错

在使用transform.Resize函数时报错

D:\Anaconda\envs\pytorch\python.exe E:/HSI/hyper_code/impro-unet/student_model/feature_maps.py
torch.Size([64, 128, 128])
torch.Size([256, 256])
Traceback (most recent call last):
  File "E:/HSI/hyper_code/impro-unet/student_model/feature_maps.py", line 224, in <module>
    a = model(m, feature_label)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "E:/HSI/hyper_code/impro-unet/student_model/feature_maps.py", line 203, in forward
    p1 = feature_map_zero(p1, feature_label, self.model_t, self.importance_ratio, self.error_rate)
  File "E:/HSI/hyper_code/impro-unet/student_model/feature_maps.py", line 156, in feature_map_zero
    one_label = transform.Resize((128, 128))(one_label)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 349, in forward
    return F.resize(img, self.size, self.interpolation, self.max_size, self.antialias)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torchvision\transforms\functional.py", line 438, in resize
    return F_t.resize(img, size=size, interpolation=interpolation.value, max_size=max_size, antialias=antialias)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torchvision\transforms\functional_tensor.py", line 490, in resize
    img = interpolate(img, size=[new_h, new_w], mode=interpolation, align_corners=align_corners)
  File "D:\Anaconda\envs\pytorch\lib\site-packages\torch\nn\functional.py", line 3835, in interpolate
    raise ValueError(
ValueError: Input and output must have the same number of spatial dimensions, but got input with with spatial dimensions of [256] and output size of [128, 128]. Please provide input tensor in (N, C, d1, d2, ...,dK) format and output size in (o1, o2, ...,oK) format.

进程已结束,退出代码1

一直说我的输入图像大小为[256] 输出为 [128, 128],可是输入是torch.Size([256, 256])。其实这个错误就是说明维度不对,应该添加一个维度,再使用这个函数。

报错的程序是这样的:

b, h, w = one_batch_feature_maps.shape
one_label = transform.Resize((h, w))(one_label)

修改:

b, h, w = one_batch_feature_maps.shape
one_label = transform.Resize((h, w))(one_label.unsqueeze(0))
one_label = one_label.squeeze(0)

这样就不会报错了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值