PaddelHub基本使用教程

PaddleHub是飞浆预训练模型应用工具,它解决了人工智能应用中的数据量少引起的模型训练效果差的问题。目前PaddleHub模型库里面已有120多个模型,现有Python代码调用和命令行调用两种使用方法。本文主要用飞浆预训练模型实现图像分类、图像分割、人脸检测、目标检测,并进步一部实现PaddleHub模型在线部署。

环境准备

1.python3.6以上
2.安装padlepaddle1.7.1
   安装链接(https://paddlepaddle.org.cn/install/quick)
3.安装paddlehub
   $: pip install paddlehub -i https://mirrors.aliyun.com/pypi/simple/
4.查看模型库(https://paddlepaddle.org.cn/hublist)

图像分类-imagenet数据集

1、python代码调用
   
    #导入模块
    import paddlehub as hub
    #导入模型,第一次调用会自动下载
    module = hub.Module(name="xception71_imagenet")   
    test_img_path = "images/dog.jpg"
    
    #set input dict  
    input_dict = {"image": [test_img_path]}
	  
    #execute predict and print the result  
    results = module.classification(data=input_dict) 
    for result in results:
	    print(result)
	    
2、命令调用:
    $:hub run xception71_imagenet --input_path test_detection.jpg

图像分割-人像分割

1、python代码调用
    
   import paddlehub as hub
   import cv2
   humanseg = hub.Module(name="deeplabv3p_xception65_humanseg")   
   test_img_path = "images/1.jpg"
   #execute predict and print the result    
   results = humanseg.segmentation(paths=[test_img_path],visualization=True,output_dir="face_detection_result")
2、命令调用:
   $:hub run deeplabv3p_xception65_humanseg --input_path test_detection.jpg

目标检测-口罩检测

1、python代码调用
   import paddlehub as hub
   import cv2
   
   module = hub.Module(name="pyramidbox_lite_server_mask")
   test_img_path = "./images/1.jpg"
   
   #set input dict
   input_dict = {"data": [cv2.imread(test_img_path)]}
   results = module.face_detection(data=input_dict)
   print(results)
   for result in results:
       print(result['data'])

2、命令调用:
   $:hub run pyramidbox_lite_mobile_mask --input_path test_detection.jpg

目标检测-人脸检测

1、python代码调用
  import paddlehub as hub
  
  module = hub.Module(name="pyramidbox_face_detection")
  test_img_path = "images/57.jpg"
  
  #execute predict and print the result
  results = module.face_detection(paths=[test_img_path],visualization=True,output_dir="face_detection_result")

2、命令调用:
  $:hub run pyramidbox_face_detection --input_path test_detection.jpg

Paddlehub模型在线部署

1.启动PaddleHub Serving
  $ hub serving start -m pyramidbox_lite_server_mask
2.client端请求
 import requests
 import json
 import cv2
 import base64
 import os
 from PIL import Image, ImageDraw

 def get_save_image_name(output_dir, image_path):
 """
 Get save image name from source image path.
 """
 if not os.path.exists(output_dir):
    os.makedirs(output_dir)
 image_name = os.path.split(image_path)[-1]
 name, ext = os.path.splitext(image_name)
 return os.path.join(output_dir, "{}".format(name)) + ext

 def draw_bounding_box_on_image(img_path,save_im_path, output_data):
    image = Image.open(img_path)
    draw = ImageDraw.Draw(image)
    for bbox in output_data:
         # draw bouding box
         if bbox['label'] == "MASK":
             draw.line([(bbox['left'], bbox['top']),
                       (bbox['left'], bbox['bottom']),
                       (bbox['right'], bbox['bottom']),
                       (bbox['right'], bbox['top']),
                       (bbox['left'], bbox['top'])],
                       width=2,
                       fill='green')
        else:
             draw.line([(bbox['left'], bbox['top']),
                       (bbox['left'], bbox['bottom']),
                       (bbox['right'], bbox['bottom']),
                       (bbox['right'], bbox['top']),
                       (bbox['left'], bbox['top'])],
                       width=2,
                       fill='red')
       # draw label
       text = bbox['label'] + ": %.2f%%" % (100 * bbox['confidence'])
       textsize_width, textsize_height = draw.textsize(text=text)
       if image.mode == 'RGB' or image.mode == 'RGBA':
            box_fill = (255, 255, 255)
            text_fill = (0, 0, 0)
       else:
            box_fill = (255)
            text_fill = (0)
       draw.rectangle(
            xy=(bbox['left'], bbox['top'] - (textsize_height + 5),
               bbox['left'] + textsize_width + 10, bbox['top'] - 3),
            fill=box_fill)
       draw.text(
            xy=(bbox['left'], bbox['top'] - 15), text=text, fill=text_fill)
    return image

 def cv2_to_base64(image):
    data = cv2.imencode('.jpg', image)[1]
    return base64.b64encode(data.tostring()).decode('utf8')

 #发送HTTP请求
 img_path = "images/2.jpg"
 output_dir = "detection_result/"
 org_im = cv2.imread(img_path)
 data = {'images':[cv2_to_base64(cv2.imread("images/2.jpg"))]}
 headers = {"Content-type": "application/json"}
 url = "http://127.0.0.1:8866/predict/pyramidbox_lite_server_mask"
 r = requests.post(url=url, headers=headers, data=json.dumps(data))

 #打印预测结果
 print(r.json()["results"])
 #后期处理
 save_name = get_save_image_name(output_dir, img_path)
image = draw_bounding_box_on_image(img_path,output_dir, r.json()["results"][0]['data'])
 #保存结果
 image.save(save_name,quality=95)

参考文档

1、https://paddlepaddle.org.cn/install/quick
2、https://paddlepaddle.org.cn/hublist

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值