paddlehub -- 開箱即用的模型庫


PaddleHub

https://github.com/PaddlePaddle/PaddleHub

令人驚嘆的已訓練好的模型工具庫, 基於Paddle。

Awesome pre-trained models toolkit based on PaddlePaddle.(260+ models including Image, Text, Audio and Video with Easy Inference & Serving deployment)

提供豐富、高質量、直接可用的已訓練好的模型

不需要深度學習背景

覆蓋四大類別,圖像、文本、音頻、視頻

開源、免費

Introduction

  • PaddleHub aims to provide developers with rich, high-quality, and directly usable pre-trained models.
  • No need for deep learning background, you can use AI models quickly and enjoy the dividends of the artificial intelligence era.
  • Covers 4 major categories of Image, Text, Audio, and Video, and supports one-click prediction, easy service deployment and transfer learning
  • All models are OPEN SOURCE, FREE to download and use them in offline scenario.

 

特定模型服務於特定場景

https://www.paddlepaddle.org.cn/hub

PaddleHub
便捷地獲取PaddlePaddle生態下的預訓練模型,完成模型的管理和一鍵預測。配合使用Fine-tune API,可以基於大規模預訓練模型快速完成遷移學習,讓預訓練模型能更好地服務於用戶特定場景的應用。
  • 無需數據和訓練,一鍵模型應用
  • 一鍵模型轉服務
  • 易用的遷移學習
  • 豐富的預訓練模型

安裝兩個庫

!pip install --upgrade paddlepaddle -i https://mirror.baidu.com/pypi/simple
!pip install --upgrade paddlehub -i https://mirror.baidu.com/pypi/simple

 

示例

幾行代碼就可使用。

如下是中文分詞工具使用

!pip install --upgrade paddlepaddle -i https://mirror.baidu.com/pypi/simple
!pip install --upgrade paddlehub -i https://mirror.baidu.com/pypi/simple

import paddlehub as hub

lac = hub.Module(name="lac")
test_text = ["今天是個好天氣。"]

results = lac.cut(text=test_text, use_gpu=False, batch_size=1, return_tag=True)
print(results)
#{'word': ['今天', '是', '個', '好天氣', '。'], 'tag': ['TIME', 'v', 'q', 'n', 'w']}

 

模型庫-modelbase

https://www.paddlepaddle.org.cn/modelbase

  • 智能視覺(PaddleCV)
    • 圖像分類
    • 目標檢測
    • 圖像分割
    • 關鍵點檢測
    • 圖像生成
    • 場景文字識別
    • 度量學習
    • 視頻
  • 智能文本處理(PaddleNLP)
    • NLP 基礎技術
    • NLP 核心技術
    • NLP系統應用
  • 智能推薦(PaddleRec)
  • 智能語音(PaddleSpeech)
  • 其他模型
 

 

chinese_ocr_db_crnn_mobile

https://www.paddlepaddle.org.cn/hubdetail?name=chinese_ocr_db_crnn_mobile&en_category=TextRecognition

 

支持中文的OCR模型。

支持三種使用方式

命令行預測

$ hub run chinese_ocr_db_crnn_mobile --input_path "/PATH/TO/IMAGE"

API調用

import paddlehub as hub import cv2 ocr = hub.Module(name="chinese_ocr_db_crnn_mobile") result = ocr.recognize_text(images=[cv2.imread('/PATH/TO/IMAGE')]) # or # result = ocr.recognize_text(paths=['/PATH/TO/IMAGE'])

服務部署

啟動PaddleHub Serving

運行啟動命令:

$ hub serving start -m chinese_ocr_db_crnn_mobile

發送預測請求

配置好服務端,以下數行代碼即可實現發送預測請求,獲取預測結果

import requests import json import cv2 import base64 def cv2_to_base64(image): data = cv2.imencode('.jpg', image)[1] return base64.b64encode(data.tostring()).decode('utf8') # 發送HTTP請求 data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]} headers = {"Content-type": "application/json"} url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile" r = requests.post(url=url, headers=headers, data=json.dumps(data)) # 打印預測結果 print(r.json()["results"])

 

DEMO

https://github.com/fanqingsong/code_snippet/blob/master/machine_learning/paddle/ocr.py

 

從驗證碼中提取數字

 

 

import paddlehub as hub
import cv2

ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
result = ocr.recognize_text(images=[cv2.imread('./test2.png')])

print(result)

 

如下為打印,粗體為提取數字。

 WARNING: Logging before InitGoogleLogging() is written to STDERR
W0310 14:03:05.659210 11502 default_variables.cpp:429] Fail to open /proc/self/io: No such file or directory [2]
/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
[2021-03-10 14:03:14,697] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
W0310 14:03:14.708413 11502 analysis_predictor.cc:1145] Deprecated. Please use CreatePredictor instead.
[2021-03-10 14:03:15,063] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
[{'save_path': '', 'data': [{'text': '6067', 'confidence': 0.8805994987487793, 'text_box_position': [[9, 2], [52, 2], [52, 16], [9, 16]]}]}]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM