Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from tensorflow.keras.preprocessing import image
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
|
7 |
+
# Carregar o modelo treinado
|
8 |
+
model = load_model('/kaggle/working/model_1.0000.h5')
|
9 |
+
|
10 |
+
def predict_image(input_image):
|
11 |
+
# Carregar uma imagem e fazer a previsão
|
12 |
+
img = image.load_img(input_image, target_size=(224, 224))
|
13 |
+
img = image.img_to_array(img)
|
14 |
+
img = np.expand_dims(img, axis=0)
|
15 |
+
img = img / 255.0 # Normalizar a imagem (como fizemos durante o treinamento)
|
16 |
+
|
17 |
+
prediction = model.predict(img)
|
18 |
+
|
19 |
+
# Interpretar o resultado
|
20 |
+
if prediction[0][0] > 0.5:
|
21 |
+
result = "DOENTE"
|
22 |
+
else:
|
23 |
+
result = "NORMAL"
|
24 |
+
|
25 |
+
# Mostrar a imagem invertida
|
26 |
+
img_inverted = 1 - img # Inverter a imagem
|
27 |
+
|
28 |
+
# Obter ativações da camada convolucional
|
29 |
+
activation_model = Model(inputs=model.input, outputs=model.layers[5].output) # Substitua 5 pelo índice da camada desejada
|
30 |
+
activations = activation_model.predict(img)
|
31 |
+
|
32 |
+
# Criar um mapa de calor das ativações
|
33 |
+
plt.figure(figsize=(12, 5))
|
34 |
+
plt.subplot(1, 3, 1)
|
35 |
+
plt.imshow(image.load_img(input_image))
|
36 |
+
plt.title(f"Resultado da previsão: {result}")
|
37 |
+
plt.axis('off')
|
38 |
+
|
39 |
+
plt.subplot(1, 3, 2)
|
40 |
+
plt.imshow(img_inverted[0])
|
41 |
+
plt.title("Imagem Invertida")
|
42 |
+
plt.axis('off')
|
43 |
+
|
44 |
+
plt.subplot(1, 3, 3)
|
45 |
+
sns.heatmap(activations[0, :, :, 0], cmap='viridis') # Ajuste o índice e o mapa de cores conforme necessário
|
46 |
+
plt.title("Mapa de Calor da Ativação")
|
47 |
+
plt.axis('off')
|
48 |
+
|
49 |
+
return plt
|
50 |
+
|
51 |
+
# Criar uma interface Gradio
|
52 |
+
iface = gr.Interface(fn=predict_image, inputs="image", outputs="matplotlib", title="Modelo de Classificação de Imagem")
|
53 |
+
iface.launch()
|