Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,10 +39,11 @@ def grad_cam(model, img, size, preprocess_func):
|
|
39 |
conv_outputs, predictions = grad_model(x_tensor)
|
40 |
loss = predictions[:, 0]
|
41 |
grads = tape.gradient(loss, conv_outputs)
|
42 |
-
cam = tf.reduce_mean(grads, axis=-1).numpy()
|
|
|
43 |
|
44 |
cam = np.maximum(cam, 0)
|
45 |
-
cam
|
46 |
heatmap = cv2.resize(cam, size)
|
47 |
heatmap = np.uint8(255 * heatmap)
|
48 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
@@ -54,6 +55,7 @@ def grad_cam(model, img, size, preprocess_func):
|
|
54 |
return Image.fromarray(cv2.cvtColor(superimposed, cv2.COLOR_BGR2RGB))
|
55 |
|
56 |
|
|
|
57 |
# Preprocessing helper
|
58 |
def preprocess(img, size, func):
|
59 |
img = img.resize(size)
|
|
|
39 |
conv_outputs, predictions = grad_model(x_tensor)
|
40 |
loss = predictions[:, 0]
|
41 |
grads = tape.gradient(loss, conv_outputs)
|
42 |
+
cam = tf.reduce_mean(grads, axis=-1).numpy()
|
43 |
+
cam = np.squeeze(cam)
|
44 |
|
45 |
cam = np.maximum(cam, 0)
|
46 |
+
cam = cam / cam.max() if cam.max() != 0 else cam
|
47 |
heatmap = cv2.resize(cam, size)
|
48 |
heatmap = np.uint8(255 * heatmap)
|
49 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
|
|
55 |
return Image.fromarray(cv2.cvtColor(superimposed, cv2.COLOR_BGR2RGB))
|
56 |
|
57 |
|
58 |
+
|
59 |
# Preprocessing helper
|
60 |
def preprocess(img, size, func):
|
61 |
img = img.resize(size)
|