Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,18 @@ import numpy as np
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import albumentations as A
|
| 6 |
|
| 7 |
-
# --- 1. Definici贸n de Clases ---
|
| 8 |
|
| 9 |
# Transformaci贸n de Etiquetas
|
| 10 |
class TargetMaskConvertTransform(ItemTransform):
|
| 11 |
def __init__(self): pass
|
| 12 |
def encodes(self, x): return x
|
| 13 |
|
| 14 |
-
# Transformaci贸n de Albumentations
|
|
|
|
| 15 |
class SegmentationAlbumentationsTransform(ItemTransform):
|
| 16 |
-
|
| 17 |
-
def
|
| 18 |
-
pass
|
| 19 |
-
def encodes(self, x):
|
| 20 |
-
return x
|
| 21 |
|
| 22 |
# M茅trica Jaccard
|
| 23 |
def JaccardMulti(inp, targ, axis=1, smooth=1e-6, reduction='mean', square_sum=False):
|
|
@@ -34,9 +32,15 @@ FILENAME = "model.pkl"
|
|
| 34 |
print(f"Descargando modelo desde {REPO_ID}...")
|
| 35 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 36 |
|
| 37 |
-
# Cargamos el learner
|
| 38 |
learn = load_learner(model_path, cpu=True)
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# --- 3. Funci贸n de Predicci贸n ---
|
| 41 |
def predict(img):
|
| 42 |
# Predicci贸n
|
|
@@ -49,13 +53,12 @@ def predict(img):
|
|
| 49 |
# Creamos la imagen RGB vac铆a
|
| 50 |
color_mask = np.zeros((h, w, 3), dtype=np.uint8)
|
| 51 |
|
| 52 |
-
# --- PALETA DE COLORES
|
| 53 |
-
# 0:Fondo
|
| 54 |
-
|
| 55 |
color_mask[mask_data == 1] = [0, 255, 0] # Hojas -> Verde
|
| 56 |
-
color_mask[mask_data == 2] = [0, 0, 255] # Madera -> Azul
|
| 57 |
-
color_mask[mask_data == 3] = [255, 0, 0] # Poste -> Rojo
|
| 58 |
-
color_mask[mask_data == 4] = [255, 255, 255] # Uvas -> Blanco
|
| 59 |
|
| 60 |
return color_mask
|
| 61 |
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
import albumentations as A
|
| 6 |
|
| 7 |
+
# --- 1. Definici贸n de Clases (Necesarias para el unpickle) ---
|
| 8 |
|
| 9 |
# Transformaci贸n de Etiquetas
|
| 10 |
class TargetMaskConvertTransform(ItemTransform):
|
| 11 |
def __init__(self): pass
|
| 12 |
def encodes(self, x): return x
|
| 13 |
|
| 14 |
+
# Transformaci贸n de Albumentations
|
| 15 |
+
# La definimos solo para que load_learner no falle al leer el archivo.
|
| 16 |
class SegmentationAlbumentationsTransform(ItemTransform):
|
| 17 |
+
def __init__(self, aug): pass
|
| 18 |
+
def encodes(self, x): return x
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# M茅trica Jaccard
|
| 21 |
def JaccardMulti(inp, targ, axis=1, smooth=1e-6, reduction='mean', square_sum=False):
|
|
|
|
| 32 |
print(f"Descargando modelo desde {REPO_ID}...")
|
| 33 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 34 |
|
| 35 |
+
# Cargamos el learner
|
| 36 |
learn = load_learner(model_path, cpu=True)
|
| 37 |
|
| 38 |
+
# --- SOLUCI脫N DEL ERROR (CIRUG脥A DE PIPELINE) ---
|
| 39 |
+
# Buscamos la transformaci贸n de Albumentations dentro del learner y la ELIMINAMOS de la lista.
|
| 40 |
+
# As铆 garantizamos que nunca se ejecute durante la inferencia.
|
| 41 |
+
learn.dls.after_item.fs = [t for t in learn.dls.after_item.fs if not isinstance(t, SegmentationAlbumentationsTransform)]
|
| 42 |
+
print("Transformaci贸n de Albumentations eliminada del pipeline de inferencia.")
|
| 43 |
+
|
| 44 |
# --- 3. Funci贸n de Predicci贸n ---
|
| 45 |
def predict(img):
|
| 46 |
# Predicci贸n
|
|
|
|
| 53 |
# Creamos la imagen RGB vac铆a
|
| 54 |
color_mask = np.zeros((h, w, 3), dtype=np.uint8)
|
| 55 |
|
| 56 |
+
# --- PALETA DE COLORES ---
|
| 57 |
+
# 0:Fondo, 1:Hojas(Verde), 2:Madera(Azul), 3:Poste(Rojo), 4:Uvas(Blanco)
|
|
|
|
| 58 |
color_mask[mask_data == 1] = [0, 255, 0] # Hojas -> Verde
|
| 59 |
+
color_mask[mask_data == 2] = [0, 0, 255] # Madera -> Azul
|
| 60 |
+
color_mask[mask_data == 3] = [255, 0, 0] # Poste -> Rojo
|
| 61 |
+
color_mask[mask_data == 4] = [255, 255, 255] # Uvas -> Blanco
|
| 62 |
|
| 63 |
return color_mask
|
| 64 |
|