10 Minutes to Deploying a Deep Learning Model on Google Cloud Platform

栏目: IT技术 · 发布时间: 3年前

内容简介:So you’ve trained a Machine Learning model that you’re ecstatic about, and now, you want to share it with the world. And so you built a web app to serve the model, only to find out that you don’t know how to host this web app on the Internet 24/7.Requireme

10 Minutes to Deploying a Deep Learning Model on Google Cloud Platform

How to deploy a Deep Learning model to GCP, entirely for free, forever

deploy a dandelion and grass image classifier onto the web, through Google Cloud Platform! Source: Pixabay

So you’ve trained a Machine Learning model that you’re ecstatic about, and now, you want to share it with the world. And so you built a web app to serve the model, only to find out that you don’t know how to host this web app on the Internet 24/7. After all, if no one can see your ML model in production, does it really exist? This tutorial came out of the need to share an easy and free way to deploy a deep learning model to production on Google Cloud Platform using its always-free compute service , the f1-micro. As an example, we’ll be deploying a dandelion and grass classifier built using the FastAI deep learning library . I hope that you will be able to deploy your trained model to the world in under 30 minutes, seamlessly and effortlessly.

Requirements: You will need just the computer you have now, and a Google account!

This tutorial will be broken down into 4 steps:

  1. Sign in to Google Cloud and create an f1-micro instance on Compute Engine
  2. Pull the trained model from Github
  3. Add swap memory
  4. Serve model onto the web with Starlette
  5. Build the web app in a Docker container
  6. Run Docker container

1. Sign in to Google Cloud and Create a f1-micro Instance

Signing up for Google Cloud Platform is free ( Image by author)

If you haven’t already, sign up for Google Cloud Platform through your Google account. You’ll have to enter your credit card, but you won’t be charged anything upon signing up. You’ll also get $300 worth of free credits that lasts for 12 months! You’ll be utilizing GCP’s free tier, so you won’t have to pay to follow this tutorial.

Once you’re in the console, go to Compute Engine and create an instance. You’ll need to:

greenr
f1-micro

a visualization of how to start your machine (Image by author)

When your instance has been created and is running, SSH into your instance.

2. Pull the Trained Model from Github

First, let’s grab the exported model that we already trained on from Github. If you’re interested in learning how to train a dandelion and grass image classifier using FastAI, follow this notebook hosted on Kaggle ! I recommend using the library and following the FastAI course if you’re interested in deep learning.

Clone this repository from Github containing the exported model, export.pkl **add guide here:

git clone https://github.com/btphan95/greenr-tutorial

3. Add Swap Memory to Our Compute Instance

This is where it gets a bit hacky. Our f1-micro instance only supports up to 0.6GB of RAM, meaning it’s ultra-weak and not capable of installing all of our required deep learning libraries, which are upwards of 750MB in size. We’re going to add swap memory to our little friend to utilize its existing HDD space as RAM to make this all work. Fortunately, I put all of this part into a script, so just run swap.sh to add 4GB of swap memory to our machine:

cd greenr-tutorial
sudo bash swap.sh

4. Serve Model onto the Web with Starlette

Now, we’re going to build a Python script that will serve our model for inference on the web using the Starlette ASGI web framework. Why Starlette and not Flask? Because Starlette, along with Uvicorn, is way faster than Flask and more scalable in production .

Copy the following code to a python script called app.py in the greenr-tutorial directory. First, type vim app.py , and then when inside, press the i button to get into insert mode, and paste the following. When you’re done, press the esc key, type in :x , and press Enter.

copy this code to app.py in the greenr-tutorial directory

This will create a Starlette server on port 8008 with a web page where a user can upload an image and get results (is it a dandelion or grass?)

Before moving on, we’re also going to add a file called requirements.txt , which will allow Docker to install all of our required libraries when building a container. Vim and copy the following text into requirements.txt in the greenr-tutorial folder:

5. Containerize App Using Docker

Now, we’re going to use Docker to build a container where our app will live. This lets us run the app in its own environment, anywhere.

First, install Docker :

uninstall old versions:

sudo apt-get remove docker docker-engine docker.io containerd runc

set up the repository:

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

add the stable repository:

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

install Docker engine:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

verify installation by running the hello-world image (it should print an informational message verifying that installation was a success):

sudo docker run hello-world

Now, in the greenr-tutorial directory, you’ll need to create a Dockerfile that gives Docker instructions to build a container. First, vim into Dockerfile and add the following lines:

This Dockerfile will install the required libraries in a Python3.6 environment, add the necessary files to the container, and run the Starlette server in app.py.

In the greenr directory, build the Docker container with the following command:

sudo docker image build -t app:latest .

6. Run Docker Container

Now, all we have to do is run our Docker container!

sudo docker run -d -p 80:8008 app:latest

Now, let’s visit the External IP address of our machine, which you can find on Compute Engine. Make sure when you enter it in your browser, to format it like this (for my instance): http://34.68.160.231/

the final deployed model on the web! ( Image by author)

If you see the above, then you made it to the end. Congrats!:tada: Now, you can keep running your machine forever, because it is part of Google’s always-free tier.


以上所述就是小编给大家介绍的《10 Minutes to Deploying a Deep Learning Model on Google Cloud Platform》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

开发高质量PHP框架与应用的实际案例解析

开发高质量PHP框架与应用的实际案例解析

Sebastian Bergmann / 刘文瀚、刘海燕 / 清华大学出版社 / 2012-6 / 49.00元

PHP已经成为最受欢迎的编程语言之一,这使得用PHP创建高质量、易维护的应用程序和框架比以往受到更多的青睐。通过使用来自于知名公司的真实案例研究,《开发高质量PHP框架与应用的实际案例解析》为Web软件体系结构的不同层次介绍了规划、执行以及测试自动化方面的内容,并解释了这些公司如何测量和测试软件质量。《开发高质量PHP框架与应用的实际案例解析》作者Sebastian Bergmann、Stefan......一起来看看 《开发高质量PHP框架与应用的实际案例解析》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具