圖像分類是人工智能領(lǐng)域的一個熱門話題绳姨,同樣在生產(chǎn)環(huán)境中也會經(jīng)常會遇到類似的需求甥捺,那么怎么快速搭建一個圖像分類兆衅,或者圖像內(nèi)容是別的API呢?
首先风响,給大家推薦一個圖像相關(guān)的庫: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)
通過這個Demo我們可以考慮將這個模塊部署到云函數(shù):
首先,我們在本地創(chuàng)建一個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
創(chuàng)建完成之后状勤,我們需要下載一下我們所依賴的模型:
- SqueezeNet(文件大行场:4.82 MB,預(yù)測時間最短持搜,精準(zhǔn)度適中)
- ResNet50 by Microsoft Research (文件大忻芩啤:98 MB,預(yù)測時間較快朵诫,精準(zhǔn)度高)
- InceptionV3 by Google Brain team (文件大行劣选:91.6 MB薄扁,預(yù)測時間慢剪返,精度更高)
- DenseNet121 by Facebook AI Research (文件大小:31.6 MB邓梅,預(yù)測時間較慢脱盲,精度最高)
我們先用第一個SqueezeNet
來做測試:
在官方文檔復(fù)制模型文件地址:
使用wget
直接安裝:
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/squeezenet_weights_tf_dim_ordering_tf_kernels.h5
接下來,我們就需要進(jìn)行安裝依賴了日缨,這里面貌似安裝的內(nèi)容蠻多的:
而且這些依賴有一些需要編譯的钱反,這就需要我們在centos + python2.7/3.6的版本下打包才可以,這樣就顯得非常復(fù)雜匣距,尤其是mac/windows用戶面哥,傷不起。
所以這時候毅待,直接用我之前的打包網(wǎng)址:
直接下載解壓尚卫,然后放到自己的項目中:
最后,一步了尸红,我們創(chuàng)建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
完成之后吱涉,執(zhí)行我們的sls --debug
部署刹泄,部署過程中會有掃碼的登陸,登陸之后等待即可怎爵,完成之后特石,我們可以復(fù)制生成的URL:
通過Python語言進(jìn)行測試,url就是我們剛才復(fù)制的+/image
:
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"))
通過網(wǎng)絡(luò)搜索一張圖片鳖链,例如我找了這個:
得到運行結(jié)果:
{"cheetah": 83.12643766403198, "Irish_terrier": 2.315458096563816, "lion": 1.8476998433470726, "teddy": 1.6655176877975464, "baboon": 1.5562783926725388}
將代碼修改一下姆蘸,進(jìn)行一下簡單的耗時測試:
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)
輸出結(jié)果:
{"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
這個數(shù)據(jù),整體性能基本是在我可以接受的范圍內(nèi)撒轮。
至此乞旦,我們通過Serveerless架構(gòu)搭建的Python版本的圖像識別/分類小工具做好了。