Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -80,7 +80,7 @@ class CustomResnet(nn.Module):
|
|
80 |
return F.log_softmax(x, dim=-1)
|
81 |
|
82 |
# Function to run inference and return top classes
|
83 |
-
def get_gradcam(model,input_img, opacity):
|
84 |
targets = None
|
85 |
inv_normalize = transforms.Normalize(
|
86 |
mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
|
@@ -93,7 +93,13 @@ def get_gradcam(model,input_img, opacity):
|
|
93 |
input_img = input_img.unsqueeze(0)
|
94 |
outputs = model(input_img)
|
95 |
_, prediction = torch.max(outputs, 1)
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
98 |
grayscale_cam = cam(input_tensor=input_img, targets=targets)
|
99 |
grayscale_cam = grayscale_cam[0, :]
|
|
|
80 |
return F.log_softmax(x, dim=-1)
|
81 |
|
82 |
# Function to run inference and return top classes
|
83 |
+
def get_gradcam(model,input_img, opacity,layer):
|
84 |
targets = None
|
85 |
inv_normalize = transforms.Normalize(
|
86 |
mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
|
|
|
93 |
input_img = input_img.unsqueeze(0)
|
94 |
outputs = model(input_img)
|
95 |
_, prediction = torch.max(outputs, 1)
|
96 |
+
if layer == "layer3":
|
97 |
+
target_layers = [model.convlayer3[-2]]
|
98 |
+
if layer == "layer2":
|
99 |
+
target_layers = [model.convlayer2[-2]]
|
100 |
+
if layer == "layer1":
|
101 |
+
target_layers = [model.convlayer1[-2]]
|
102 |
+
#target_layers = [model.convlayer3[-2]]
|
103 |
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
104 |
grayscale_cam = cam(input_tensor=input_img, targets=targets)
|
105 |
grayscale_cam = grayscale_cam[0, :]
|