在使用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)
这样就不会报错了。