Question about how to deploy an application with trained weights.

estoy tratando de desplegar una palicacion de la cual ya tengo los pesos entrenados y esta funcionando en local, los pesos estan en un archivo .pt el cual tengo en la ruta de mi github el nombre del archivo que tiene los pesos optimos es ‘modelo_guardado.pt’, voy a adjuntar una parte del donde enruto el modelo

#Import All the Required Libraries
import cv2
import streamlit as st
from pathlib import Path
import sys
from ultralytics import YOLO
from PIL import Image

#Get the absolute path of the current file
FILE = Path(file).resolve()

ROOT = FILE.parent

MODEL_DIR = ROOT/‘weights’

#import torch
#import ultralytics.nn.tasks
#torch.serialization.add_safe_globals([ultralytics.nn.tasks.DetectionModel])

#DETECTION_MODEL = aplicativo-limpio/weights/‘modelo_guardado.pt’
DETECTION_MODEL = MODEL_DIR/‘modelo_guardado.pt’

al hacer el despliegue tengo el siguiente aviso:

Unable to load model. Check the sepcified path: weights/modelo_guardado.pt

Weights only load failed. This file can still be loaded, to do so you have two options, e[1mdo those steps only if you trust the source of the checkpointe[0m. (1) In PyTorch 2.6, we changed the default value of the weights_only argument in torch.load from False to True. Re-running torch.load with weights_only set to False will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source. (2) Alternatively, to load with weights_only=True please check the recommended steps in the following error message. WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed global by default. Please use torch.serialization.add_safe_globals([ultralytics.nn.tasks.DetectionModel]) or the torch.serialization.safe_globals([ultralytics.nn.tasks.DetectionModel]) context manager to allowlist this global if you trust this class/function.

Check the documentation of torch.load to learn more about types accepted by default with weights_only torch.load — PyTorch 2.7 documentation.

I’ve tried everything in the instructions, but I haven’t been able to get the model to work. Please, if anyone can help me, I’d appreciate it.

Thank you very much for your help.
Greetings from Colombia

Hi @Juan15,

I can’t say for sure, but if I was you, I’d focus on making sure that the deployed app actually includes the files themselves, and that they’re located in the place you expect.

In general, I would just do that by adding a bunch of st.write statements to show the current path, and what’s in the directories.

For example, you might do st.write(ROOT.absolute()), st.write(MODEL_DIR.iterdir()), etc.

And also just st.write(DETECTION_MODEL.exists())

1 Like

Hello, good afternoon.

After trying several methods, I realized that the Ultralytics version I had in the requirements.txt file didn’t match the version I used to train the weights. After changing the version, the application ran properly with all the trained models. I’m very grateful, and I hope this observation helps other users :slight_smile:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.