Problems and solutions in machine learning of function computing deployment

Time:2019-10-20

Abstract:With the popularity of serverless, it has become an inevitable trend to migrate applications to the cloud. Let’s take a look today at how to migrate machine learning applications to function computing. 1. For local development, let’s take a look at the steps of local development of machine learning application. We can roughly summarize the local development into three steps: code writing, installation dependency, and running debugging.

With the popularity of serverless, it has become an inevitable trend to migrate applications to the cloud.

Let’s take a look today at how to migrate machine learning applications to function computing.

  1. Local development

First, let’s take a look at the steps of developing machine learning applications locally. We can roughly summarize the local development into three steps: code writing, installation dependency, and running debugging. Let’s take a look at each.

1.1 coding

Suppose our project structure is:

Problems and solutions in machine learning of function computing deployment

Among them, index.py stores the machine learning related code, model_data stores the data model, and PIC stores the pictures to be tested.

The content of index.py is (the code refers to this article):

Problems and solutions in machine learning of function computing deployment

1.2 installation dependency

Before running an application, you need to install the application dependent modules, which mainly rely on OpenCV and tensorflow. The installation method is simple:

Problems and solutions in machine learning of function computing deployment

After executing these two commands, opencv Python and tensorflow are installed in the system directory. Under Linux, the default is / usr / local / lib / pythonx.y/site-packages.

1.3 operation

At runtime, python will automatically find and load relevant modules under the configured path.

Problems and solutions in machine learning of function computing deployment

After these three steps, we have completed the development of local machine learning application. Next, we will see how to migrate the application to function calculation.

  1. Migration function calculation

2.1 comparison between local development and function calculation development

First of all, we need to do some preparatory work. Let’s think about the difference between the development mode of function computing application and that of local application.

  1. Code entry. In local development, the code can omit the main function or provide the main function as the program entry, but in function calculation, the function entry is fixed, and the function entry of non HTTP trigger must be a function with two parameters, such as def handler (event, context).
  2. Module dependency. When developing locally, the modules that the project depends on are usually installed in a directory of the system. For example, PIP install tensorflow executed above. For function computing, in order to optimize the application to the maximum extent, the operating space open to users is usually relatively small. Therefore, for function calculation, the installation project cannot rely on the running environment at present. We can only package custom modules together. Reference resources.
  3. Function. When developing locally, you need to use Python commands or IDE to run the code. In function calculation, we need to deploy the application to function calculation first, and then trigger the execution manually through trigger or console.

Next, we change the code according to the different development methods of these three points.

2.2 modification code

2.2.1 code entry modification

This is relatively simple, just

Problems and solutions in machine learning of function computing deployment

Modified to

Problems and solutions in machine learning of function computing deployment

And delete the following code:

Problems and solutions in machine learning of function computing deployment

2.2.2. Module dependency

This one is a little more complicated. Different languages have different processing logic because of different module loading mechanisms. For example, for Java, whether you use Maven or gradle, you can easily package code and dependencies into jar with one key. Unfortunately, python does not have such a mechanism at present.

Let’s first make a simple classification of Python dependent modules according to the scenario.

Application dependency: for the module installed with PIP in this example, such as PIP install tensorflow, we call it application dependency for the moment.

System dependency: in some scenarios, the library installed by Python is only the encapsulation of the underlying C and C + + library calls. For example, when using ZBar, in addition to PIP install ZBar, install the corresponding library in the system: apt get install – y libzbar dev. We call the libraries that need to be installed using the system package manager, like libzbar dev, system dependency.

Resource dependency: for some applications, such as machine learning, the data model needs to be loaded after startup. The data model needs to be ready when the program starts. We call this dependency resource dependency for now. Resource dependency is very special. It is required by our application logic and usually has a large volume.

For application dependency, we can change the installation location of PIP through its – t parameter, such as PIP install – t $(PWD) tensorflow. And the loading behavior can be changed through sys.path, so that the module can be loaded from the specified directory.

For system dependency, we can download DEB package through apt get, and then use DEB package to install to the specified directory.

Problems and solutions in machine learning of function computing deployment

For the link library included in the system dependency, its loading behavior can be changed through the LD? Library? Path variable.

For resource dependency, because control is in our code, we only need to change the processing logic of the code.

According to the above description, we can make the following table:

Problems and solutions in machine learning of function computing deployment

