# How to run any ML package on GCP | The Models

> How to run your “fill in the blank” machine learning software on Google Cloud Platform

- Author: Nyghtowl (Melanie Warrick)
- URL: https://nyghtowl.com/posts/2017/07/ml-on-gcp-the-models/
- Published: 2017-07-18
- Tags: machine learning, GCP
- Originally published: https://medium.com/@nyghtowl/how-to-run-any-ml-package-on-gcp-the-models-a06f3bbf1dfa
- Site index for agents: https://nyghtowl.com/llms.txt




### How to run any ML package on GCP | The Models

![](img-01.jpeg)

In the [previous post](https://medium.com/@warrick.melanie/how-to-run-any-ml-package-on-gcp-the-setup-7196268cefc3), I showed you how to setup a virtual machine (VM) on Google Cloud Platform (GCP) so you can get started running your machine learning package of choice. Examples shared in this post are from different top frameworks in the ML space with a Python focused. Any programming language can be used as well as any package because you are just using a remote computer that you can shape as needed.

This post assumes you already have an understanding of machine learning (ML) and deep learning (DL), and in essence, you have models you are already working with. If you need to understand these concepts then spend time reading up on ML and DL and then come back. There are resources at the end of this post that you can use as a starting point.

---

### **Part 2: Setup and Run Machine Learning Software**

Now that you have a server setup it’s time to get your code on. If you used the [install scrip](https://github.com/nyghtowl/mixed-tape/blob/master/alpha-track/gce-install-gpu.sh)t in the previous post all requirements will be setup. This post covers the following key steps to set up and run ML software:

* Setup software &amp; data
* Develop &amp; load code
* Run code
* Review results

I’ve referenced great existing code examples that help illustrate these concepts:

* Kaggle-titanic with Jupyter Notebook
* Keras CIFAR-10
* PyTorch SVHN

The first example focuses more on data analysis and covers some of the well known algorithms in ML such as SVMs, Random Forests and Logistic Regression. The last two use a neural net model (complex ML model) and focus on image analysis which is a popular research area in the field.

***Side note about Data Setup*** *There are different approaches to setting up data for model access. This post is focused on creating an instance with enough space to hold the data that you will use for these examples. There are many ML projects that require connecting data sources to the server, and I recommend looking into* [*data products on GCP*](https://cloud.google.com/storage-options/) *or elsewhere for help on what products to use, how to setup and how to connect outside data sources.*

*Also, we use data that has already been cleaned and is ready to load into the model for training. Typically, you will spend most of your time gathering and cleaning up data to prepare it for modeling.*

#### Kaggle-titanic Data Science Example

The [Kaggle-titanic](http://agconti.github.io/kaggle-titanic/) example gives a great overview of data science methodology, ML models and core data science Python libraries (e.g. NumPy, Pandas, SciKit-Learn, SciPy and Matplotlib). The [original competition’s](https://www.kaggle.com/c/titanic) goal was to help educate on ML basics.

Kaggle provides a platform and service for data science exploration. It’s become a well know resource for researchers to work with a variety of data sets and models as well as companies to expand and access experts for help with different challenges.

The Github repository for this example is a couple years old, but the concepts are very relevant. Do a little searching to see the more current syntax for the packages, but know that the Titanic notebook will run with just minor adjustments as noted below.

***Setup***If you didn’t use the install script go to the the [Github repo](https://github.com/agconti/kaggle-titanic) and follow instructions there. To use this example, make sure you’ve configured Jupyter Notebook according to the previous post. Also, the data is downloaded when you clone the repository.

***Load Code***Run the following command in the VM to clone the example repository:

```
$ git clone https://github.com/agconti/kaggle-titanic
```

***Run Code***To use the Titanic Jupyter Notebook in this example, run the following commands on your server:

```
$ cd kaggle-titanic  
$ jupyter notebook
```

In your browser address bar, enter the VM’s External IP as follows:

```
http://[External IP]:8888 OR   
https://[External IP]:8888
```

A page opens asking for the password you defined when running the install setup script in the previous post.

![](img-02.jpeg)

After entering the password, you will have access to the Jupyter file directory in the browser and click on the Titanic notebook to open and run.

***Review Results***Now play around with the notebook to see how the models work, and how to experiment with them.

***Minor Adjustments***   
When running the notebook, it threw the following error under the data visualization section:

```
IOPub data rate exceeded.  
The notebook server will temporarily stop sending output  
to the client in order to avoid crashing it.  
To change this limit, set the config variable  
`--NotebookApp.iopub_data_rate_limit`.
```

To get around this make a change to the config file:

```
$ vi ~/.jupyter/jupyter_notebook_config.py
```

Un-comment the following line and add a couple zeros on the end before saving.

```
c.NotebookApp.iopub_data_rate_limit = 1000000000
```

Also, in the Jupyter Notebook, change .ix to .iloc

#### *Keras CIFAR-10 Example*

[Keras](https://keras.io/) is a neural net (NN) API that has gained a significant following because it provides an easy interface and sits on top of other frameworks (Theano, TensorFlow and CNTK). This example uses the CIFAR-10 dataset which is a curated group of 60k images covering 10 categories to help with image classification modeling. More information can be found at [CIFAR-10 project page](https://www.cs.toronto.edu/~kriz/cifar.html).

***Setup***If you didn’t use the install script review [how to install Keras](https://keras.io/%27).

*(Optional) Change backend to TensorFlow*The default background framework is Theano and you can change it to TensorFlow in the config file:

```
$ vi $HOME/.keras/keras.json
```

```
Change:
```

```
“image_dim_ordering”: “th” to “image_dim_ordering”: “tf”
```

```
“backend”: “theano” to “backend”: “tensorflow”
```

When running the code, it handles downloading the data to the server and how to transform and access it.

***Load Code***I’ve modified the [original example](https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py) with functionality to save and evaluate the model. You can copy it on your computer with the following commands:

```
$ cd ~ &amp;&amp; mkdir keras-code &amp;&amp; cd keras-code  
$ wget https://raw.githubusercontent.com/nyghtowl/mixed-tape/master/alpha-track/keras_cifar10.py
```

***Run Code***When running code, use [screen](https://www.gnu.org/software/screen/) to create a persistent terminal session to run any ML code that can take more than a couple minutes to complete (which covers most scenarios). Basically, if you want to exit out of the VM it will not stop the program from running. This is very helpful when you want to sleep or change locations or just work on something else and the model takes hours (maybe days to run).

```
$ screen -S cifar # new screen session  
$ screen -r cifar # resume screen session
```

You can run it on the command line by changing the directory to where the script lives and using the line:

```
$ python keras_cifar10.py
```

***Review Results***The code will print out the accuracy and show the predictions on 20 different test images. The accuracy score is just below 50%. Thus, there is room for improvement with tuning and model exploration which is a great opportunity to practice. And if you improve on this score then you should post a PR to the original Keras example repo.

#### *PyTorch Example*

PyTorch is a newer neural net framework this year that integrates Python with Torch, a framework that has a solid history in NN research. This example uses the [Street View House Numbers (SVHN)](http://ufldl.stanford.edu/housenumbers/)dataset which is a real-world set of 600k images obtained from house numbers in Google Street View images. The goal is recognizing digits in natural images by experimenting with object recognition models.

***Setup***If you didn’t use the install script review the [Github repo](https://github.com/potterhsu/SVHNClassifier-PyTorch) for installation.

Download format1 data from &lt;http://ufldl.stanford.edu/housenumbers/&gt; into into a data folder and untar.

```
$ cd ~  
$ mkdir data &amp;&amp; cd data  
$ wget http://ufldl.stanford.edu/housenumbers/train.tar.gz  
$ wget http://ufldl.stanford.edu/housenumbers/test.tar.gz  
$ wget http://ufldl.stanford.edu/housenumbers/extra.tar.gz  
$ tar -xvzf test.tar.gz   
$ tar -xvzf train.tar.gz   
$ tar -xvzf extra.tar.gz  
$ rm *.tar.gz
```

***Load Code***Run the following code in the terminal to clone the example repository:

```
$ cd ~  
$ git clone https://github.com/potterhsu/SVHNClassifier-PyTorch
```

Convert the data to lmdb structure( ~30 minutes).

```
$ cd ../SVHNClassifier-PyTorch # change into the SVHN example repo  
$ python convert_to_lmdb.py — data_dir ../data # convert data
```

***Run Code***Run the example with the following command line:

```
$ python train.py --data_dir ../data --logdir ./logs
```

It took ~30 hours to fully train with the configuration from the first post.

***Review Results***You can evaluate the code by running the following:

```
$ python eval.py — data_dir ./data ./logs/model-100.tar
```

Accuracy score is about 95% on this example.

#### Last Thoughts

There are many other examples out there with large and small datasets and various model structures. Plus, there are plenty of ways to configure the virtual machine to optimize and speed up model training, and I haven’t even touched on distributed computing. As said in the beginning, the goal is to make Google Cloud Platform more approachable for any machine learning package. Take what works for you from this, and build on it.

#### References

[Python, Kaggle-Titanic Example](https://github.com/agconti/kaggle-titanic/blob/master/Titanic.ipynb)  
[Keras Overview](https://keras.io/)  
[Keras CIFAR-10 Example](https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py)  
[PyTorch Overview](http://pytorch.org/)   
[PyTorch SVHN Example](https://github.com/potterhsu/SVHNClassifier-PyTorch) *(Thanks for granting use Potter Hsu)*[TensorFlow Overview](http://tensorflow.org)  
Yuval Netzer, Tao Wang, Adam Coates, Alessandro Bissacco, Bo Wu, Andrew Y. Ng Reading Digits in Natural Images with Unsupervised Feature Learning *NIPS Workshop on Deep Learning and Unsupervised Feature Learning 2011*. ([PDF](http://ufldl.stanford.edu/housenumbers/nips2011_housenumbers.pdf))  
[CIFAR-10 by Alex Krizhevsky, Vinod Nair, and Geoffrey Hinton](https://www.cs.toronto.edu/~kriz/cifar.html)   
[Street View House Numbers (SVHN) by Yuval Netzer, Tao Wang, Adam Coates, Alessandro Bissacco, Bo Wu, Andrew Y. Ng](http://ufldl.stanford.edu/housenumbers/)

