Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Keras RetinaNet

Not Maintained

Open Source Love GitHub Python 3.6 GitHub Stars LinkedIn

What is it

This is the Keras implementation of RetinaNet for 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.

If this repository helps you in anyway, show your love ❤️ by putting a on this project ✌️

Object Detection:

The RetinaNet used is a single, unified network composed of a resnet50 backbone network and two task-specific subnetworks. The backbone is responsible for computing a convolution feature map over an entire input image and is an off-the-self convolution network. The first subnet performs classification on the backbones output; the second subnet performs convolution bounding box regression. The RetinaNet is a good model for object detection but getting it to work was a challenge. I underestimated the high number of classes and the size of the data set but was still able to land a bronze medal (Top 20%) among 450 competitors with some tweaks. The benchmark file is added for reference with the local score for predictions and the parameter used.

Visual Relationship:

I focused on Object detection and used a simple multi class linear regressor for relationship prediction. Unlike the usual approach of using a LSTM, I experimented with a Random Forest Classifier and a Multi Output Classifier from sklearn just to prove LSTM doesn't have much intelligence behind it and it was just a statistical tool. And the local classification scores proved I was right with giving me an accuracy greater than 90%. And since my visual relationship was based on how good my object detector performed I was not able to get a better score but with this model I was able to land a bronze model (Top 30%) among 230 competitors.

Lessons Learned with Tips:
  1. Not to threshold the predictions and leave the low confidence predictions in the submission file. Because of the way average precision works, you cannot be penalised for adding additional false positives with a lower confidence than all your other predictions, however you can still improve your recall if you find additional objects that weren’t previously detected.
  2. The number of steps and epochs, due to the number of images in the train set, having a balanced number of steps and epochs is very important and more important than that is to take all these classes and divide it into bins. Where each bin is occupied by classes with similar frequency in the data set to prepare proper epoch.
  3. When running the training for the classes, to make sure that each class (within an epoch) has similar number of occurrences by implementing a sampler to do this work.

📋 Getting Started

The build was made for the Google AI Object Detection and Visual Relationship Kaggle challenge so if you are using this project on Googles' Open Image data set follow the instructions below to run the module. Also the code is written in such a way that you can take individual modules to build a custom model as per your needs. So when you install the model, make sure you turn the imports into absolute imports or follow the Folder Structure shown below.

📀 Software Prerequisites

  • keras
  • keras-resnet
  • tensorflow
  • pandas
  • numpy
  • pillow
  • opencv
  • sklearn

💻 Hardware Prerequisites

The code was initially run on a NVIDIA GeForce GTX 1050 Ti but the model exploded since for the Open Image data set consisted of 1,743,042 Images and 500 classes with 12,195,144 bounding boxes and the image size was resized to 600 by 600. Resizing the images could have solved the issue but did not try it. Instead the code was run on a NVIDIA Tesla K80 and the model worked fine and to convert the training model to a inference model NVIDIA Tesla P100 was used. So I would recommend a K80 or a higher version of GPU.

📘 Folder Structure

- challenge2018 (The folder containing data files for the challenge)
- images
    - train (consists of the train images)
    - test (consists of the test images)
- keras_retinanet (keras retinanet package)
    - callbacks
    - models
    - preprocessing
    - trainer
    - utils


Run the from the trainer folder.

Usage main_dir(path/to/main directory) dataset_type(oid)


First run the to convert the training model to inference model. Then run the for evaluation. Evaluation is defaulted for both object detection and visual relationship identification, to select between the object detection and the visual relationship identification add 'od' or 'vr' when calling the

Usage main_dir(path/to/main directory) model_in(model name to be used to convert) main_dir(path/to/main directory) model_in(model name to be used for evaluation)

📄 Documentation

  • CALLED: at by the create callbacks function
  • DOES: returns a set of callbacks used for training

  • CALLED: at by the main function
  • DOES: returns a Logistic Regression regressor for visual relationship prediction

  • CALLED: at by the train function
  • DOES: Load the retinanet model using the correct backbone.

  • CALLED: at by the backbone function
  • DOES: Constructs a retinanet model using a resnet backbone.

  • CALLED: at by the resnet_retinanet function
  • DOES: Construct a RetinaNet model on top of a backbone

  • CALLED: at by the OpenImagesGenerator class
  • DOES: creates a train and validation generator for processing

  • CALLED: at by the Generator class
  • DOES: transformations and pre processing on the images

  • CALLED: at by the create_generators function
  • DOES: returns train and validation generators

  • CALLED: stand alone file to convert the train model to inference model
  • DOES: converts a train model to inference model

  • CALLED: stand alone evaluation file
  • DOES: object and visual relationship detection and identification

  • CALLED: at
  • DOES: the training

  • CALLED: stand alone file to be called to start training
  • DOES: initiates the training

  • CALLED: at
  • DOES: Generate anchors for bbox detection

  • CALLED: stand alone file
  • DOES: creates ddata files based on the downloaded train and test images

  • CALLED: at by the create_models function
  • DOES: freeze layers for training

  • CALLED: at
  • DOES: Applies a prior probability to the weights

  • CALLED: at
  • DOES: Keras layer for filtering detections

  • CALLED: at by the create_models function
  • DOES: calculate the focal and smooth_l1 losses

👽 Authors

🔑 License

This project is licensed under the MIT License - see the LICENSE file for details

📢 Acknowledgments

  • Inspiration from Fizyr Keras RetinaNet


Keras implementation of RetinaNet for object detection and visual relationship identification





No releases published


No packages published