Skip to content

lloydaxeph/simple_frcnn_implementation_from_scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple F-RCNN Implementation from Scratch

1.0 About

A simple demonstration on how to implement a Faster Region-Based Convolutional Neural Network (F-RCNN) from scratch using pytorch. This is a direct implementation of the Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks paper.

Basic summary of what the model does:

  1. Generate region proposals for each image.
  2. Calculates the IOU (Intersection Over Union) of each proposed region (PR) vs the ground truth (GT) data.
  3. Performs Transfer learning using the PRs with labels.
  4. Performs classification on the PRs.

1.1 Two Stage Detector Model

To do this, we have to divide the model into 2 stages. The main part which is the Region Proposal Network (RPN). And the other one which is the Classification Module.
Two Stage Detector Model Diagram

2.0 Getting Started

2.1 Installation

Install the required packages

pip3 install -r requirements.txt

2.2 Data Format

Data folder should have directory names images for the image (.jpg/png) files and annotations for the annotations file (.txt).

NOTE: Each for each image file in the images director, there should be a corresponding annotations file with the same name in the annotations directory.

data_folder
│
└───images
│   │   image001.jpg
│   │   image002.jpg
│   │...
│
└───annotations
    │   image001.txt
    │   image002.txt
    │...

Annotations format would be similar to YOLO's where the file should be formatted with one row per object in class x_center y_center width height format. However, the difference here is that the data is not normalized.

0 233.698 634.239 562.796 1023.359
1 311.171 151.679 759.360 1023.359
...

2.3 Create a Custom Dataset

You can create a Custom Dataset using the following code:

train_ds = CustomDataset(data_path=train_data_path, image_size=image_size, normalize=normalize)
test_ds = CustomDataset(data_path=test_data_path, image_size=image_size, normalize=normalize)
val_ds = CustomDataset(data_path=val_data_path, image_size=image_size, normalize=normalize)

2.4 Train a Model

You can create and train a CustomObjectDetector model using the following code:

detector = CustomObjectDetector(train_data=train_ds, 
                                test_data=test_ds, 
                                val_data=test_ds,
                                early_stopping_patience=early_stopping_patience,
                                anc_scales=anc_scales,
                                anc_ratios=anc_ratios)
detector.train(epochs=epochs, batch_size=batch_size, learning_rate=learning_rate)

1.5 Use the Model

You can demonstrate how your trained model works by using the CustomObjectDetector's test_images function.

detector.test_images(num_images=3)

For the complete demonstration, you can follow the sample_implementation.py script above.

About

Simple Implementation of Faster RCNN from scratch using pytorch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages