DJL: Deep Java Library and How To Get Started

Written by dhruveshpatel | Published 2020/10/25
Tech Story Tags: open-source | aws | tech | deep-learning | java | digital-transformation | cloud-computing | agile-mindset

TLDR DJL: Deep Java Library is an open-source library to build and deploy deep learning models in Java. DJL is built by AWS and is open source. It provides a native Java library & expedite machine learning and deep learning journey. Developers can use their existing Eclipse/ IntelliJ to build, train and deploy models and DJL makes it easy to integrate these models with Java Apps. We will learn how to implement inference code with a ModelZoo model to detect dogs in an image.via the TL;DR App

Want to get your hands dirty with Machine Learning / Deep Learning, but have a Java background and not sure where to start? Then read on! This article is about using an existing Java skillset and ramp-up your journey to start building deep learning models.

What is DJL?

DJL is an open-source library to build and deploy deep learning in Java. DJL is built by AWS and is open source.
DJL provides a native Java development experience and functions like any other regular Java library & expedite machine learning and deep learning journey. Developers can use their existing IDE (Eclipse/ IntelliJ) to build, train and deploy models and DJL makes it easy to integrate these models with Java Apps.
The simple APIs abstract away the complexity involved in developing Deep learning models. With the bundled set of pre-trained models from ModelZoo, users can immediately start integrating Deep learning into their Java applications.
DJL is engine-agnostic and goes with Java fundamental "Write once, run anywhere". A developer can develop a model using DJL and run it on an engine of your choice - MXNet, Pytorch, TensorFlow.

DJL Example for Object Detection

Object detection is a computer vision technique for locating instances of objects in images or videos.
In this example, we will learn how to implement inference code with a ModelZoo model to detect dogs in an image. Source code for this model is here.
Local Software Setup
  1. Install JDK 8 (DJL has some known issues with JDK 11)
  2. Install IntelliJ a IDE
  3. Import DJL project using IntelliJ
For Java source code for ObjectDetection.java class, which is using DJL can be found here.

Run the Object Detection Example:

Input image is used as below:
Build the project and run
Use the following command to run the project:
cd examples
./gradlew run -Dmain=ai.djl.examples.inference.ObjectDetection
Output should look like the following:
[INFO ] - Detected objects image has been saved in: build/output/detected-dog_bike_car.png
[INFO ] - [
          	class: "car", probability: 0.99991, bounds: [x=0.611, y=0.137, width=0.293, height=0.160]
          	class: "bicycle", probability: 0.95385, bounds: [x=0.162, y=0.207, width=0.594, height=0.588]
          	class: "dog", probability: 0.93752, bounds: [x=0.168, y=0.350, width=0.274, height=0.593]
          ]
An output image with bounding box will be saved as build/output/detected-dog_bike_car.png:

Conclusion

Within Deep learning landscape, there are lots of resources for Python users like NumPy for data analysis or frameworks such as MXNet, TensorFlow. But very few sources for Java. Thanks to the AWS team, DJL provides open-source tools to train and serve Deep learning models in Java and make it easy to start your deep learning journey.
Happy coding!
Please like, share and comment with your views/opinions.
Disclaimer: This post is my personal opinion and does not reflect those of any of my past, present or future employers or affiliations.

Written by dhruveshpatel | A seasoned Cloud Solution Architect and hands-on Cloud-native backend developer
Published by HackerNoon on 2020/10/25