The Watson Visual Recognition service can be trained to recognize your own custom classes in images. In this lab you'll learn how to train a Watson Visual Recognition custom classifier to recognize wedding scenes using 50 images of weddings to serve as positive examples and 50 images that are not weddings to serve as negative examples.
After creating the custom classifier, you'll test it with different images not used to train it to validate it's accuracy.
-
A space in IBM Cloud US South or United Kingdom regions.
As of 2/5/2018, the Machine Learning service on IBM Cloud is only available in the US South or United Kingdom regions.
Watson Studio is your IDE for various Watson Services, Machine Learning and Data Science, combining opensource tools, and libraries into a unified Cloud based platform for discovering and sharing insights. For this lab we're using the Visual Recognition tooling to create an test a custom classifier.
-
In your browser go to the IBM Cloud Dashboard and click
Catalog
. -
In the navigation menu at the left, select
AI
and then selectWatson Studio
.
- Verify this service is being created in the
Dallas region
, and you've selected thelite/free
pricing plan.
Note the lite/free
plan only allows you to add a single user to your project, and is limited in the compute capacity hours. More details on limits and how to monitor usage is available in the documentation.
-
Click
Create
-
Launch your newly created Watson Studio Environment by clicking
Get Started
IBM Watson Studio is a collaborative environment with AI tools that you and your team can use to collect and prepare training data, and to design, train, and deploy machine learning models.
Ranging from graphical tools you can use to build a model in minutes, to tools that automate running thousands of experiment training runs and hyperparameter optimization, Watson Studio AI tools support popular frameworks, including: TensorFlow, Caffe, PyTorch, and Keras.
You can think of Watson Studio AI tools in four categories:
- Visual recognition
- Natural language classification
- Machine learning
- Deep learning
Documentation is available here
- Projects - Organize resources used when working with data; here you see your most recently updated projects
- Toos - Quick links to commonly used Data Science and ML Tools including RStudio, Data Refinery, Jupyter Notebooks, or a Visual Neturl Network Model Builder
- Catalog - Create and manage data policies for managed or connected data resources
- Community - Links to the best content found by IBM Data Scientists, including example notebooks, datasets, and tutorials
- Services - Create Watson, data, and compute services and connections. Such as Watson Visual Recognition, or Apache Spark
- Manage - Account wide configuration, including Anaconda environments, security, catalogs, billing
- Hamburger Menu - Access to IBM Cloud dashboard and tools
- IBM Studio Menu - Quick link to the Watson Studio welcome page
- Account Profile and Settings - Personal account settings
A project is how you organize your resources to achieve a particular goal. Your project resources can include data, collaborators, and analytic assets like notebooks and models. Projects depends on a connection to object storage to store assets. Each project has a separate bucket to hold the project's assets.
- From the Watson Studio dashboard getting started display, click on
Create Project
, orNew Project
- Select project type. There are many different tools built into Watson Studio and multiple views are built to simplify the features shown to users. Select the
Visual Recognition
and clickCreate Project
.
-
A Watson Studio Visual Recognition project depends on an instance of the Watson Visual Recognition Service. If you don't already have an instance of Watson Visual Recognition created you can create a new instance of the service directly from the New Project dialog. Under
Define Watson Visual Recognition
clickAdd
. In the Watson Visual Recognition service creation menu, accept the default options and click onCreate
. -
With the Visual Recognition service instance created, click
Refresh
allowing Watson Studio to discover the newly created service. Enter Wedding Detection as the project name and clickCreate
. -
From within the new project click on the pencil icon next to the default name of the new custom classifier and change the name to
Weddings
In the panel on the right labeled Upload to project , click on Browse
to upload the image file you'll use to create a classifier. Select the file weddings50.zip in the training_data sub folder of this project.
Repeat for the file notweddings50.zip in the training_data sub folder of this project.
- Click on
Create a class
and name the class wedding.
- Select the file wedding50.zip at the bottom right of the UI and drag it unto the tile created for the
wedding
class you just created. It will take a few seconds for all 50 images in the file to be processed.
Repeat for the file notwedding50.zip dropping it onto the tile for negative examples
- Click on the
Train model
button
It will take a few minutes to train the model. Now would be a good time for a cup of coffee.
- Click on the link to the testing tool
- Click on the
Test
tab
- Click on the
browse
link and select a file in test_data/wedding sub folder of this project.
The results will indicate how confident your classifier is that the image you selected is a wedding picture. (1.0 being the highest confidence and 0 being the lowest).
- Click on
Clear results
. Click on thebrowse
again and select a file in test_data/notwedding sub folder of this project. Generally the confidence for non wedding pictures should be significantly lower.
-
Switch over to the
Overview
tab -
Click on the icon next to the
Model ID
to copy it to the clipboard
-
Create a text file and paste the copied Model ID. Leave the text file open.
-
From the menu select
Services->Watson Services
-
In the
Visual Recognition
section click on the name of your service instance -
Click on the
Credentials
tab
- Click on the icon to display the service credentials and then copy the
apikey
value to the text file you opened earlier.
This will test the classifier using 60 test images via a standalone app. There are two apps to chose from: Python or Java.
You can install Python/Java locally or use Docker
If you use Docker you can run either app without additional software installs.
Docker requirements: Docker for your platform
If you really don't want to install anything locally, you can always use the online tool: http://play-with-docker.com
If you don't use Docker then you'll need one of the following installed locally
Python:
- Python 3.5 or later.
Java:
- Java 1.7 or later JVM
- Gradle Build Tool Note: Version 4.7 was used to verify this example
-
Edit the file settings.py Edit the file settings.properties in the watson-vr-python-tester sub folder of this project. Put in the values of your apikey and Model ID that you saved earlier. Note the Model ID is referred to as the CLASSIFIER_ID in the settings file.
-
In a command prompt or terminal navigate to the watson-vr-python-tester sub folder of this project.
If using Docker (not locally instally Python), run the following commands to get a bash shell in a Python environment before running the commands in the next step.
docker run -it --rm -v "$(cd ../ && pwd):/repo" python:3.5-jessie bash
cd /repo/watson-vr-python-tester
Run the subsequent commands from your command prompt to install and run the app
pip install -r requirements.txt
python watsonvr-wedding-tester.py
Verify the app runs without errors and the output looks like the following.
Classifying 60 test images ...
Finished classifying 10 images
Finished classifying 20 images
Finished classifying 30 images
Finished classifying 40 images
Finished classifying 50 images
Finished classifying 60 images
Number of files 60
True positives 27
True negatives 30
False positives 0
False negatives 3
Accuracy 95.00%
Recall 0.90
Precision 1.00
F1 Score 0.95
False negative list
../test_data/wedding/0006.jpg
../test_data/wedding/0029.jpg
../test_data/wedding/0023.jpg
Look at the files that were reported as false positives or false negatives and see if you can guess why the classifier had problems with these particular images.
If you are using Docker, exit the container by running the command exit
in the command prompt
-
Edit the file settings.py Edit the file settings.properties in the watson-vr-java-tester/src/main/resources sub folder of this project. Put in the values of your apikey and Model ID that you saved earlier. Note the Model ID is referred to as the CLASSIFIER_ID in the settings file.
-
In a command prompt or terminal navigate to the watson-vr-java-tester sub folder of this project.
If using Docker (not locally instally Java), run the following commands to get a bash shell in a Java+Gradle environment before running the commands in the next step.
docker run -it --rm -v "$(cd ../ && pwd):/repo" gradle:slim bash
cd /repo/watson-vr-java-tester
Run the subsequent commands from your command prompt to build and run the app
./gradlew build
./gradlew run
gradlew.bat build
gradlew.bat run
Verify the app runs without errors and the output looks like the following.
Classifying 60 test images ...
Finished classifying 10 images
Finished classifying 20 images
Finished classifying 30 images
Finished classifying 40 images
Finished classifying 50 images
Finished classifying 60 images
Number of files 60
True positives 27
True negatives 30
False positives 0
False negatives 3
Accuracy 95.00%
Recall 0.90
Precision 1.00
F1 Score 0.95
False negative list
test_data/wedding/0006.jpg
test_data/wedding/0029.jpg
test_data/wedding/0023.jpg
Look at the files that were reported as false positives or false negatives and see if you can guess why the classifier had problems with these particular images.
If you are using Docker, exit the container by running the command exit
in the command prompt
Congratulations ! You successfully created a classifier to detect wedding pictures. With just 50 examples and 50 negative examples your were able to quickly created a classifier that is approximately 95% accurate on some randomly selected test examples.