Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastai.vision.all import
|
| 3 |
-
from pathlib import Path
|
| 4 |
|
| 5 |
-
# 1
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
Normalize.from_stats(*imagenet_stats)]
|
| 9 |
|
| 10 |
-
# 2
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
item_tfms=item_tfms, batch_tfms=batch_tfms,
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
# 3) Crea el learner con la arquitectura y métricas
|
| 18 |
-
learn = cnn_learner(
|
| 19 |
-
dls, resnet34,
|
| 20 |
-
metrics=[accuracy, Precision(average="weighted"), Recall(average="weighted")]
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
# 4) Carga *solo* los pesos que subiste como tobacco_weights.pth
|
| 24 |
-
learn.load("tobacco_weights") # sin extensión .pth
|
| 25 |
|
| 26 |
def predict_image(img):
|
| 27 |
-
|
|
|
|
| 28 |
return {str(pred): float(probs[idx])}
|
| 29 |
|
| 30 |
-
# 5) Interfaz Gradio
|
| 31 |
iface = gr.Interface(
|
| 32 |
fn=predict_image,
|
| 33 |
-
inputs=gr.Image(type="pil", label="Sube una imagen
|
| 34 |
outputs=gr.Label(num_top_classes=3, label="Predicciones"),
|
| 35 |
-
title="Clasificador Tobacco-3482",
|
| 36 |
-
description="Carga tu imagen y recibe las 3 clases más probables."
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastai.vision.all import load_learner, PILImage
|
|
|
|
| 3 |
|
| 4 |
+
# 1. Carga directa de los pesos en un learner preconfigurado
|
| 5 |
+
learn = load_learner('export.pkl') if Path('export.pkl').exists() \
|
| 6 |
+
else load_learner('model.pkl') # por si lo quieres probar
|
|
|
|
| 7 |
|
| 8 |
+
# 2. O, si usas la opción 2 con .save():
|
| 9 |
+
# from fastai.vision.all import cnn_learner, resnet34, imagenet_stats
|
| 10 |
+
# learn = cnn_learner(dls=None, arch=resnet34, metrics=[], cbs=[])
|
| 11 |
+
# learn.load('tobacco_weights')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def predict_image(img):
|
| 14 |
+
# img ya es PILImage si usas type="pil"
|
| 15 |
+
pred, idx, probs = learn.predict(PILImage.create(img))
|
| 16 |
return {str(pred): float(probs[idx])}
|
| 17 |
|
|
|
|
| 18 |
iface = gr.Interface(
|
| 19 |
fn=predict_image,
|
| 20 |
+
inputs=gr.Image(type="pil", label="Sube una imagen"),
|
| 21 |
outputs=gr.Label(num_top_classes=3, label="Predicciones"),
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|