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:
- Generate region proposals for each image.
- Calculates the IOU (Intersection Over Union) of each proposed region (PR) vs the ground truth (GT) data.
- Performs Transfer learning using the PRs with labels.
- Performs classification on the PRs.
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.
Install the required packages
pip3 install -r requirements.txt
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
...
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)
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)
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.