我使用 Pytorch 进行图像分类,使用了 CNN 模型,我试图将我的原始图像叠加在使用 gradcam 的预测上,但它给了我运行时错误:
代码:
from torchvision.io.image import read_image
from torchvision.transforms.functional import normalize, resize, to_pil_image
from torchcam.methods import SmoothGradCAMpp
from torchcam.utils import overlay_mask
model_cnn.eval()
img = read_image(r"image.JPEG")
input_tensor = normalize(resize(img, (224, 224)) / 255., [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
with SmoothGradCAMpp(model_cnn) as cam_extractor:
# Preprocess your data and feed it to the model
out = model_cnn(input_tensor.unsqueeze(0))
# Retrieve the CAM by passing the class index and the model output
activation_map = cam_extractor(out.squeeze(0).argmax().item(), out)
result = overlay_mask(to_pil_image(img), to_pil_image(activation_map[0].squeeze(0), mode='F'), alpha=0.5)
plt.imshow(result); plt.axis('off'); plt.tight_layout();
plt.show()
**Error**: RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x200704 and 4096x1024)
来自 train_dataloader 的图像形状:
data_iter = iter(train_dl)
images , label = next(data_iter)
print(images.shape)
torch.Size([32, 3, 32, 32])
如何用形状尺寸解决这个错误,我已经根据从谷歌获得的资源调整了图像的大小,但它没有帮助。