Spaces:
Sleeping
Sleeping
Commit ·
744978f
1
Parent(s): a0d7cf9
Fix grad heatmap image
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from matplotlib import cm
|
| 6 |
|
| 7 |
from guided_backprop import GuidedBackprop
|
| 8 |
-
from utils import range_norm,
|
| 9 |
|
| 10 |
|
| 11 |
class GradioApp:
|
|
@@ -48,7 +47,7 @@ class GradioApp:
|
|
| 48 |
gr.Markdown("## Visualize activation maps")
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column(scale=1):
|
| 51 |
-
chosen_layer = gr.Dropdown(label="Layer", info="Choose from the layers", interactive=True)
|
| 52 |
chosen_filter = gr.Slider(label="Filter", info="Choose from the filters", interactive=True)
|
| 53 |
color = gr.Radio(["heatmap", "gray"], value="heatmap", label="Color", info="Choose the color of the activation map")
|
| 54 |
|
|
@@ -127,15 +126,16 @@ class GradioApp:
|
|
| 127 |
input_grad = self.input_grad[0].permute(1,2,0).detach()
|
| 128 |
input_grad = range_norm(input_grad)
|
| 129 |
input_grad = Image.fromarray(np.uint8(input_grad*255))
|
|
|
|
| 130 |
return input_grad
|
| 131 |
|
| 132 |
def apply_input_grad(self, input_image, grad_ratio):
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return
|
| 139 |
|
| 140 |
def launch(self):
|
| 141 |
self.app.launch()
|
|
@@ -143,4 +143,4 @@ class GradioApp:
|
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
gradio_app = GradioApp()
|
| 146 |
-
gradio_app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from matplotlib import cm
|
| 5 |
|
| 6 |
from guided_backprop import GuidedBackprop
|
| 7 |
+
from utils import range_norm, grad2heatmapped
|
| 8 |
|
| 9 |
|
| 10 |
class GradioApp:
|
|
|
|
| 47 |
gr.Markdown("## Visualize activation maps")
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=1):
|
| 50 |
+
chosen_layer = gr.Dropdown(label="Layer", value="conv_layer0", info="Choose from the layers", interactive=True)
|
| 51 |
chosen_filter = gr.Slider(label="Filter", info="Choose from the filters", interactive=True)
|
| 52 |
color = gr.Radio(["heatmap", "gray"], value="heatmap", label="Color", info="Choose the color of the activation map")
|
| 53 |
|
|
|
|
| 126 |
input_grad = self.input_grad[0].permute(1,2,0).detach()
|
| 127 |
input_grad = range_norm(input_grad)
|
| 128 |
input_grad = Image.fromarray(np.uint8(input_grad*255))
|
| 129 |
+
self.input_grad_img = input_grad
|
| 130 |
return input_grad
|
| 131 |
|
| 132 |
def apply_input_grad(self, input_image, grad_ratio):
|
| 133 |
+
heatmapped = grad2heatmapped(
|
| 134 |
+
input_image,
|
| 135 |
+
self.input_grad_img,
|
| 136 |
+
grad_ratio)
|
| 137 |
+
|
| 138 |
+
return heatmapped
|
| 139 |
|
| 140 |
def launch(self):
|
| 141 |
self.app.launch()
|
|
|
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
gradio_app = GradioApp()
|
| 146 |
+
gradio_app.launch()
|
utils.py
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def range_norm(img):
|
| 2 |
min = img.min()
|
| 3 |
max = img.max()
|
| 4 |
eps = 1e-6
|
| 5 |
return (img-min)/(max-min+eps)
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
|
| 5 |
def range_norm(img):
|
| 6 |
min = img.min()
|
| 7 |
max = img.max()
|
| 8 |
eps = 1e-6
|
| 9 |
return (img-min)/(max-min+eps)
|
| 10 |
|
| 11 |
+
|
| 12 |
+
def grad2heatmapped(input_image, grad_image, ratio):
|
| 13 |
+
input_image = np.array(input_image)
|
| 14 |
+
grad_image = np.array(grad_image)
|
| 15 |
+
|
| 16 |
+
# Invert negative pixels
|
| 17 |
+
grad_image[grad_image<100] += 128
|
| 18 |
+
|
| 19 |
+
# Apply thresholding and blur to obtain heatmap
|
| 20 |
+
th = cv2.threshold(grad_image, 140, 255, cv2.THRESH_BINARY)[1]
|
| 21 |
+
blur = cv2.GaussianBlur(th, (11,11), 11)
|
| 22 |
+
heatmap = cv2.applyColorMap(blur, cv2.COLORMAP_JET)
|
| 23 |
+
|
| 24 |
+
# Apply edge padding to heatmap to have 256x256 size
|
| 25 |
+
heatmap = np.pad(heatmap, ((16,16),(16,16),(0,0)), 'edge')
|
| 26 |
+
|
| 27 |
+
# Upsample heatmap to input_image size
|
| 28 |
+
heatmap = cv2.resize(heatmap, (input_image.shape[1], input_image.shape[0]))
|
| 29 |
+
|
| 30 |
+
# Superimpose heatmap on input_image
|
| 31 |
+
heatmapped = cv2.addWeighted(input_image, 1-ratio, heatmap, ratio, 0)
|
| 32 |
+
|
| 33 |
+
return heatmapped
|