Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from PIL import Image
|
|
8 |
|
9 |
# Función personalizada para DepthwiseConv2D
|
10 |
def custom_depthwise_conv2d(**kwargs):
|
11 |
-
kwargs.pop('groups', None)
|
12 |
return DepthwiseConv2D(**kwargs)
|
13 |
|
14 |
custom_objects = {'DepthwiseConv2D': custom_depthwise_conv2d}
|
@@ -24,7 +24,7 @@ except Exception as e:
|
|
24 |
print(f"Error al cargar el modelo: {e}")
|
25 |
raise
|
26 |
|
27 |
-
# Imprimir nombres de las capas del modelo
|
28 |
print("Nombres de las capas en el modelo:")
|
29 |
for layer in model.layers:
|
30 |
print(layer.name)
|
@@ -73,68 +73,43 @@ def grad_cam(model, img_array, last_conv_layer_name, pred_index=None):
|
|
73 |
return heatmap.numpy()
|
74 |
|
75 |
# Generar el mapa de calor de activación usando Grad-CAM
|
76 |
-
def generate_heatmap(image, prediction, last_conv_layer_name="
|
77 |
-
# Redimensionar la imagen a (224, 224)
|
78 |
img_array = tf.image.resize(image, (224, 224))
|
79 |
-
img_array = tf.expand_dims(img_array, axis=0)
|
80 |
|
81 |
-
# Obtén el Grad-CAM para la clase predicha
|
82 |
heatmap = grad_cam(model, img_array, last_conv_layer_name)
|
83 |
-
|
84 |
-
# Redimensionar el mapa de calor al tamaño de la imagen
|
85 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
86 |
-
|
87 |
-
# Convertir el mapa de calor a un rango de 0 a 255 para aplicar color
|
88 |
heatmap = np.uint8(255 * heatmap)
|
89 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
90 |
-
|
91 |
-
# Superponer el mapa de calor en la imagen original
|
92 |
superimposed_image = cv2.addWeighted(np.array(image), 0.6, heatmap, 0.4, 0)
|
93 |
-
|
94 |
-
# Convertir a formato PIL para mostrar en Gradio
|
95 |
img_pil = Image.fromarray(superimposed_image)
|
96 |
return img_pil
|
97 |
|
98 |
def classify_image(image):
|
99 |
-
# Redimensionar la imagen a (224, 224) antes de la predicción
|
100 |
image_resized = tf.image.resize(image, (224, 224))
|
101 |
-
image_resized = tf.expand_dims(image_resized, axis=0)
|
102 |
prediction = model.predict(image_resized).flatten()
|
103 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
104 |
|
105 |
# Generar mapa de calor utilizando Grad-CAM
|
106 |
heatmap_image = generate_heatmap(np.array(image), prediction)
|
107 |
-
|
108 |
return confidences, heatmap_image
|
109 |
|
110 |
# Configuración de la interfaz de Gradio
|
111 |
title = "AI-DERM DETECTION"
|
112 |
-
|
113 |
-
|
114 |
-
)
|
115 |
-
description = (
|
116 |
-
"Utilizamos la interfaz de usuario generada por Gradio para ingresar imágenes a nuestra red neuronal..."
|
117 |
-
)
|
118 |
-
|
119 |
-
# Ejemplos de imágenes
|
120 |
examples = [
|
121 |
['./123.jpg'],
|
122 |
['./acne-closed-comedo-2.jpg'],
|
123 |
['./distal-subungual-onychomycosis-86.jpeg'],
|
124 |
['./cherry-angioma-16.jpg'],
|
125 |
-
['./malignant-melanoma-16.jpg']
|
126 |
-
['./tinea-primary-lesion-15.jpeg'],
|
127 |
-
['./congenital-nevus-35.jpg'],
|
128 |
-
['./tinea-body-137.jpg'],
|
129 |
-
['./atopic-13.jpg'],
|
130 |
-
['./atopic-7.jpg']
|
131 |
]
|
132 |
|
133 |
-
# Lanzar la interfaz de Gradio
|
134 |
gr.Interface(
|
135 |
fn=classify_image,
|
136 |
title=title,
|
137 |
-
article=article,
|
138 |
description=description,
|
139 |
inputs=gr.Image(),
|
140 |
outputs=[gr.Label(num_top_classes=4), gr.Image(label="Mapa de Calor")],
|
|
|
8 |
|
9 |
# Función personalizada para DepthwiseConv2D
|
10 |
def custom_depthwise_conv2d(**kwargs):
|
11 |
+
kwargs.pop('groups', None)
|
12 |
return DepthwiseConv2D(**kwargs)
|
13 |
|
14 |
custom_objects = {'DepthwiseConv2D': custom_depthwise_conv2d}
|
|
|
24 |
print(f"Error al cargar el modelo: {e}")
|
25 |
raise
|
26 |
|
27 |
+
# Imprimir nombres de las capas del modelo
|
28 |
print("Nombres de las capas en el modelo:")
|
29 |
for layer in model.layers:
|
30 |
print(layer.name)
|
|
|
73 |
return heatmap.numpy()
|
74 |
|
75 |
# Generar el mapa de calor de activación usando Grad-CAM
|
76 |
+
def generate_heatmap(image, prediction, last_conv_layer_name="top_conv"): # Reemplaza "top_conv" según el nombre que encuentres
|
|
|
77 |
img_array = tf.image.resize(image, (224, 224))
|
78 |
+
img_array = tf.expand_dims(img_array, axis=0)
|
79 |
|
|
|
80 |
heatmap = grad_cam(model, img_array, last_conv_layer_name)
|
|
|
|
|
81 |
heatmap = cv2.resize(heatmap, (image.shape[1], image.shape[0]))
|
|
|
|
|
82 |
heatmap = np.uint8(255 * heatmap)
|
83 |
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
|
|
|
|
|
84 |
superimposed_image = cv2.addWeighted(np.array(image), 0.6, heatmap, 0.4, 0)
|
|
|
|
|
85 |
img_pil = Image.fromarray(superimposed_image)
|
86 |
return img_pil
|
87 |
|
88 |
def classify_image(image):
|
|
|
89 |
image_resized = tf.image.resize(image, (224, 224))
|
90 |
+
image_resized = tf.expand_dims(image_resized, axis=0)
|
91 |
prediction = model.predict(image_resized).flatten()
|
92 |
confidences = {labels[i]: float(prediction[i]) for i in range(len(labels))}
|
93 |
|
94 |
# Generar mapa de calor utilizando Grad-CAM
|
95 |
heatmap_image = generate_heatmap(np.array(image), prediction)
|
|
|
96 |
return confidences, heatmap_image
|
97 |
|
98 |
# Configuración de la interfaz de Gradio
|
99 |
title = "AI-DERM DETECTION"
|
100 |
+
description = "Interfaz para diagnóstico automatizado de enfermedades de la piel"
|
101 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
examples = [
|
103 |
['./123.jpg'],
|
104 |
['./acne-closed-comedo-2.jpg'],
|
105 |
['./distal-subungual-onychomycosis-86.jpeg'],
|
106 |
['./cherry-angioma-16.jpg'],
|
107 |
+
['./malignant-melanoma-16.jpg']
|
|
|
|
|
|
|
|
|
|
|
108 |
]
|
109 |
|
|
|
110 |
gr.Interface(
|
111 |
fn=classify_image,
|
112 |
title=title,
|
|
|
113 |
description=description,
|
114 |
inputs=gr.Image(),
|
115 |
outputs=[gr.Label(num_top_classes=4), gr.Image(label="Mapa de Calor")],
|