From the course: Foundations of AI and Machine Learning for Java Developers

Sample VisRec code to train a PredAI model - Java Tutorial

From the course: Foundations of AI and Machine Learning for Java Developers

Sample VisRec code to train a PredAI model

- [Instructor] Let's take a look at how to use JSR 381 with an example. We're going to see if we can create a simple model that can detect if an image is of a light-colored chihuahua or not. This is called a binary classifier because there are two possibilities: a light-colored chihuahua, or not a light-colored chihuahua. So we need to create a model first. Once the model is trained, then we can use that model to classify new images. So to train the model, we need two datasets. One dataset contains images of light-colored chihuahuas, and the other dataset contains images that are not light-colored chihuahuas. Since there are two choices, we call this a binary classifier, which is a very, very common use case. You can certainly have applications that have several classifiers, but let's try a simple binary classifier's example first. Before we write any JSR 381 Java code, we need to prepare our data sets. Now, Stanford University has a very good set of open source dog images, so let's use those. I chose a very small set, 872 images of light-colored chihuahuas from that dataset. Those images are in one folder called Chihuahua, and there's another collection of images that are not light-colored chihuahuas. That folder is called negative. We also need two helper files. One is a very small labels file that identifies the two target classes. In this case, it just says chihuahua and negative. Another helper file is our index file. This file contains all the path names of the images that we use in our training, and the type of image each is that is its label. The third file is called the network architecture file. This file describes the neural network architecture. For most binary classifiers, we can use a default architecture. Now that we have the data sets and administrative files prepared, let's look at the JSR 381 Java code. To create a model with JSR 381, the necessary code is very short. There's only one builder method needed to create the model. That's the beauty of JSR 381. It allows you to build a machine learning model. By using the simple builder pattern, which all Java developers are familiar with. We want our application to create an image classifier since we're dealing with images, specifically, we want to classify buffered images. Notice that image classifier is an interface. It can classify other categories, but here we are classifying buffered images for visual recognition using a neural network image classifier. We also specify the dimensions of the images, which is a necessary step. Here we use 64 by 64 pixel images, so all the images will be scaled to these dimensions before they're fed into the model. Next is the path name of the labels file which contains the labels of the images that will be used for training and image recognition. This file is very small, for our binary class file contains two lines. One line says Chihuahua, and a second line says negative. Now, we have to give JSR 381 a list of path names of all the images that will be used for training along with their labels. Then we specify an architecture for our neural network. In our example, this network is a convolutional neural network, and we are using the JSR reference implementation based upon the open source Deep Netts Community Edition. This file contains parameters of the underlying convolutional type of neural network. Now, the details of this file are beyond the scope of this course, but most of the parameters work well out of the box. You can find several detailed explanations of neural network architecture explanations online. For example, on a Deep Netts website. Of course, we want to specify the name of the file to which we want to export the model. We'd need this so we can use the model later on. Now, we need to specify the target error that is acceptable. The training procedure is an iterative procedure where the neural network inspects the image, then it repeatedly tunes the internal parameters in order to minimize the recognition error. Max error is the maximum acceptable error threshold. When the error falls below this value, the network is considered to be trained. You can also specify the maximum iterations to use. Training iterations are sometimes called epochs. The last parameter is called the learning rate parameter, and it's a bit tricky to set. It specifies the amount of error that will be used to adjust the internal parameters. It's also beyond the scope of this course, but typically, this is set to a very small number.

Contents