Roboflow Python Package: Create Versions and Train Models

How to generate dataset versions, export them, and train models as you would in the Roboflow web application.

Written by Mohamed Traore

Last published at: January 5th, 2023

Overview


The Roboflow web application helps you to build better computer vision models, faster.

Available functionality includes: creating projects, dataset upload, labeling workflow management, exporting datasets, model training, model testing, and model deployment. The Roboflow Python package also offers the ability to perform these actions.

This is helpful for those looking to make their Machine Learning Operations (MLOps) pipelines more "code-centric," or to simply interact with Roboflow without having to navigate to the web-based application.

This article is focused on dataset generation, dataset export, and model training with the Roboflow Python package.

Video Overview

A full video tutorial for generation, export and model training.

Create Dataset Versions and Train Models Using the Roboflow Python Package (YouTube)


Generate Versions

project.generate_version(settings={})


https://docs.roboflow.com/python/platform-actions#generate

Example:

generation_settings = {"pre-processing": {
    "auto-orient": True, "resize": {"width": 640, "height": 640, "format": "Stretch To"},
    }, "augmentation": 
    {"flip": {"horizontal": True, "vertical": False}
    }}

project.generate_version(settings=generation_settings)


Export Versions


https://docs.roboflow.com/python/platform-actions#export-and-download


Zip your dataset into your specified export format:

Example:

version.download(model_format="yolov5")


Download your dataset to where your python code is being executed:

Example:

version.download(model_format="yolov5", location="/downloads")


The available export format is determined by your project type (i.e Object Detection, Single-Label Classification, Multi-Label Classification, Instance Segmentation and Semantic Segmentation).


Valid Formats:

Object Detection Download & Export Formats

"object-detection": [
    "clip",
    "coco",
    "createml",
    "darknet",
    "multiclass",
    "tensorflow",
    "tfrecord",
    "voc",
    "yolokeras",
    "yolov4pytorch",
    "yolov4scaled",
    "yolov5-obb",
    "yolov5pytorch",
    "yolov7pytorch",
    "mt-yolov6",
    "retinanet",
    "benchmarker"
 ]

Single-Label Classification Download & Export Formats

"classification": ["folder", "clip"]

Multi-Label Classification Download & Export Formats

"multilabel-classification": ["multiclass", "folder", "clip"]

Instance Segmentation Download & Export Formats

"instance-segmentation": [    
    "coco-segmentation",
    "clip",
    "coco",
    "createml",
    "darknet",
    "multiclass",
    "tensorflow",
    "tfrecord",
    "voc",
    "yolokeras",
    "yolov4pytorch",
    "yolov4scaled",
    "yolov5-obb",
    "yolov5pytorch",
    "yolov7pytorch",
    "mt-yolov6",
    "retinanet",
    "benchmarker"
]

Semantic Segmentation Download & Formats

"semantic-segmentation": ["coco-segmentation", "png-mask-semantic"]


Train Models

Train models at the project-level, and the version-level.


Finding Your Project ID, Model ID, and Version Number:

For my Face Detection dataset, the link available to me and other members of my workspace for accessing the project is: https://app.roboflow.com/mohamed-traore-2ekkp/face-detection-mik1i/

  • NOTE: This is a dataset in a Public (Community) workspace, so it is publicly-viewable on Roboflow Universe. The Roboflow Universe access link is: https://universe.roboflow.com/mohamed-traore-2ekkp/face-detection-mik1i/. Accessing the dataset from this link will only give you the ability to view, clone, or download the images labeled and added to the dataset. You can also deploy the model since there is at least one version trained with Roboflow Train.


Training at the Project-Level

Training at the project-level will result in a new version being generated and trained with Roboflow Train.


Train from Scratch - Object Detection, "Fast" model

##pip install roboflow

from roboflow import Roboflow
rf = Roboflow(api_key="INSERT_YOUR_PRIVATE_API_KEY")
project = rf.workspace("mohamed-traore-2ekkp").project("face-detection-mik1i")

## generate a version with pre-processing and augmentation settings, and train
## it from scratch
generation_settings = {"pre-processing": { "auto-orient": True, "resize": {"width": 640, "height": 640, "format": "Stretch To"}, }, "augmentation": {"flip": {"horizontal": True, "vertical": False}}}

project.train(settings=generation_settings, speed="fast")


Train from a Previous Checkpoint - Object Detection, "Fast" model

##pip install roboflow

from roboflow import Roboflow
rf = Roboflow(api_key="INSERT_YOUR_PRIVATE_API_KEY")
project = rf.workspace("mohamed-traore-2ekkp").project("face-detection-mik1i")

## generate a version with pre-processing and augmentation settings, and train
## it with the "Fast" model, from the version 15 checkpoint
generation_settings = {"pre-processing": { "auto-orient": True, "resize": {"width": 640, "height": 640, "format": "Stretch To"}, }, "augmentation": {"flip": {"horizontal": True, "vertical": False}}}

project.train(settings=generation_settings, speed="fast", checkpoint="mohamed-traore-2ekkp/face-detection-mik1i/15")


Training at the Version-Level

Training at the version-level will result in the specified version being trained with Roboflow Train.


Train from Scratch - Object Detection, "Accurate" model

##pip install roboflow
from roboflow import Roboflow

rf = Roboflow(api_key="INSERT_YOUR_PRIVATE_API_KEY")
project = rf.workspace("mohamed-traore-2ekkp").project("face-detection-mik1i")
version = project.version(16) #this is an untrained project version

## Train the "Fast" model, from the version 15 checkpoint
version.train(speed="fast")


Train from a Previous Checkpoint - Object Detection, "Accurate" model

##pip install roboflow
from roboflow import Roboflow

rf = Roboflow(api_key="INSERT_YOUR_PRIVATE_API_KEY")
project = rf.workspace("mohamed-traore-2ekkp").project("face-detection-mik1i")
version = project.version(16) #this is an untrained project version

##version 15 was previously trained with the "Fast" model
version.train(speed="fast", checkpoint="mohamed-traore-2ekkp/face-detection-mik1i/15")


As a note, you can choose to specify version generation settings, or not. For best practices, specify version generation settings when training from checkpoints, or generating new versions

Next Steps

  1. Monitor Your Training Graph During Training: https://docs.roboflow.com/train/monitoring-your-training-job
  2. Learn how to interpret your training results: Roboflow Train: Understanding Training Graphs‍ 
  3. Test your model with the Deploy Tab
  4. Improve your model with Active Learning: What is Active Learning?