11. Predict with pre-trained CenterNet models¶
This article shows how to play with pre-trained CenterNet models with only a few lines of code.
First let’s import some necessary libraries:
from gluoncv import model_zoo, data, utils from matplotlib import pyplot as plt
Load a pretrained model¶
Let’s get an CenterNet model trained with on Pascal VOC
dataset with resnet18_v1b as the base model. By specifying
pretrained=True, it will automatically download the model from the model
zoo if necessary. For more pretrained models, please refer to
Downloading /root/.mxnet/models/center_net_resnet18_v1b_voc-38c509d4.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/center_net_resnet18_v1b_voc-38c509d4.zip... 0%| | 0/51562 [00:00<?, ?KB/s] 0%| | 93/51562 [00:00<01:09, 744.06KB/s] 1%|1 | 519/51562 [00:00<00:22, 2301.36KB/s] 4%|4 | 2176/51562 [00:00<00:06, 7264.63KB/s] 15%|#5 | 7837/51562 [00:00<00:01, 24116.50KB/s] 27%|##6 | 13835/51562 [00:00<00:01, 35875.51KB/s] 42%|####1 | 21468/51562 [00:00<00:00, 46525.92KB/s] 57%|#####7 | 29439/51562 [00:00<00:00, 56578.32KB/s] 69%|######9 | 35824/51562 [00:00<00:00, 56337.89KB/s] 83%|########3 | 42892/51562 [00:01<00:00, 60533.71KB/s] 95%|#########5| 49062/51562 [00:01<00:00, 55453.78KB/s] 51563KB [00:01, 43114.92KB/s]
Pre-process an image¶
Next we download an image, and pre-process with preset data transforms. Here we specify that we resize the short edge of the image to 512 px. You can feed an arbitrarily sized image, however, models may perform better with certain input resolutions since they are trained with 512x512 images.
You can provide a list of image file names, such as
gluoncv.data.transforms.presets.yolo.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.
Shape of pre-processed image: (1, 3, 512, 683)
Inference and display¶
The forward function will return all detected bounding boxes, and the corresponding predicted class IDs and confidence scores. Their shapes 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:
Total running time of the script: ( 0 minutes 2.862 seconds)