Kalbe-x-Bangkit
commited on
Commit
•
0296c3c
1
Parent(s):
045c94c
Add app-gradcam and their model
Browse files- app-gradcam.py +71 -0
- model_renamed.h5 +3 -0
app-gradcam.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import cv2
|
7 |
+
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
|
8 |
+
|
9 |
+
class GradCAM(object):
|
10 |
+
|
11 |
+
def __init__(self, model, alpha=0.8, beta=0.3):
|
12 |
+
self.model = model
|
13 |
+
self.alpha = alpha
|
14 |
+
self.beta = beta
|
15 |
+
|
16 |
+
def apply_heatmap(self, heatmap, image):
|
17 |
+
heatmap = cv2.resize(heatmap, image.shape[:-1])
|
18 |
+
heatmap = cv2.applyColorMap(np.uint8(255 * heatmap), cv2.COLORMAP_JET)
|
19 |
+
superimposed_img = cv2.addWeighted(np.array(image).astype(np.float32), self.alpha,
|
20 |
+
np.array(heatmap).astype(np.float32), self.beta, 0)
|
21 |
+
return np.array(superimposed_img).astype(np.uint8)
|
22 |
+
|
23 |
+
def gradCAM(self, x_test=None, name='block5_conv3', index_class=0):
|
24 |
+
with tf.GradientTape() as tape:
|
25 |
+
last_conv_layer = self.model.get_layer(name)
|
26 |
+
grad_model = tf.keras.Model([self.model.input], [self.model.output, last_conv_layer.output])
|
27 |
+
model_out, last_conv_layer = grad_model(np.expand_dims(x_test, axis=0))
|
28 |
+
class_out = model_out[:, index_class]
|
29 |
+
grads = tape.gradient(class_out, last_conv_layer)
|
30 |
+
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
|
31 |
+
last_conv_layer = last_conv_layer[0]
|
32 |
+
heatmap = last_conv_layer @ pooled_grads[..., tf.newaxis]
|
33 |
+
heatmap = tf.squeeze(heatmap)
|
34 |
+
heatmap = np.maximum(heatmap, 0)
|
35 |
+
heatmap /= np.max(heatmap)
|
36 |
+
heatmap = np.array(heatmap)
|
37 |
+
return self.apply_heatmap(heatmap, x_test)
|
38 |
+
|
39 |
+
# Streamlit app
|
40 |
+
st.title("Grad-CAM Visualization")
|
41 |
+
|
42 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
43 |
+
|
44 |
+
if uploaded_file is not None:
|
45 |
+
try:
|
46 |
+
# Load the uploaded image
|
47 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
48 |
+
img = cv2.imdecode(file_bytes, 1)
|
49 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
50 |
+
|
51 |
+
st.image(img, caption='Uploaded Image.', use_column_width=True)
|
52 |
+
|
53 |
+
# Preprocess the image for the model (assuming the model expects 224x224 images)
|
54 |
+
img_resized = cv2.resize(img, (224, 224))
|
55 |
+
img_array = np.expand_dims(img_resized, axis=0)
|
56 |
+
|
57 |
+
# Load the model
|
58 |
+
model_path = './model/model_renamed.h5' # Update this path to your model's path
|
59 |
+
model = tf.keras.models.load_model(model_path)
|
60 |
+
|
61 |
+
# Initialize GradCAM
|
62 |
+
grad_cam = GradCAM(model)
|
63 |
+
|
64 |
+
# Compute GradCAM heatmap
|
65 |
+
heatmap_img = grad_cam.gradCAM(img_array[0])
|
66 |
+
|
67 |
+
# Display the GradCAM heatmap
|
68 |
+
st.image(heatmap_img, caption='Grad-CAM Heatmap.', use_column_width=True)
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Error: {e}")
|
model_renamed.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6461443f6a901db1a6113df875a28028b4bc26835114d93c8c4040606d974be8
|
3 |
+
size 29417936
|