How to set `zoo_url` for a custom model in DeGirum?

Hi everyone,
I am trying to run inference on a video using a custom model with the DeGirum library. However, I am not sure how to correctly set the zoo_url parameter in the dg.load_model() function to point to my custom model. Below is my current code:

import degirum as dg
import cv2

# Declaring variables
your_model_name = "model-name"
your_host_address = "@local"  # Can be dg.CLOUD, host:port, or dg.LOCAL
your_model_zoo = "degirum/hailo"  # What should I replace this with for my custom model?
your_token = "<token>"

# Specify the video file
your_video = "path/video.mp4"

# Load the model
model = dg.load_model(
    model_name=your_model_name,
    inference_host_address=your_host_address,
    zoo_url=your_model_zoo,
    token=your_token
)

# Open the video file
stream = cv2.VideoCapture(your_video)

# Define a generator function to produce video frames
def frame_source(stream):
    while True:
        ret, frame = stream.read()
        if not ret:
            break
        yield frame

# Run predict_batch on frames from the video
for result in model.predict_batch(frame_source(stream)):
    print(result)

# Release the stream
stream.release()

Hi @farukcinemre25
Welcome to the DeGirum community. To run inference with a model in local folder, you need to have a folder with all the model assets (.hef file, model json, labels files, any python postprocessor). Then in the zoo_url you need to specify the path to this folder. Please let us know if this works.

import degirum as dg
import cv2

# Load the segmentation model from the model zoo.
# Replace '<path_to_model_zoo>' with the directory path to your model assets.
model = dg.load_model(
    model_name='yolov8n_seg',
    inference_host_address='@local',
    zoo_url='home/pi/hailo_examples/model_zoo'
)

# Run inference on an input image.
# Replace '<path_to_input_image>' with the actual path to your image.
inference_result = model('/home/pi/hailo_examples/assets/panel1.jpg')

# The segmentation overlay (with masks and labels) is available via the image_overlay attribute.
cv2.imshow("Segmentation Output", inference_result.image_overlay)

# Wait until the user presses 'x' or 'q' to close the window.
while True:
    key = cv2.waitKey(0) & 0xFF
    if key == ord('x') or key == ord('q'):
        break

cv2.destroyAllWindows()

I took the example code I wrote above from the GitHub repository and tried running it on the RPi 5 8GB model, but I’m getting an error as shown in the image. You can see that my model exists in the file structure. How can I run my own model?.
The file structure is as follows:
The folder path: home/pi/hailo_examples/model_zoo/
Contents of the folder:

  • yolov8n_seg.hef
  • yolov8n_seg.json
  • labels_coco.json

Hi @farukcinemre25
Can you share the contents of the json file?

Also, can you run the code snippet below and let me know what you see?

import degirum as dg

zoo_1 = dg.connect(
    inference_host_address='@local',
    zoo_url='home/pi/hailo_examples/model_zoo'
)
print(zoo_1.list_models())

zoo_2 = dg.connect(
    inference_host_address='@local',
    zoo_url='/home/pi/hailo_examples/model_zoo'
)
print(zoo_2.list_models())

{
    "ConfigVersion": 10,
    "DEVICE": [
        {
            "DeviceType": "HAILO8",
            "RuntimeAgent": "HAILORT",
            "SupportedDeviceTypes": "HAILORT/HAILO8"
        }
    ],
    "PRE_PROCESS": [
        {
            "InputType": "Image",
            "InputN": 1,
            "InputH": 640,
            "InputW": 640,
            "InputC": 3,
            "InputPadMethod": "letterbox",
            "InputResizeMethod": "bilinear",
            "InputQuantEn": true
        }
    ],
    "MODEL_PARAMETERS": [
        {
            "ModelPath": "yolov8n_seg.hef"
        }
    ],
    "POST_PROCESS": [
        {
            "OutputPostprocessType": "SegmentationYoloV8",
            "LabelsPath": "labels_coco.json",
            "OutputNumClasses": 1,
            "OutputConfThreshold": 0.3,
            "SigmoidOnCLS": true
        }
    ]
}

the upper one is yolov8n_seg.json, and the lower one is labels_coco.json

