Use Florence-2 to train a YOLOv5 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

Florence-2

to train a

YOLOv5

model.

To label data for a YOLOv5 model with Florence-2, 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!

Install Dependencies

First, install Autodistill and the required model dependencies:

pip install autodistill autodistill-yolov5 autodistill-florence-2 supervision

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:

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

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.

To start labeling your images, run the following code:

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

Train a Model

To train a

YOLOv5

model using your newly-labeled dataset, run the following code:


from autodistill_yolov5 import YOLOv5

target_model = YOLOv5("YOLOv5n.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.

Upload Model to Roboflow (Optional)

You can deploy your trained model to Roboflow. By deploying your model to Roboflow, you can run inference on our infinitely-scalable API. As your inference demands grow, our you will continue to see high levels of performance thanks to autoscaling infrastructure that is always on.

Roboflow offers:

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

To deploy your model to Roboflow, run the following code:


import roboflow

PROJECT_ID = ""
DATASET_VERSION = 1

roboflow.login()

rf = roboflow.Roboflow()

project = rf.workspace().project(PROJECT_ID)
project.version(DATASET_VERSION).deploy(model_type="yolov5", model_path=f"./runs/detect/train/")