Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import tensorflow as tf
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
from tensorflow.keras.layers import DepthwiseConv2D
|
|
|
6 |
from PIL import Image
|
7 |
|
8 |
# Función personalizada para DepthwiseConv2D
|
@@ -38,28 +39,50 @@ labels = [
|
|
38 |
'Urticaria Ronchas', 'Tumores Vasculares', 'Vasculitis', 'Verrugas Molusco'
|
39 |
]
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# Aplicar transformaciones al heatmap
|
53 |
-
heatmap = np.maximum(heatmap, 0) # Aplicar ReLU
|
54 |
-
heatmap /= np.max(heatmap) # Normalizar
|
55 |
-
heatmap = np.uint8(255 * heatmap) # Convertir a rango 0-255
|
56 |
-
|
57 |
# Redimensionar el mapa de calor al tamaño de la imagen
|
58 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
|
|
|
|
|
|
59 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
60 |
|
61 |
-
# Superponer el
|
62 |
-
superimposed_image = cv2.addWeighted(image, 0.6, heatmap, 0.4, 0)
|
63 |
|
64 |
# Convertir a formato PIL para mostrar en Gradio
|
65 |
img_pil = Image.fromarray(superimposed_image)
|
@@ -72,7 +95,7 @@ def classify_image(image):
|
|
72 |
prediction = model.predict(image_resized).flatten()
|
73 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
74 |
|
75 |
-
# Generar mapa de calor
|
76 |
heatmap_image = generate_heatmap(np.array(image), prediction)
|
77 |
|
78 |
return confidences, heatmap_image
|
|
|
3 |
import numpy as np
|
4 |
import cv2
|
5 |
from tensorflow.keras.layers import DepthwiseConv2D
|
6 |
+
from tensorflow.keras.models import Model
|
7 |
from PIL import Image
|
8 |
|
9 |
# Función personalizada para DepthwiseConv2D
|
|
|
39 |
'Urticaria Ronchas', 'Tumores Vasculares', 'Vasculitis', 'Verrugas Molusco'
|
40 |
]
|
41 |
|
42 |
+
# Función Grad-CAM para generar el mapa de calor de activación
|
43 |
+
def grad_cam(model, img_array, last_conv_layer_name, pred_index=None):
|
44 |
+
grad_model = Model(
|
45 |
+
[model.inputs], [model.get_layer(last_conv_layer_name).output, model.output]
|
46 |
+
)
|
47 |
+
|
48 |
+
with tf.GradientTape() as tape:
|
49 |
+
last_conv_layer_output, preds = grad_model(img_array)
|
50 |
+
if pred_index is None:
|
51 |
+
pred_index = tf.argmax(preds[0])
|
52 |
+
class_channel = preds[:, pred_index]
|
53 |
+
|
54 |
+
# Calcula los gradientes de la salida de la clase respecto a la salida de la última capa convolucional
|
55 |
+
grads = tape.gradient(class_channel, last_conv_layer_output)
|
56 |
+
|
57 |
+
# Promediar sobre cada filtro
|
58 |
+
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
|
59 |
+
|
60 |
+
# Ponderar cada filtro en el mapa de características de la última capa convolucional
|
61 |
+
last_conv_layer_output = last_conv_layer_output[0]
|
62 |
+
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
|
63 |
+
heatmap = tf.squeeze(heatmap)
|
64 |
+
|
65 |
+
# Normalizar entre 0 y 1 para el mapa de calor
|
66 |
+
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
|
67 |
+
return heatmap.numpy()
|
68 |
+
|
69 |
+
def generate_heatmap(image, prediction, last_conv_layer_name="conv5_block3_out"):
|
70 |
+
# Redimensionar la imagen a (224, 224)
|
71 |
+
img_array = tf.image.resize(image, (224, 224))
|
72 |
+
img_array = tf.expand_dims(img_array, axis=0) # Añadir una dimensión para el batch
|
73 |
+
|
74 |
+
# Obtén el Grad-CAM para la clase predicha
|
75 |
+
heatmap = grad_cam(model, img_array, last_conv_layer_name)
|
76 |
|
|
|
|
|
|
|
|
|
|
|
77 |
# Redimensionar el mapa de calor al tamaño de la imagen
|
78 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
79 |
+
|
80 |
+
# Convertir el mapa de calor a un rango de 0 a 255 para aplicar color
|
81 |
+
heatmap = np.uint8(255 * heatmap)
|
82 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
83 |
|
84 |
+
# Superponer el mapa de calor en la imagen original
|
85 |
+
superimposed_image = cv2.addWeighted(np.array(image), 0.6, heatmap, 0.4, 0)
|
86 |
|
87 |
# Convertir a formato PIL para mostrar en Gradio
|
88 |
img_pil = Image.fromarray(superimposed_image)
|
|
|
95 |
prediction = model.predict(image_resized).flatten()
|
96 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
97 |
|
98 |
+
# Generar mapa de calor utilizando Grad-CAM
|
99 |
heatmap_image = generate_heatmap(np.array(image), prediction)
|
100 |
|
101 |
return confidences, heatmap_image
|