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

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

共享经济大趋势

共享经济大趋势

倪云华 虞仲轶 / 2016-1-1 / 49.00

2015年互联网界乃至整个商界的最热门字眼,恐怕就是“共享经济”了。共享经济模式正以前所未有的速度与规模席卷全球。那么,共享经济为什么会产生?其本质是什么?共享经济会为我们带来什么价值?成功的共享经济商业模式是怎样的?如何管理和运作一家共享经济企业?在未来,共享经济还将面临哪些挑战?共享经济的下一个发展机会在哪里?传统经济又该如何应对? 作为国内第一本系统性阐述共享经济的书籍,本书通过对全球......一起来看看 《共享经济大趋势》 这本书的介绍吧!

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

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码