How to tutorials

How to do image recognition using deep learning in Java

This tutorial shows how to create a simple binary image classifier using a convolutional neural network, that can assign images of Java community mascot, Duke, to one of two possible categories:
1) Duke or
2) Non-Duke image.

Step by Step Instructions

  1. Download the Deep Netts
  2. Download the image data set with examples of Duke and Non-Duke images. Unpack downloaded zip.
  3. Run the Deep Netts development tool (for details you can look at Getting Started doc in the downloaded package)
  4. Create a new Deep Netts project by clicking [File > New Project] in the main menu, enter the project name and click [Next]
  5. Choose [Classify Images] option and click [Next]
  6. Choose [Binary Classification] option (since there are only two categories of images: duke or non-duke image). Click [Next]
  7. Specify image directory to unpacked downloaded image set. Wizard will automatically detect or generate category labels image index. Click Next.
  8. Set image data preprocessing option: resize and center, uncheck invert pixels and upsample to balance classes. These settings are not critical for this small data set, so other settings would work as well. Click [Next]
  9. Specify small convolutional neural network architecture (add 1 convolutional layer with 3 channels that perform image filtering and feature detection, one max-pooling layer that scales down an image, and one fully connected layer that helps with classification with width 16). Other settings are preselected by the expert wizard for this type of task. Click [Next]
  10. Set main training parameters that include network architecture and training/test set split options (or accept settings provided by the expert wizard). Click Next.
  11. Set additional training algorithm parameters (or accept settings provided by the expert wizard ). Click Next.
  12. Run Training by right-clicking training file and then select [Run Training File] menu option. In training dialog check [Visualize Layers] option to be able to observe layer activity during the training. You can also observe network outputs, error and accuracy graphs and inspect detailed logs for each training iteration (aka epoch). You can analyze the training process and test metrics in log (confusion matrix, accuracy, precision, recall). An entire log file will be available for later inspection and comparison in [Training Logs] project folder.
  13. The trained convolutional network is available as a serialized object in [Trained Networks] project folder
  14. To test the classification of the trained model with the entire data set use [Tools > Test Image Set] in the main menu
  15. You can use the trained network in your Java application using Deep Netts library which is also available as a part of the download package.

To use the trained convolutional network in your Java app, use the following Java code:

// Load trained convolutional network
ConvolutionalNetwork trainedNet=  FileIO.createFromFile("trainedNetwork.dnet", ConvolutionalNetwork.class);
// turn on image preprocessing used during the training
((ImagePreprocessing)trainedNet.getPreprocessing()).setEnabled(true);	
// Create image classifier based on trained network and VisRec API
ImageClassifierNetwork imageClassifier = new ImageClassifierNetwork (trainedNet);
// load image to classify
BufferedImage image = ImageIO.read(new File("someImage.jpg"));
// Classify new image and get probabilities for all possible classes
Map<String, Float> results = imageClassifier.classify(image);

Follow the same procedure to create binary classifiers for other image sets, and note that it may require to tune some of the parameters.

Resources

Learn More