File size: 1,734 Bytes
db8ae5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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('/kaggle/working/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()