python - torch.nn.fucntional.interpolate():参数设置
问题描述
我正在使用torch.nn.functional.interpolate()
调整图像大小。
首先,我使用transforms.ToTensor()
将图像转换为张量,其大小为 (3, 252, 252),(252, 252) 是导入图像的大小。我想要做的是用interpolate()
函数创建一个大小为 (3, 504, 504) 的张量。
我设置了 para scale_factor=2
,但它返回了 (3, 252, 504) 张量。然后我设置它scale_factor=(1,2,2)
并收到这样的尺寸冲突错误:
size shape must match input shape. Input is 1D, size is 3
那么我应该如何设置参数以接收 (3, 504, 504) 张量?
解决方案
如果您正在使用scale_factor
,则需要提供一批图像而不是单个图像。所以你需要通过使用添加一批unsqueeze(0)
然后赋予它interpolate
如下功能:
import torch
import torch.nn.functional as F
img = torch.randn(3, 252, 252) # torch.Size([3, 252, 252])
img = img.unsqueeze(0) # torch.Size([1, 3, 252, 252])
out = F.interpolate(img, scale_factor=(2, 2), mode='nearest')
print(out.size()) # torch.Size([1, 3, 504, 504])
推荐阅读
- javascript - 如果查询为真,则禁用字段
- bash - 每次安装时都需要手动创建符号链接
- python - 如何将机器学习分类方法应用于一维时间序列数据
- java - 为什么 executeUpdate() 函数不起作用?给出在普通项目而不是基于 maven 的项目中解决的步骤
- c# - IX509PrivateKey::C# 中的导出方法
- angular - Azure 应用服务 500 错误超时 Http Post
- jquery - 使用 JQuery 更改后如何从表中选择第一行?
- javascript - jQuery on click 循环通过相同的 .wrappers 中的图像独立
- android - 如何检查我的应用程序是否正在android中调试?
- asp.net - asp.net 样板 (ABP) OdataController 和 AddODataQueryFilter