首页 > 编程语言 >Yolov8-源码解析-十一-

Yolov8-源码解析-十一-

时间:2024-09-05 12:07:19浏览次数:4  
标签:hub HUB Ultralytics Yolov8 源码 model 解析 your page

Yolov8 源码解析(十一)


comments: true
description: Learn how to run inference using the Ultralytics HUB Inference API. Includes examples in Python and cURL for quick integration.
keywords: Ultralytics, HUB, Inference API, Python, cURL, REST API, YOLO, image processing, machine learning, AI integration

Ultralytics HUB Inference API

The Ultralytics HUB Inference API allows you to run inference through our REST API without the need to install and set up the Ultralytics YOLO environment locally.

Ultralytics HUB screenshot of the Deploy tab inside the Model page with an arrow pointing to the Ultralytics Inference API card


Watch: Ultralytics HUB Inference API Walkthrough

Python

To access the Ultralytics HUB Inference API using Python, use the following code:

import requests

# API URL, use actual MODEL_ID
url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

# Headers, use actual API_KEY
headers = {"x-api-key": "API_KEY"}

# Inference arguments (optional)
data = {"size": 640, "confidence": 0.25, "iou": 0.45}

# Load image and send request
with open("path/to/image.jpg", "rb") as image_file:
    files = {"image": image_file}
    response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

!!! note "Note"

Replace `MODEL_ID` with the desired model ID, `API_KEY` with your actual API key, and `path/to/image.jpg` with the path to the image you want to run inference on.

cURL

To access the Ultralytics HUB Inference API using cURL, use the following code:

curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
	-H "x-api-key: API_KEY" \
	-F "image=@/path/to/image.jpg" \
	-F "size=640" \
	-F "confidence=0.25" \
	-F "iou=0.45"

!!! note "Note"

Replace `MODEL_ID` with the desired model ID, `API_KEY` with your actual API key, and `path/to/image.jpg` with the path to the image you want to run inference on.

Arguments

See the table below for a full list of available inference arguments.

Argument Default Type Description
image image Image file to be used for inference.
url str URL of the image if not passing a file.
size 640 int Size of the input image, valid range is 32 - 1280 pixels.
confidence 0.25 float Confidence threshold for predictions, valid range 0.01 - 1.0.
iou 0.45 float Intersection over Union (IoU) threshold, valid range 0.0 - 0.95.

Response

The Ultralytics HUB Inference API returns a JSON response.

Classification

!!! Example "Classification Model"

=== "`ultralytics`"

    ```py
    from ultralytics import YOLO

    # Load model
    model = YOLO("yolov8n-cls.pt")

    # Run inference
    results = model("image.jpg")

    # Print image.jpg results in JSON format
    print(results[0].tojson())
    ```

=== "cURL"

    ```py
    curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
        -H "x-api-key: API_KEY" \
        -F "image=@/path/to/image.jpg" \
        -F "size=640" \
        -F "confidence=0.25" \
        -F "iou=0.45"
    ```

=== "Python"

    ```py
    import requests

    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}

    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}

    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)

    print(response.json())
    ```

=== "Response"

    ```py
    {
      success: true,
      message: "Inference complete.",
      data: [
        {
          class: 0,
          name: "person",
          confidence: 0.92
        }
      ]
    }
    ```

Detection

!!! Example "Detection Model"

=== "`ultralytics`"

    ```py
    from ultralytics import YOLO

    # Load model
    model = YOLO("yolov8n.pt")

    # Run inference
    results = model("image.jpg")

    # Print image.jpg results in JSON format
    print(results[0].tojson())
    ```

=== "cURL"

    ```py
    curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
        -H "x-api-key: API_KEY" \
        -F "image=@/path/to/image.jpg" \
        -F "size=640" \
        -F "confidence=0.25" \
        -F "iou=0.45"
    ```

=== "Python"

    ```py
    import requests

    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}

    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}

    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)

    print(response.json())
    ```

=== "Response"

    ```py
    {
      success: true,
      message: "Inference complete.",
      data: [
        {
          class: 0,
          name: "person",
          confidence: 0.92,
          width: 0.4893378019332886,
          height: 0.7437513470649719,
          xcenter: 0.4434437155723572,
          ycenter: 0.5198975801467896
        }
      ]
    }
    ```

