Edupy commited on
Commit
bca1dfd
·
1 Parent(s): fa901be

Usando from_pretrained_fastai para carga limpia

Browse files
Files changed (1) hide show
  1. app.py +6 -15
app.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  import torch
4
  import timm
5
  from PIL import Image
6
- from torchvision import transforms
7
  from fastai.vision.all import *
8
 
9
  # 1. Categorías (Asegúrate de que el orden sea el mismo que dls.vocab en Colab)
@@ -12,16 +11,15 @@ categories = ['Bug', 'Dark', 'Dragon', 'Electric', 'Fairy', 'Fighting',
12
  'Normal', 'Poison', 'Psychic', 'Rock', 'Steel', 'Water']
13
 
14
  def load_model_fastai_style(weights_path):
15
- # En lugar de from_empty, creamos dls mínimos con Datasets vacíos
16
- # Esto es universal y no depende de métodos de clase que cambian
17
  empty_ds = Datasets([None], [[]])
18
  dls = DataLoaders.from_dsets(empty_ds, empty_ds, path='.', bs=1, device='cpu')
19
  dls.vocab = categories
20
 
21
- # Construimos el Learner
22
- learn = vision_learner(dls, 'convnext_tiny', pretrained=False)
23
 
24
- # Cargar pesos con control de errores
25
  state = torch.load(weights_path, map_location='cpu', weights_only=False)
26
  if isinstance(state, dict) and 'model' in state:
27
  learn.model.load_state_dict(state['model'])
@@ -31,19 +29,12 @@ def load_model_fastai_style(weights_path):
31
  learn.model.eval()
32
  return learn
33
 
34
- # Cargamos el modelo
35
  learn = load_model_fastai_style('checkpoint_1.pth')
36
 
37
  def predict(img):
38
- # 1. Convertir a PILImage de Fastai
39
- img = PILImage.create(img)
40
- # 2. Resize manual al tamaño de entrenamiento
41
- img = img.resize((126, 126))
42
-
43
- # 3. Predicción
44
- # El método predict gestiona internamente la normalización que el modelo espera
45
  pred, pred_idx, probs = learn.predict(img)
46
-
47
  return {categories[i]: float(probs[i]) for i in range(len(categories))}
48
 
49
  # Interfaz
 
3
  import torch
4
  import timm
5
  from PIL import Image
 
6
  from fastai.vision.all import *
7
 
8
  # 1. Categorías (Asegúrate de que el orden sea el mismo que dls.vocab en Colab)
 
11
  'Normal', 'Poison', 'Psychic', 'Rock', 'Steel', 'Water']
12
 
13
  def load_model_fastai_style(weights_path):
14
+ # Creamos dls mínimos
 
15
  empty_ds = Datasets([None], [[]])
16
  dls = DataLoaders.from_dsets(empty_ds, empty_ds, path='.', bs=1, device='cpu')
17
  dls.vocab = categories
18
 
19
+ # Especificamos loss_func explícitamente para evitar el AssertionError
20
+ learn = vision_learner(dls, 'convnext_tiny', pretrained=False, loss_func=CrossEntropyLossFlat())
21
 
22
+ # Cargar pesos
23
  state = torch.load(weights_path, map_location='cpu', weights_only=False)
24
  if isinstance(state, dict) and 'model' in state:
25
  learn.model.load_state_dict(state['model'])
 
29
  learn.model.eval()
30
  return learn
31
 
32
+ # Carga
33
  learn = load_model_fastai_style('checkpoint_1.pth')
34
 
35
  def predict(img):
36
+ img = PILImage.create(img).resize((126, 126))
 
 
 
 
 
 
37
  pred, pred_idx, probs = learn.predict(img)
 
38
  return {categories[i]: float(probs[i]) for i in range(len(categories))}
39
 
40
  # Interfaz