roboflow-python icon indicating copy to clipboard operation
roboflow-python copied to clipboard

Deploy Weights Error

Open jondave opened this issue 1 year ago • 4 comments

Hi I am trying to deploy my model trained on local computer to RoboFlow.

Deploy code:

import roboflow

rf = roboflow.Roboflow(api_key=ROBOFLOW_API_KEY)
project = rf.workspace("<workspace>").project("<project>")

version = project.version(1)
version.deploy("yolov9", "<ffile_path>", "weights/best.pt")

This gives an error

Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.
        (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 numpy.core.multiarray._reconstruct was not an allowed global by default. Please use `torch.serialization.add_safe_globals([_reconstruct])` or the `torch.serialization.safe_globals([_reconstruct])` 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 https://pytorch.org/docs/stable/generated/torch.load.html.

Torch version: 2.6.0 Ultralytics version: 8.3.73 Roboflow version: 1.1.53

Any help appreciated.

jondave avatar Feb 07 '25 15:02 jondave

Downgraded

Torch version: 2.6.0 to 2.5.1 Torchvision to 0.20.1 Ultralytics version: 8.3.73 to 8.3.40 Roboflow version: 1.1.53 to 1.1.48

They resolved the weights error but now I get the error. ModuleNotFoundError: No module named 'models'

loading Roboflow workspace...
loading Roboflow project...
You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
Traceback (most recent call last):
  File "roboflow_upload_model.py", line 20, in <module>
    version.deploy(model_type="yolov9", model_path=f"yolov9/runs/train/exp3")
  File "yolov9/yolov9-env/lib/python3.12/site-packages/roboflow/core/version.py", line 566, in deploy
    model = torch.load(os.path.join(model_path, filename))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "yolov9/yolov9-env/lib/python3.12/site-packages/torch/serialization.py", line 1360, in load
    return _load(
           ^^^^^^
  File "yolov9/yolov9-env/lib/python3.12/site-packages/torch/serialization.py", line 1848, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "yolov9/yolov9-env/lib/python3.12/site-packages/torch/serialization.py", line 1837, in find_class
    return super().find_class(mod_name, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'models'

jondave avatar Feb 10 '25 15:02 jondave

Any resolve? I too have this issue.

TheOracle2212 avatar Feb 28 '25 11:02 TheOracle2212

So, I had to upgrade everything (All my libraries) to the latest versions, and then retrain my model to YOLOv8 (version doesn't matter), from YOLOv5n, and then it all worked fine. Hopefully someone finds this useful.

TheOracle2212 avatar Feb 28 '25 23:02 TheOracle2212

Hello, I've been having the same problem, but I don't really get how you fixed it. Did you have to retrain a model from scratch? Were you unable to make use of the model weights you saved from Roboflow? Thanks in advance!

giulliabraga avatar May 06 '25 18:05 giulliabraga