我使用开源Pytorch Grad CAM代码来可视化我的模型,如下所示:
classifier = cnn.CNN(in_shape=(1,28,28), n_classes=10).cuda()
Grad-CAM实现来自这里:https://github.com/jacobgil/pytorch-grad-cam/
然而,当我跑的时候
def preprocess_image(img):
means = [0.485, 0.456, 0.406]
stds = [0.229, 0.224, 0.225]
preprocessed_img = img.copy()[:, :, ::-1]
for i in range(3):
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] - means[i]
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] / stds[i]
preprocessed_img = \
np.ascontiguousarray(np.transpose(preprocessed_img, (2, 0, 1)))
preprocessed_img = torch.from_numpy(preprocessed_img)
preprocessed_img.unsqueeze_(0)
input = preprocessed_img.requires_grad_(True)
return input
model = classifier
grad_cam = GradCam(model=model, feature_module=model.features, target_layer_names=["2"], use_cuda=True)
img = cv2.imread('/content/drive/My Drive/pic.png', 1)
img = np.float32(cv2.resize(img, (224, 224))) / 255
input = preprocess_image(img)
input = input[:, 0:1, :, :]
target_index = None
mask = grad_cam(input, target_index)
show_cam_on_image(img, mask)
我得到这个错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-30-fe930a4e6b09> in <module>()
155 # Otherwise, targets the requested index.
156 target_index = None
--> 157 mask = grad_cam(input, target_index)
158 show_cam_on_image(img, mask)
6 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1674 ret = torch.addmm(bias, input, weight.t())
1675 else:
-> 1676 output = input.matmul(weight.t())
1677 if bias is not None:
1678 output += bias
RuntimeError: mat1 dim 1 must match mat2 dim 0
如果这有帮助,下面是print(model.features)
的输出:
Sequential(
(0): Conv2d(1, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)
(1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(4): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU(inplace=True)
(6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(7): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(8): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace=True)
(10): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(11): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(12): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(13): ReLU(inplace=True)
(14): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
如何修复此错误并使渐变凸轮按预期运行
目前没有回答
相关问题 更多 >
编程相关推荐