Where Spring Boot Meets Machine Learning Services: A Study

This is an article from DZone's 2022 Enterprise AI Trend Report.

For more:


Read the Report

In 1978, Bell Labs computer scientist Brian Kernighan introduced the most commonly known programming task in history: 

Most commonly known programming task in history

This very simple C program was designed to communicate a system-generated message. The approach continues to be an initial step when learning programming languages, frameworks, and even system integrations. By establishing this basic premise, application code subsequently produces applications that manage intellectual property for the organizations which own them. 

Now, almost 45 years later, the concept of machine learning (ML) has matured in a manner offering a competitive approach to understanding and solving daily business needs. As a subset of artificial intelligence, ML introduces models that are trained so that algorithms can be used to make decisions without being explicitly programmed to do so. 

While Python maintains an edge in the ML space, Java developers have the option of using the Deep Java Library (DJL) to employ ML patterns while leveraging their existing Java knowledge. DJL is an open-source, high-level, engine-agnostic Java framework for deep learning that allows established models to be integrated with existing applications. 

This article will demonstrate how Spring Boot and Java 11+ can be used with DJL to auto-classify images uploaded for a fictional photo competition.

Example Use Case

Consider an animal photo competition held over social media. Contestants simply need to upload a photo and include a specified tag to enter the competition. The most impressive photos for a given set of categories will receive some form of prize. Assume the following animal categories have been established: 

As photos are tagged and uploaded, integration services will retrieve the image and user’s information and then submit the image for auto-classification. This process will be handled by a brand-new service called the machine-learning-service.  

The service will return the following information upon completing the classification request:

Judges will then be able to review the submissions at the category level, allowing them to focus on selecting the best photos. 

Ideal Design

In this example, the service team tasked with creation of the machine-learning-service has a strong background in Java and the Spring Boot framework. The team can build upon existing standards for introducing a RESTful API that can accept binary images that need to be classified. Upon classification of the image, the results will be returned to the API consumer using the following JSON schema: 

JSON schema results returned to API consumer

The only challenge for the service team is to understand how the auto-classification will work. 

Deep Java Library (DJL) In Action

The DJL includes an existing classification model, which should provide the necessary criteria modeling required for the machine-learning-service to utilize to classify contestant submissions. The following logic can be configured as a Java Bean and automatically load when the service starts:

Java Bean

With the criteria established, auto-classification of a given InputStream (image) can leverage the ZooModel provided by DJL using a very small amount of code: 

Auto-classification of given InputStream

The results of the classifyImage() method are returned as a newly created ClassificationDTO object: 

Results of classifyImage()Introducing the Spring Boot Application

Aside from using Spring Boot 2.6.2 and Java 11, the following dependencies are required for the machine-learning-service:

Dependencies required for machine-learning-service

To process the request, the ClassificationController is added, as shown below:

ClassificationController added

The controller will accept an HTTP POST request that includes the binary image and return the ClassificationDTO noted above.

Spring Boot in Action

Starting the machine-learning-service will present the following information to the console:  

Console info from starting machine-learning-service

Figure 1

The CriteriaConfig class successfully loaded the classification criteria, allowing the service to quickly auto-classify images. As an example, the following image was downloaded from Pixabay and renamed to be cat.jpg for simplicity:

Tabby cat image

Figure 2

Using a simple cURL command, the image can be auto-classified using the machine-learning-service:

cURL command to auto-classify image

The Spring Boot service and DJL logs the following messages during processing: 

Messages logged during processing with Spring Boot and DJL

This results in the following JSON response being returned: 

JSON response

The tabby cat classification has the highest probability of a match, which is what the machine-learning-service returns to the requestor. If the DEBUG log level is enabled in the application.yml, all the classification results are logged to the console:

Classification results logged to console

For more information on probability values, please check out the documentation.

Conclusion

Starting in 2021, I have been trying to live by the following mission statement, which I feel can apply to any IT professional: 

“Focus your time on delivering features/functionality which extends the value of your intellectual property. Leverage frameworks, products, and services for everything else.”

– J. Vester

The Spring Boot framework and Deep Java Library fall into line with my personal mission statement. In a very short amount of time, a service was created to completely meet the requirements to auto-classify images — without requiring any knowledge of a new programming language. 

If desired, the DJL could be further employed to train and utilize custom models for use cases where one of the included models is not an ideal match. While additional time is required to understand, introduce, and train custom models, there is still no need to learn a new program language or service-tier framework to meet the needs of the request. 

The full source code for this example is available on GitLab.

This is an article from DZone's 2022 Enterprise AI Trend Report.

For more:


Read the Report

 

 

 

 

Top