ongkn commited on
Commit
59cea8b
·
1 Parent(s): 47be84a

Update gradcam.py

Browse files
Files changed (1) hide show
  1. gradcam.py +4 -3
gradcam.py CHANGED
@@ -74,9 +74,10 @@ class GradCam():
74
  def print_top_categories(self, model, img_tensor, top_k=5):
75
  logits = model(img_tensor.unsqueeze(0)).logits
76
  probabilities = torch.nn.functional.softmax(logits, dim=1)
77
- indices = logits.cpu()[0, :].detach().numpy().argsort()[-top_k :][::-1]
78
- for i in indices:
79
- print(f"Predicted class (sorted from most confident) {i}: {model.config.id2label[i]}, confidence: {probabilities[0][i].item()}")
 
80
 
81
  def reshape_transform_vit_huggingface(self, x):
82
  activations = x[:, 1:, :]
 
74
  def print_top_categories(self, model, img_tensor, top_k=5):
75
  logits = model(img_tensor.unsqueeze(0)).logits
76
  probabilities = torch.nn.functional.softmax(logits, dim=1)
77
+ topIdx = logits.cpu()[0, :].detach().numpy().argsort()[-1]
78
+ topClass = model.config.id2label[topIdx]
79
+ topScore = probabilities[0][topIdx].item()
80
+ return {"label": topClass, "score": topScore}
81
 
82
  def reshape_transform_vit_huggingface(self, x):
83
  activations = x[:, 1:, :]