【问题标题】:Convert image to proper dimension PyTorch将图像转换为适当尺寸的 PyTorch
【发布时间】:2018-11-12 10:09:58
【问题描述】:

我有一个输入图像,形状为 [H, W, C] 的 numpy 数组,其中 H - 高度、W - 宽度和 C - 通道。
我想将其转换为 [B, C, H, W] 其中 B - 批量大小,每次都应等于 1,并更改 C 的位置。

_image = np.array(_image)
h, w, c = _image.shape
image = torch.from_numpy(_image).unsqueeze_(0).view(1, c, h, w)

那么,这是否会正确保留图像,即不会替换原始图像像素值?

【问题讨论】:

    标签: python numpy image-processing deep-learning pytorch


    【解决方案1】:

    我更喜欢以下内容,它不会修改原始图像,只需根据需要添加一个新轴:

    _image = np.array(_image)
    image = torch.from_numpy(_image)
    image = image[np.newaxis, :] 
    # _unsqueeze works fine here too
    

    然后根据需要交换轴:

    image = image.permute(0, 3, 1, 2)
    # permutation applies the following mapping
    # axis0 -> axis0
    # axis1 -> axis3
    # axis2 -> axis1
    # axis3 -> axis2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2019-01-25
      • 2020-06-19
      • 1970-01-01
      • 2020-10-25
      • 2021-10-09
      • 1970-01-01
      相关资源
      最近更新 更多