Friends of OpenJDK Today

Quick Start with Machine Learning in Java

October 07, 2020

Author(s)

  • Avatar photo
    Zoran Sevarac

    Zoran is an artificial intelligence software developer, entrepreneur, Java Champion, and researcher. He's the creator of the popular and award winning educational Java open source neural network software Neuroph and ... Learn more

So you're a Java developer and you want to do some machine learning. Some of the questions that you might be wondering about are—what can machine learning do for me anyway, which library to use, which algorithms, and is there a common standard API?

Since recently there is a standard API that was created to address exactly these questions. Meet JSR 381, a standard Java API for Visual Recognition using machine learning.

VisRec API was designed to be used for machine learning tasks for Java developers with a minimum background in machine learning and to be intuitive for Java developers getting started with machine learning.

Beside basic visual recognition tasks, such as image classification and object detection, it provides support for common machine learning tasks, such as classification and regression.

Since it is an official Java technology standard, multiple implementations are possible and currently there are two available:

Here is an example of Java code based on the VisRec API to build and use a classifier. Without any explanations, it should be clear to you what is happening:

ImageClassifier<BufferedImage> classifier = 
NeuralNetImageClassifier.builder()
   .inputClass(BufferedImage.class)
   .imageHeight(28)
   .imageWidth(28)
   .labelsFile(dataSet.getLabelsFile())
   .trainingFile(dataSet.getTrainingFile())
   .networkArchitecture(new File("mnist.json"))
   .modelFile(new File("mnist.dnet"))
   .maxError(1.4f)
   .maxEpochs(100)
   .learningRate(0.01f)
   .build();

BufferedImage image = ImageIO.read(new File(input.getFile()));
Map<String, Float> results = classifier.classify(image);

For detailed step-by-step instructions and examples, see the getting started guide.

Related Articles

View All

Author(s)

  • Avatar photo
    Zoran Sevarac

    Zoran is an artificial intelligence software developer, entrepreneur, Java Champion, and researcher. He's the creator of the popular and award winning educational Java open source neural network software Neuroph and ... Learn more

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard