Spaces:
Sleeping
Sleeping
File size: 3,964 Bytes
083bf6f adf2111 abed96a 7a80d19 abed96a adf2111 2283b5b adf2111 2283b5b 73036df e0ee69d 2283b5b 73036df 2283b5b 73036df e0ee69d 083bf6f adf2111 2283b5b 907b41c adf2111 2283b5b 083bf6f adf2111 2283b5b 907b41c |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# import gradio as gr
# import tensorflow as tf
# import numpy as np
# # Defina a camada personalizada FixedDropout
# class FixedDropout(tf.keras.layers.Dropout):
# def _get_noise_shape(self, inputs):
# if self.noise_shape is None:
# return self.noise_shape
# symbolic_shape = tf.shape(inputs)
# noise_shape = [symbolic_shape[axis] if shape is None else shape
# for axis, shape in enumerate(self.noise_shape)]
# return tuple(noise_shape)
# # Registre a camada personalizada FixedDropout
# tf.keras.utils.get_custom_objects()['FixedDropout'] = FixedDropout
# # Carregue seu modelo TensorFlow treinado
# model = tf.keras.models.load_model('modelo_treinado.h5')
# # Defina uma função para fazer previsões
# def classify_image(input_image):
# # Redimensione a imagem para as dimensões corretas (192x256)
# input_image = tf.image.resize(input_image, (192, 256)) # Redimensione para as dimensões esperadas
# input_image = (input_image / 255.0) # Normalize para [0, 1]
# input_image = np.expand_dims(input_image, axis=0) # Adicione a dimensão de lote
# # Faça a previsão usando o modelo
# prediction = model.predict(input_image)
# # Assumindo que o modelo retorna probabilidades para duas classes, você pode retornar a classe com a maior probabilidade
# class_index = np.argmax(prediction)
# class_labels = ["Normal", "Cataract"] # Substitua pelas suas etiquetas de classe reais
# predicted_class = class_labels[class_index]
# return predicted_class
# # Crie uma interface Gradio
# input_interface = gr.Interface(
# fn=classify_image,
# inputs="image", # Especifique o tipo de entrada como "image"
# outputs="text" # Especifique o tipo de saída como "text"
# )
# # Inicie o aplicativo Gradio
# input_interface.launch()
import gradio as gr
import tensorflow as tf
import numpy as np
# Defina a camada personalizada FixedDropout
class FixedDropout(tf.keras.layers.Dropout):
def _get_noise_shape(self, inputs):
if self.noise_shape is None:
return self.noise_shape
symbolic_shape = tf.shape(inputs)
noise_shape = [symbolic_shape[axis] if shape is None else shape
for axis, shape in enumerate(self.noise_shape)]
return tuple(noise_shape)
# Registre a camada personalizada FixedDropout
tf.keras.utils.get_custom_objects()['FixedDropout'] = FixedDropout
# Carregue seu modelo TensorFlow treinado
model = tf.keras.models.load_model('modelo_treinado.h5')
# Defina uma função para fazer previsões
def classify_image(input_image):
# Redimensione a imagem para as dimensões corretas (192x256)
input_image = tf.image.resize(input_image, (192, 256)) # Redimensione para as dimensões esperadas
input_image = (input_image / 255.0) # Normalize para [0, 1]
input_image = np.expand_dims(input_image, axis=0) # Adicione a dimensão de lote
# Faça a previsão usando o modelo
prediction = model.predict(input_image)
# Assumindo que o modelo retorna probabilidades para duas classes, você pode retornar a classe com a maior probabilidade
class_index = np.argmax(prediction)
class_labels = ["Normal", "Cataract"] # Substitua pelas suas etiquetas de classe reais
predicted_class = class_labels[class_index]
# Retorne a classe prevista e as probabilidades das classes
class_probabilities = {class_labels[i]: round(float(prediction[0][i]), 4) for i in range(len(class_labels))}
return predicted_class, class_probabilities
# Crie uma interface Gradio
input_interface = gr.Interface(
fn=classify_image,
inputs="image", # Especifique o tipo de entrada como "image"
outputs=["text", "text"], # Especifique dois tipos de saída: classe e probabilidades
output_labels=["Predicted Class", "Class Probabilities"] # Rotule as saídas
)
# Inicie o aplicativo Gradio
input_interface.launch()
|