RuntimeError: shape '[32, 3, 224, 224]' is invalid for input of size 50176

RuntimeError: shape '[32, 3, 224, 224]' is invalid for input of size 50176

首先,我已经在 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

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

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

以上代码将复制灰度图像 3 次 channel-wise,得到的张量大小为 1x3x224x224

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

欢迎对答案提出任何建议或错误更正