Spaces:
Sleeping
Sleeping
File size: 2,683 Bytes
adf2111 08cbfae 259195c 2d807c6 083470e 08cbfae 2d807c6 08cbfae c8fb571 2d807c6 08cbfae c8fb571 08cbfae c8fb571 08cbfae c8fb571 7e43527 c8fb571 b0a8cf0 c8fb571 b0a8cf0 c8fb571 b0a8cf0 c8fb571 2d807c6 977a5b3 c8fb571 6c07df1 c8fb571 08cbfae 2d807c6 bcdae47 c8fb571 fd93f84 083470e 6a61f29 08cbfae 2d807c6 c8fb571 083470e 08cbfae 8d9c7cb 08cbfae 6c07df1 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import tensorflow as tf
import efficientnet.tfkeras as efn
import numpy as np
import gradio as gr
import plotly.express as px
# Dimensões da imagem
IMG_HEIGHT = 224
IMG_WIDTH = 224
# Função para construir o modelo
def build_model(img_height, img_width, n):
inp = tf.keras.layers.Input(shape=(img_height, img_width, n))
efnet = efn.EfficientNetB0(
input_shape=(img_height, img_width, n),
weights='imagenet',
include_top=False
)
x = efnet(inp)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(2, activation='softmax')(x)
model = tf.keras.Model(inputs=inp, outputs=x)
opt = tf.keras.optimizers.Adam(learning_rate=0.000003)
loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01)
model.compile(optimizer=opt, loss=loss, metrics=['accuracy'])
return model
# Carregue o modelo treinado
loaded_model = build_model(IMG_HEIGHT, IMG_WIDTH, 3)
loaded_model.load_weights('modelo_treinado.h5')
# Função para realizar o pré-processamento da imagem de entrada
def preprocess_image(input_image):
# Redimensione a imagem para as dimensões esperadas pelo modelo
input_image = tf.image.resize(input_image, (IMG_HEIGHT, IMG_WIDTH))
# Normalização dos valores de pixel para o intervalo [0, 1]
input_image = input_image / 255.0
# Outras transformações, se necessárias (por exemplo, normalização adicional)
return input_image
# Função para fazer previsões usando o modelo treinado
def predict_image(input_image):
# Realize o pré-processamento na imagem de entrada
input_image = preprocess_image(input_image)
# Faça uma previsão usando o modelo carregado
input_image = tf.expand_dims(input_image, axis=0)
prediction = loaded_model.predict(input_image)
# A saída será uma matriz de previsões (no caso de classificação de duas classes, será algo como [[probabilidade_classe_0, probabilidade_classe_1]])
# Adicione lógica para interpretar o resultado e formatá-lo para exibição
class_names = ["Normal", "Cataract"]
predicted_class = class_names[np.argmax(prediction)]
probability = prediction[0][np.argmax(prediction)]
# Retorna a previsão em formato de string
return f"""
Predicted Class: {predicted_class}
Probability: {probability:.2%}
"""
# Crie uma interface Gradio para fazer previsões
iface = gr.Interface(
fn=predict_image,
inputs=gr.inputs.Image(label="Upload an Image", type="pil"),
outputs=gr.outputs.Output(label="Prediction", type="string"),
interpretation="text",
custom_interpretation_fn=predict_image
)
# Execute a interface Gradio
iface.launch()
|