Watson on the IBM Cloud lets you integrate artificial intelligence (AI) into your applications and store, train, and manage your data in a secure cloud. With Watson Visual Recognition, you can quickly and accurately tag, classify, and train visual content using machine learning.

Learning objectives

In this tutorial, you’ll learn how to integrate Watson services in your iOS application and how to use Core ML APIs to enable artificial intelligence features directly on a device.

Prerequisites

To use this tutorial, you’ll need:

  • MacOS 10.11 El Capitan or later
  • The latest version of Xcode
  • iOS 11 or later (on your iPhone or iPad if you want the application to be on your device)
  • CocoaPods
  • An IBM Cloud account

Note: If you don’t want to deploy the model to an iOS device, the only requirement is an IBM Cloud account.

Estimated time

It should take you approximately 30 minutes to complete this tutorial.

Steps

Prepare your data

To create the model used in this tutorial training images must be provided to the classifier. To save time, we prepared the data for you.

example_training_images

Each class that you create should have at least 10 images. However, for optimal training time and accuracy it’s best to have approximately 200 images sized at 224 x 224 pixels. Also, it’s a good practice to have images from an environment comparable to what you expect to classify. For example, because this is an iOS app, photos from your smartphone camera are probably ideal verses professional photos or image search results. Also, try and change the background, lighting, and any other variables that you can think of!

Get the training data

Download the training image data set and unzip the contents to your desktop.

If you’d like to take your own training photos feel free to do so. Again, make sure to have at least 10 photos for each classification. Then, prepare your data by classifying each image.

  1. Create an empty folder for each type of cable

    classified_images

  2. Sort through your images, putting each in the correct folder based on the type of cable.

  3. After all of your images are in the correct folder, archive each folder to create individual .zip files.

    Note: On OS X, “compress” is used to create a .zip file.

    classified_images_final

Register your accounts

Watson Studio

To register your account on Watson Studio:

  1. Go to IBM Cloud and sign up for a free account or log in.

Note: Select United States as your region.

Apple ID

To register your account with an Apple ID:

  1. Go to https://appleid.apple.com.
  2. Follow the steps to log in with your username and password or create a new account.
    • If you already know your Apple ID username and password, you can skip this step.
    • If you do not want to deploy the model to an iOS device, the only requirement is an IBM Cloud account.

Create a new app

After you’ve created an account, navigate to the Starter Kit landing page. We have a large catelog of starter kits that come pre-configured with Watson Services – it’s a simple way to start any project! To get started, click Create app.

Watson_welcome_screen

From here, you can name your apps and select the pricing plan for your visual recognition service – select Lite (the free tier) for this app.

Now, you want to create a new project, so click Create.

new_project_screen

On the back end, the starter kit is allocating the propper services and preparing the workspace. Now you’re ready to navigate to the Visual Recognition dashboard. Because the starter kit already allocated the service, you can click Launch Tool. Launching the tool opens a new tab, but make sure to keep both open.

choices_screen

Now you’ll begin creating your custom model. Start by clicking Create Model within the box labeled Classify Images.

visual_recognition_landing

The first time you create a custom classifier, it prompts you to create a new project. To create custom models, you will use Watson Studio, so you must first create the project where your models and training images will live.

Name your project and click Add to add a Cloud Object Storage instance to your project. Object Storage is used to store all of your training images

object_storage

Click Create.

Create a model

The next part of the process is creating your own custom visual recognition model. First, you can rename your model by clicking on the name and typing your own name. Now, you must upload your training images. On the right side, click Browse, and navigate to the location where you dowloaded the training images. Select and upload the images by clicking Open.

upload_training_images

training_area

Next, we’ll create a new class for each type of connector. We’ll do this by dragging the ZIP files from the side panel onto the Create a class box, a new class will be created using the name of the ZIP file. Alternatively, we can click Create a class, define a name for the class, and then drag the correct files to each defined class.

drag_zip_files

drag_zip_files

After all your classes are complete, and you are ready to train your model. Press Train Model.

train_model

The training time can vary depending on the amount of training data. A good approximation is a few seconds per image. Because you have approximatey 200 images, you can expect the model to take 5-10 minutes to train.

Get the code

After the training is complete, navigate back to the tab with the app starter kit page where you began.

choices_screen

You’ll see in the top-right corner, a blue button labeled Download Code. An iOS app with starter code is generated, and it will even grab your Visual Recognition credentials as well as custom classifier information.

After the app is finished generating the code, save the zip file to your computer and unarchive it.

Note: Because I named the app My First Custom Model, the directory is named accordingly. If you named it something else, the following steps must be changed to reflect the name you chose.

  1. From Launchpad, search for terminal and click the icon to open the application.

    terminal

  2. Change to the directory with the following command.

     bash
     cd MyFirstCustomModel-Swift
    
  3. Get the Watson SDK by running the following command.

     bash
      pod install
    

    watson_sdk

After the dependencies have been built by CocoaPods, no more actions are needed. The unique credentials to your Watson Visual Recognition service have been injected into the application during the code generation.

Test the application

Now you’re ready to test your application. First, you’ll make sure the app builds on your computer. To do this, open the MyFirstCustomModel.xcworkspace file (it’s important to not open the .xcodeproj file).

open_file

This opens the Xcode project. One thing you’ll need to change is the Bundle Identifier to something unique. Try adding .lastname to the end, using your last name.

general_tab

Running the app

  1. To run in the simulator, choose a phone simulator from the drop-down menu at the top and click Build and then Run (the play icon).

phone_option

The app should look like this if all goes well.

running_sim

If there are any issues with the build, check the General tab for errors. You might have to sign in with your Apple ID to sign the app. Depending on simulators installed on your computer, you might also have to change the Deployment Target as well.

Deploy the app to an OS device

To deploy the app:

  1. Select the project editor (the name of the project with a blue icon).

  2. Under the Signing section, click Add Account.

    add_account

  3. Log in with your Apple ID and password.

    apple_id

    You should see a new personal team created.

  4. Close the Preferences window.

Now you must create a certificate to sign your app with.

  1. Select the General tab.
  2. Select the personal team that was just created from the Team menu.
  3. Plug in your iOS device.
  4. Select your device from the device menu to the right of the Build and run icon.
  5. Click Build and run.
  6. On your device, you should see the app appear as an installed app.
  7. When you try to run the app the first time, it will prompt you to approve the developer.
  8. In your iOS settings, navigate to General > Device Management.
  9. Tap your email, and tap trust.

Now you’re ready to run the app!

You should see something like the following image after you’ve run the app and tested it by taking a picture.

final_image

Summary

You now have the tools necesary to build your own application using Watson Visual Recognition and Core ML. To extend this example, you could also create multiple custom models, each to identify another aspect of the items that you want to classify. For example, you could build a model to identify “electronic accessories,” then another to identify the “type of cable” or “type of hardware.” Have fun!