Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
import tensorflow as tf
|
2 |
import efficientnet.tfkeras as efn
|
3 |
-
from tensorflow.keras.layers import Input, GlobalAveragePooling2D, Dense
|
4 |
-
import numpy as np
|
5 |
import gradio as gr
|
6 |
|
7 |
# Dimensões da imagem
|
@@ -10,15 +8,15 @@ IMG_WIDTH = 224
|
|
10 |
|
11 |
# Função para construir o modelo
|
12 |
def build_model(img_height, img_width, n):
|
13 |
-
inp = Input(shape=(img_height, img_width, n))
|
14 |
efnet = efn.EfficientNetB0(
|
15 |
input_shape=(img_height, img_width, n),
|
16 |
weights='imagenet',
|
17 |
include_top=False
|
18 |
)
|
19 |
x = efnet(inp)
|
20 |
-
x = GlobalAveragePooling2D()(x)
|
21 |
-
x = Dense(2, activation='softmax')(x)
|
22 |
model = tf.keras.Model(inputs=inp, outputs=x)
|
23 |
opt = tf.keras.optimizers.Adam(learning_rate=0.000003)
|
24 |
loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01)
|
@@ -42,7 +40,7 @@ def preprocess_image(input_image):
|
|
42 |
return input_image
|
43 |
|
44 |
# Função para fazer previsões usando o modelo treinado
|
45 |
-
def
|
46 |
# Realize o pré-processamento na imagem de entrada
|
47 |
input_image = preprocess_image(input_image)
|
48 |
|
@@ -51,22 +49,19 @@ def predict_image(input_image):
|
|
51 |
prediction = loaded_model.predict(input_image)
|
52 |
|
53 |
# 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]])
|
54 |
-
class_names = ["
|
55 |
-
probabilities = prediction[0]
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
for class_name, probability in zip(class_names, probabilities):
|
60 |
-
bars.append(gr.outputs.Bar(label=class_name, confidence=probability))
|
61 |
|
62 |
-
return
|
63 |
|
64 |
# Crie uma interface Gradio para fazer previsões
|
65 |
iface = gr.Interface(
|
66 |
-
|
67 |
-
inputs="
|
68 |
-
outputs=
|
69 |
-
|
70 |
)
|
71 |
|
72 |
# Execute a interface Gradio
|
|
|
1 |
import tensorflow as tf
|
2 |
import efficientnet.tfkeras as efn
|
|
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
# Dimensões da imagem
|
|
|
8 |
|
9 |
# Função para construir o modelo
|
10 |
def build_model(img_height, img_width, n):
|
11 |
+
inp = tf.keras.layers.Input(shape=(img_height, img_width, n))
|
12 |
efnet = efn.EfficientNetB0(
|
13 |
input_shape=(img_height, img_width, n),
|
14 |
weights='imagenet',
|
15 |
include_top=False
|
16 |
)
|
17 |
x = efnet(inp)
|
18 |
+
x = tf.keras.layers.GlobalAveragePooling2D()(x)
|
19 |
+
x = tf.keras.layers.Dense(2, activation='softmax')(x)
|
20 |
model = tf.keras.Model(inputs=inp, outputs=x)
|
21 |
opt = tf.keras.optimizers.Adam(learning_rate=0.000003)
|
22 |
loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01)
|
|
|
40 |
return input_image
|
41 |
|
42 |
# Função para fazer previsões usando o modelo treinado
|
43 |
+
def predict(input_image):
|
44 |
# Realize o pré-processamento na imagem de entrada
|
45 |
input_image = preprocess_image(input_image)
|
46 |
|
|
|
49 |
prediction = loaded_model.predict(input_image)
|
50 |
|
51 |
# 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]])
|
52 |
+
class_names = ["Not Hot Dog", "Hot Dog"]
|
|
|
53 |
|
54 |
+
# Determine a classe mais provável
|
55 |
+
predicted_class = class_names[np.argmax(prediction)]
|
|
|
|
|
56 |
|
57 |
+
return predicted_class
|
58 |
|
59 |
# Crie uma interface Gradio para fazer previsões
|
60 |
iface = gr.Interface(
|
61 |
+
predict,
|
62 |
+
inputs=gr.inputs.Image(label="Upload hot dog candidate", type="file"),
|
63 |
+
outputs=gr.outputs.Label(num_top_classes=2),
|
64 |
+
title="Hot Dog? Or Not?",
|
65 |
)
|
66 |
|
67 |
# Execute a interface Gradio
|