2.2.3 download dependent logic

For our demo application, there are two kinds of dependencies, one is application dependency, the other is resource dependency. What we need to deal with in particular is application dependency. We need to create a directory named applib in the project directory, and download the application depends on that directory. It should be noted that if the referenced module uses the executable or library file compiled by C / C + + / go, it is recommended to use the Sbox of fcli for downloading.

Problems and solutions in machine learning of function computing deployment

After execution, you will find that applib contains the application dependencies required by the project.

2.2.4 package dependency upload to OSS

The application and resource dependence of machine learning are usually large, which can easily exceed the limit of function calculation on code package (50m). To avoid this problem, we need to upload these dependencies to OSS.

Problems and solutions in machine learning of function computing deployment

After execution, the project will generate an additional compressed package named applib.zip, which can be uploaded to OSS.

Similarly, do the same for resource dependency:

Problems and solutions in machine learning of function computing deployment

2.2.5 initialization dependency

Here we provide a template code, which is responsible for downloading resources from OSS to local, decompressing and configuring the corresponding environment variables when the function is started for the first time. We can create a file named loader.py in the project. The content is:

Here we provide a template code, which is responsible for downloading resources from OSS to local, decompressing and configuring the corresponding environment variables when the function is started for the first time. We can create a file named loader.py in the project. The content is:

Problems and solutions in machine learning of function computing deployment

This code will first read the applibobject environment variable to download application dependencies from OSS, and extract it to the directory represented by the environment variable applibdir.

Next, the modelobject environment variable will be read to download resource dependencies from OSS and extract to the directory represented by the environment variable modeldir.

Finally, when the dependency is ready, the handler function in index.py is called.

In theory, this code can be used in any other scenario that needs to download application dependency and resource dependency. What we need to modify in index.py is to change the fixed path that the original model depends on to use modeldir to get the path.

2.3 local operation and commissioning

After the code is written, our directory structure is adjusted to:

Problems and solutions in machine learning of function computing deployment

Let’s run it locally to see the effect. Here we use the FC dcoker tool introduced by function calculation.

In order to avoid unnecessary Download for each local debugging, we take off the trick, mount the application dependency and resource dependency to FC docker, and turn on the local ID:

Problems and solutions in machine learning of function computing deployment

2.4 deployment

The local development is completed. Next, we need to deploy the application to the online. Here we use the function calculation to deduce the fun tool.

The procedure for using the fun tool is as follows:

  1. Go to the binary version of the platform corresponding to the release page, and unzip it to use. Or you can use NPM install @ alicloud / fun – G directly.
  2. Use fun config to configure AK, region and other information.
  3. Write template.yml
  4. Fun deploy deployment

Yes, there is no need to log in to the console for tedious configuration, just provide a template.yml under the project:

Problems and solutions in machine learning of function computing deployment

So far, we have another template.yml in our project with the structure of

Problems and solutions in machine learning of function computing deployment

Through this template.yml, after performing fun deploy, you can create the corresponding services and functions, and configure the environment variables of the functions.

Problems and solutions in machine learning of function computing deployment

Even if the code is modified, just execute the fun deploy repeatedly.

Next, open the https://fc.console.aliyun.com/ console, find the created services and functions in turn, and click execute to get the output consistent with the local output:

Problems and solutions in machine learning of function computing deployment

2.5 supplement

In the above example, we only list application dependency and resource dependency. The processing logic of system dependency is relatively simple. For example, let’s take ZBar as an example. In addition to installing ZBar through PIP in applib, you need to create a new lib directory under the code directory and execute in this directory through sandbox:

Problems and solutions in machine learning of function computing deployment

After execution, the directory structure changes to:

Problems and solutions in machine learning of function computing deployment

As mentioned above, we need to modify LD “library” path to change the loading behavior of system dependency, so we need to add a line under environment variables in template.yaml:

Problems and solutions in machine learning of function computing deployment

At this point, you can directly use ZBar in index.py and other files.

  1. summary

As a result, we finally applied machine learning to function calculation. Reviewing all the above operations, we can see that most of the transformation work can be solved by tools. Both template.yml and loader.py can be used directly. The only real need for developers to operate is to download and modify the reference path of resource dependency.

Author: tanhe123

Read the original text

This is the original content of yunqi community, which can not be reproduced without permission.