[英]How to I reshape the 2D array like this? (By using tensor)
I want to resize my image from 32 * 32 to 16 * 16. (By using torch.tensor) Like decreasing the resolution?我想将图像的大小从 32 * 32 调整为 16 * 16。(通过使用 torch.tensor)喜欢降低分辨率吗? Can anyone help me?
谁能帮我?
If you have an image (stored in a tensor) and you want to decrease it's resolution , then you are not reshaping
it, but rather resizing it.如果你有一张图像(存储在张量中)并且你想降低它的分辨率,那么你不是在
reshaping
它,而是调整它的大小。
To that end, you can use pytorch's interpolate
:为此,您可以使用 pytorch 的
interpolate
:
import torch
from torch.nn import functional as nnf
y = nnf.interpolate(x[None, None, ...], size=(16, 16), mode='bicubic', align_corners=False, antialias=True)
Notes:笔记:
nnf.interpolate
operates on batches of multi-channel images, that is, it expects its input x
to have 4 dimensions: batch
- channels
- height
- width
. nnf.interpolate
对成批的多通道图像进行操作,也就是说,它期望其输入x
具有 4 个维度: batch
- channels
- height
- width
。 So, if your x
is a single image with a single channel (eg, an MNIST digit) you'll have to create a singleton batch dimension and a singleton channel dimension.x
是具有单个通道的单个图像(例如,MNIST 数字),您将必须创建一个单件批次维度和一个单件通道维度。align_corners
and antialias
-- make sure you are using the right configuration for your needs.align_corners
和antialias
- 确保您使用正确的配置来满足您的需求。 For more information regarding aliasing and alignment when resizing images you can look at ResizeRight .有关调整图像大小时的锯齿和对齐的更多信息,您可以查看ResizeRight 。
you can use resize from torchvision transforms:您可以使用 torchvision 变换中的调整大小:
import torchvision.transform as T
T.Resize(16)(your_image)
you can also chain it to other transforms您还可以将其链接到其他转换
preprocess = T.Compose([
T.Resize(N),
T.CenterCrop(N),
T.ToTensor(),
T.Normalize(
mean=mean,
std=std
)
])
preprocess(your_image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.