自動定義:Cortex 可以自動定義需要負責生產工作的 API;
多框架支持:Cortex 支持多種機器學習框架,包括 TensorFlow、PyTorch、scikit-learn、XGBoost 等;
CPU/GPU 支持:Cortex 能夠在 CPU 或者 GPU 上進行推理工作;
回滾式更新:Cortex 可以對部署的 API 直接更新;
日誌流:Cortex 會保留部署模型的日誌流,並在 CLI 上顯示;
預測監控:Cortex 能夠監控網絡的評價指標,並追蹤預測結果;
最小配置:部署時,用戶只需要在一個名為 cortex.yaml 的文件中配置相關屬性。
這一項目是怎樣工作的?具體而言,每當用戶運行 cortex deploy 時,命令行將配置屬性和代碼發送到伺服器集群上。每個模型都載入到一個 Docker 容器中,包括相關的 Python 包和處理請求的代碼。模型通過網絡服務,如 Elastic Load Balancing (ELB)、Flask、TensorFlow Serving 和 ONNX Runtime 公開 API 給用戶使用。容器通過 Elastic Kubernetes Service (EKS) 進行控制,而日誌文件和評價指標的記錄和列印工作由 CloudWatch 完成。# predictor.py
model = download_my_model()def predict(sample, metadata): return model.predict(sample["text"])如上所示,用戶需要做的是定義代表這個 API 的函數,使其能夠根據輸入數據返回輸出。# cortex.yaml
- kind: deploymentname: sentiment
- kind: apiname: classifierpredictor: path: predictor.pytracker: model_type: classificationcompute: gpu: 1第二個步驟中,用戶需要創建一個新的 yaml 文件,這個文件用於配置相關屬性。具體而言,用戶可以定義部署模型的名稱,本例中名為 classifierpredictor。然後還需要定義 API 的名稱,如 classifierpredictor 以及路徑、模型的類型和使用的 GPU 數量等。$ cortex deploy
creating classifier (http://***.amazonaws.com/sentiment/classifier)以 AWS 為例,以上工作完成後,用戶可以創建這個 API,使其利用 AWS 進行工作。$ curl http://***.amazonaws.com/sentiment/classifier \-X POST -H "Content-Type: application/json" \-d '{"text": "the movie was great!"}'
positive$ cortex get classifier --watch
status up-to-date available requested last update avg latencylive 1 1 1 8s 123ms
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
首先用戶需要在模型上定義 API 接口函數,使其可以通過函數輸入數據,並返回模型的推理結果。這一 py 文件被定義為 handler.py:# handler.py
import tensorflow as tfimport tensorflow_hub as hubfrom bert import tokenization, run_classifier
labels = ["negative", "positive"]
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)
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, [0, 1], 128, tokenizer) return {"input_ids": [input_feature.input_ids]}
def post_inference(prediction, signature, metadata): return labels[prediction["labels"][0]]接著,用戶需要定義配置 yaml 文件,在文件中指定相關屬性,這裡需要注意,文件名必須定義為 cortex.yaml:# cortex.yaml
- kind: deployment name: sentiment
- 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 文件)。$ cortex deploy
deployment started如果需要獲得監控信息,則需要輸入 cortex get 命令:$ cortex get classifier
status up-to-date available requested last update avg latencylive 1 1 1 8s$ cortex get classifier
url: http:
$ curl http: -X POST -H "Content-Type: application/json" \ -d '{"review": "The movie was great!"}'"positive贈送三本噹噹熱銷書籍
《Halcon機器視覺算法原理與編程實戰》
👆掃描二維碼 2 秒 購買書籍
內容介紹:《Halcon機器視覺算法原理與編程實戰》適合需要全面學習機器視覺算法的初學者,希望掌握Halcon進行機器視覺項目開發的程式設計師,需要了解機器視覺項目開發方法的工業客戶、機器視覺軟體開發項目經理、專業培訓機構的學員,以及對機器視覺算法興趣濃厚的人員閱讀。2、PyTorch中文版官方教程來了,附pdf下載
3、神作《統計學習要素》的中文翻譯、代碼實現及其習題解答,附下載
4、400頁《TensorFlow 2.0 深度學習算法實戰》中文版教材免費下載(附隨書代碼+pdf)
5、PDF&PPT下載 | Github 9.9K Star的《神經網絡與深度學習》
6、300 頁乾貨!李宏毅《一天搞懂深度學習》(附下載)
喜歡文章,點個在看