Visual Intelligence Summit: Oct 22 in San Francisco Get your ticket

Use YOLO-World to train a YOLO-NAS Object Detection model

Before you can train a computer vision model, you need labeled data on which to train your model. The more accurate the labels, or annotations, are, the higher the performance the model will achieve.

Overview

Autodistill allows you to use state-of-the-art foundation models that know a lot about a variety of objects to label data for your project. You can then train a new model with your labeled data. This whole process uses around a dozen lines of code.

To learn more about how autodistill works, read our overview guide or watch our YouTube tutorial.

In this guide, we will show you how to use YOLO-World to train a YOLO-NAS model.

To label data for a YOLO-NAS model with YOLO-World, you will:

  1. Install Autodistill
  2. Create a dataset
  3. Build a prompt to label images in the dataset
  4. Label our data on Autodistill
  5. Train a new model
  6. (Optional) Train a model or export your data

Let's get started!

Step 1: Install Dependencies

First, install Autodistill and the required model dependencies:

Shell
pip install autodistill autodistill-yolonas autodistill-yolo-world supervision

Step 2: Create a Dataset

Before you can label a dataset, you need a dataset with which to work.

Roboflow has a few resources that can help you create a dataset for your project:

  • Roboflow Collect: Collect images in the background for use in your project.
  • Roboflow Universe: Collect images from datasets made by the Roboflow community.

You can use any folder of images you have on your local machine with Autodistill, too.

Step 3: Label Images

Autodistill has two model types:

  1. A Base Model, which automatically labels your data, and;
  2. A Target Model, which trains on your labeled data.

To label your dataset with a Base Model, you need to provide prompt(s) that are relevant to the classes you want to label.

Replace "example" below with the prompt you want to use. Replace "class" with the name of the class you want the prompt results to be saved as in your dataset. Also, replace the IMAGE_NAME with an image from your dataset.

The code cell below loads the base model with your prompt on the provided image, then visualizes the results.

You may need to experiment with a few prompts.

Python
from autodistill_yolo_world import YOLOWorldModel
from autodistill.detection import CaptionOntology
from autodistill.utils.plot import plot

# define an ontology to map class names to our YOLO-World prompt
# the ontology dictionary has the format {caption: class}
# where caption is the prompt sent to the base model, and class is the label that will
# be saved for that caption in the generated annotations
# then, load the model
base_model = YOLOWorldModel(
    ontology=CaptionOntology(
        {
            "person": "person",
            "a forklift": "forklift"
        }
    )
)

# predict on an image
result = base_model.predict("image.jpeg", confidence=0.1)

plot(
    image=cv2.imread("./image.jpeg"),
    classes=base_model.ontology.classes(),
    detections=result
)

# label a folder of images
base_model.label("./context_images", extension=".jpeg")

To start labeling your images, run the following code:

base_model.label(input_folder="./images", output_folder="./dataset")

Step 4: Train a Model

To train a YOLO-NAS model using your newly-labeled dataset, run the following code:

Python
from autodistill_yolonas import YOLONAS

target_model = YOLONAS("yolo_nas_s.pt")

# train a model
target_model.train("./context_images_labeled", epochs=20)

# run inference on the new model
pred = target_model.predict("./context_images_labeled/train/images/image.jpg", confidence=0.5)

After running this cell, you will have model weights that you can use to run inference on your new model.

Step 5: Upload Model to Roboflow (Optional)

Fully managed, infinitely-scalable APIs for deployed models

SDKs for common deployment targets (NVIDIA Jetson, Luxonis OAK, Docker, and more)

SOC II Type 1 Compliant

Trusted by 250,000+ developers

Start where you are

Speak with an AI expert

Our team will help you start solving business problems on the first call.

  • Solution architecting
  • Live demonstration
  • Pricing and specifications
  • Feasibility assessment

Over 16,000 organizations build with Roboflow.

Rivian
Pella
Chobani
USG Corporation
BNSF Railway
American Woodmark
Outcomes
Patrick Industries
Peer Robotics