DHEIVER's picture
Update app.py
39ee3c6
raw
history blame
No virus
1.72 kB
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
# Carregar o modelo treinado
model = load_model('model_1.0000.h5')
def predict_image(input_image):
# Carregar uma imagem e fazer a previsão
img = image.load_img(input_image, target_size=(224, 224))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = img / 255.0 # Normalizar a imagem (como fizemos durante o treinamento)
prediction = model.predict(img)
# Interpretar o resultado
if prediction[0][0] > 0.5:
result = "DOENTE"
else:
result = "NORMAL"
# Mostrar a imagem invertida
img_inverted = 1 - img # Inverter a imagem
# Obter ativações da camada convolucional
activation_model = Model(inputs=model.input, outputs=model.layers[5].output) # Substitua 5 pelo índice da camada desejada
activations = activation_model.predict(img)
# Criar um mapa de calor das ativações
plt.figure(figsize=(12, 5))
plt.subplot(1, 3, 1)
plt.imshow(image.load_img(input_image))
plt.title(f"Resultado da previsão: {result}")
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(img_inverted[0])
plt.title("Imagem Invertida")
plt.axis('off')
plt.subplot(1, 3, 3)
sns.heatmap(activations[0, :, :, 0], cmap='viridis') # Ajuste o índice e o mapa de cores conforme necessário
plt.title("Mapa de Calor da Ativação")
plt.axis('off')
return plt
# Criar uma interface Gradio
iface = gr.Interface(fn=predict_image, inputs="image", outputs="matplotlib", title="Modelo de Classificação de Imagem")
iface.launch()