Serverless practice: easily classify and predict images with 20 lines of Python code

Image classification is a hot topic in the field of artificial intelligence. Generally speaking, it is to distinguish the objects of different categories according to the different characteristics reflected in the image information. Image classification uses computer to analyze image quantitatively, and classifies each pixel or region of image into one of several categories, instead of human visual interpretation.

In real life, we will also encounter application scenarios of image classification, for example, we often recognize flower information by taking photos of flowers, and match the person information by face, etc. Generally, image recognition or classification tools collect data on the client side and calculate the results on the server side. Therefore, there are usually special API s for image recognition, and cloud manufacturers will provide similar capabilities for a fee:

  • Huawei cloud image label

  • Tencent cloud image analysis

Through an interesting Python library, this paper will try to quickly build the image classification function on the cloud function, and combine with the API gateway to provide the API function externally, so as to realize a "image classification API" of Serverless architecture.

Getting started with ImageAI

First, we need a dependency Library: ImageAI.

What is ImageAI? Its official documents are described as follows:

ImageAI is a python library designed to enable developers to use simple lines of code to build applications and systems with deep learning and computer vision capabilities. Based on the principle of simplicity, ImageAI supports the most advanced machine learning algorithms for image prediction, custom image prediction, object detection, video detection, video object tracking and image prediction training. ImageAI currently supports four different machine learning algorithms trained on ImageNet-1000 datasets for image prediction and training. ImageAI also supports object detection, video detection, and object tracking using RetinaNet trained on COCO datasets. Finally, ImageAI will provide more extensive and specialized support for computer vision, including but not limited to image recognition in special environments and special fields.

The simple understanding is that the ImageAI dependency library can help users complete basic image recognition and video target extraction. However, although ImageAI provides some data sets and models, we can also conduct additional training and customized expansion according to our own needs.

Its official code gives a simple Demo like this:

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)

We can run it locally. When picture 1.jpg is specified as the following figure:

The results are as follows:

convertible  :  52.459537982940674
sports_car  :  37.61286735534668
pickup  :  3.175118938088417
car_wheel  :  1.8175017088651657
minivan  :  1.7487028613686562

Let ImageAI go to the cloud (deploy to Serverless Architecture)

Through the above Demo, we can consider deploying this module to cloud functions:

  • First, create a Python project locally: mkdir imageDemo
  • New file: vim index.py
  • According to some special forms of cloud functions, we partially modified Demo
    • Put the initialization code in the outer layer;
    • Take the prediction part as the part that needs to be executed to trigger, and put it in the entry method (here is the main? Handler);
    • The combination of cloud function and API gateway is not very friendly to binary file support, so picture transmission is carried out through base64 here;
    • The input parameter is set as {"picture": base64} of picture, and the output parameter is set as: {"prediction": result of picture classification}

The implementation code is as follows:

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
 

After creation, download the dependent model:

  • SqueezeNet (file size: 4.82 MB, shortest prediction time, moderate accuracy)
  • ResNet50 by Microsoft Research (file size: 98 MB, fast prediction time, high accuracy)
  • Perception V3 by Google brain team (file size: 91.6 MB, slow prediction time, higher accuracy)
  • DenseNet121 by Facebook AI Research (file size: 31.6 MB, slow prediction time, highest accuracy)

Because we only use it for testing, we can choose a smaller model: SqueezeNet:

Copy the model file address in the official document:

Install directly using wget:

wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/squeezenet_weights_tf_dim_ordering_tf_kernels.h5

Next, perform a dependency installation:

Because Tencent cloud service product does not support online installation dependency in Python Runtime, you need to manually package dependency and upload it. In Python's various dependency libraries, there are many dependencies that may have the process of compiling and generating binary files, which will lead to the failure of general packaging dependencies in different environments.

Therefore, the best way is to package through the corresponding operating system + language version. We pack dependency in CentOS + Python 3.6.

For many Mac OS users and Windows users, this is really not a very friendly process, so in order to facilitate your use, I have made an online packaging dependency tool on the Serverless architecture, so you can directly use this tool to package:

After generating the compressed package, download and decompress it directly and put it into your own project:

Finally, create serverless.yaml

imageDemo:
  component: "@serverless/tencent-scf"
  inputs:
    name: imageDemo
    codeUri: ./
    handler: index.main_handler
    runtime: Python3.6
    region: ap-guangzhou
    description: image recognition / classification Demo
    memorySize: 256
    timeout: 10
    events:
      - apigw:
          name: imageDemo_apigw_service
          parameters:
            protocols:
              - http
            serviceName: serverless
            description: image recognition / classification DemoAPI
            environment: release
            endpoints:
              - path: /image
                method: ANY

After completion, execute sls --debug deployment. During the deployment process, there will be code scanning login. After login, wait. After completion, you can see the deployment address.

Basic test

Through Python language testing, the interface address is the + / image just copied, for example:

import json
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= json.dumps({'picture': s}).encode("utf-8")
)).read().decode("utf-8"))

Search the web for a picture:

Get the operation result:

{
	"prediction": {
		"cheetah": 83.12643766403198,
		"Irish_terrier": 2.315458096563816,
		"lion": 1.8476998433470726,
		"teddy": 1.6655176877975464,
		"baboon": 1.5562783926725388
	}
}

Through this result, we can see that the basic classification / prediction of pictures has been successful. In order to prove the delay of this interface, the program can be basically modified:

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-9p7hbgvg-1256773370.gz.apigw.tencentcs.com/release/image'
    
    print(urllib.request.urlopen(urllib.request.Request(
        url = url,
        data= json.dumps({'picture': s}).encode("utf-8")
    )).read().decode("utf-8"))
 
    print("cost: ", time.time() - start_time)

Output results:

{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  2.1161561012268066
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.1259253025054932
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.3322770595550537
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.3562259674072266
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.0180821418762207
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.4290671348571777
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.5917718410491943
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.1727900505065918
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  2.962592840194702
{"prediction":{"cheetah":83.12643766403198,"Irish_terrier":2.315458096563816,"lion":1.8476998433470726,"teddy":1.6655176877975464,"baboon":1.5562783926725388}}
cost:  1.2248001098632812

From the above data, we can see that the overall time consumption is basically controlled between 1-1.5 seconds.

Of course, if you want to test the interface performance more, for example, you can see the performance of the interface in the case of concurrency through concurrency testing.

So far, we have finished the image recognition / classification gadget of Python version built by serverless architecture.

summary

There are many applications related to artificial intelligence under the Serverless architecture. This paper implements an image classification / prediction interface through an existing dependency library. imageAI is a relatively high degree of freedom dependency library, which can be used to customize its own model according to its own needs. This article is a good introduction, expecting more people to deploy their "artificial intelligence" API through the Serverless architecture.

Serverless Framework trial plan

We invite you to experience the most convenient way to develop and deploy Serverless. During the trial period, the related products and services provide free resources and professional technical support to help your business quickly and conveniently realize Serverless!

One More Thing

What can you do in three seconds? Take a sip, read an email, or - deploy a complete Serverless Application?

Copy link to PC browser: https://serverless.cloud.tencent.com/deploy/express

Deploy in 3 seconds and experience the fastest Serverless HTTP Practical development!

Transfer gate:

Welcome to: Serverless Chinese network , you can Best practices Experience more about Serverless application development!

Recommended reading: Serverless architecture: from principle, design to project implementation

Tags: Python github JSON vim

Posted on Fri, 08 May 2020 04:21:25 -0400 by jcampbell1