【发布时间】: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