IBM / pytorch-on-watson-studio

Use Watson Studio and PyTorch to create a machine learning model to recognize hand-written digits
https://developer.ibm.com/patterns/handwritten-digit-recognizer-in-watson-studio-and-pytorch/
Apache License 2.0
23 stars 40 forks source link
data-science jupiter-notebook machine-learning pytorch watson-studio

Use Watson Studio and PyTorch to create a machine learning model to recognize handwritten digits

Overview

Recognizing handwritten numbers is a piece of cake for humans, but it's a non-trivial task for machines. Nowadays, with the advancement of machine learning, people have made machines more and more capable of performing this task. We now have mobile banking apps that can scan checks in seconds and accounting software that can extract dollar amounts from thousands of contracts in minutes. If you are interested in knowing how this all works, please follow along with this code pattern as we take you through the steps to create a simple handwritten digit recognizer in Watson Studio with PyTorch.

What is Watson Studio?

Watson Studio is an integrated environment for data scientists, developers and domain experts to collaboratively work with data to build, train and deploy models at scale. If you are new to Watson Studio, the best way to understand it is to see it in action

What is PyTorch?

PyTorch is a relatively new deep learning framework. Yet, it has begun to gain adoption especially among researchers and data scientists. The strength of PyTorch is its support of dynamic computational graph while most deep learning frameworks are based on static computational graph. In addition, its strong NumPy like GPU accelerated tensor computation has allowed Python developers to easily learn and build deep learning networks for GPUs and CPUs alike.

In this code pattern, you will use Jupyter Notebook in Watson Studio and access preinstalled and optimized PyTorch environments through the Python client library of the Watson Machine Learning service, which has a set of REST APIs in its core that allows users to submit training jobs, monitor status, and store and deploy models.

When you have completed this code pattern, you will understand how to:

Flow

architecture

  1. Log into IBM Watson Studio
  2. Run the Jupyter notebook in Watson Studio
  3. Use PyTorch to download and process the data
  4. Use Watson Machine Learning to train and deploy the model

Included components

Featured technologies

Steps

  1. Sign up for Watson Studio
  2. Create a new project
  3. Create the notebook
  4. Create a Watson Machine Learning Service instance
  5. Create HMAC credentials for the Cloud Object Storage instance
  6. Run the notebook
  7. See the results

1. Sign up for Watson Studio

Sign up for IBM's Watson Studio. By creating a project in Watson Studio a free tier Object Storage service will be created in your IBM Cloud account. Take note of your service names as you will need to select them in the following steps.

Note: When creating your Object Storage service, select the Free storage type in order to avoid having to pay an upgrade fee.

2. Create a new project

From the Watson Studio home page, click on the Navigation Menu icon on the top left, expand the Project option, then click on the View all projectstab. Once you land in the My projects page, click on the "New project" button and then select the Create an empty project option.

studio-create-new-project.png

studio-create-new-project-2.png

studio-project-assets.png

3. Create the notebook

From the project dashboard view, select the Add to project tab and click on the Notebook button.

studio-create-notebook.png

Use the From URL tab to create our notebook.

studio-create-notebook-2.png

4. Create or access the Watson Machine Learning Service instance

If you have existing running instance of Watson Machine Learning (WML) Service, you can go to the IBM Cloud Resources page and click on the desired WML service to access the service details.

watson-ml-access.png

If you do not already have a running instance of the WML service, follow these steps to create one.

watson-ml-create.png

Once the service instance is created or you have landed in the service instance page of your choice, navigate to Service credentials, view credentials and make note of them. If you don't see any credentials available, create a New credential.

If you get this error: "You do not have the required permission to assign role 'Writer'. Contact the account owner to update your access." Give yourself writer access by:

watson-ml-creds.png

notebook-add-wml-creds.png

Execute the following steps to associate a WML service to your project:

5. Create HMAC credentials for the Cloud Object Storage instance

To run the notebook available with this pattern, you must create a Keyed-Hashing for Message Authentication (HMAC) set of credentials for your Cloud Object Storage instance.

watson-obj-store-creds.png

watson-obj-store-add-creds.png

watson-obj-store-new-creds.png

notebook-add-wos-creds.png

6. Run the notebook

To view your notebooks, select Notebooks in the project Assets list. To run a notebook, simply click on the edit icon listed in the row associated with the notebook in the Notebooks list.

studio-notebook-list.png

Some background on executing notebooks:

When a notebook is executed, what is actually happening is that each code cell in the notebook is executed, in order, from top to bottom.

Each code cell is selectable and is preceded by a tag in the left margin. The tag format is In [x]:. Depending on the state of the notebook, the x can be:

  • A blank, this indicates that the cell has never been executed.
  • A number, this number represents the relative order this code step was executed.
  • A *, which indicates that the cell is currently executing.

There are several ways to execute the code cells in your notebook:

  • One cell at a time.
    • Select the cell, and then press the Play button in the toolbar.
  • Batch mode, in sequential order.
    • From the Cell menu bar, there are several options available. For example, you can Run All cells in your notebook, or you can Run All Below, that will start executing from the first cell under the currently selected cell, and then continue executing all cells that follow.
  • At a scheduled time.
    • Press the Schedule button located in the top right section of your notebook panel. Here you can schedule your notebook to be executed once at some future time, or repeatedly at your specified interval.

7. See the results

Once the model is trained we can use it to recognize handwritten digits.

Note: With only 1 epoch, the results might be less then perfect.

digits

Sample output

View a copy of the notebook including output here.

Links

Learn more

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