Pytorch中的resize和reshape

torch.reshape()

官方文档的大致意思是:

返回与输入具有相同数据和元素数量的张量,但是具有指定形状。如果可能,返回的张量将是输入的视图,也就是说原本的tensor并没有被改变,如果想要改变那么就将改变的tensor赋值给原本的就行,即:tensor_temp = torch.reshape(tensor_temp, xxx))。

个人理解是给数组一个新的形状,数据和元素数量及顺序不变,按照形状截断、重新拼起来成为一个张量,可以使用reshape用来增加数组的维度。

代码示例:

import torch
a=torch.tensor([[[1,2,3],[4,5,6]],
                [[7,8,9],[10,11,12]]])
print("a的shape:",a.shape)
b=torch.reshape(a,((4,3,1)))
print("b:",b)
print("b的shape:",b.shape)
a的shape: torch.Size([2, 2, 3])
b: tensor([[[ 1],
         [ 2],
         [ 3]],

        [[ 4],
         [ 5],
         [ 6]],

        [[ 7],
         [ 8],
         [ 9]],

        [[10],
         [11],
         [12]]])
b的shape: torch.Size([4, 3, 1])

占位符-1:

import torch
a=torch.tensor([[[1,2,3],[4,5,6]],
                [[7,8,9],[10,11,12]]])
b=torch.reshape(a,(-1,))
c=torch.reshape(a,(-1,1))
d=torch.reshape(a,((-1,1,1)))
e=torch.reshape(a,((-1,1,1,1)))
f=torch.reshape(a,((-1,3,4)))

# a的shape:torch.Size([2, 2, 3])
# b的shape:torch.Size([12])
# c的shape:torch.Size([12, 1])
# d的shape: torch.Size([12, 1, 1])
# e的shape: torch.Size([12, 1, 1, 1])
# f的shape: torch.Size([1, 3, 4])

-1表示表示其具体值由其他维度信息和元素总个数推断出来。

一个使用场景:

对于torch.nn.functional.conv2d这个函数

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

其中参数input – input tensor of shape (minibatch, in_channels, iH, iW)是一个4D张量,如果要把下面这样一个输入矩阵,变为4D张量,就可以使用reshape函数。

[[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]]

input = torch.reshape(input,[1,1,5,5])

torch.transforms.Resize()

transforms.Resize()可以作为数据预处理管道中的一个步骤,用于将输入的图像按照指定的大小或缩放因子进行调整。这样可以确保输入的图像满足模型的输入要求,或者保持不同数据样本具有相同的输入尺寸,在训练或推理过程中方便批次处理。

用来调整PILImage对象的尺寸,注意不能是用io.imread或者cv2.imread读取的图片,这两种方法得到的是ndarray,input只能是PIL Image or Tensor

from torchvision import transforms

img_path = "xxx"
img = Image.open(img_path)

trans1 = transforms.Resize([h, w]) # 同时指定长宽,写为trans1 = transforms.Resize((h, w))也是可以的,transforms.Resize 接受的参数可以是整数、元组或列表。
trans2 = transforms.Resize(x) # 将图片短边缩放至x,长宽比保持不变

img1 = trans1(img)
img2 = trans2(img)

resize虽然会改变图片的长宽比,但是本身并没有发生裁切,仍可以通过resize方法返回原来的形状。

要注意的一点是PILImage对象size属性返回的是w, h,而resize的参数顺序是h, w。



参考:

https://blog.csdn.net/qq_51533157/article/details/122816008

https://blog.csdn.net/qq_35008185/article/details/118224044

https://segmentfault.com/a/1190000043683736

相关推荐

  1. Pytorchresizereshape

    2023-12-09 20:38:02       27 阅读
  2. 【水】pytorch:torch.reshapetorch.Tensor.view区别

    2023-12-09 20:38:02       42 阅读
  3. PyTorch Dataset、DataLoader enumerate()

    2023-12-09 20:38:02       40 阅读
  4. PyTorchAOTAutograd、PrimTorchTorchInductor

    2023-12-09 20:38:02       27 阅读
  5. pytorchdatasetdataloader

    2023-12-09 20:38:02       18 阅读
  6. PyTorch模块、类函数命名调用

    2023-12-09 20:38:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 20:38:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 20:38:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 20:38:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 20:38:02       18 阅读

热门阅读

  1. [原创] FPGA的JTAG烧录不稳定或烧录失败原因分析

    2023-12-09 20:38:02       48 阅读
  2. CCF-走迷宫(bfs)

    2023-12-09 20:38:02       37 阅读
  3. 使用es256算法生成jwt

    2023-12-09 20:38:02       45 阅读