20 lines of code: image classification can be easily done in Python under the Serverless architecture

"Image classification" is a hot topic in the field of artificial intelligence. We often meet similar needs of image classification in real life and even in the production environment of business. How can we quickly build an API for image classification or content recognition?

We consider using Serverless Framework The image recognition module is deployed to Tencent cloud function SCF.

Here we will use an image related Library: ImageAI. The official gives a simple 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)

The next four steps are: create project → install dependency → configure yml file → deploy

Create Python project locally

First, we create a Python project locally: mkdir imageDemo`

Then create a new file: ` ` 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

Download installation dependency

After the project is created, 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)

Let's test with the first 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, the installation depends on a lot of content

Note here: some of these dependencies need to be compiled, so they can only be packaged in centos + python2.7/3.6, which is very complex, especially for mac/windows users.

At this time, you can directly use my previous packing website:

After downloading and decompressing, you can directly put it into your own project:

Create yml file

Then create the serverless.yaml configuration file

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

deploy

Use the serverless command (sls can be used for short) to deploy. Add the -- debug parameter to view the deployment details:

$ sls --debug

If your account doesn't Land or register Tencent cloud can directly scan the QR code in the command line through wechat to authorize login and registration.

Access the URL output from the command line. The URL is the + / image we just copied. Test it in 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"))

For example, we use this picture to test:

Get the operation result:

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

Modify the code for a simple time-consuming test:

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)

Output results:

{"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

The overall performance of this data is basically within the acceptable range.

The Python image recognition / classification tool based on the Serverless architecture is finished!

Portal:

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: Front-end Python github vim Google

Posted on Thu, 12 Mar 2020 06:19:36 -0400 by ricta