Developers guides

Image Recognition in Java Using Deep Learning

Image Recognition in Java
This tutorial shows you how to build simple image recognition aka binary image classifier in pure Java. Image classifier uses deep learning to learn from examples how to automatically put images into appropriate categories / folders. In this tutorial we’ll use example images of food to build classifier that is able to distinguish hot from not hot dog images. See this funny video from TV Show Sillicon Valley to get the basic idea. Binary image classification means that the algorithm performs two class yes/no classification of images.

For the impatient GitHub repo with full example and data set is available here

In order to run the example you need to download and install Deep Netts.

Image Recognition in Java

The following code loads images from a file system, and prepares them for neural network training.

        ImageSet imageSet = new ImageSet(imageWidth, imageHeight, dataSetPath);

Convolutional neural networks are specialized for image recognition tasks, and the following code creates small convolutional network which is configured to perform binary image classification.

        ConvolutionalNetwork convNet = ConvolutionalNetwork.builder()
                .addInputLayer(imageWidth, imageHeight, 3)
                .addConvolutionalLayer(3, Filter.ofSize(3))
                .addMaxPoolingLayer(2, 2)
                .addOutputLayer(1, ActivationType.SIGMOID)

To train neural network using example/training images you need to run the backpropagation training algorithm using loaded image set using the following code:

        BackpropagationTrainer trainer = convNet.getTrainer();

To see how accurately neural network is able to recognize images perform the following test:

        EvaluationMetrics testResults = convNet.test(imageSet);

To save a trained neural network into file, to be able to use it in your app just call the save method and specify name of the file to save the network into:

      // SAVE THE MODEL - save trained neural network to file"HotDogClassifier.dnet");

To use the trained image classifier in your app use the following code snippet:

        // load image
        BufferedImage image = File("hotdog-dataset/hot_dog/7896.jpg")).getSubimage(0, 0, 64, 64);

        // load trained convolutional network
        ConvolutionalNetwork convNet = ConvolutionalNetwork.load("HotDogClassifier.dnet", ConvolutionalNetwork.class);
        // instantiate image classifier using convolutional neural network
        ImageClassifier<BufferedImage> imageClassifier = new ImageClassifierNetwork(convNet);
        // classify image and get results
        Map<String, Float> results = imageClassifier.classify(image); // result is a map with image labels as keys and coresponding probability


Full source of the example

Deep Netts download

Step by step guide for installing Deep Netts