OBB

!!! Example "OBB Model"

=== "`ultralytics`"

    ```py
    from ultralytics import YOLO

    # Load model
    model = YOLO("yolov8n-obb.pt")

    # Run inference
    results = model("image.jpg")

    # Print image.jpg results in JSON format
    print(results[0].tojson())
    ```

=== "cURL"

    ```py
    curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
        -H "x-api-key: API_KEY" \
        -F "image=@/path/to/image.jpg" \
        -F "size=640" \
        -F "confidence=0.25" \
        -F "iou=0.45"
    ```

=== "Python"

    ```py
    import requests

    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}

    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}

    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)

    print(response.json())
    ```

=== "Response"

    ```py
    {
      success: true,
      message: "Inference complete.",
      data: [
        {
          class: 0,
          name: "person",
          confidence: 0.92,
          obb: [
            0.669310450553894,
            0.6247171759605408,
            0.9847468137741089,
            ...
          ]
        }
      ]
    }
    ```

Segmentation

!!! Example "Segmentation Model"

=== "`ultralytics`"

    ```py
    from ultralytics import YOLO

    # Load model
    model = YOLO("yolov8n-seg.pt")

    # Run inference
    results = model("image.jpg")

    # Print image.jpg results in JSON format
    print(results[0].tojson())
    ```

=== "cURL"

    ```py
    curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
        -H "x-api-key: API_KEY" \
        -F "image=@/path/to/image.jpg" \
        -F "size=640" \
        -F "confidence=0.25" \
        -F "iou=0.45"
    ```

=== "Python"

    ```py
    import requests

    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}

    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}

    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)

    print(response.json())
    ```

=== "Response"

    ```py
    {
      success: true,
      message: "Inference complete.",
      data: [
        {
          class: 0,
          name: "person",
          confidence: 0.92,
          segment: [0.44140625, 0.15625, 0.439453125, ...]
        }
      ]
    }
    ```

Pose

!!! Example "Pose Model"

=== "`ultralytics`"

    ```py
    from ultralytics import YOLO

    # Load model
    model = YOLO("yolov8n-pose.pt")

    # Run inference
    results = model("image.jpg")

    # Print image.jpg results in JSON format
    print(results[0].tojson())
    ```

=== "cURL"

    ```py
    curl -X POST "https://api.ultralytics.com/v1/predict/MODEL_ID" \
        -H "x-api-key: API_KEY" \
        -F "image=@/path/to/image.jpg" \
        -F "size=640" \
        -F "confidence=0.25" \
        -F "iou=0.45"
    ```

=== "Python"

    ```py
    import requests

    # API URL, use actual MODEL_ID
    url = "https://api.ultralytics.com/v1/predict/MODEL_ID"

    # Headers, use actual API_KEY
    headers = {"x-api-key": "API_KEY"}

    # Inference arguments (optional)
    data = {"size": 640, "confidence": 0.25, "iou": 0.45}

    # Load image and send request
    with open("path/to/image.jpg", "rb") as image_file:
        files = {"image": image_file}
        response = requests.post(url, headers=headers, files=files, data=data)

    print(response.json())
    ```

=== "Response"

    ```py
    {
      success: true,
      message: "Inference complete.",
      data: [
        {
          class: 0,
          name: "person",
          confidence: 0.92,
          keypoints: [
            0.5290805697441101,
            0.20698919892311096,
            1.0,
            0.5263055562973022,
            0.19584226608276367,
            1.0,
            0.5094948410987854,
            0.19120082259178162,
            1.0,
            ...
          ]
        }
      ]
    }
    ```

comments: true
description: Explore seamless integrations between Ultralytics HUB and platforms like Roboflow. Learn how to import datasets, train models, and more.
keywords: Ultralytics HUB, Roboflow integration, dataset import, model training, AI, machine learning

Ultralytics HUB Integrations

Learn about Ultralytics HUB integrations with various platforms and formats.

Datasets

Seamlessly import your datasets in Ultralytics HUB for model training.

