我想知道我是否可以在Pytorch中构建一个图像大小调整模块,它将3*H*W的torch.tensor作为输入并返回张量作为调整大小的图像.
我知道可以将张量转换为PIL图像并使用torchvision,但我也希望将调整后的图像中的渐变传播回原始图像,以下示例将返回此类错误(在Windows 10上的PyTorch 0.4.0中) :
import numpy as np
from torchvision import transforms
t2i = transforms.ToPILImage()
i2t = transforms.ToTensor()
trans = transforms.Compose(
t2i, transforms.Resize(size=200), i2t]
)
test = np.random.normal(size=[3, 300, 300])
test = torch.tensor(test, requires_grad=True)
resized = trans(test)
resized.backward()
print(test.grad)
Traceback (most recent call last):
File "D:/Projects/Python/PyTorch/test.py", line 41, in <module>
main()
File "D:/Projects/Python/PyTorch/test.py", line 33, in main
resized = trans(test)
File "D:\Anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 42, in __call__
img = t(img)
File "D:\Anaconda3\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py", line 103, in __call__
return F.to_pil_image(pic, self.mode)
File …
Run Code Online (Sandbox Code Playgroud)