自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

模型秒變API只需一行代碼,支持TensorFlow等框架

新聞 機器學習
近日,GitHub 上有了這樣一個項目,能夠讓用戶一行代碼將任意模型打包為 API。這一工具無疑能夠幫助開發(fā)者在實際的生產應用中快速部署模型。

[[283641]]

還在為機器學習模型打包成 API 發(fā)愁?這個工具能讓你一行代碼直接打包。

專注于機器學習應用的人們知道,從訓練好的模型到實際的工業(yè)生產工具還有一定的距離。其中工作量很大的地方在于將模型打包,預留 API 接口,并和現有的生產系統(tǒng)相結合。近日,GitHub 上有了這樣一個項目,能夠讓用戶一行代碼將任意模型打包為 API。這一工具無疑能夠幫助開發(fā)者在實際的生產應用中快速部署模型。

項目地址:https://github.com/cortexlabs/cortex

項目特點和原理

該項目名為 Cortex,是一個命令行工具。作者表示,該項目具有以下優(yōu)點:

  • 自動定義:Cortex 可以自動定義需要負責生產工作的 API;
  • 多框架支持:Cortex 支持多種機器學習框架,包括 TensorFlow、PyTorch、scikit-learn、XGBoost 等;
  • CPU/GPU 支持:Cortex 能夠在 CPU 或者 GPU 上進行推理工作;
  • 回滾式更新:Cortex 可以對部署的 API 直接更新;
  • 日志流:Cortex 會保留部署模型的日志流,并在 CLI 上顯示;
  • 預測監(jiān)控:Cortex 能夠監(jiān)控網絡的評價指標,并追蹤預測結果;
  • 最小配置:部署時,用戶只需要在一個名為 cortex.yaml 的文件中配置相關屬性。
模型秒變API只需一行代碼,支持TensorFlow等框架

這一項目是怎樣工作的?具體而言,每當用戶運行 cortex deploy 時,命令行將配置屬性和代碼發(fā)送到服務器集群上。每個模型都載入到一個 Docker 容器中,包括相關的 Python 包和處理請求的代碼。模型通過網絡服務,如 Elastic Load Balancing (ELB)、Flask、TensorFlow Serving 和 ONNX Runtime 公開 API 給用戶使用。容器通過 Elastic Kubernetes Service (EKS) 進行控制,而日志文件和評價指標的記錄和打印工作由 CloudWatch 完成。

使用方法

使用過程主要分為以下三個步驟:

定義模型的 API

  1. # predictor.py 
  2. model = download_my_model()def predict(sample, metadata): return model.predict(sample["text"]) 

如上所示,用戶需要做的是定義代表這個 API 的函數,使其能夠根據輸入數據返回輸出。

配置部署

  1. $ curl http://***.amazonaws.com/sentiment/classifier \-X POST -H "Content-Type: application/json" \-d '{"text": "the movie was great!"}' 
  2. positive 

第二個步驟中,用戶需要創(chuàng)建一個新的 yaml 文件,這個文件用于配置相關屬性。

具體而言,用戶可以定義部署模型的名稱,本例中名為 classifierpredictor。然后還需要定義 API 的名稱,如 classifierpredictor 以及路徑、模型的類型和使用的 GPU 數量等。

