transforms.Resize()

transforms.Resize()

类原型:

CLASS torchvision.transforms.Resize(size, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=None)
    
作用:
    将输入图像(PIL Image or Tensor)调整为给定的大小。如果图像是torch张量,则期望它具有[C,H, W]形状,C其中表示任意数量的张量维度
    
参数:
    size (sequence or int):
    	期望输出大小。如果size是(h, w)这样的序列,则输出size将与此匹配。如果size为int,图像的较小边缘将匹配此数字。即,如果高度>宽度,那么图像将被重新缩放为(size*高度/宽度,size)
    interpolation (InterpolationMode):
    	由torchvision.transforms.InterpolationMode定义的期望插值枚举。默认为InterpolationMode.BILINEAR。如果输入是张量,只有InterpolationMode
    max_size (int, optional):
		调整后图像的长边允许的最大值:如果根据size调整后图像的长边大于max size,则再次调整图像,使长边等于max size。因此,size可能被否决,即较小的边可能比大小短。这只在size为int(或在torchscript模式下长度为1的序列)时才支持。
    antialias (bool, optional) 
        抗锯齿标志。如果img是PIL Image,该标志将被忽略,并且始终使用反别名。如果img是Tensor,该标志默认为False, InterpolationMode可以设置为True。双线性和插值模式。双三次的模式。这有助于使PIL图像的输出和张量更接近。    

注意:

不能是用io.imread或者cv2.imread读取的图片,这两种方法得到的是ndarray

  • 将图片短边缩短至size (int),长宽比保持不变

    transforms.Resize(size)
    
  • 一般输入深度网络的特征图长宽是相等的,需要同时制定长宽

    transforms.Resize([h, w]
    
  • 实例程序:

    img = Image.open("data/val/Dog/2.jpg")
    print(np.array(img).shape)
    
    resize = transforms.Resize((150,150))
    img_resize = resize(img)
    print(np.array(img_resize).shape)
    

    运行结果:

    (199, 188, 3)
    (150, 150, 3)
    

你可能感兴趣的:(深度学习,python,计算机视觉,深度学习)