How to get started with Deep Java Library?

How to get started with Deep Java Library?
How to get started with Deep Java Library?

Deep Java Library, abbreviated as DJL is an open-source library used for building and deploying deep learning models compatible with Java with its large-scale and high-level APIs.

DJL is highly reliable as it is developed and maintained by the Amazon Web Server. Amazon officially announced the release of Deep Java Library (DJL) as an open-source at the AWS re-Invent conference, in the year 2019. The DJL is a library composed of numerous Java APIs used for simplifying, training, testing, deploying, analysing, and predicting data by building deep-learning models.

Java is recognised as one of the widely used programming languages since the late 90s globally, with an extensively amplified developers’ community. Currently, due to its numerous machine-learning resources and deep-learning frameworks, Python tends to compete robustly with the popularity of Java and is recognised as the most used programming language for machine learning.

Usually, developers from the Java background pre-assume that Machine Learning and Deep Learning libraries are not available in Java, but this is a false statement. There are abundant Machine Learning Libraries that are compatible with Java. Developers with Java skillset can easily build deep learning models by including any of the machine learning or deep learning libraries. DJL was introduced with the sole aim of making the deep-learning tools open-source and easily accessible for Java developers, availing well-acquainted object-oriented concepts and APIs.

DJL was particularly designed to be an agnostic framework. Analogous to other Java frameworks and libraries, DJL also stands out as an engine-agnostic framework and agrees with the Java fundamental principle, “Write once, run anywhere”. This allows developers to build their deep learning model using DJL on a specific engine and execute it on any other machine of their choice, such as MXNet, Pytorch, or TensorFlow.

DJL also provides a native Java development experience and comes with a wide range of in-built functions similar to the pre-existing Java libraries and various other machine learning and deep learning scripts. For creating a familiar Java experience and efficient integration of these models with the User Interface of any Java Web Apps or Android Apps.

DJL allows Java developers to use pre-existing IDE (Eclipse/ IntelliJ) to build, train and deploy machine learning models. DJL is compatible with the commonly used Java IDEs or the Jupyter Notebook-based code execution for Java-based Deep learning.

Image Source: Towards Data Science

Using Java Native Access (JNA), DJL abstracts the widely used deep-learning functions that provide the implementation for or Apache MXNet and TensorFlow currently, this is carried out by using the data-mechanisms of Object-Oriented Programming Systems. The abstraction is also carried out with the help of APIs, the main source code is hidden from the user, and the parameters and headers need to be passed to invoke an API.

These elementary APIs abstract the complexity involved in working with Deep learning models and neural networks. For immediate integration of Deep learning models into Java applications, pre-trained models from ModelZoo can be used.

DJL is a super intuitive and easy to use tool kit, it is widely used for developing classification models such as footwear or clothes classifier models. The high-level APIs are easy to train and test. You need to provide a few training samples as images to train your model, basically, teach your model to identify objects. Post-this your model becomes intuitive and can run predictions for testing and classifying objects.

These models are then integrated with e-commerce apps or elsewhere, for optimisation search and providing recommendations to the user. The Machine Learning cycle is so concise that a ninety per cent accurate model can be designed within an hour using DJL. In this article, we will tell you about getting started with Deep Java learning and creating your first pre-trained Deep learning model in a few minutes.

Getting started with Deep Java Library

Object detection can be defined as a computer vision technique for locating instances of objects in images or videos with their respective name tags or labels. In the example given below, we are going to detect person objects, this can be used for focusing the camera lens while a soccer game is being played.

Step One: Setting up the environment

blog banner 1

There are 3 steps for setting up the DJL environment on your system:

  • Install JDK 8 (DJL has some known issues with JDK 11)
  • Install IntelliJ a IDE
  • Import DJL project using IntelliJ

For the detailed installation guide, click the link.

Step Two: Including the repositories and the dependencies in the build. gradle file of your Java App

You must keep in mind that the runtime dependencies for MXNet are distinct for Linux and macOS environments. Refer to the GitHub documentation for clear statistics.

For adding DJL to any of your Java-based application projects, create a gradle project with IntelliJ IDEA and add the code given below to your build.gradle config.

