Deep learning experiment tutorial: Build a TensorFlow model to recognize handwritten digits using the MNIST data set (Watson Machine Learning)
This tutorial guides you through using the MNIST computer vision data set to train a deep learning TensorFlow model to recognize handwritten digits. In this tutorial, you will train, deploy, and test the model with experiment builder.
Prerequisite
IBM Software Hub scheduling service is a component required to run experiments.
Steps overview
This tutorial presents the basic steps for training a deep learning model with experiment builder in Watson Studio:
- Task 1: Download the sample code
- Task 2: Train the model
- Task 3: Monitor training progress and results
- Task 4: Deploy the trained model
- Task 5: Test the deployed model
This tutorial does not demonstrate distributed deep learning, or using the Watson Machine Learning hyperparameter optimization feature.
Preview the tutorial
Watch this video to see how to build a TensorFlow model model to recognize handwritten digits.
This video provides a visual method to learn the concepts and tasks in this documentation.
Task 1: Download the sample code
Download sample TensorFlow model-building Python code from here: tf_model_with_metrics_2_1.zip.
The tf_model_with_metrics_2_1.zip
zip file contains three files:
- convolutional_network.py - Model-building Python code
- input_data.py - A "helper" file for reading the MNIST data files
- emetrics.py - A "helper" file for reading metrics
This sample code will download the data files it needs to run training.
Note:
- When deploying the model, you will need to save the model into a required directory in the model training script, and the directory is
$RESULT_DIR/model
whereRESULT_DIR
is an environment variable accessible by the model training script. - To monitor training progress and results, you will need to log the training metrics in the model training scripts. For example, using the
record
interface from theEMetrics
module that can be directly used in the model training script:from emetrics import EMetrics with EMetrics.open() as em: for epoch in range(0,total_epochs): # perform training for epoch # compute training metrics and assign to values train_metric1, train_metric2 etc em.record("training",epoch_num,{'metric1': train_metric1, 'metric2': train_metric2}) # compute test metrics and assign to values test_metric1, test_metric2 etc # NOTE for these metrics use the group EMetrics.TEST_GROUP so that the service will recognize these metrics as computed on test/validation data em.record(EMetrics.TEST_GROUP,epoch_num,{'metric1': test_metric1, 'metric2': test_metric2})
Task 2: Train the model
Follow these steps to create a deep learning experiment in a project to train the model:
- Open a project.
- Click New Asset > Build deep learning experiments.
- Specify a name for the experiment and an optional description.
- Add a model defintion.
-
Specify a name and a description.
-
Click Browse to upload the sample code,
tf_model_with_metrics_2_1.zip
. -
Specify the framework that is used in the model-building code: tensorflow_rt24.1-py3.11.
-
Specify either CPU or GPU as the resource type.
-
Set the hardware specification: For example, 1 GPU, generic, 1 CPU, and 4GB RAM.
-
Set the training type to Single node.
-
Set the execution command, specify this command for running the model-building code:
convolutional_network.py --trainImagesFile train-images-idx3-ubyte.gz --trainLabelsFile train-labels-idx1-ubyte.gz --testImagesFile t10k-images-idx3-ubyte.gz --testLabelsFile t10k-labels-idx1-ubyte.gz --learningRate 0.001 --trainingIters 6000 ``` 1. Click **Create** to save the model definition.
-
- Click Create and run.
Task 3: Monitor training progress and results
Follow these steps to monitor the progress of a training run in the Training Runs tab of experiment builder.
- During training, The Training runs tab shows you the current progress of the training: Queued, In progress, or Completed.
- When the training run is complete, click on the training run name to view:
- The Monitor tab to view the progress of the training
- The Overview tab to view the experiment configuration
- The Logs tab to see the details of the training results, including logs and other output.
Task 4: Deploy the trained model
You can use your trained model to classify new images only after the model has been deployed. Follow these steps to promote the model to a deployment space, and then deploy the model:
- In the deep learning experiments list, click on your training run.
- Wait for the training run to complete, and save the model in the Completed section:
- Click the Actions menu
at the end of the row for the model, and choose Save model.
- Provide a name for the model, and click Save. This stores the model in the Watson Machine Learning repository and saves it to the project.
- Click the Actions menu
- On the Assets tab, you can see the saved model.
- To deploy the model, you must first promote it to a deployment space.
- Click the Overflow menu
at the end of the row for the model, and choose Promote to space.
- Select an existing deployment space for the Target space or create a new deployment space.
- Select the option to Go to the model in the space after promoting it.
- Click Promote.
- Click the Overflow menu
- In the deployment space, click New deployment.
- Choose Online.
- Type a name for the deployment.
- Click Create.
- Monitor the status of the model deployment.
Task 5: Test the deployed model
Follow these steps to test your deployed model from the deployment details page:
- On your local computer, download this sample payload JSON file with input data corresponding to the handwritten digits "5" and "4": MNIST_sample_payload_V4.json
- Click the deployment name to view the deployment.
- Click the Test tab.
- Click Browse local files, and select the downloaded payload file
tf-mnist-test-payload.json
. - Click Predict.
Sample output:
{
"values": [
5,
4
]
}
This output shows: the first input data was correctly classified as belonging to the class "5", and the second input data was correctly classified as belonging to the class "4".
Parent topic: Deep learning