PaddleHub可以便捷地获取PaddlePaddle生态下的预训练模型,完成模型的管理和一键预测。配合使用Fine-tune API,可以基于大规模预训练模型快速完成迁移学习,让预训练模型能更好地服务于用户特定场景的应用。

模型概述:

模型概述

Faster_RCNN是两阶段目标检测器。通过对图像生成候选区域,提取特征,判别特征类别并修正候选框位置。Faster_RCNN整体网络可以分为4个主要内容,一是ResNet-50作为基础卷积层,二是区域生成网络,三是Rol Align,四是检测层。Faster_RCNN是在MS-COCO数据集上预训练的模型。该PaddleHub Module可支持预测。

代码及效果示例:

import paddlehub as hub

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

#faster_rcnn_coco2017

module = hub.Module(name="faster_rcnn_coco2017")

test_img_path = "./body2.jpg"

# 预测结果展示

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

# set input dict

input_dict = {"image": [test_img_path]}

# execute predict and print the result

results = module.object_detection(data=input_dict)

for result in results:

    print(result)

test_img_path = "./output/body2.jpg"

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

[2020-01-08 06:54:40,430] [    INFO] - Installing faster_rcnn_coco2017 module

2020-01-08 06:54:40,430-INFO: Installing faster_rcnn_coco2017 module

[2020-01-08 06:54:40,462] [    INFO] - Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017

2020-01-08 06:54:40,462-INFO: Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017


百度AI攻略:Paddlehub实现目标检测_第1张图片

[2020-01-08 06:54:40,984] [    INFO] - 169 pretrained paramaters loaded by PaddleHub

2020-01-08 06:54:40,984-INFO: 169 pretrained paramaters loaded by PaddleHub

image with bbox drawed saved as /home/aistudio/work/output/body2.jpg

{'path': './body2.jpg', 'data': [{'left': 89.073586, 'right': 331.32816, 'top': 50.749474, 'bottom': 277.31653, 'label': 'person', 'confidence': 0.9966834}]}


百度AI攻略:Paddlehub实现目标检测_第2张图片

In[6]

import paddlehub as hub

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

#faster_rcnn_coco2017

module = hub.Module(name="faster_rcnn_coco2017")

test_img_path = "./body1.jpg"

# 预测结果展示

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

# set input dict

input_dict = {"image": [test_img_path]}

# execute predict and print the result

results = module.object_detection(data=input_dict)

for result in results:

    print(result)

test_img_path = "./output/body1.jpg"

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

[2020-01-08 06:55:16,472] [    INFO] - Installing faster_rcnn_coco2017 module

2020-01-08 06:55:16,472-INFO: Installing faster_rcnn_coco2017 module

[2020-01-08 06:55:16,483] [    INFO] - Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017

2020-01-08 06:55:16,483-INFO: Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017


百度AI攻略:Paddlehub实现目标检测_第3张图片

[2020-01-08 06:55:17,044] [    INFO] - 169 pretrained paramaters loaded by PaddleHub

2020-01-08 06:55:17,044-INFO: 169 pretrained paramaters loaded by PaddleHub

image with bbox drawed saved as /home/aistudio/work/output/body1.jpg

{'path': './body1.jpg', 'data': [{'left': 333.45242, 'right': 802.91956, 'top': 318.4439, 'bottom': 1052.2673, 'label': 'person', 'confidence': 0.7535156}, {'left': 209.79471, 'right': 795.71326, 'top': 319.2016, 'bottom': 1515.5725, 'label': 'person', 'confidence': 0.99797016}, {'left': 723.31616, 'right': 1350.0393, 'top': 217.41296, 'bottom': 1949.7135, 'label': 'person', 'confidence': 0.9976978}, {'left': 264.5524, 'right': 828.4072, 'top': 852.47485, 'bottom': 2025.8547, 'label': 'person', 'confidence': 0.84229875}, {'left': 734.1046, 'right': 1297.6768, 'top': 236.46027, 'bottom': 1131.4011, 'label': 'person', 'confidence': 0.99232954}, {'left': 736.2953, 'right': 1197.4048, 'top': 893.317, 'bottom': 2082.668, 'label': 'person', 'confidence': 0.74164623}, {'left': 1153.4473, 'right': 1451.6208, 'top': 1204.4583, 'bottom': 1735.3435, 'label': 'handbag', 'confidence': 0.6623996}]}


百度AI攻略:Paddlehub实现目标检测_第4张图片

In[7]

import paddlehub as hub

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

#faster_rcnn_coco2017

module = hub.Module(name="faster_rcnn_coco2017")

test_img_path = "./vehicle1.jpg"

# 预测结果展示

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

# set input dict

input_dict = {"image": [test_img_path]}

# execute predict and print the result

results = module.object_detection(data=input_dict)

for result in results:

    print(result)

test_img_path = "./output/vehicle1.jpg"

img = mpimg.imread(test_img_path)

plt.imshow(img)

plt.axis('off')

plt.show()

[2020-01-08 06:55:41,351] [    INFO] - Installing faster_rcnn_coco2017 module

2020-01-08 06:55:41,351-INFO: Installing faster_rcnn_coco2017 module

[2020-01-08 06:55:41,362] [    INFO] - Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017

2020-01-08 06:55:41,362-INFO: Module faster_rcnn_coco2017 already installed in /home/aistudio/.paddlehub/modules/faster_rcnn_coco2017


百度AI攻略:Paddlehub实现目标检测_第5张图片

[2020-01-08 06:55:41,753] [    INFO] - 169 pretrained paramaters loaded by PaddleHub

2020-01-08 06:55:41,753-INFO: 169 pretrained paramaters loaded by PaddleHub

image with bbox drawed saved as /home/aistudio/work/output/vehicle1.jpg

{'path': './vehicle1.jpg', 'data': [{'left': 172.94551, 'right': 587.33716, 'top': 164.10345, 'bottom': 380.49017, 'label': 'car', 'confidence': 0.99788874}]}


百度AI攻略:Paddlehub实现目标检测_第6张图片