fbpx
Pedro Cavaléro Image
Pedro Cavaléro, Engineering Partner at Commit

DJL: Testing Deep Learning Object Detection using Java and Spring Boot

May 4, 2022 in CHOP

Pedro Cavaléro is a Commit Engineer with 18 years of experience in software architecture, and team leadership and enjoys seeing beautifully architected software being used by everyone. He loves spending time with his wife and daughter, going to the movies, and having fun. 

My Commit HOP Project

Deep Learning is a fascinating topic as it unlocks many blockers within software development that would otherwise be close to impossible to achieve.

This project consists of a small web app, created using Java and Spring Boot to run Tensorflow models for Object Detection use case, using a pure Java Deep Learning library created by Amazon, DJL . It allows us to choose a model from tensorflow hub and an image and shows the objects detected in the image.

We all know that Python is the lingua franca (bridge language) for machine learning and Deep Learning. The most important dl engines are Tensorflow, Pytorch and MXNet. DJL can use models created by all these engines in a native way, allowing the data science teams to create the models on the engine they like most and the engineering team can use it directly in their Java projects, calling the models directly.

Spring Boot is the most famous web framework for Java today, supporting many different kinds of architectures such as microservices for example. Integrating this technology with a dl library is a good way to create Deep Learning services for our systems.

Trying different models

This web app will allow testing different tensorflow models that can be found at Tensorflow Hub.

The first part consists in allow getting the model link from tfhub and try a Object Detection in a image you upload to the system:

Copy the url of the model:

And paste it together with some more parameters for object detection:

The web app needs to download the model, put it into memory and then the inference is executed:

Here we can see why it is important to test different models.

Using a model in an optimized way

After a model is chosen or trained, running it requires some configurations that optimize it  to run in a Spring Boot app. It is accomplished by pre-loading the model during app startup time. A new endpoint was created to try this approach (/object-detection-mobilenet) where we preload the mobileNet model and test the detections. 

In a Spring way, preloading means creating a Spring Bean of the model. The model was loaded from ModelZoo using the criteria defined below and then the predictor was created using the prototype scope. This means that unlike the other two models we tried in the previous section (which are created each time it is requested), this new one is created once per application instance leaving only the predictor to be created by request and then closed to avoid any kind of memory leak.

When we try this version, we have the result below:

And if you try it, you will see that it is much faster than the other one.

Containerizing the app

No project can be considered done if it is not containerized. We all know that Spring Boot 2.6 has an inbuilt plugin to create Docker containers from Spring boot apps directly. However, I bumped into an issue  showing that the fonts needed to create the detection boxes were not available in the container.

Therefore, a new customized Dockerfile should have to be created to put the commands to install the fonts and set the environment variables accordingly. The Dockerfile can be found at the Github repo. 

Other categories

Other Deep Learning categories could be tested as well: Image Enhancements, Style Transfer, Action recognition and others. Continuing this project would help me understand more about Deep Learning and its context and much more about the framework.

Conclusion

In this project I could show how to use the Amazon Java framework for Deep Learning, DJL, in a Spring Boot project, trying different models from TensorFlow Hub and containerizing it to make it easy to deploy.

Commit onboarding projects allow us to study some topics that we mostly don’t have the opportunity to apply on a daily basis. It’s a really good way to welcome new employees, bringing them to the community, showcase an individuals work, ask for help and help one another .

Deep Learning is a huge topic and the study must be continuous, but even a marathon starts with the first step!

The github repo of this project can be found here: https://github.com/pedrocavalero/commit-hop-demo 

###

Interested in joining our waitlist? Sign up now. We hire Software Engineers to build their careers with promising early-stage startups. Join the waitlist today!