Run a Deep Learning Model in Java: A Quick Take

As deep learning use cases expand, here’s how to run a prediction model using a sports example.

artificial intelligence neural network human face picture id1150038843

We are excited to announce the Deep Java Library (DJL), an open source library to develop, train and run deep learning models in Java using intuitive, high-level APIs. If you are a Java user interested in learning deep learning, DJL is a great way to start learning. If you’re a Java developer working with deep learning models, DJL will simplify the way you train and run predictions. In this post, we will show how to run a prediction with a pre-trained deep learning model in minutes.

Before we start coding, we want to share our motivation for building this library. In surveying the deep learning landscape, we found an abundance of resources for Python users. For instance, NumPy for data analysis; Matplotlib for visualizations; frameworks such as MXNet, PyTorch, TensorFlow, and many more. But there are very few resources for Java users, even though it is the most popular language in enterprise. We set out with the goal to provide millions of Java users open source tools to train and serve deep learning models in a language they are already familiar with.

DJL is built with native Java concepts on top of existing deep learning frameworks. It offers users access to the latest innovations in deep learning and the ability to work with cutting edge hardware. The simple APIs abstract away the complexity involved in developing deep learning models, making them easy to learn and easy to apply. With the bundled set of pre-trained models in model-zoo, users can immediately start integrating deep learning into their Java applications.

screen shot 2020 09 30 at 1.02.30 pm AWS

* Other frameworks currently not supported.

DJL by Example

Deep learning is penetrating into enterprise across a variety of use cases. In retail, it’s used to forecast customer demand and analyze customer interactions with chatbots. In the automotive industry, it is used to navigate autonomous vehicles and find quality defects in manufacturing. And in the sports industry, it’s changing the way the game is played with real-time coaching and training insights. Imagine being able to model your opponents moves or determine how to position your team using deep learning models. You can learn about how the Seattle Seahawks uses deep learning to inform game strategy and accelerate decision-making in this article.

In this post, we share an example that struck a chord with the soccer fans on our team. We demonstrate an objection detection model that identifies players from an image using a pre-trained Single Shot Detector model from the DJL model-zoo. You can run this example in both Linux and macOS.

To use DJL with an application project, create a gradle project with IntelliJ IDEA and add the following to your build.gradle config.

screen shot 2020 09 30 at 1.03.28 pm AWS

Note: the runtime dependencies for MXNet are different for Linux and macOS environments. Refer to the GitHub documentation.

We use this soccer image for detection.

screen shot 2020 09 30 at 1.12.43 pm AWS

We run prediction with the code block shared below. This code loads a SSD Model from the model-zoo, creates a Predictor from the model, and uses the predict function to identify the objects in the image. A helper utility function then lays out bounding boxes around the detected objects.

screen shot 2020 09 30 at 1.36.11 pm AWS

This code identifies the three players in the image and saves the result as ssd.png in the working directory.

screen shot 2020 09 30 at 1.20.53 pm AWS

This code and library can be easily adapted to test and run other models from model-zoo. But the fun doesn’t stop there! You can use the Question answering model to train your own text assistant or the image classification model to identify objects on the grocery shelf and many more. Please visit our Github repo for more examples.

Learn more and contribute

In this post, we introduced DJL, our humble effort to offer Java users the latest and greatest deep learning development experience. We demonstrated how DJL can detect objects from images in minutes with our pre-trained model. We provide many more examples and additional documentation on the DJL GitHub repository.

We welcome the community’s participation in our journey. Head over to our Github repository and join our slack channel to get started.




Copyright © 2020 IDG Communications, Inc.