Upload 2 files
Browse files
pytorch_grad_cam/base_cam.py
CHANGED
|
@@ -19,6 +19,8 @@ class BaseCAM:
|
|
| 19 |
self.cuda = use_cuda
|
| 20 |
if self.cuda:
|
| 21 |
self.model = model.cuda()
|
|
|
|
|
|
|
| 22 |
self.reshape_transform = reshape_transform
|
| 23 |
self.compute_input_gradient = compute_input_gradient
|
| 24 |
self.uses_gradients = uses_gradients
|
|
@@ -62,6 +64,7 @@ class BaseCAM:
|
|
| 62 |
cam = get_2d_projection(weighted_activations)
|
| 63 |
else:
|
| 64 |
cam = weighted_activations.sum(axis=1)
|
|
|
|
| 65 |
return cam
|
| 66 |
|
| 67 |
def forward(self, input_tensor, target_encoding, target_category=None, eigen_smooth=False):
|
|
@@ -73,6 +76,7 @@ class BaseCAM:
|
|
| 73 |
requires_grad=True)
|
| 74 |
# output will be the image encoding
|
| 75 |
output = self.activations_and_grads(input_tensor)
|
|
|
|
| 76 |
if isinstance(target_category, int):
|
| 77 |
target_category = [target_category] * input_tensor.size(0)
|
| 78 |
|
|
|
|
| 19 |
self.cuda = use_cuda
|
| 20 |
if self.cuda:
|
| 21 |
self.model = model.cuda()
|
| 22 |
+
else:
|
| 23 |
+
self.model = model.cpu()
|
| 24 |
self.reshape_transform = reshape_transform
|
| 25 |
self.compute_input_gradient = compute_input_gradient
|
| 26 |
self.uses_gradients = uses_gradients
|
|
|
|
| 64 |
cam = get_2d_projection(weighted_activations)
|
| 65 |
else:
|
| 66 |
cam = weighted_activations.sum(axis=1)
|
| 67 |
+
|
| 68 |
return cam
|
| 69 |
|
| 70 |
def forward(self, input_tensor, target_encoding, target_category=None, eigen_smooth=False):
|
|
|
|
| 76 |
requires_grad=True)
|
| 77 |
# output will be the image encoding
|
| 78 |
output = self.activations_and_grads(input_tensor)
|
| 79 |
+
|
| 80 |
if isinstance(target_category, int):
|
| 81 |
target_category = [target_category] * input_tensor.size(0)
|
| 82 |
|