图像分类是人工智能领域的一个热门话题,一样在生产环境中也会常常会遇到相似的需求,那么怎么快速搭建一个图像分类,或者图像内容是别的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版本的图像识别/分类小工具作好了。