Skip to content
Pytorch implementation of RetinaNet object detection.
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
images some screenshots Jun 12, 2018
lib major development Jun 12, 2018
LICENSE Update (#25) Sep 11, 2018 major development Jun 12, 2018 Multi gpu (#6) Jul 13, 2018 Multi gpu (#6) Jul 13, 2018 Multi gpu (#6) Jul 13, 2018 Multi gpu (#6) Jul 13, 2018 Multi gpu (#6) Jul 13, 2018 major development Jun 12, 2018 command line configuration Jun 14, 2018


img3 img5

Pytorch implementation of RetinaNet object detection as described in Focal Loss for Dense Object Detection by Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He and Piotr Dollár.

This implementation is primarily designed to be easy to read and simple to modify.


Currently, this repo achieves 33.7% mAP at 600px resolution with a Resnet-50 backbone. The published result is 34.0% mAP. The difference is likely due to the use of Adam optimizer instead of SGD with weight decay.


  1. Clone this repo

  2. Install the required packages:

apt-get install tk-dev python-tk
  1. Install the python packages:
pip install cffi

pip install pandas

pip install pycocotools

pip install cython

pip install opencv-python

pip install requests

  1. Build the NMS extension.
cd pytorch-retinanet/lib
cd ../

Note that you may have to edit line 14 of if you want to change which version of python you are building the extension for.


The network can be trained using the script. Currently, two dataloaders are available: COCO and CSV. For training on coco, use

python --dataset coco --coco_path ../coco --depth 50

For training using a custom dataset, with annotations in CSV format (see below), use

python --dataset csv --csv_train <path/to/train_annots.csv>  --csv_classes <path/to/train/class_list.csv>  --csv_val <path/to/val_annots.csv>

Note that the --csv_val argument is optional, in which case no validation will be performed.

Pre-trained model

A pre-trained model is available at:

The state dict model can be loaded using:

retinanet = model.resnet50(num_classes=dataset_train.num_classes(),)

The pytorch model can be loaded directly using:

retinanet = torch.load(PATH_TO_MODEL)


To visualize the network detection, use

python --dataset coco --coco_path ../coco --model <path/to/>

This will visualize bounding boxes on the validation set. To visualise with a CSV dataset, use:

python --dataset csv --csv_classes <path/to/train/class_list.csv>  --csv_val <path/to/val_annots.csv> --model <path/to/>


The retinanet model uses a resnet backbone. You can set the depth of the resnet model using the --depth argument. Depth must be one of 18, 34, 50, 101 or 152. Note that deeper models are more accurate but are slower and use more memory.

CSV datasets

The CSVGenerator provides an easy way to define your own datasets. It uses two CSV files: one file containing annotations and one file containing a class name to ID mapping.

Annotations format

The CSV file with annotations should contain one annotation per line. Images with multiple bounding boxes should use one row per bounding box. Note that indexing for pixel values starts at 0. The expected format of each line is:


Some images may not contain any labeled objects. To add these images to the dataset as negative examples, add an annotation where x1, y1, x2, y2 and class_name are all empty:


A full example:


This defines a dataset with 3 images. img_001.jpg contains a cow. img_002.jpg contains a cat and a bird. img_003.jpg contains no interesting objects/animals.

Class mapping format

The class name to ID mapping file should contain one mapping per line. Each line should use the following format:


Indexing for classes starts at 0. Do not include a background class as it is implicit.

For example:




img1 img2 img4 img6 img7 img8

You can’t perform that action at this time.