圖像分類是人工智能領域的一個熱門話題,一樣在生產環境中也會常常會遇到相似的需求,那麼怎麼快速搭建一個圖像分類,或者圖像內容是別的API呢?python
首先,給你們推薦一個圖像相關的庫:ImageAI
git
經過官方給的代碼,咱們能夠看到一個簡單的Demo:github
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咱們能夠考慮將這個模塊部署到雲函數:vim
首先,咱們在本地建立一個Python的項目:windows
mkdir imageDemocentos
而後新建文件:vim index.pyapi
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
來作測試:架構
在官方文檔複製模型文件地址:less
使用wget
直接安裝:
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/squeezenet_weights_tf_dim_ordering_tf_kernels.h5
接下來,咱們就須要進行安裝依賴了,這裏面貌似安裝的內容蠻多的:
並且這些依賴有一些須要編譯的,這就須要咱們在centos + python2.7/3.6的版本下打包才能夠,這樣就顯得很是複雜,尤爲是mac/windows用戶,傷不起。
因此這時候,直接用我以前的打包網址:
直接下載解壓,而後放到本身的項目中:
最後,一步了,咱們建立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
完成以後,執行咱們的sls --debug
部署,部署過程當中會有掃碼的登錄,登錄以後等待便可,完成以後,咱們能夠複製生成的URL:
經過Python語言進行測試,url就是咱們剛纔複製的+/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"))
經過網絡搜索一張圖片,例如我找了這個:
獲得運行結果:
{"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
這個數據,總體性能基本是在我能夠接受的範圍內。
至此,咱們經過Serveerless架構搭建的Python版本的圖像識別/分類小工具作好了。