Spaces:
Build error
Build error
Commit
·
6088cd1
1
Parent(s):
311d8c3
Update app.py
Browse files
app.py
CHANGED
@@ -61,39 +61,38 @@ def predict_image(get_image):
|
|
61 |
pred, idx, probs = learn.predict(get_image)
|
62 |
return dict(zip(categories, map(float, probs)))
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
with gr.Column():
|
93 |
label = gr.Label(label="Predicted Class")
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
|
98 |
|
99 |
#interpretation="default"
|
|
|
61 |
pred, idx, probs = learn.predict(get_image)
|
62 |
return dict(zip(categories, map(float, probs)))
|
63 |
|
64 |
+
def interpretation_function(image_path, model, target_layer, target_category=None):
|
65 |
+
# Load and preprocess the image
|
66 |
+
image = Image.open(image_path)
|
67 |
+
preprocess = transforms.Compose([
|
68 |
+
transforms.Resize((224, 224)),
|
69 |
+
transforms.ToTensor(),
|
70 |
+
])
|
71 |
+
input_image = preprocess(image).unsqueeze(0) # Add a batch dimension
|
72 |
+
input_image = input_image.to('cuda' if torch.cuda.is_available() else 'cpu')
|
73 |
+
|
74 |
+
# Create an instance of GradCAM
|
75 |
+
cam = GradCAM(model=model, target_layer=target_layer)
|
76 |
+
|
77 |
+
# Compute the CAM
|
78 |
+
cam_image = cam(input_tensor=input_image, target_category=target_category)
|
79 |
+
|
80 |
+
# Show the CAM on the original image
|
81 |
+
visualization = show_cam_on_image(input_image, cam_image)
|
82 |
+
visualization.show()
|
83 |
+
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
with gr.Row():
|
87 |
+
with gr.Column():
|
88 |
+
input_img = gr.Image(label="Input Image", shape=(224, 224))
|
89 |
+
with gr.Row():
|
90 |
+
interpret = gr.Button("Interpret")
|
91 |
+
with gr.Column():
|
|
|
92 |
label = gr.Label(label="Predicted Class")
|
93 |
+
with gr.Column():
|
94 |
+
interpretation = gr.components.Interpretation(input_img)
|
95 |
+
interpret.click(interpretation_function(get_image,learn, learn.layer4[-1],target_category=None), input_img, interpretation)
|
96 |
|
97 |
|
98 |
#interpretation="default"
|