AWS 部署

  1. $ cortex deploy 
  2. creating classifier (http://***.amazonaws.com/sentiment/classifier) 

以 AWS 為例,以上工作完成后,用戶可以創(chuàng)建這個 API,使其利用 AWS 進行工作。

當然,用戶還可以實時保存推斷結果,如下所示:

  1. $ curl http://***.amazonaws.com/sentiment/classifier \-X POST -H "Content-Type: application/json" \-d '{"text": "the movie was great!"}' 
  2. positive 

此外,用戶還可以監(jiān)控運行結果。

  1. $ cortex get classifier --watch 
  2. status up-to-date available requested last update avg latencylive 1 1 1 8s 123ms 
  3. class countpositive 8negative 4 

使用教程

為了讓用戶更好地使用這一工具,項目作者同時還提供了一些使用案例。包括:

  • 基于 TensorFlow 和 BERT 進行情感分析:https://github.com/cortexlabs/cortex/tree/0.10/examples/tensorflow/sentiment-analysis
  • 基于 TensorFlow 和 Inception 模型進行圖像分類:https://github.com/cortexlabs/cortex/tree/0.10/examples/tensorflow/image-classifier
  • 基于 PyTorch 和 DistilGPT2 進行文本生成:https://github.com/cortexlabs/cortex/tree/0.10/examples/pytorch/text-generator
  • 基于 XGBoost / ONNX 進行虹膜分類:https://github.com/cortexlabs/cortex/tree/0.10/examples/xgboost/iris-classifier

以使用 BERT 進行情感分析為例:

首先用戶需要在模型上定義 API 接口函數,使其可以通過函數輸入數據,并返回模型的推理結果。這一 py 文件被定義為 handler.py:

  1. # handler.py 
  2. import tensorflow as tfimport tensorflow_hub as hubfrom bert import tokenization, run_classifier 
  3. labels = ["negative""positive"
  4. with tf.Graph().as_default(): bert_module = hub.Module("https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1") info = bert_module(signature="tokenization_info", as_dict=True)with tf.Session() as sess: vocab_file, do_lower_case = sess.run([info["vocab_file"], info["do_lower_case"]]) tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case) 
  5.  
  6. def pre_inference(sample, signature, metadata): input_example = run_classifier.InputExample(guid="", text_a=sample["review"], label=0) input_feature = run_classifier.convert_single_example(0, input_example, [01], 128, tokenizer) return {"input_ids": [input_feature.input_ids]} 
  7.  
  8. def post_inference(prediction, signature, metadata): return labels[prediction["labels"][0]] 

接著,用戶需要定義配置 yaml 文件,在文件中指定相關屬性,這里需要注意,文件名必須定義為 cortex.yaml:

  1. # cortex.yaml 
  2. - kind: deployment name: sentiment 
  3. - kind: api name: classifier tensorflow: model: s3://cortex-examples/tensorflow/sentiment-analysis/bert request_handler: handler.py tracker: model_type: classification 

從中可以看到,yaml 文件中需要指定出使用的模型,以及作為 API 接口的 py 文件(即第一步中定義了的 py 文件)。

然后進行部署即可:

  1. $ cortex deploy 
  2. deployment started 

如果需要獲得監(jiān)控信息,則需要輸入 cortex get 命令:

  1. $ cortex get classifier --watch 
  2. status up-to-date available requested last update avg latencylive 1 1 1 8s 

還可以用命令行獲得實時預測:

  1. $ cortex get classifier 
  2. url: http://***.amazonaws.com/sentiment/classifier 
  3. $ curl http://***.amazonaws.com/sentiment/classifier \ -X POST -H "Content-Type: application/json" \ -d '{"review": "The movie was great!"}'"positive 

 

責任編輯:張燕妮 來源: 機器之心
相關推薦

2020-02-19 15:02:23

代碼開發(fā)工具

2018-03-08 11:43:18

PandasTB級數據Spark

2021-04-19 10:38:06

代碼開發(fā)工具

2022-05-13 09:36:06

Python水印命令

2021-04-22 11:27:24

Python命令水印

2016-12-02 08:53:18

Python一行代碼

2023-11-10 09:41:44

Python代碼

2021-08-23 17:49:02

代碼開發(fā)模型

2014-02-12 13:43:50

代碼并行任務

2022-04-09 09:11:33

Python

2017-04-05 11:10:23

Javascript代碼前端

2021-05-11 20:46:17

Python代碼分類

2025-02-14 08:05:15

2021-11-02 16:25:41

Python代碼技巧

2020-08-19 10:30:25

代碼Python多線程

2020-09-09 16:00:22

Linux進程

2021-08-31 09:49:37

CPU執(zhí)行語言

2017-04-13 19:20:18

Python代碼并行任務

2023-09-12 10:10:57

開發(fā)者工具開源

2023-10-10 08:28:56

JavaAPI分析引擎
點贊
收藏

51CTO技術棧公眾號