Skip to content

Commit b1cab2d

Browse files
committed
Merge branch 'master' of github.com:jacobgil/pytorch-grad-cam into master
2 parents 37d5aa3 + a2a23f8 commit b1cab2d

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ two smoothing methods are supported:
282282
Usage: `python cam.py --image-path <path_to_image> --method <method> --output-dir <output_dir_path> `
283283

284284

285-
To use with a specific device, like cpu, cuda, cuda:0 or mps:
285+
To use with a specific device, like cpu, cuda, cuda:0, mps or hpu:
286286
`python cam.py --image-path <path_to_image> --device cuda --output-dir <output_dir_path> `
287287

288288
----------

cam.py

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def get_args():
7878
'kpcacam': KPCA_CAM
7979
}
8080

81+
if args.device=='hpu':
82+
import habana_frameworks.torch.core as htcore
83+
8184
model = models.resnet50(pretrained=True).to(torch.device(args.device)).eval()
8285

8386
# Choose the target layer you want to compute the visualization for.

pytorch_grad_cam/base_cam.py

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def __init__(
2525

2626
# Use the same device as the model.
2727
self.device = next(self.model.parameters()).device
28+
if 'hpu' in str(self.device):
29+
try:
30+
import habana_frameworks.torch.core as htcore
31+
except ImportError as error:
32+
error.msg = f"Could not import habana_frameworks.torch.core. {error.msg}."
33+
raise error
34+
self.__htcore = htcore
2835
self.reshape_transform = reshape_transform
2936
self.compute_input_gradient = compute_input_gradient
3037
self.uses_gradients = uses_gradients
@@ -97,6 +104,8 @@ def forward(
97104
self.model.zero_grad()
98105
loss = sum([target(output) for target, output in zip(targets, outputs)])
99106
loss.backward(retain_graph=True)
107+
if 'hpu' in str(self.device):
108+
self.__htcore.mark_step()
100109

101110
# In most of the saliency attribution papers, the saliency is
102111
# computed with a single target layer.

pytorch_grad_cam/utils/model_targets.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,9 @@ class SemanticSegmentationTarget:
5959
def __init__(self, category, mask):
6060
self.category = category
6161
self.mask = torch.from_numpy(mask)
62-
if torch.cuda.is_available():
63-
self.mask = self.mask.cuda()
64-
if torch.backends.mps.is_available():
65-
self.mask = self.mask.to("mps")
6662

6763
def __call__(self, model_output):
68-
return (model_output[self.category, :, :] * self.mask).sum()
64+
return (model_output[self.category, :, :] * self.mask.to(model_output.device)).sum()
6965

7066

7167
class FasterRCNNBoxScoreTarget:

0 commit comments

Comments
 (0)