02. Predict with pre-trained Faster RCNN models

This article shows how to play with pre-trained Faster RCNN model.

First let’s import some necessary libraries:

from matplotlib import pyplot as plt
import gluoncv
from gluoncv import model_zoo, data, utils

Load a pretrained model

Let’s get an Faster RCNN model trained on Pascal VOC dataset with ResNet-50 backbone. By specifying pretrained=True, it will automatically download the model from the model zoo if necessary. For more pretrained models, please refer to Model Zoo.

The returned model is a HybridBlock gluoncv.model_zoo.FasterRCNN with a default context of cpu(0).

net = model_zoo.get_model('faster_rcnn_resnet50_v1b_voc', pretrained=True)

Out:

Downloading /root/.mxnet/models/faster_rcnn_resnet50_v1b_voc-447328d8.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/faster_rcnn_resnet50_v1b_voc-447328d8.zip...

  0%|          | 0/121887 [00:00<?, ?KB/s]
  0%|          | 104/121887 [00:00<03:06, 652.25KB/s]
  0%|          | 512/121887 [00:00<01:08, 1780.14KB/s]
  2%|1         | 2201/121887 [00:00<00:20, 5784.64KB/s]
  5%|4         | 6058/121887 [00:00<00:07, 15284.25KB/s]
 10%|#         | 12565/121887 [00:00<00:03, 29858.45KB/s]
 15%|#4        | 17954/121887 [00:00<00:02, 34978.97KB/s]
 20%|##        | 24406/121887 [00:00<00:02, 43400.21KB/s]
 25%|##5       | 30841/121887 [00:00<00:01, 49463.13KB/s]
 31%|###       | 37297/121887 [00:01<00:01, 50923.27KB/s]
 36%|###6      | 43970/121887 [00:01<00:01, 55397.97KB/s]
 41%|####      | 49944/121887 [00:01<00:01, 56647.34KB/s]
 47%|####7     | 57558/121887 [00:01<00:01, 62328.59KB/s]
 52%|#####2    | 63911/121887 [00:01<00:00, 59519.80KB/s]
 58%|#####7    | 70469/121887 [00:01<00:00, 61245.07KB/s]
 63%|######2   | 76679/121887 [00:01<00:00, 57373.32KB/s]
 69%|######8   | 83508/121887 [00:01<00:00, 60411.45KB/s]
 74%|#######3  | 89969/121887 [00:01<00:00, 61600.26KB/s]
 79%|#######9  | 96408/121887 [00:02<00:00, 60035.11KB/s]
 85%|########4 | 103104/121887 [00:02<00:00, 62002.00KB/s]
 90%|########9 | 109598/121887 [00:02<00:00, 62677.86KB/s]
 95%|#########5| 116222/121887 [00:02<00:00, 62336.36KB/s]
121888KB [00:02, 49136.42KB/s]

Pre-process an image

Next we download an image, and pre-process with preset data transforms. The default behavior is to resize the short edge of the image to 600px. But you can feed an arbitrarily sized image.

You can provide a list of image file names, such as [im_fname1, im_fname2, ...] to gluoncv.data.transforms.presets.rcnn.load_test() if you want to load multiple image together.

This function returns two results. The first is a NDArray with shape (batch_size, RGB_channels, height, width). It can be fed into the model directly. The second one contains the images in numpy format to easy to be plotted. Since we only loaded a single image, the first dimension of x is 1.

Please beware that orig_img is resized to short edge 600px.

im_fname = utils.download('https://github.com/dmlc/web-data/blob/master/' +
                          'gluoncv/detection/biking.jpg?raw=true',
                          path='biking.jpg')
x, orig_img = data.transforms.presets.rcnn.load_test(im_fname)

Out:

Downloading biking.jpg from https://github.com/dmlc/web-data/blob/master/gluoncv/detection/biking.jpg?raw=true...

  0%|          | 0/244 [00:00<?, ?KB/s]
100%|##########| 244/244 [00:00<00:00, 30734.88KB/s]

Inference and display

The Faster RCNN model returns predicted class IDs, confidence scores, bounding boxes coordinates. Their shape are (batch_size, num_bboxes, 1), (batch_size, num_bboxes, 1) and (batch_size, num_bboxes, 4), respectively.

We can use gluoncv.utils.viz.plot_bbox() to visualize the results. We slice the results for the first image and feed them into plot_bbox:

demo faster rcnn

Total running time of the script: ( 0 minutes 6.422 seconds)

Gallery generated by Sphinx-Gallery