-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2471e96
commit f6d748d
Showing
115 changed files
with
5,653 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
on: | ||
github: | ||
branches: | ||
only: main | ||
|
||
jobs: | ||
CloneRepo: | ||
resources: | ||
instance-type: C3 | ||
uses: git-checkout@v1 | ||
with: | ||
# url: https://github.com/gradient-ai/mnist-sample.git | ||
url: context.event.github.url | ||
ref: context.event.github.ref | ||
outputs: | ||
mnist-sample: | ||
type: volume | ||
TrainModel: | ||
resources: | ||
instance-type: C3 | ||
env: | ||
PS_MODEL_PATH: /my-trained-model | ||
uses: container@v1 | ||
with: | ||
args: | ||
- bash | ||
- -c | ||
- >- | ||
cd /inputs/mnist-sample && python mnist.py | ||
image: tensorflow/tensorflow:1.15.5-py3 | ||
needs: | ||
- CloneRepo | ||
inputs: | ||
mnist-sample: CloneRepo.outputs.mnist-sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Docker image for running examples in Tensorflow models. | ||
# base_image depends on whether we are running on GPUs or non-GPUs | ||
FROM ubuntu:latest | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
ca-certificates \ | ||
build-essential \ | ||
git \ | ||
python \ | ||
python-pip \ | ||
python-setuptools | ||
|
||
RUN pip install tf-nightly | ||
|
||
# Checkout tensorflow/models at HEAD | ||
RUN git clone https://github.com/tensorflow/models.git /tensorflow_models | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Docker image for running examples in Tensorflow models. | ||
# base_image depends on whether we are running on GPUs or non-GPUs | ||
FROM nvidia/cuda:9.0-cudnn7-runtime-ubuntu16.04 | ||
|
||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
ca-certificates \ | ||
build-essential \ | ||
git \ | ||
python \ | ||
python-pip \ | ||
python-setuptools | ||
|
||
RUN pip install tf-nightly-gpu | ||
|
||
# Checkout tensorflow/models at HEAD | ||
RUN git clone https://github.com/tensorflow/models.git /tensorflow_models | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Paperspace | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,195 @@ | ||
# MNIST | ||
# MNIST in TensorFlow | ||
|
||
This repository demonstrates using Paperspace Gradient to train and deploy a deep learning model to recognize handwritten characters, which is a canonical sample problem in machine learning. | ||
|
||
We build a convolutional neural network to classify the [MNIST | ||
dataset](http://yann.lecun.com/exdb/mnist/) using the | ||
[tf.data](https://www.tensorflow.org/api_docs/python/tf/data), | ||
[tf.estimator.Estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator), | ||
and | ||
[tf.layers](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf) | ||
APIs. | ||
|
||
# Gradient Setup | ||
|
||
## Single Node Training on Gradient | ||
|
||
### Install Gradient CLI | ||
|
||
``` | ||
pip install -U gradient | ||
``` | ||
|
||
[Please check our documentation on how to install Gradient CLI and obtain an API Key](https://docs.paperspace.com/gradient/get-started/install-the-cli) | ||
|
||
### Create project and get the project id | ||
|
||
[Please check our documentation on how to create a project and get the project id](https://docs.paperspace.com/gradient/get-started/managing-projects) | ||
Your project ID will look like `pr1234567`. | ||
|
||
### Create and start a workflow | ||
|
||
``` | ||
gradient workflows create --name mnist-sample --projectId pr1234567 | ||
+--------------+--------------------------------------+ | ||
| Name | ID | | ||
+--------------+--------------------------------------+ | ||
| mnist-sample | 12345678-1234-1234-1234-1234567890ab | | ||
+--------------+--------------------------------------+ | ||
``` | ||
|
||
Clone this repo, and change directoru into it, or copy [mnist-sample.yaml](mnist-sample.yaml) to your local machine. | ||
|
||
Then run the workflow using the workflow ID from the create workflow command above. | ||
|
||
``` | ||
gradient workflows run --id 12345678-1234-1234-1234-1234567890ab --path mnist-sample.yaml | ||
``` | ||
|
||
That's it! | ||
|
||
### Exporting a Model for inference | ||
|
||
#### Export your Tensorflow model | ||
|
||
In order to serve a Tensorflow model, simply export a SavedModel from your Tensorflow program. [SavedModel](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md) is a language-neutral, recoverable, hermetic serialization format that enables higher-level systems and tools to produce, consume, and transform TensorFlow models. | ||
|
||
Please refer to [Tensorflow documentation](https://www.tensorflow.org/guide/saved_model#save_and_restore_models) for detailed instructions on how to export SavedModels. | ||
|
||
#### Example code showing how to export your model: | ||
|
||
``` | ||
tf.estimator.train_and_evaluate(mnist_classifier, train_spec, eval_spec) | ||
#Starting to Export model | ||
image = tf.placeholder(tf.float32, [None, 28, 28]) | ||
input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn({ | ||
'image': image, | ||
}) | ||
mnist_classifier.export_savedmodel(<export directory>, | ||
input_fn, | ||
strip_default_attrs=True) | ||
#Model Exported | ||
``` | ||
|
||
We use TensorFlow's [SavedModelBuilder module](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/builder.py) to export the model. SavedModelBuilder saves a "snapshot" of the trained model to reliable storage so that it can be loaded later for inference. | ||
|
||
For details on the SavedModel format, please see the documentation at [SavedModel README.md](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md). | ||
|
||
For export directory, be sure to set it to `PS_MODEL_PATH` when running a model deployment on Gradient: | ||
|
||
``` | ||
export_dir = os.path.abspath(os.environ.get('PS_MODEL_PATH')) | ||
``` | ||
|
||
You can also use Gradient SDK to ensure you have the correct path: | ||
|
||
``` | ||
from gradient_sdk.utils import data_dir, model_dir, export_dir | ||
``` | ||
|
||
# (Optional) Local Setup using a Virtual Environment | ||
|
||
Users sometimes run into local machine environment issues when trying to use Python. A common solution for this is to create and use a Python virtual environment to run Python from within. To do so: | ||
|
||
1. Create and activate a Python virtual environment (we recommend using python3.7+): | ||
|
||
``` | ||
cd mnist-sample | ||
python3 -m venv venv | ||
source venv/bin/activate | ||
``` | ||
|
||
2. Install the required Python packages: | ||
|
||
``` | ||
pip install -r requirements-local.txt | ||
``` | ||
|
||
# Local Training | ||
|
||
To train a the mnist model locally: | ||
|
||
1. Make sure you have the latest version of TensorFlow installed. | ||
|
||
2. Also make sure you've [added the models folder to your Python path](https://github.com/mlcommons/training/blob/master/image_classification/tensorflow/official/README.md#running-the-models); otherwise you may encounter an error like `ImportError: No module named mnist`. | ||
|
||
3. Download the code from GitHub: | ||
|
||
``` | ||
git clone [email protected]:Paperspace/mnist-sample.git | ||
``` | ||
|
||
4. Start training the model: | ||
|
||
``` | ||
python mnist.py | ||
``` | ||
|
||
_Note: local training will take a long time, so be prepared to wait!_ | ||
|
||
If you want to shorten model training time, you can change the max steps parameter: | ||
|
||
``` | ||
python mnist.py --max_steps=1500 | ||
``` | ||
|
||
The mnist dataset is downloaded to the `./data` directory. | ||
|
||
Model results are stored in the `./models` directory. | ||
|
||
Both directories can be safely deleted if you would like to start the training over from the beginning. | ||
|
||
## Exporting the model to a specific directory | ||
|
||
You can export the model into a specific directory, in the Tensorflow [SavedModel](https://www.tensorflow.org/guide/saved_model) format, by using the argument `--export_dir`: | ||
|
||
``` | ||
python mnist.py --export_dir /tmp/mnist_saved_model | ||
``` | ||
|
||
If no export directory is specified, the model is saved to a timestamped directory under `./models` subdirectory (e.g. `mnist-sample/models/1513630966/`). | ||
|
||
## Testing a Tensorflow Serving-deployed model on your local machine using Docker | ||
|
||
Open another terminal window and run the following in the directory where you cloned this repo: | ||
|
||
``` | ||
docker run -t --rm -p 8501:8501 -v "$PWD/models:/models/mnist" -e MODEL_NAME=mnist tensorflow/serving | ||
``` | ||
|
||
Now you can test the local inference endpoint by running: | ||
|
||
``` | ||
python serving_rest_client_test.py | ||
``` | ||
|
||
Optionally you can provide a path to an image file to run a prediction on: | ||
|
||
``` | ||
python serving_rest_client_test.py --path example3.png | ||
``` | ||
|
||
Once you've completed local testing using the tensorflow/serving docker container, stop the running container as follows: | ||
|
||
``` | ||
docker ps | ||
docker kill <container-id-or-name> | ||
``` | ||
|
||
## Training the model on a node with a GPU for use with Tensorflow Serving on a node with only a CPU | ||
|
||
If you are training on Tensorflow using a GPU but would like to export the model for use in Tensorflow Serving on a CPU-only server, you can train and/or export the model using `--data_format=channels_last`: | ||
|
||
``` | ||
python mnist.py --data_format=channels_last | ||
``` | ||
|
||
The SavedModel will be saved in a timestamped directory under `models` subdirectory (e.g. `mnist-sample/models/1513630966/`). | ||
|
||
## Inspecting and getting predictions with the SavedModel file | ||
|
||
You can also use the [`saved_model_cli`](https://www.tensorflow.org/guide/saved_model#cli_to_inspect_and_execute_savedmodel) tool to inspect and execute the SavedModel. |
Empty file.
Oops, something went wrong.