掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
概述

創(chuàng)新互聯(lián)公司長(zhǎng)期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為額敏企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站建設(shè),額敏網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
在移動(dòng)設(shè)備上使用 TensorFlow Lite 模型運(yùn)行推理不僅僅是與模型交互,還需要額外的代碼來(lái)處理復(fù)雜的邏輯,如數(shù)據(jù)轉(zhuǎn)換、預(yù)處理/后處理、加載關(guān)聯(lián)文件等。
- 額外的代碼
- https://tensorflow.google.cn/lite/guide/lite_support
今天,我們將為大家介紹 TensorFlow Lite Task Library,這是一組功能強(qiáng)大且易于使用的模型接口,可代您處理大多數(shù)預(yù)處理和后處理以及其他復(fù)雜邏輯。Task Library 支持主流的機(jī)器學(xué)習(xí)任務(wù),包括圖像分類與分割、目標(biāo)檢測(cè)和自然語(yǔ)言處理。模型接口針對(duì)每個(gè)任務(wù)進(jìn)行過(guò)專門(mén)設(shè)計(jì),可實(shí)現(xiàn)最佳性能和易用性——現(xiàn)在,只需 5 行代碼就可以在受支持任務(wù)的預(yù)訓(xùn)練和自定義模型上執(zhí)行推理!目前,Task Library 已廣泛用于許多 Google 產(chǎn)品的生產(chǎn)環(huán)境中。
- TensorFlow Lite Task Library
- https://tensorflow.google.cn/lite/inference_with_metadata/task_library/overview
支持的 ML 任務(wù)
TensorFlow Lite Task Library 目前支持六個(gè) ML 任務(wù),包括視覺(jué)和自然語(yǔ)言處理用例。下面將逐一進(jìn)行簡(jiǎn)要介紹。
支持的模型
Task Library 與下列已知的模型源兼容:
Task Library 還支持符合每個(gè) Task API 的模型兼容性要求的自定義模型。關(guān)聯(lián)的文件(即標(biāo)簽圖和 vocab 文件)和處理參數(shù)(如果適用)應(yīng)正確填充到模型元數(shù)據(jù)中。有關(guān)更多詳細(xì)信息,請(qǐng)參見(jiàn) TensorFlow 網(wǎng)站上針對(duì)每個(gè) API 的文檔。
- 模型元數(shù)據(jù)
- https://tensorflow.google.cn/lite/convert/metadata
- TensorFlow 網(wǎng)站上針對(duì)每個(gè) API 的文檔https://tensorflow.google.cn/lite/inference_with_metadata/task_library/overview
使用 Task Library 運(yùn)行推理
Task Library 可跨平臺(tái)工作,并且在 Java、C++(實(shí)驗(yàn)性)和 Swift(實(shí)驗(yàn)性)上均受支持。使用 Task Library 運(yùn)行推理十分簡(jiǎn)單,只需編寫(xiě)幾行代碼。例如,您可以使用 DeepLab v3 TFLite 模型在 Android 中分割飛機(jī)圖像(圖 1),如下所示:
- // Create the API from a model file and options
- String modelPath = "path/to/model.tflite"
- ImageSegmenterOptions options = ImageSegmenterOptions.builder().setOutputType(OutputType.CONFIDENCE_MASK).build();ImageSegmenter imageSegmenter = ImageSegmenter.createFromFileAndOptions(context, modelPath, options);// Segment an imageTensorImage image = TensorImage.fromBitmap(bitmap);List results = imageSegmenter.segment(image);
ImageSegmenter 輸入圖像
分割蒙版
然后,您可以在結(jié)果中使用彩色標(biāo)簽和類別蒙版來(lái)構(gòu)造分割蒙版圖像,如圖 2 所示。
三個(gè)文本 API 均支持 Swift。要在 iOS 中使用 SQuAD v1 TFLite 模型對(duì)給定的上下文和問(wèn)題執(zhí)行問(wèn)答,您可以運(yùn)行:
- let modelPath = "path/to/model.tflite"
- // Create the API from a model file
- let mobileBertAnswerer = TFLBertQuestionAnswerer.mobilebertQuestionAnswerer(modelPath: modelPath)let context = """
- The Amazon rainforest, alternatively, the Amazon Jungle, also known in \
- English as Amazonia, is a moist broadleaf tropical rainforest in the \
- Amazon biome that covers most of the Amazon basin of South America. This \
- basin encompasses 7,000,000 square kilometers(2,700,000 square miles), of \
- which 5,500,000 square kilometers(2,100,000 square miles) are covered by \
- the rainforest. This region includes territory belonging to nine nations.
- """
- let question = "Where is Amazon rainforest?"
- // Answer a questionlet answers = mobileBertAnswerer.answer(context: context, question: question)// answers.[0].text could be “South America.”
- DeepLab v3 TFLite 模型
- https://tfhub.dev/tensorflow/lite-model/deeplabv3/1/metadata/1
- SQuAD v1 TFLite 模型https://tfhub.dev/tensorflow/lite-model/albert_lite_base/squadv1/1
為您的用例構(gòu)建一個(gè) Task API
如果現(xiàn)有 Task 庫(kù)不支持您的用例,則您可以利用 Task API 基礎(chǔ)架構(gòu)并構(gòu)建自定義 C++/Android/iOS 推理 API。有關(guān)更多詳細(xì)信息,請(qǐng)參閱本指南。
- 指南
- https://tensorflow.google.cn/lite/inference_with_metadata/task_library/customized_task_api
未來(lái)工作
我們將繼續(xù)改善 Task Library 的用戶體驗(yàn)。近期的路線圖如下:
反饋
歡迎大家提供反饋,并就 Task Library 中支持的新用例給出建議。請(qǐng)向 [email protected]發(fā)送電子郵件或在 GitHub 中提 issue。
- issue
- https://github.com/tensorflow/tflite-support/issues/new
致謝
這項(xiàng)成果離不開(kāi)以下人員的共同努力:
以及 Tian Lin、Sijia Ma、YoungSeok Yoon、Yuqi Li、Hsiu Wang、Qifei Wang、Alec Go、Christine Kaeser-Chen、Yicheng Fan、Elizabeth Kemp、Willi Gierke、Arun Venkatesan、Amy Jang、Mike Liang、Denis Brulé、Gaurav Nemade、Khanh LeViet、Luiz GUStavo Martins、Shuangfeng Li、Jared Duke、Erik Vee、Sarah Sirajuddin 以及 Tim Davis 都對(duì)本項(xiàng)目給予了大力支持,在此一并表示感謝。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流