Note
Click here to download the full example code
03. Predict with pre-trained YOLO models¶
This article shows how to play with pre-trained YOLO 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 YOLOv3 model trained with on Pascal VOC
dataset with Darknet53 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
Model Zoo.
net = model_zoo.get_model('yolo3_darknet53_voc', pretrained=True)
Out:
Downloading /root/.mxnet/models/yolo3_darknet53_voc-f5ece5ce.zip from https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/yolo3_darknet53_voc-f5ece5ce.zip...
0%| | 0/223069 [00:00<?, ?KB/s]
0%| | 102/223069 [00:00<04:38, 801.70KB/s]
0%| | 510/223069 [00:00<01:40, 2207.73KB/s]
1%| | 2186/223069 [00:00<00:30, 7201.25KB/s]
3%|2 | 6478/223069 [00:00<00:18, 11542.03KB/s]
5%|4 | 10811/223069 [00:00<00:11, 17851.78KB/s]
7%|6 | 15152/223069 [00:00<00:10, 20602.78KB/s]
8%|7 | 17315/223069 [00:01<00:10, 18808.40KB/s]
12%|#1 | 25747/223069 [00:01<00:05, 33517.27KB/s]
15%|#4 | 32975/223069 [00:01<00:04, 42930.22KB/s]
19%|#8 | 41469/223069 [00:01<00:03, 53749.08KB/s]
22%|##1 | 48894/223069 [00:01<00:02, 59259.88KB/s]
26%|##5 | 56986/223069 [00:01<00:02, 65015.99KB/s]
29%|##9 | 64881/223069 [00:01<00:02, 68959.12KB/s]
32%|###2 | 72142/223069 [00:01<00:02, 70008.54KB/s]
36%|###5 | 79384/223069 [00:02<00:02, 51897.63KB/s]
38%|###8 | 85416/223069 [00:02<00:02, 47837.93KB/s]
42%|####1 | 92824/223069 [00:02<00:02, 53845.95KB/s]
45%|####5 | 101182/223069 [00:02<00:01, 61173.82KB/s]
49%|####8 | 108306/223069 [00:02<00:01, 63793.18KB/s]
52%|#####1 | 115140/223069 [00:02<00:01, 63470.24KB/s]
55%|#####5 | 123613/223069 [00:02<00:01, 69298.68KB/s]
59%|#####8 | 131062/223069 [00:02<00:01, 69692.98KB/s]
62%|######2 | 139408/223069 [00:02<00:01, 73597.55KB/s]
66%|######5 | 146927/223069 [00:03<00:01, 65994.37KB/s]
69%|######8 | 153775/223069 [00:03<00:01, 52338.29KB/s]
73%|#######2 | 162414/223069 [00:03<00:01, 60219.84KB/s]
76%|#######5 | 169073/223069 [00:03<00:00, 60525.86KB/s]
79%|#######8 | 175576/223069 [00:03<00:00, 60083.12KB/s]
82%|########2 | 183898/223069 [00:03<00:00, 66193.20KB/s]
86%|########5 | 191415/223069 [00:03<00:00, 68650.94KB/s]
89%|########9 | 199476/223069 [00:03<00:00, 72013.45KB/s]
93%|#########2| 207018/223069 [00:03<00:00, 72920.62KB/s]
96%|#########6| 214974/223069 [00:04<00:00, 74720.19KB/s]
100%|#########9| 222546/223069 [00:04<00:00, 70806.86KB/s]
223070KB [00:04, 53088.97KB/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. Once constraint for YOLO is that input height and width can be divided by 32.
You can provide a list of image file names, such as [im_fname1, im_fname2,
...]
to 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.
im_fname = utils.download('https://raw.githubusercontent.com/zhreshold/' +
'mxnet-ssd/master/data/demo/dog.jpg',
path='dog.jpg')
x, img = data.transforms.presets.yolo.load_test(im_fname, short=512)
print('Shape of pre-processed image:', x.shape)
Out:
Downloading dog.jpg from https://raw.githubusercontent.com/zhreshold/mxnet-ssd/master/data/demo/dog.jpg...
0%| | 0/160 [00:00<?, ?KB/s]
100%|##########| 160/160 [00:00<00:00, 27338.93KB/s]
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:
class_IDs, scores, bounding_boxs = net(x)
ax = utils.viz.plot_bbox(img, bounding_boxs[0], scores[0],
class_IDs[0], class_names=net.classes)
plt.show()
Total running time of the script: ( 0 minutes 8.161 seconds)