Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -91,44 +91,6 @@ def superimpose_images(base_image, overlay_image, alpha):
|
|
91 |
|
92 |
return Image.fromarray(blended_array)
|
93 |
|
94 |
-
def compute_gradcam(image_tensor, model):
|
95 |
-
target_layer = model.layer4[-1]
|
96 |
-
gradients, activations = [], []
|
97 |
-
|
98 |
-
def backward_hook(module, grad_input, grad_output):
|
99 |
-
gradients.append(grad_output[0])
|
100 |
-
|
101 |
-
def forward_hook(module, input, output):
|
102 |
-
activations.append(output)
|
103 |
-
|
104 |
-
target_layer.register_forward_hook(forward_hook)
|
105 |
-
target_layer.register_backward_hook(backward_hook)
|
106 |
-
|
107 |
-
output = model(image_tensor)
|
108 |
-
class_idx = output.argmax(dim=1).item()
|
109 |
-
model.zero_grad()
|
110 |
-
output[:, class_idx].backward()
|
111 |
-
|
112 |
-
grads = gradients[0].cpu().data.numpy()[0]
|
113 |
-
acts = activations[0].cpu().data.numpy()[0]
|
114 |
-
weights = np.mean(grads, axis=(1, 2))
|
115 |
-
cam = np.zeros(acts.shape[1:], dtype=np.float32)
|
116 |
-
|
117 |
-
for i, w in enumerate(weights):
|
118 |
-
cam += w * acts[i]
|
119 |
-
|
120 |
-
cam = np.maximum(cam, 0)
|
121 |
-
cam = cv2.resize(cam, (224, 224))
|
122 |
-
cam = (cam - cam.min()) / (cam.max() - cam.min())
|
123 |
-
return cam
|
124 |
-
|
125 |
-
def overlay_heatmap(image, heatmap):
|
126 |
-
heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
|
127 |
-
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
|
128 |
-
image = np.array(image.resize((224, 224)))
|
129 |
-
superimposed_img = cv2.addWeighted(image, 0.6, heatmap, 0.4, 0)
|
130 |
-
return Image.fromarray(superimposed_img)
|
131 |
-
|
132 |
# Prediction function
|
133 |
def predict(image, brightness, contrast, hue, overlay_image, alpha):
|
134 |
"""Apply filters, superimpose, classify image, and visualize results."""
|
@@ -148,9 +110,6 @@ def predict(image, brightness, contrast, hue, overlay_image, alpha):
|
|
148 |
output = model(image_tensor)
|
149 |
probabilities = F.softmax(output, dim=1).cpu().numpy()[0]
|
150 |
|
151 |
-
heatmap = compute_gradcam(image_tensor, model)
|
152 |
-
heatmap_image = overlay_heatmap(final_image, heatmap)
|
153 |
-
|
154 |
# Generate Bar Chart
|
155 |
with plt.xkcd():
|
156 |
fig, ax = plt.subplots(figsize=(5, 3))
|
@@ -161,7 +120,7 @@ def predict(image, brightness, contrast, hue, overlay_image, alpha):
|
|
161 |
for i, v in enumerate(probabilities):
|
162 |
ax.text(i, v + 0.02, f"{v:.2f}", ha='center', fontsize=10)
|
163 |
|
164 |
-
return final_image,
|
165 |
|
166 |
# Gradio Interface
|
167 |
with gr.Blocks() as interface:
|
@@ -178,7 +137,6 @@ with gr.Blocks() as interface:
|
|
178 |
|
179 |
with gr.Column():
|
180 |
processed_image = gr.Image(label="Final Processed Image")
|
181 |
-
heatmap_image = gr.Image(label="Heatmap Visualization")
|
182 |
bar_chart = gr.Plot(label="Class Probabilities")
|
183 |
|
184 |
inputs = [image_input, brightness, contrast, hue, overlay_input, alpha]
|
|
|
91 |
|
92 |
return Image.fromarray(blended_array)
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Prediction function
|
95 |
def predict(image, brightness, contrast, hue, overlay_image, alpha):
|
96 |
"""Apply filters, superimpose, classify image, and visualize results."""
|
|
|
110 |
output = model(image_tensor)
|
111 |
probabilities = F.softmax(output, dim=1).cpu().numpy()[0]
|
112 |
|
|
|
|
|
|
|
113 |
# Generate Bar Chart
|
114 |
with plt.xkcd():
|
115 |
fig, ax = plt.subplots(figsize=(5, 3))
|
|
|
120 |
for i, v in enumerate(probabilities):
|
121 |
ax.text(i, v + 0.02, f"{v:.2f}", ha='center', fontsize=10)
|
122 |
|
123 |
+
return final_image, fig
|
124 |
|
125 |
# Gradio Interface
|
126 |
with gr.Blocks() as interface:
|
|
|
137 |
|
138 |
with gr.Column():
|
139 |
processed_image = gr.Image(label="Final Processed Image")
|
|
|
140 |
bar_chart = gr.Plot(label="Class Probabilities")
|
141 |
|
142 |
inputs = [image_input, brightness, contrast, hue, overlay_input, alpha]
|