20行コード:Severlessアーキテクチャの下でPythonで簡単に画像の分類を完成する
7375 ワード
「画像分類」は人工知能分野の話題で、私たちは実際の生活の中で甚だしきに至っては業務の生産環境の中で、よく画像分類の似たような需要に出会って、どのように迅速に1つの画像分類あるいはコンテンツ識別のAPIを構築することができますか?
Severless Frameworkを使用して、テンセントクラウド関数SCFに画像認識モジュールを配備することを検討します.
ここでは画像関連のライブラリを使用します:
次に、プロジェクトの作成→依存のインストール→ymlファイルの構成→配置の4つのステップに分けます.
Pythonプロジェクトのローカル作成
まず、Pythonのプロジェクトをローカルで作成します:mkdir imageDemo`
次にファイルを新規作成します:`vim index.py`
ダウンロードのインストール依存
プロジェクトの作成が完了したら、依存モデルをダウンロードします.
まず最初の
公式ドキュメントでモデルファイルアドレスをコピーするには、次の手順に従います.
次にインストールに依存します.この中にはインストールの内容が多いようです.
ここで注意する必要があるのは、一部の依存はコンパイルする必要があるため、centos+python 2.7/3.6のバージョンでパッケージ化することができます.これは複雑で、特にmac/windowsユーザーには怪我をしません.
この時、私の前のパッケージサイトを直接使うことができます.
ダウンロードして解凍した後、直接自分のプロジェクトに入れます.
ymlファイルの作成
次に、
配置
もしあなたのアカウントがテンセントクラウドにログインしていないか、登録していない場合は、微信スキャンコマンドラインのQRコードを直接通じて、ログインと登録を許可することができます.
コマンドラインから出力されたURLにアクセスします.URLは先ほどコピーした+
たとえば、この図を使用してテストします.
実行結果:
コードを修正して、簡単な時間のテストを行います.
出力結果:
このデータは,全体的な性能がほぼ許容範囲内である.
Severlessアーキテクチャに基づいて構築されたPython画像認識/分類ツールが大成功!
転送ゲート: GitHub: github.com/serverless 公式サイト:serverless.com
ようこそ:Severless中国語ネットワーク、ベストプラクティスでより多くのSeverlessアプリケーションの開発を体験できます!
推荐阅读:《Severlessアーキテクチャ:原理、设计からプロジェクト実戦まで》
Severless Frameworkを使用して、テンセントクラウド関数SCFに画像認識モジュールを配備することを検討します.
ここでは画像関連のライブラリを使用します:
ImageAI
、公式は簡単なdemoをあげました:from imageai.Prediction import ImagePrediction
import os
execution_path = os.getcwd()
prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction + " : " + eachProbability)
次に、プロジェクトの作成→依存のインストール→ymlファイルの構成→配置の4つのステップに分けます.
Pythonプロジェクトのローカル作成
まず、Pythonのプロジェクトをローカルで作成します:mkdir imageDemo`
次にファイルを新規作成します:`vim index.py`
from imageai.Prediction import ImagePrediction
import os, base64, random
execution_path = os.getcwd()
prediction = ImagePrediction()
prediction.setModelTypeAsSqueezeNet()
prediction.setModelPath(os.path.join(execution_path, "squeezenet_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()
def main_handler(event, context):
imgData = base64.b64decode(event["body"])
fileName = '/tmp/' + "".join(random.sample('zyxwvutsrqponmlkjihgfedcba', 5))
with open(fileName, 'wb') as f:
f.write(imgData)
resultData = {}
predictions, probabilities = prediction.predictImage(fileName, result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
resultData[eachPrediction] = eachProbability
return resultData
ダウンロードのインストール依存
プロジェクトの作成が完了したら、依存モデルをダウンロードします.
- SqueezeNet( :4.82 MB, , )
- ResNet50 by Microsoft Research ( :98 MB, , )
- InceptionV3 by Google Brain team ( :91.6 MB, , )
- DenseNet121 by Facebook AI Research ( :31.6 MB, , )
まず最初の
SqueezeNet
でテストします.公式ドキュメントでモデルファイルアドレスをコピーするには、次の手順に従います.
wget
を使用して直接インストールします.wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/squeezenet_weights_tf_dim_ordering_tf_kernels.h5
次にインストールに依存します.この中にはインストールの内容が多いようです.
ここで注意する必要があるのは、一部の依存はコンパイルする必要があるため、centos+python 2.7/3.6のバージョンでパッケージ化することができます.これは複雑で、特にmac/windowsユーザーには怪我をしません.
この時、私の前のパッケージサイトを直接使うことができます.
ダウンロードして解凍した後、直接自分のプロジェクトに入れます.
ymlファイルの作成
次に、
serverless.yaml
プロファイルを作成します.imageDemo:
component: "@serverless/tencent-scf"
inputs:
name: imageDemo
codeUri: ./
handler: index.main_handler
runtime: Python3.6
region: ap-guangzhou
description: / Demo
memorySize: 256
timeout: 10
events:
- apigw:
name: imageDemo_apigw_service
parameters:
protocols:
- http
serviceName: serverless
description: / DemoAPI
environment: release
endpoints:
- path: /image
method: ANY
配置
serverless
コマンド(コマンド略称sls
)を使用して配置し、--debug
パラメータを追加して配置の詳細を表示します.$ sls --debug
もしあなたのアカウントがテンセントクラウドにログインしていないか、登録していない場合は、微信スキャンコマンドラインのQRコードを直接通じて、ログインと登録を許可することができます.
コマンドラインから出力されたURLにアクセスします.URLは先ほどコピーした+
/image
で、Python言語でテストします.import urllib.request
import base64
with open("1.jpg", 'rb') as f:
base64_data = base64.b64encode(f.read())
s = base64_data.decode()
url = 'http://service-9p7hbgvg-1256773370.gz.apigw.tencentcs.com/release/image'
print(urllib.request.urlopen(urllib.request.Request(
url = url,
data=s.encode("utf-8")
)).read().decode("utf-8"))
たとえば、この図を使用してテストします.
実行結果:
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
コードを修正して、簡単な時間のテストを行います.
import urllib.request
import base64, time
for i in range(0,10):
start_time = time.time()
with open("1.jpg", 'rb') as f:
base64_data = base64.b64encode(f.read())
s = base64_data.decode()
url = 'http://service-hh53d8yz-1256773370.bj.apigw.tencentcs.com/release/test'
print(urllib.request.urlopen(urllib.request.Request(
url = url,
data=s.encode("utf-8")
)).read().decode("utf-8"))
print("cost: ", time.time() - start_time)
出力結果:
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 2.1161561012268066
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.1259253025054932
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.3322770595550537
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.3562259674072266
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.0180821418762207
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.4290671348571777
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.5917718410491943
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.1727900505065918
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 2.962592840194702
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
cost: 1.2248001098632812
このデータは,全体的な性能がほぼ許容範囲内である.
Severlessアーキテクチャに基づいて構築されたPython画像認識/分類ツールが大成功!
転送ゲート:
ようこそ:Severless中国語ネットワーク、ベストプラクティスでより多くのSeverlessアプリケーションの開発を体験できます!
推荐阅读:《Severlessアーキテクチャ:原理、设计からプロジェクト実戦まで》