Skip to content

Commit 19ec53e

Browse files
authored
workshop updates (#80)
Removal of some out-of-date stuff, new labs content, updates of existing labs to better reflect current TF API, colab support in notebooks, addition of 'eager' example, dockerfile update.
1 parent beec903 commit 19ec53e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4165
-9831
lines changed

INSTALL.md

Lines changed: 35 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,230 +1,76 @@
11

2-
**Note**: you do not need to follow these instructions for the [Strata workshop](https://conferences.oreilly.com/strata/strata-ny/public/schedule/detail/60847) or the [Strange Loop](https://www.thestrangeloop.com) workshop. For those workshops, you just need [TensorFlow](https://www.tensorflow.org/install/) and [Jupyter notebook](http://jupyter.org/install.html) installed on your laptop.
2+
# Installation
33

4-
# Installation instructions for the TensorFlow/Cloud ML workshop
4+
We suggest installing the necessary libraries locally on your laptop as described below in "__Local Installation__", as that will let you run TensorBoard as part of the workshop, and let you run python scripts as well as notebooks.
55

6-
- [Project and Cloud ML setup](#project-and-cloud-ml-setup)
7-
- [Docker-based installation](#docker-based-installation)
8-
- [Download the container image](#download-the-container-image)
9-
- [Create a directory to hold data files needed by the workshop](#create-a-directory-to-hold-data-files-needed-by-the-workshop)
10-
- [Run the container](#run-the-container)
11-
- [Authorize gcloud in the running container](#authorize-gcloud-in-the-running-container)
12-
- [Restarting the container later](#restarting-the-container-later)
13-
- [Running the Docker container on a VM](#running-the-docker-container-on-a-vm)
14-
- [Virtual environment-based installation](#virtual-environment-based-installation)
15-
- [Install Conda + Python 2.7 to use as your local virtual environment](#install-conda--python-27-to-use-as-your-local-virtual-environment)
16-
- [Install TensorFlow into a virtual environment](#install-tensorflow-into-a-virtual-environment)
17-
- [Install some Python packages](#install-some-python-packages)
18-
- [Install the Google Cloud SDK](#install-the-google-cloud-sdk)
19-
- [Cloud ML SDK installation (for 'transfer learning' preprocessing)](#cloud-ml-sdk-installation-for-transfer-learning-preprocessing)
20-
- [Set up some data files used in the examples](#set-up-some-data-files-used-in-the-examples)
21-
- [Transfer learning example](#transfer-learning-example)
22-
- [Optional: Clone/Download the TensorFlow repo from GitHub](#optional-clonedownload-the-tensorflow-repo-from-github)
6+
But if you have trouble with your installation, you will be able to run everything but TensorBoard in [Colab notebooks instead](https://colab.research.google.com/).
237

24-
* * *
8+
## Local Installation
259

26-
You can set up for the workshop in two different, mutually-exclusive ways:
10+
It's highly recommended to use a Python virtual environment for this workshop, either [Virtualenv](https://virtualenv.pypa.io/en/stable) or [Conda](http://conda.pydata.org/miniconda.html). Either Python 2.7 or Python 3 will work.
2711

28-
- [Running in a docker container](#docker-based-installation).
29-
- [Installing the necessary packages into a virtual environment](#virtual-environment-based-installation).
12+
Then install the following into your virtual environment:
3013

31-
You will need to do the [Cloud Project and Cloud ML setup](#project-and-cloud-ml-setup) in either case.
14+
- Install [TensorFlow](https://www.tensorflow.org/install). Your TensorFlow version must be >=1.7 to run all the labs. Note that even if you're running a conda virtual environment, use `pip install tensorflow` to install TensorFlow (*not* the `conda install` version, which may not be up to date.)
3215

33-
## Project and Cloud ML setup
16+
- Install [Jupyter](https://jupyter.org/install.html).
3417

35-
Follow the instructions below to create a project, initialize it for Cloud ML, and set up a storage bucket to use for the workshop examples.
18+
- Optionally, install `matplotlib`. (If you're using a conda virtual environment, use `conda install matplotlib`.)
3619

37-
**Note: Skip the "Setting up your environment" section** of the linked-to page at this step. (If you run in a docker container, it is already done for you).
20+
### Test your installation
3821

39-
* [Setting Up Your GCP Project](https://cloud.google.com/ml/docs/how-tos/getting-set-up#setting_up_your_google_cloud_project )
40-
* [Initializing Cloud ML for your project](https://cloud.google.com/ml/docs/how-tos/getting-set-up#initializing_your_product_name_short_project)
41-
* [Setting up your Cloud Storage Bucket](https://cloud.google.com/ml/docs/how-tos/getting-set-up#setting_up_your_cloud_storage_bucket)
42-
43-
## Docker-based installation
44-
45-
We're providing a [Docker](https://www.docker.com/) container image with all the necessary libraries included, for you to download.
46-
47-
To use it, you'll need to have [Docker installed](https://docs.docker.com/engine/installation/).
48-
To run some of the examples, you'll likely need to configure it with at least 4GB of memory.
49-
50-
If you like, you can start up a Google Compute Engine (GCE) VM with docker installed, and run the container there. This is a good option if you don't want to install Docker locally. See the instructions at the end of this section for how to do that.
51-
52-
### Download the container image
53-
54-
Once Docker is installed and running, download the workshop image:
55-
56-
```sh
57-
$ docker pull gcr.io/google-samples/tf-workshop:v7
58-
```
59-
60-
[Here's the Dockerfile](https://github.com/amygdala/tensorflow-workshop/tree/master/workshop_image) used to build this image.
61-
62-
### Create a directory to hold data files needed by the workshop
63-
64-
Create a directory (called, say, `workshop-data`) to mount as a volume when you start up the docker container. You can put data/output in this directory so that it is accessible both within and outside the running container.
65-
66-
### Run the container
67-
68-
Once you've downloaded the container image, you can run it like this:
22+
Start up a jupyter notebooks server from the command line:
6923

7024
```sh
71-
docker run -v `pwd`/workshop-data:/root/tensorflow-workshop-master/workshop-data -it \
72-
-p 6006:6006 -p 8888:8888 -p 5000:5000 gcr.io/google-samples/tf-workshop:v7
25+
jupyter notebook .
7326
```
7427

75-
Edit the path to the directory you're mounting as appropriate. The first component of the `-v` arg is the local directory, and the second component is where you want to mount it in your running container.
28+
Create a new notebook and paste the following python code into a notebook cell:
7629

77-
### Authorize gcloud in the running container
30+
```python
31+
import tensorflow as tf
7832

79-
In the docker container, run the following commands, replacing `<your-project-name` with your project name.
80-
For the second two, you will get a URL to paste in your browser, to obtain an auth code. In the browser, allow access, then copy the resulting code and paste it at the prompt in your running docker container.
81-
82-
```shell
83-
gcloud config set project <your-project-name>
84-
gcloud auth login
85-
gcloud auth application-default login
86-
```
87-
88-
### Restarting the container later
89-
90-
If you later exit your container and then want to restart it again, you can find the container ID by running:
91-
92-
```sh
93-
$ docker ps -a
94-
```
95-
96-
Then, run:
97-
98-
```sh
99-
$ docker start <container_id>
33+
print(tf.__version__)
10034
```
10135

102-
(`docker ps` should then show it running). Once the workshop container is running again, you can exec back into it like this:
36+
'Run' the cell. Ensure that there are no import errors and that the TensorFlow version is as expected.
10337

104-
```sh
105-
$ docker exec -it <container_id> bash
106-
```
38+
## Using Colaboratory
10739

108-
### Running the Docker container on a VM
40+
Instead of running in a local installation, all of the jupyter notebooks in this workshop can also be run on [Colab](https://colab.research.google.com/), with the exception of the lab sections that use TensorBoard.
10941

110-
It is easy to set up a Google Compute Engine (GCE) VM on which to run the Docker container. We sketch the steps below.
42+
The lab section READMEs include links to launch the workshop notebooks in colab.
11143

112-
First, make sure that your project has the GCE API enabled. An easy way to do this is to go to the [Cloud Console](https://console.cloud.google.com/), and visit the Compute Engine panel. It should display a button to enable the API.
11344

114-
- Connect to your project's [Cloud Shell](https://cloud.google.com/shell/).
45+
## Google Cloud Platform
11546

116-
- From the Cloud Shell, create a container-optimized image as described [here](https://cloud.google.com/container-optimized-os/docs/how-to/create-configure-instance). E.g.:
117-
118-
```
119-
gcloud compute instances create mlworkshop \
120-
--image-family gci-stable \
121-
--image-project google-containers \
122-
--zone us-central1-b --boot-disk-size=100GB \
123-
--machine-type n1-standard-1
124-
```
125-
126-
- Set up a default network firewall rule for: tcp:8888, tcp:6006, and tcp:5000. (This opens the ports we'll use for jupyter, Tensorboard, and Flask.) E.g:
127-
128-
```shell
129-
gcloud compute firewall-rules create mlworkshop --allow tcp:8888,tcp:6006,tcp:5000
130-
```
47+
Some of the labs include instructions for optionally running training and prediction on Google Cloud Platform (GCP) [Cloud ML Engine](https://cloud.google.com/ml-engine).
13148

132-
- SSH to your image. You can do this from the Cloud Console by visiting the Compute Engine panel, and clicking on the 'SSH' pulldown to the right of your instance.
49+
If you want to play along, set up a GCP account: click the **Try it free** button at the top of this page:
50+
[cloud.google.com](https://cloud.google.com/).
13351

134-
Then, once you've ssh'd to the VM, follow the instructions above to download and run the Docker container there.
135-
Note: **Docker is already installed** on the 'container-optimized' VMs.
13652

137-
When you're done with the workshop, you may want to *stop* or *delete* your VM instance.
53+
## Docker image
13854

139-
You can also delete your firewall rule:
55+
A Docker image is also available.
14056

141-
```shell
142-
gcloud compute firewall-rules delete mlworkshop
143-
```
144-
145-
* * *
146-
## Virtual environment-based installation
147-
148-
(These steps are not necessary if you have already completed the instructions for running the Docker image.)
149-
150-
We highly recommend that you use a virtual environment for your TensorFlow installation rather than a direct install onto your machine. The instructions below walk you through a `conda` install, but a `virtualenv` environment will work as well.
151-
152-
### Install Conda + Python 2.7 to use as your local virtual environment
153-
154-
Anaconda is a Python distribution that includes a large number of standard numeric and scientific computing packages. Anaconda uses a package manager called "conda" that has its own environment system similar to Virtualenv.
155-
156-
Follow the instructions [here](https://www.continuum.io/downloads). The [miniconda version](http://conda.pydata.org/miniconda.html) should suffice.
157-
158-
### Install TensorFlow into a virtual environment
159-
160-
Follow the instructions [on the TensorFlow site](https://www.tensorflow.org/get_started/os_setup#anaconda_installation) to create a Conda environment with Python 2.7, *activate* it, and then install TensorFlow within it.
161-
162-
**Note**: Install TensorFlow version 0.12, by using the [**pip install steps**](https://www.tensorflow.org/get_started/os_setup#anaconda_installation#using_pip) for conda rather than conda-forge.
163-
164-
If you'd prefer to use virtualenv, see [these instructions](https://www.tensorflow.org/get_started/os_setup#virtualenv_installation) instead.
165-
166-
Remember to activate your environment in all the terminal windows you use during this workshop.
167-
168-
### Install some Python packages
57+
### Download the container image
16958

170-
With your conda environment activated, install the following packages:
59+
Once Docker is installed and running, download the workshop image:
17160

17261
```sh
173-
$ conda install numpy
174-
$ conda install scipy
175-
$ pip install sklearn pillow
176-
$ conda install matplotlib
177-
$ conda install jupyter
62+
docker pull gcr.io/google-samples/tf-workshop:v8
17863
```
17964

180-
(If you are using `virtualenv` instead of `conda`, install the packages using the equivalent `pip` commands instead).
181-
182-
183-
### Install the Google Cloud (gcloud) SDK
184-
185-
Follow the installation instructions [here](https://cloud.google.com/sdk/downloads), then run:
186-
187-
```
188-
gcloud components install beta
189-
```
190-
191-
To get the `gcloud beta ml` commands.
192-
193-
### Cloud ML SDK installation (for 'transfer learning' preprocessing)
194-
195-
The Cloud ML SDK is needed to run the 'preprocessing' stage in the [Cloud ML transfer
196-
learning](workshop_sections/transfer_learning/cloudml) example. It requires Python 2.7 to install. It's possible to
197-
skip this part of setup for most of the exercises.
198-
199-
To install the SDK, follow the setup instructions
200-
[on this page](https://cloud.google.com/ml/docs/how-tos/getting-set-up).
201-
(Assuming you've followed the instructions above, you will have already done some of these steps. **Install TensorFlow version 0.12** as described in [this section](#install-tensorflow-into-a-virtual-environment), not 0.11)
202-
203-
**Note**: if you have issues with the pip install of `python-snappy`, and are running in a conda virtual environment, try `conda install python-snappy` instead.
204-
205-
You don't need to download the Cloud ML samples or docs for this workshop, though you may find it useful to grab them
206-
anyway.
207-
208-
209-
## Set up some data files used in the examples
210-
211-
### Transfer learning example
65+
[Here's the Dockerfile](https://github.com/amygdala/tensorflow-workshop/tree/master/workshop_image) used to build this image.
21266

213-
Because we have limited workshop time, we've saved a set of
214-
[TFRecords]([TFRecords](https://www.tensorflow.org/api_docs/python/python_io/))
215-
generated as part of the [Cloud ML transfer learning](workshop_sections/transfer_learning/cloudml)
216-
example. To save time, copy them now to your own bucket as follows.
67+
### Run the container
21768

218-
Set the `BUCKET` variable to point to your GCS bucket (replacing `your-bucket-name` with the actual name), then copy the records to your bucket. Then, set the GCS_PATH variable to the newly copied subdir.
69+
Once you've downloaded the container image, run it like this:
21970

220-
```shell
221-
BUCKET=gs://your-bucket-name
222-
gsutil cp -r gs://tf-ml-workshop/transfer_learning/hugs_preproc_tfrecords $BUCKET
223-
GCS_PATH=$BUCKET/hugs_preproc_tfrecords
71+
```sh
72+
docker run -it \
73+
-p 6006:6006 -p 8888:8888 -p 5000:5000 gcr.io/google-samples/tf-workshop:v8
22474
```
22575

22676

227-
## Optional: Clone/Download the TensorFlow repo from GitHub
228-
229-
We'll be looking at some examples based on code in the tensorflow repo. While it's not necessary, you might want to clone or download it [here](https://github.com/tensorflow/tensorflow), or grab the latest release [here](https://github.com/tensorflow/tensorflow/releases).
230-

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11

22
# TensorFlow workshop materials
33

4-
This repo contains materials for use in a TensorFlow workshop.
5-
Contributions are not currently accepted. This is not an official Google product.
4+
This repo contains materials for use in TensorFlow workshops.
65

7-
This document points to more information for each workshop lab.
6+
Contributions are not currently accepted. This is not an official Google product.
87

98
<!---
109
[** add note re: TF versions tested with **].
@@ -20,21 +19,21 @@ This document points to more information for each workshop lab.
2019

2120
- [Introducing MNIST, and building a simple linear classifier in TensorFlow](workshop_sections/mnist_series/01_README_mnist_simple.md).
2221
- [Using TensorFlow's high-level APIs to build an MNIST DNN Classifier, and introducing TensorBoard](workshop_sections/mnist_series/02_README_mnist_tflearn.md).
23-
- [Building custom `Estimator`s for a version of MNIST that uses CNNs](workshop_sections/mnist_series/mnist_cnn_custom_estimator/README.md), using either TensorFlow or [Keras](https://keras.io/) layers.
22+
- [An example of using Keras with TensorFlow eager mode, on the'Fashion MNIST' dataset](workshop_sections/mnist_series/mnist_eager_keras.ipynb): This notebook shows an example of using Keras with TensorFlow eager mode, on the 'Fashion MNIST' dataset. This notebook requires TensorFlow >=1.7.
23+
- [Building Custom `Estimator`s for a version of MNIST that uses CNNs](workshop_sections/mnist_series/mnist_cnn_custom_estimator/README.md), using either TensorFlow or [Keras](https://keras.io/) layers.
2424

2525

2626
## 'Wide & Deep'
2727

28-
- [Using a tf.estimator to train a 'Wide & Deep' model](workshop_sections/wide_n_deep/README.md).
28+
- [Using a tf.estimator to train a 'Wide & Deep' model](workshop_sections/wide_n_deep/README.md). The example highlights use of [`tf.feature_columns`](https://www.tensorflow.org/versions/master/get_started/feature_columns), which are intermediaries between raw data and Estimators, enabling you to transform a diverse range of raw data into formats that Estimators can use, and allowing easy experimentation.
29+
It also includes the use of [**`tf.estimator.train_and_evaluate`**](https://www.tensorflow.org/api_docs/python/tf/estimator/train_and_evaluate) and [**Datasets**](https://www.tensorflow.org/api_docs/python/tf/data/Dataset).
2930

30-
## Transfer Learning
3131

32-
- [Transfer learning: using a trained model to 'bootstrap' learning new classifications](workshop_sections/transfer_learning/README.md).
33-
+ [Using Cloud ML](workshop_sections/transfer_learning/cloudml)
34-
+ **(possibly outdated)** [Using a custom Estimator](workshop_sections/transfer_learning/TF_Estimator)
32+
## Extras
3533

36-
- **(possibly outdated)** [Building a word2vec model using a Custom Estimator, and exploring the learned embeddings](workshop_sections/word2vec/README.md). Introducing [TFRecords](https://www.tensorflow.org/api_guides/python/python_io).
34+
- [Transfer learning: using a trained model to 'bootstrap' learning new classifications](transfer_learning/README.md) [using Cloud ML Engine](workshop_sections/transfer_learning/cloudml). This example still works (and is fun to play with), but uses TensorFlow v1.2. So it doesn't demonstrate current best practices.
3735

38-
## Extras
36+
- **(probably outdated)** [Building a word2vec model using a Custom Estimator, and exploring the learned embeddings](workshop_sections/word2vec/README.md). Introducing [TFRecords](https://www.tensorflow.org/api_guides/python/python_io).
37+
38+
In addition, there is an [extras](workshop_sections/extras/README.md) directory, that contains some older labs not currently used in this workshop (& which may not necessarily run with the latest version of TF), but which may be of interest.
3939

40-
In addition, there is an [extras](workshop_sections/extras/README.md) directory, that contains some older labs not currently used in this workshop (and which may not necessarily run with the latest version of TF), but which may be of interest.

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tensorflow>=1.7
2+
matplotlib
3+
numpy
4+
pandas
5+
jupyter

workshop_image/Dockerfile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,36 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
FROM gcr.io/tensorflow/tensorflow:latest-devel
14+
FROM gcr.io/tensorflow/tensorflow:latest
1515

1616
RUN pip install --upgrade pip
1717
RUN apt-get update
1818
RUN apt-get install -y unzip python-dev python-pip zlib1g-dev libjpeg-dev libblas-dev
1919
RUN apt-get install -y liblapack-dev libatlas-base-dev libsnappy-dev libyaml-dev gfortran
2020
RUN apt-get install -y python-scipy
2121

22-
RUN pip install sklearn nltk pillow setuptools
22+
RUN pip install sklearn pillow setuptools jupyter
2323
RUN pip install flask google-api-python-client
2424
RUN pip install pandas python-snappy scipy scikit-learn requests uritemplate
25-
# RUN pip install --upgrade --force-reinstall https://storage.googleapis.com/cloud-ml/sdk/cloudml.latest.tar.gz
25+
RUN python -mpip install matplotlib
2626

27+
# RUN pip install nltk
2728
# RUN python -c "import nltk; nltk.download('punkt')"
2829

29-
RUN curl https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-171.0.0-linux-x86_64.tar.gz | tar xvz
30+
RUN curl https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-198.0.0-linux-x86_64.tar.gz | tar xvz
3031
RUN ./google-cloud-sdk/install.sh -q
31-
# RUN ./google-cloud-sdk/bin/gcloud components install beta
3232

3333
ADD download_git_repo.py download_git_repo.py
3434
ENV PATH="${PATH}:/root/google-cloud-sdk/bin"
3535

36+
ADD start_notebook.sh start_notebook.sh
37+
RUN chmod +x start_notebook.sh
38+
3639
# TensorBoard
3740
EXPOSE 6006
38-
# IPython
41+
# For jupyter
3942
EXPOSE 8888
4043
# Flask
4144
EXPOSE 5000
4245

43-
CMD ["sh", "-c", "python download_git_repo.py ; /bin/bash"]
46+
CMD ["sh", "-c", "python download_git_repo.py; ./start_notebook.sh ; /bin/bash"]

workshop_image/start_notebook.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
jupyter notebook --allow-root &> /dev/null &

0 commit comments

Comments
 (0)