Skip to main content

Pretrained YOLO inference

YoloWorkflow deploys a pretrained Ultralytics YOLO model as an HTTP API. A volume is not required when you use pretrained weights.

Create a Python file in your own project:

import base64
from pathlib import Path

from cgc.sdk import Image
from cgc.sdk.recipes.yolo import YoloConfig, YoloWorkflow


workflow = YoloWorkflow(
name="yolo-inference",
image=Image.from_registry("ultralytics/ultralytics:latest"),
config=YoloConfig(
model_name="yolo11n.pt",
inference_cpu=4,
inference_memory=16,
inference_gpu=1,
inference_gpu_type="nvidia-rtx-a5000",
),
)

endpoint = workflow.deploy_pretrained_inference(
wait=True,
reuse_existing=True,
)
endpoint.wait_http("/health", timeout_seconds=300)

prediction = endpoint.post_json(
"/predict",
{"image_url": "https://ultralytics.com/images/bus.jpg"},
timeout=120,
)

for detection in prediction["detections"]:
print(detection["class_name"], detection["confidence"])

image_bytes = base64.b64decode(prediction["annotated_image_base64"])
Path("yolo_prediction.jpg").write_bytes(image_bytes)

Configuration​

ArgumentMeaning
model_namePretrained Ultralytics weights, such as yolo11n.pt.
inference_cpuCPU cores reserved for the endpoint.
inference_memoryMemory in GB.
inference_gpuGPU count. Use 0 for CPU inference.
inference_gpu_typeCGC GPU identifier. Use an empty string when inference_gpu=0.

For CPU inference:

config=YoloConfig(
model_name="yolo11n.pt",
inference_cpu=4,
inference_memory=16,
inference_gpu=0,
inference_gpu_type="",
)

CPU inference is usually slower but avoids requesting a GPU.

The /predict endpoint accepts either:

{"image_url": "https://example.com/image.jpg"}

or:

{"image_base64": "..."}

Delete the endpoint when it is no longer needed:

endpoint.delete()

See the complete yolo_inference.py script on the Workflow examples page.

note

reuse_existing=True does not update the model, image or environment of an existing resource. The example leaves the service running until explicit deletion.