【问题标题】:RuntimeError: shape '[32, 3, 224, 224]' is invalid for input of size 50176RuntimeError: shape '[32, 3, 224, 224]' 对于大小为 50176 的输入无效
【发布时间】:2022-06-28 22:47:53
【问题描述】:

首先,我已经在 224,224,3 张图像上训练了一个模型,现在我正在研究从 MNIST 数据集代码库中获取的可视化。下面的代码在灰度图像上工作得很好,但是当我用于彩色图像时它没有成功。

代码运行良好

with torch.no_grad():
    while True:
        image = cv2.imread("example.png", flags=cv2.IMREAD_GRAYSCALE)
        print(image.shape)
        input_img_h, input_img_w = image.shape
        image = scale_transformation(image, scale_factor=scale_factors[scale_idx_factor])
        image = rotation_transformation(image, angle=rotation_factors[rotation_idx_factor])
        scale_idx_factor = (scale_idx_factor + 1) % len(scale_factors)
        rotation_idx_factor = (rotation_idx_factor + 1) % len(rotation_factors)

        image_tensor = torch.from_numpy(image) / 255.
        print("image_tensor.shape:", image_tensor.shape)

        image_tensor = image_tensor.view(1, 1, input_img_h, input_img_w)

        image_tensor = T.Normalize((0.1307,), (0.3081,))(image_tensor)
        image_tensor = image_tensor.to(device)

        out = model(image_tensor)

        image = np.repeat(image[..., np.newaxis], 3, axis=-1)
        roi_y, roi_x = input_img_h // 2, input_img_w // 2
        plot_offsets(image, save_output, roi_x=roi_x, roi_y=roi_y)

        save_output.clear()
        image = cv2.resize(image, dsize=(224, 224))
        cv2.imshow("image", image)
        key = cv2.waitKey(30)
        if key == 27:
            break

有问题的代码:我只更改了图像大小

with torch.no_grad():
    while True:
        image = cv2.imread("image_06764.jpg")
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        print('Original Dimensions : ', image.shape)

        width = 224
        height = 224
        dim = (width, height)
        image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
        # print(resized.shape[0])
        input_img_h = image.shape[0]
        input_img_w = image.shape[1]

        image = scale_transformation(image, scale_factor=scale_factors[scale_idx_factor])
        print("dfdf", image.shape)
        image = rotation_transformation(image, angle=rotation_factors[rotation_idx_factor])
        scale_idx_factor = (scale_idx_factor + 1) % len(scale_factors)
        rotation_idx_factor = (rotation_idx_factor + 1) % len(rotation_factors)

        image_tensor = torch.from_numpy(image) / 255.
        print("ggggggggggg", image_tensor.size())

        image_tensor = image_tensor.view(32, 3, input_img_h, input_img_w)
        print("image_tensor.shape:", image_tensor.shape)
        image_tensor = T.Normalize((0.1307,), (0.3081,))(image_tensor)
        image_tensor = image_tensor.to(device)
        out = model(image_tensor)

        image = np.repeat(image[..., np.newaxis], 3, axis=-1)
        roi_y, roi_x = input_img_h // 2, input_img_w // 2
        plot_offsets(image, save_output, roi_x=roi_x, roi_y=roi_y)

        save_output.clear()
        image = cv2.resize(image, dsize=(224, 224))
        cv2.imshow("image", image)
        key = cv2.waitKey(30)
        if key == 27:
            break

追溯

Traceback (most recent call last):
  File "/media/cvpr/CM_1/tutorials/Deformable_Convolutionv_V2/offset_visualization.py", line 184, in <module>
    image_tensor = image_tensor.view(32, 3, input_img_h, input_img_w)
RuntimeError: shape '[32, 3, 224, 224]' is invalid for input of size 50176

【问题讨论】:

  • 如果您使用大于或小于您用于训练的图像的图像进行测试,您将会遇到问题。训练和测试的图像尺寸(包括颜色)必须保持不变。
  • @stateMachine 在训练中,图像大小为 224,224,我给出相同大小进行测试。输入通道为 3。

标签: opencv pytorch tensor torch


【解决方案1】:

image_tensor是张量大小50176,可以调整为224x224。但是,您正在尝试将其大小调整为 32x3x224x224。 试试这个:

image_tensor = image_tensor.view(1, 1, input_img_h, input_img_w).repeat(1, 3, 1, 1)

以上代码将按通道复制灰度图像 3 次,生成张量大小为 1x3x224x224

另外,为什么要用image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 将彩色图像转换为灰度图像?去掉就不会有频道问题了。

欢迎任何建议或更正答案的错误

【讨论】:

  • 实施后,Traceback(最后一次调用):文件“/media/cvpr/CM_1/tutorials/Deformable_Convolutionv_V2/offset_visualization.py”,第 184 行,在 image_tensor = image_tensor.view (1, input_img_h, input_img_w).unsqueeze(0).unsqueeze(0).repeat(1, 3, 1, 1) RuntimeError: repeat dims的维数不能小于tensor的维数
  • 我不必要地添加了两次.unsqueeze(0)。修好了!
  • 它有效。彩色图像呢? image_tensor = image_tensor.view(1, 3, input_img_h, input_img_w).repeat(1, 3, 1, 1)。这是正确的吗?
  • 如果将1x3x224x224 的张量大小重复(1,3,1,1) 次,它将变为1x9x224x224,这不是RGB 或BGR 图像。
  • 哦,有什么解决办法吗?
猜你喜欢
  • 2020-09-03
  • 2022-09-30
  • 2021-03-30
  • 1970-01-01
  • 2021-09-03
  • 2021-12-16
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多