This article is Request! Tips for developing on Azure using Python![PR] Microsoft Japan Advent Calendar 2020 This is the article on the 24th day.
Using Flask, Pytorch and Azure, I created a web app ** that transforms photos into Van Gogh's style. I will introduce the procedure from environment construction to publication so that it will be an article that can be referred to when creating a web application using deep learning.
** 1. When you open the website and send the image you want to process, **
↓ Selected image
** 2. An image converted to Van Gogh style will be returned. ** **
** You can access the site you actually created from the link below. Please play with it. ** ** Web application that converts images to Van Gogh style
By the way, here is a portrait of Van Gogh. Somehow the style of painting is similar ...
I tried to summarize the outline of the created web application in the figure. ** This time, I used Azure, a Microsoft cloud service, as a web server within the scope of the free service (¥ 22,500 credit that can be used for 30 days after registration). ** The actual image processing part used CycleGAN implemented in Pytorch, and the appearance of the website was easily created only with HTML. Then, using Flask, which is a Python API, I created a link between Python and HTML.
The actual work and time required to create and publish a web application that implements the CycleGAN model are as follows.
Total: 7 hours
The hardest part was choosing a deep learning model and modifying the code for the web app. Last time had a hard time handling images in the web application, but this time there was no trouble around that, and the deployment work itself was completed in about an hour.
Most machine learning libraries and deep learning models published on Github are developed on Ubuntu First on Linux. Therefore, in order to eliminate the extra work caused by the OS being Windows, we will build a WSL environment.
I think the Microsoft Official WSL Installation Guide is the easiest way to install WSL.
I have selected the following Linux distributions:
OS environment | Linux distribution |
---|---|
Windows10(64-bit) ver. 20H2(OS build 19042.685) | Ubuntu 20.04 LTS |
When the installation is complete, you can start it by typing "wsl" at the command prompt or Windows Terminal.
Check if the target Ubuntu has been installed with the following command.
ubuntu version check
> lsb_release -a
It seems that it was installed normally.
We will install Anaconda to prepare the Python execution environment on WSL. (For Linux and Mac, just run the regular Anaconda installer) Regarding the installation of Anaconda on WSL, I think this article is simple and easy to understand.
By the way, please note that recently, Large-scale commercial use of Anaconda is charged.
After starting WSL, you can check the Python version by entering the following command.
python version check
$ python --version
It was installed successfully.
Choose a machine learning model to implement in your web app. When I was looking for a good GAN model on Github, I found CycleGAN. For those who are interested in theory, the treatise is here. If you use Github code, be careful about the license. The above code was a BSD License, so basically commercial use is OK.
By the way, the repository used this time had the following trained models. ・ Color monochrome images ・ Convert horses to zebras (I often see them) ・ Convert summer landscape to winter landscape
There is also a CycleGAN Demo Site (https://affinelayer.com/pixsrv/). This site will generate an original cat from the outline you drew.
To deploy the selected machine learning model, use Azure App Service on Linux and Flask, the API of Python. Azure App Service on Linux is a service that allows you to deploy web apps on Linux very easily without having to build a troublesome environment.
After registering an Azure account, we will do the Official Tutorial to learn the flow of deployment.
** Hello, Azure! If you can create a site, it's OK for the time being. ** **
Next, modify the CycleGAN code published on Github to the Web application specifications. I think most deep learning model repositories have scripts for making inferences from input data using trained models. (Test.py file in the above repository) By modifying this, it will be used as an image processing function called from the main code of the web application.
The following three points were mainly corrected.
** How to write relative import ** I changed the folder structure and the name scope changed accordingly, so I fixed it. (Modify the module called by test.py)
** Remove and function unnecessary parts of inference code (test.py) ** Removed unnecessary parts such as model visualization code. I made it in the form of a function called from main.py. Along with that, the processing of pyorch Tensor type ⇒ numpy conversion of the data output by inference was added.
** Fix argparse ** argparse is a convenient library that allows you to easily implement the process of receiving arguments from the command line. It seems that it is mostly used in recent deep learning model implementations, but it is unnecessary when using it as a function of a web application, so I fixed it. (Delete unnecessary ones, specify all the rest with Default) You can also specify the CPU here, the model used, and the trained parameters.
I think that other deep learning models can be used if they are written in Pytorch or TensorFlow by modifying similar points.
We will deploy the web application on Azure App Service on Linux. The folder structure is as follows.
My folder (any name is OK) ├ static/ │ └ CycleGAN model ├ templates/ │ └ index.html ├ app.py └ requirements.txt
I will briefly describe each role. -** static : Enter the static data (CycleGAN model this time) that you want to access while the application is running. - templates : Save the html file. - app.py : A python file that describes data processing and cooperation with html. This is the first script to be executed in the app. - requirements.txt **: A file containing the python library and version required to run app.py. The environment is automatically built according to this at the time of deployment.
Now that you've done the tutorial and are ready to deploy your web app, just follow the one line below.
python version check
$ az webapp up --sku F1 --name <app-name>
By the way, you can select the machine level with the --sku argument. I can use F1 for free, but I chose S2 with 3.50 GB of RAM because I have free credit for ¥ 22,500. You can check the selectable options at here.
The deployed code is published in This Github repository. The trained weights could not be uploaded due to file size restrictions. If you want to actually run it, create cyclegan2gogh/static/checkpoints in the above folder and save the learned weights downloaded by here and it will work. Alternatively, you can prepare the training data and deploy the model you trained yourself.
Building a GPU Deep Learning Environment on Ubuntu CUDA on WSL User Guide pytorch-CycleGAN-and-pix2pix Anaconda Terms of Service Quick Start: Create a Python App in Azure App Service on Linux (https://docs.microsoft.com/ja-jp/azure/app-service/quickstart-python?tabs=bash&pivots=python-framework-flask)
Thank you for reading to the end. It was quite a rush, but I managed to complete the process from environment construction to release in one day. If you don't mind, thank you for LGTM!
Recommended Posts