Detectron2 tutorial: train a custom object detector

Learn to prepare YOLO annotations, train a RetinaNet R101 alpaca detector in Google Colab, inspect validation loss, and predict on a local CPU.

Player not loading? Watch on YouTube

Uploaded on May 8, 2023, this tutorial covers Detectron2 with a RetinaNet R101 baseline. The supplied source does not specify a Detectron2 release version, so the installation walkthrough reflects the tutorial's dated environment.

Felipe uses an already annotated, single-class alpaca dataset. His scripts expect YOLO-format labels containing a class ID, bounding-box center coordinates, width and height. Both train and val directories contain images and anns folders, with matching image and annotation filenames apart from their extensions. Image annotation itself is covered in an earlier YOLOv8 tutorial.

The code walkthrough explains dataset registration, conversion to absolute XYWH boxes, model configuration and a custom validation-loss class. Although the training script exposes CPU and GPU options, Felipe stops the local run and trains in Google Colab after uploading the data and scripts to Google Drive. His example uses 6,000 iterations and saves checkpoints every 500 steps; he reports roughly two hours for his dataset.

He smooths training and validation losses from metrics.json and selects the checkpoint near 3,000 steps, while noting possible overfitting. Predictions then run on his local CPU with a 0.5 confidence threshold. The example also returns unexpected class IDs. Felipe recommends filtering them, but leaves that check as an exercise rather than implementing it in the demonstrated prediction script.