After a dataset is imported in Ultralytics HUB, you can train a model on your dataset just like you would using the Ultralytics HUB datasets.

Roboflow

You can easily filter the Roboflow datasets on the Ultralytics HUB Datasets page.

Ultralytics HUB screenshot of the Datasets page with Roboflow provider filter

Ultralytics HUB supports two types of integrations with Roboflow, Universe and Workspace.

Universe

The Roboflow Universe integration allows you to import one dataset at a time into Ultralytics HUB from Roboflow.

Import

When you export a Roboflow dataset, select the Ultralytics HUB format. This action will redirect you to Ultralytics HUB and trigger the Dataset Import dialog.

You can import your Roboflow dataset by clicking on the Import button.

Ultralytics HUB screenshot of the Dataset Import dialog with an arrow pointing to the Import button

Next, train a model on your dataset.

Ultralytics HUB screenshot of the Dataset page of a Roboflow Universe dataset with an arrow pointing to the Train Model button

Remove

Navigate to the Dataset page of the Roboflow dataset you want to remove, open the dataset actions dropdown and click on the Remove option.

Ultralytics HUB screenshot of the Dataset page of a Roboflow Universe dataset with an arrow pointing to the Remove option

??? tip "Tip"

You can remove an imported [Roboflow](https://roboflow.com/?ref=ultralytics) dataset directly from the [Datasets](https://hub.ultralytics.com/datasets) page.

![Ultralytics HUB screenshot of the Datasets page with an arrow pointing to the Remove option of one of the Roboflow Universe datasets](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/integrations/hub_roboflow_remove_1.jpg)

Workspace

The Roboflow Workspace integration allows you to import an entire Roboflow Workspace at once into Ultralytics HUB.

Import

Navigate to the Integrations page by clicking on the Integrations button in the sidebar.

Type your Roboflow Workspace private API key and click on the Add button.

??? tip "Tip"

You can click on the **Get my API key** button which will redirect you to the settings of your [Roboflow](https://roboflow.com/?ref=ultralytics) Workspace from where you can obtain your private API key.

Ultralytics HUB screenshot of the Integrations page with an arrow pointing to the Integrations button in the sidebar and one to the Add button

This will connect your Ultralytics HUB account with your Roboflow Workspace and make your Roboflow datasets available in Ultralytics HUB.

Ultralytics HUB screenshot of the Integrations page with an arrow pointing to one of the connected workspaces

Next, train a model on your dataset.

Ultralytics HUB screenshot of the Dataset page of a Roboflow Workspace dataset with an arrow pointing to the Train Model button

Remove

Navigate to the Integrations page by clicking on the Integrations button in the sidebar and click on the Unlink button of the Roboflow Workspace you want to remove.

Ultralytics HUB screenshot of the Integrations page  with an arrow pointing to the Integrations button in the sidebar and one to the Unlink button of one of the connected workspaces

??? tip "Tip"

You can remove a connected [Roboflow](https://roboflow.com/?ref=ultralytics) Workspace directly from the Dataset page of one of the datasets from your [Roboflow](https://roboflow.com/?ref=ultralytics) Workspace.

![Ultralytics HUB screenshot of the Dataset page of a Roboflow Workspace dataset with an arrow pointing to the remove option](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/integrations/hub_roboflow_workspace_remove_2.jpg)

??? tip "Tip"

You can remove a connected [Roboflow](https://roboflow.com/?ref=ultralytics) Workspace directly from the [Datasets](https://hub.ultralytics.com/datasets) page.

![Ultralytics HUB screenshot of the Datasets page with an arrow pointing to the Remove option of one of the Roboflow Workspace datasets](https://raw.githubusercontent.com/ultralytics/assets/main/docs/hub/integrations/hub_roboflow_remove_1.jpg)

Models

Exports

After you train a model, you can export it to 13 different formats, including ONNX, OpenVINO, CoreML, TensorFlow, Paddle and many others.

Ultralytics HUB screenshot of the Deploy tab inside the Model page with an arrow pointing to the Export card and all formats exported

The available export formats are presented in the table below.

Format format Argument Model Metadata Arguments
PyTorch - yolov8n.pt -
TorchScript torchscript yolov8n.torchscript imgsz, optimize, batch
ONNX onnx yolov8n.onnx imgsz, half, dynamic, simplify, opset, batch
OpenVINO openvino yolov8n_openvino_model/ imgsz, half, int8, batch
TensorRT engine yolov8n.engine imgsz, half, dynamic, simplify, workspace, int8, batch
CoreML coreml yolov8n.mlpackage imgsz, half, int8, nms, batch
TF SavedModel saved_model yolov8n_saved_model/ imgsz, keras, int8, batch
TF GraphDef pb yolov8n.pb imgsz, batch
TF Lite tflite yolov8n.tflite imgsz, half, int8, batch
TF Edge TPU edgetpu yolov8n_edgetpu.tflite imgsz, batch
TF.js tfjs yolov8n_web_model/ imgsz, half, int8, batch
PaddlePaddle paddle yolov8n_paddle_model/ imgsz, batch
NCNN ncnn yolov8n_ncnn_model/ imgsz, half, batch

Exciting New Features on the Way

标签:hub,HUB,Ultralytics,Yolov8,源码,model,解析,your,page
From: https://www.cnblogs.com/apachecn/p/18398140

相关文章

  • Yolov8-源码解析-十四-
    Yolov8源码解析(十四)comments:truedescription:LearnhowtointegrateYOLOv8withTensorBoardforreal-timevisualinsightsintoyourmodel'strainingmetrics,performancegraphs,anddebuggingworkflows.keywords:YOLOv8,TensorBoard,modeltraining,......
  • Yolov8-源码解析-十三-
    Yolov8源码解析(十三)comments:truedescription:DiveintoourguideonYOLOv8'sintegrationwithKaggle.FindoutwhatKaggleis,itskeyfeatures,andhowtotrainaYOLOv8modelusingtheintegration.keywords:WhatisKaggle,WhatisKaggleUsedFor,......
  • Yolov8-源码解析-十七-
    Yolov8源码解析(十七)comments:truedescription:HarnessthepowerofUltralyticsYOLOv8forreal-time,high-speedinferenceonvariousdatasources.Learnaboutpredictmode,keyfeatures,andpracticalapplications.keywords:Ultralytics,YOLOv8,modelpred......
  • Yolov8-源码解析-四十一-
    Yolov8源码解析(四十一).\yolov8\ultralytics\utils\callbacks\raytune.py#UltralyticsYOLO......
  • Yolov8-源码解析-四十四-
    Yolov8源码解析(四十四).\yolov8\ultralytics\utils\triton.py#UltralyticsYOLO......
  • Yolov8-源码解析-四十三-
    Yolov8源码解析(四十三).\yolov8\ultralytics\utils\patches.py#UltralyticsYOLO......
  • Yolov8-源码解析-四十二-
    Yolov8源码解析(四十二).\yolov8\ultralytics\utils\loss.py#导入PyTorch库中需要的模块importtorchimporttorch.nnasnnimporttorch.nn.functionalasF#从Ultralytics工具包中导入一些特定的功能fromultralytics.utils.metricsimportOKS_SIGMAfromultralytics......
  • Yolov8-源码解析-一-
    Yolov8源码解析(一)comments:truedescription:LearnhowtocontributetoUltralyticsYOLOopen-sourcerepositories.Followguidelinesforpullrequests,codeofconduct,andbugreporting.keywords:Ultralytics,YOLO,open-source,contribution,pullrequest......
  • Yolov8-源码解析-三十六-
    Yolov8源码解析(三十六).\yolov8\ultralytics\models\yolo\pose\__init__.py#导入模块predict中的PosePredictor类#导入模块train中的PoseTrainer类#导入模块val中的PoseValidator类from.predictimportPosePredictorfrom.trainimportPoseTrainerfrom.......
  • Yolov8-源码解析-六-
    Yolov8源码解析(六)comments:truedescription:MasterhyperparametertuningforUltralyticsYOLOtooptimizemodelperformancewithourcomprehensiveguide.Elevateyourmachinelearningmodelstoday!.keywords:UltralyticsYOLO,hyperparametertuning,machi......