plugins {
	    id 'java'
	}
	
	repositories {                           
	    jcenter()
	}
	
	dependencies {
	    compile "org.apache.logging.log4j:log4j-slf4j-impl:2.12.1"
	    compile "ai.djl.mxnet:mxnet-model-zoo:0.2.0"
	    runtime "ai.djl.mxnet:mxnet-native-mkl:1.6.0-a:linux-x86_64"
	    // Comment above line for osx and un-comment line below
	    //runtime "ai.djl.mxnet:mxnet-native-mkl:1.6.0-a:osx-x86_64"
	}   

Code Source: build.Gradle hosted with by GitHub

Step Three: Import the DJL classes in your source code

For using the DJL libraries in your Java code, you need to import the DJL classes so that you can create the objects and use the prediction function.

import ai.djl.modality.cv.ImageVisualization;
import ai.djl.modality.cv.DetectedObjects;
import ai.djl.modality.cv.util.BufferedImageUtils;
import ai.djl.mxnet.zoo.MxModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;

Step Four: Import the image processing classes in your source code

For processing the image you need to include the image classes for creating an image buffer and accessing the file i/o.

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

Step Five: Write the complete source code for detection of a person object

We add the object classifier code in the main activity and run the prediction model for getting the results. The code given below loads an SSD Model from the model-zoo builds a Predictor from the model and uses the predict function to identify the objects given in the image. A helper utility function is used for adding graphical elements to the image, such as it lays out enclosing boxes around the objects detected by the Java code.

import ai.djl.modality.cv.ImageVisualization;
import ai.djl.modality.cv.DetectedObjects;
import ai.djl.modality.cv.util.BufferedImageUtils;
import ai.djl.mxnet.zoo.MxModelZoo;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.training.util.ProgressBar;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class SimpleSSDExample {
public static void main(String[] args) throws Exception{
// Get image file path
BufferedImage img = BufferedImageUtils
.fromUrl("https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/pose/soccer.png");

//Get resnet model from model zoo.
ZooModel<BufferedImage, DetectedObjects> model =
MxModelZoo.SSD.loadModel(new ProgressBar());
//Create a new predictor from model and predict on image.
DetectedObjects predictResult = model.newPredictor().predict(img);
// Draw Bounding boxes on image
ImageVisualization.drawBoundingBoxes(img, predictResult);
//Save result
ImageIO.write(img, "png", new File("ssd.png"));
model.close();
}
}

Code Source: SimpleSSDInference.java hosted with by GitHub

Step Six: Test the model by providing an input image and then analysing the results

The above code identifies the three players in the input image and saves the result as ssd.png in the working directory of your system.

The input image given to the classifier model for detection
Image Source: Offered under Apache-2.0 license on Gluon-CV
Post training the model it can detect persons as objects
Image Source: Offered under Apache-2.0 license on Gluon-CV

Deep Java Library is not just about identifying and classifying objects; it is about the various software products and paradigms that can be created out of it such as a face mask detection software, a footwear classifier model for e-commerce sites, it can also be used by the security system for identifying any unusual or suspicious objects, by the defense for detecting any vehicles or soldiers.

Machine learning models have a very high computing capacity; if robust APIs are used they can detect high-distant obstacles and objects. In automated cars, deep learning models are used for identifying any sudden obstacle or any other vehicle running on the street.

Nowadays, deep learning java library models are widely been used for developing virtual reality-based games such as racing games or shooting games. Object identification is also used by the camera app for focusing on the object as well as by virtual-reality based snap chat or Instagram filters.

Click on the following links to learn more about DJL:

  • To visit the official DJL library GitHub repository:
    https://github.com/awslabs/djl
  • To learn about a few examples of the DJL library:
    https://github.com/awslabs/djl/tree/master/examples

We assume that you already have a Java IDE on your system, and after reading this article you have hands-on knowledge about deep java library, so don’t delay and quickly get the Deep Java Library set-up on your laptop and create your first classifier model.

If you have any pre-existing Java Web App or Android App, such as an e-commerce app, you can integrate your shoe or dress classifier model with it, to make it, even more, user-friendly. You can read more at the official documentation of DJL, by clicking on the link.

To keep the flow on information going, check out more blogs and visit our course page.

By Vanshika Singolia