ongkn commited on
Commit
b128442
·
unverified ·
1 Parent(s): 0272244
Files changed (1) hide show
  1. gradcam.py +6 -7
gradcam.py CHANGED
@@ -33,11 +33,11 @@ class GradCam():
33
  def __init__(self):
34
  pass
35
 
36
- def category_name_to_index(model, category_name):
37
  name_to_index = dict((v, k) for k, v in model.config.id2label.items())
38
  return name_to_index[category_name]
39
 
40
- def run_grad_cam_on_image(model: torch.nn.Module,
41
  target_layer: torch.nn.Module,
42
  targets_for_gradcam: List[Callable],
43
  reshape_transform: Optional[Callable],
@@ -67,14 +67,14 @@ class GradCam():
67
  return np.hstack(results)
68
 
69
 
70
- def print_top_categories(model, img_tensor, top_k=5):
71
  logits = model(img_tensor.unsqueeze(0)).logits
72
  probabilities = torch.nn.functional.softmax(logits, dim=1)
73
  indices = logits.cpu()[0, :].detach().numpy().argsort()[-top_k :][::-1]
74
  for i in indices:
75
  print(f"Predicted class (sorted from most confident) {i}: {model.config.id2label[i]}, confidence: {probabilities[0][i].item()}")
76
 
77
- def reshape_transform_vit_huggingface(x):
78
  activations = x[:, 1:, :]
79
  activations = activations.view(activations.shape[0],
80
  14, 14, activations.shape[2])
@@ -118,7 +118,7 @@ if __name__ == "__main__":
118
  cv.resizeWindow("DFF Image", 2500, 700)
119
  # cv.waitKey(0)
120
  # cv.destroyAllWindows()
121
- grad_cam_image = run_grad_cam_on_image(model=model,
122
  target_layer=target_layer_gradcam,
123
  targets_for_gradcam=targets_for_gradcam,
124
  input_tensor=tensor_resized,
@@ -130,5 +130,4 @@ if __name__ == "__main__":
130
  cv.resizeWindow("Grad-CAM Image", 2000, 1250)
131
  cv.waitKey(0)
132
  cv.destroyAllWindows()
133
- gradCam.print_top_categories(model, tensor_resized)
134
-
 
33
  def __init__(self):
34
  pass
35
 
36
+ def category_name_to_index(self, model, category_name):
37
  name_to_index = dict((v, k) for k, v in model.config.id2label.items())
38
  return name_to_index[category_name]
39
 
40
+ def run_grad_cam_on_image(self, model: torch.nn.Module,
41
  target_layer: torch.nn.Module,
42
  targets_for_gradcam: List[Callable],
43
  reshape_transform: Optional[Callable],
 
67
  return np.hstack(results)
68
 
69
 
70
+ def print_top_categories(self, model, img_tensor, top_k=5):
71
  logits = model(img_tensor.unsqueeze(0)).logits
72
  probabilities = torch.nn.functional.softmax(logits, dim=1)
73
  indices = logits.cpu()[0, :].detach().numpy().argsort()[-top_k :][::-1]
74
  for i in indices:
75
  print(f"Predicted class (sorted from most confident) {i}: {model.config.id2label[i]}, confidence: {probabilities[0][i].item()}")
76
 
77
+ def reshape_transform_vit_huggingface(self, x):
78
  activations = x[:, 1:, :]
79
  activations = activations.view(activations.shape[0],
80
  14, 14, activations.shape[2])
 
118
  cv.resizeWindow("DFF Image", 2500, 700)
119
  # cv.waitKey(0)
120
  # cv.destroyAllWindows()
121
+ grad_cam_image = gradCam.run_grad_cam_on_image(model=model,
122
  target_layer=target_layer_gradcam,
123
  targets_for_gradcam=targets_for_gradcam,
124
  input_tensor=tensor_resized,
 
130
  cv.resizeWindow("Grad-CAM Image", 2000, 1250)
131
  cv.waitKey(0)
132
  cv.destroyAllWindows()
133
+ gradCam.print_top_categories(model, tensor_resized)