{
    "0": "solar_cell"
}
Traceback (most recent call last):
  File "/home/pi/hailo_examples/model_zoo/test.py", line 7, in <module>
    print(zoo_1.list_models())
          ^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/zoo_manager.py", line 206, in list_models
    return self._zoo.list_models(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 101, in list_models
    self._get_model_list(False),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 563, in _get_model_list
    model_list = self._cloud_server_request(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 498, in _cloud_server_request
    raise DegirumException(details) from None
degirum.exceptions.DegirumException: 404 Client Error: Not Found for url: https://hub.degirum.com/zoo/v1/public/models/home/pi/hailo_examples/model_zoo

and code output

Hi @farukcinemre25
Can you add one more line to the JSON file just below the ConfigVersion?

"Checksum": "926bf34651d94e850361ad272b141a61af0097e64e46f3a7519e7dff84c8f323",

The actual value of checksum does not matter but it is a required field.

Traceback (most recent call last):
  File "/home/pi/hailo_examples/model_zoo/test.py", line 6, in <module>
    model = dg.load_model(
            ^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/__init__.py", line 220, in load_model
    return zoo.load_model(model_name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/zoo_manager.py", line 270, in load_model
    model = self._zoo.load_model(model_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 627, in load_model
    assets = self._get_model_assets(model, True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 580, in _get_model_assets
    model_info = self._cloud_server_request(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 498, in _cloud_server_request
    raise DegirumException(details) from None
degirum.exceptions.DegirumException: 404 Client Error: Not Found for url: https://hub.degirum.com/zoo/v1/public/models/home/pi/hailo_examples/model_zoo/yolov8n_seg/info
(degirum_env) pi@cm4:~/hailo_examples/model_zoo $ python test.py
Traceback (most recent call last):
  File "/home/pi/hailo_examples/model_zoo/test.py", line 6, in <module>
    model = dg.load_model(
            ^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/__init__.py", line 220, in load_model
    return zoo.load_model(model_name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/zoo_manager.py", line 270, in load_model
    model = self._zoo.load_model(model_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 627, in load_model
    assets = self._get_model_assets(model, True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 580, in _get_model_assets
    model_info = self._cloud_server_request(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 498, in _cloud_server_request
    raise DegirumException(details) from None
degirum.exceptions.DegirumException: 404 Client Error: Not Found for url: https://hub.degirum.com/zoo/v1/public/models/home/pi/hailo_examples/model_zoo/yolov8n_seg/info
(degirum_env) pi@cm4:~/hailo_examples/model_zoo $ python test.py
Traceback (most recent call last):
  File "/home/pi/hailo_examples/model_zoo/test.py", line 6, in <module>
    model = dg.load_model(
            ^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/__init__.py", line 220, in load_model
    return zoo.load_model(model_name, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/zoo_manager.py", line 270, in load_model
    model = self._zoo.load_model(model_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 627, in load_model
    assets = self._get_model_assets(model, True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 580, in _get_model_assets
    model_info = self._cloud_server_request(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/log.py", line 59, in wrap
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/pi/hailo_examples/degirum_env/lib/python3.11/site-packages/degirum/_zoo_accessor.py", line 498, in _cloud_server_request
    raise DegirumException(details) from None
degirum.exceptions.DegirumException: 404 Client Error: Not Found for url: https://hub.degirum.com/zoo/v1/public/models/home/pi/hailo_examples/model_zoo/yolov8n_seg/info

I get this error it works when I use your models, I get an error when I use my own model

Hi @farukcinemre25
Did you try with zoo_url='/home/pi/hailo_examples/model_zoo' (with / in front)?

Hi @farukcinemre25,

The error degirum.exceptions.DegirumException: 404 Client Error: Not Found for url: https://hub.degirum.com/zoo/v1/public/models/home/pi/hailo_examples/model_zoo/yolov8n_seg/info you got suggests that you specified incorrect value for zoo_url argument.

When working with local zoo, you need to specify full valid path to that local zoo directory as zoo_url argument. In your case you specified not valid path. Namely, it does not start with slash, so it is treated as relative path, but such relative path does not exist in your CWD, assuming that your CWD is not root dir.

Please change your code this way (note leading slash in path; make sure that /home/pi/hailo_examples/model_zoo dir exists and contains your model):

    model_name='yolov8n_seg',
    inference_host_address='@local',
    zoo_url='/home/pi/hailo_examples/model_zoo'
)

I am sure about the path of the model. All the necessary files are located in the /home/pi/hailo_examples/model_zoo/ directory within the model_zoo folder. When I change the model_name in the test.py code and use one of the models from the model_zoo, it works without any issues. However, when I try to use my own model, it causes problems. It doesn’t take the model locally but instead tries to fetch it from the internet, which is why I couldn’t run my own model. This part needs a solution.

import degirum as dg
import cv2

# Load the segmentation model from the model zoo.
# Replace '<path_to_model_zoo>' with the directory path to your model assets.
model = dg.load_model(
    model_name='yolov8n_coco_seg--1280x1280_quant_hailort_hailo8_1',
    inference_host_address='@local',
    zoo_url='degirum/hailo'
)

# Run inference on an input image.
# Replace '<path_to_input_image>' with the actual path to your image.
inference_result = model('assets/Cat.jpg')

# The segmentation overlay (with masks and labels) is available via the image_overlay attribute.
cv2.imshow("Segmentation Output", inference_result.image_overlay)

# Wait until the user presses 'x' or 'q' to close the window.
while True:
    key = cv2.waitKey(0) & 0xFF
    if key == ord('x') or key == ord('q'):
        break

cv2.destroyAllWindows()

This example works, and I can get the cat output without any issues. The problem occurs when I try to run a model located in my local folder; I encounter an error.

Hi @farukcinemre25
In your code where you specify the path to model zoo url, there is / missing. You need to change

# Load the segmentation model from the model zoo.
# Replace '<path_to_model_zoo>' with the directory path to your model assets.
model = dg.load_model(
    model_name='yolov8n_seg',
    inference_host_address='@local',
    zoo_url='home/pi/hailo_examples/model_zoo'
)

to

# Load the segmentation model from the model zoo.
# Replace '<path_to_model_zoo>' with the directory path to your model assets.
model = dg.load_model(
    model_name='yolov8n_seg',
    inference_host_address='@local',
    zoo_url='/home/pi/hailo_examples/model_zoo'
)

Hi @farukcinemre25,

In your post How to set `zoo_url` for a custom model in DeGirum? - #3 by farukcinemre25 you have the following code:

model = dg.load_model(
    model_name='yolov8n_seg',
    inference_host_address='@local',
    zoo_url='home/pi/hailo_examples/model_zoo'
)

As you can see, zoo_url is specified as zoo_url='home/pi/hailo_examples/model_zoo', without leading slash. Please change it to zoo_url='/home/pi/hailo_examples/model_zoo' – add leading slash.

Hi @farukcinemre25
Is your issue resolved or are you still having problems running the models from a local zoo?