【问题标题】:ValueError: axes don't match array Pytorch Transpose operationValueError:轴与数组 Pytorch 转置操作不匹配
【发布时间】:2020-05-25 02:56:30
【问题描述】:

我正在尝试将 PIL 图像转换为 Torch 变量类型。这是它的代码:-

def preprocess_image(pil_im, resize_im=True):
    """
        Processes image for CNNs
    Args:
        PIL_img (PIL_img): PIL Image or numpy array to process
        resize_im (bool): Resize to 224 or not
    returns:
        im_as_var (torch variable): Variable that contains processed float tensor
    """
    # mean and std list for channels (Imagenet)
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    #ensure or transform incoming image to PIL image
    if type(pil_im) != Image.Image:
        try:
            pil_im = Image.fromarray(pil_im)
        except Exception as e:
            print("could not transform PIL_img to a PIL Image object. Please check input.")

    # Resize image
    if resize_im:
        pil_im = pil_im.resize((224, 224), Image.ANTIALIAS)

    im_as_arr = np.float32(pil_im)
    print(im_as_arr.shape)
    im_as_arr = im_as_arr.transpose(2, 0, 1)  # Convert array to D,W,H
    # Normalize the channels
    for channel, _ in enumerate(im_as_arr):
        im_as_arr[channel] /= 255
        im_as_arr[channel] -= mean[channel]
        im_as_arr[channel] /= std[channel]
    # Convert to float tensor
    im_as_ten = torch.from_numpy(im_as_arr).float()
    # Add one more channel to the beginning. Tensor shape = 1,3,224,224
    im_as_ten.unsqueeze_(0)
    # Convert to Pytorch variable
    im_as_var = Variable(im_as_ten, requires_grad=True)
    return im_as_var

original_image =  Image.open('blahblah.jpeg')
prep_img = preprocess_image(original_image)

我收到一个错误提示

ValueError                                Traceback (most recent call last)
<ipython-input-27-08cf62156870> in <module>()
      1 original_image =  Image.open('blahblah.jpeg')
----> 2 prep_img = preprocess_image(original_image)

<ipython-input-22-ad146391ce9d> in preprocess_image(pil_im, resize_im)
    155     im_as_arr = np.float32(pil_im)
    156     print(im_as_arr.shape)
--> 157     im_as_arr = im_as_arr.transpose(2, 0, 1)  # Convert array to D,W,H
    158     # Normalize the channels
    159     for channel, _ in enumerate(im_as_arr):

ValueError: axes don't match array

我认为转置操作存在一些问题。但是我在另一个用例上对其进行了测试,并且效果很好。不知道是什么触发了这个错误。

【问题讨论】:

  • 我认为transpose() 方法只是旋转了 PIL 图像? The docs

标签: python python-3.x numpy pytorch


【解决方案1】:

您的图像没有三个维度,这意味着它不是 RGB 图像,而是其他图像,例如一张灰度图像,从技术上讲,它只有一个通道,但 NumPy 会自动删除奇异维度,因此它的大小不是 [H, W, 1],而是大小为 [H, W] .

由于您似乎期待 RGB 图像,您可以通过使用 PIL 转换图像来确保图像处于 RGB 模式。

pil_img = pil_img.convert("RGB")

【讨论】:

  • 非常感谢!我后来想通了!
【解决方案2】:

pil_im = pil_im.resize((224, 224), Image.ANTIALIAS) 行更改了数组,使其没有第三维(即灰度)。

im_as_arr = im_as_arr.transpose(2, 0, 1) 行假定图像具有三个维度(即 RBG)。

在调用 transpose 方法之前,您需要将图像转换为 RBG 并更改调整大小; via-a-vis Michael Jungo建议的方法

【讨论】:

  • 非常感谢!我想通了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 2021-03-11
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多