Deep Learning with Spring Boot and DJL
Deep Learning with Spring Boot and DJL
In this tutorial we review how to create a sample Deep Learning Java app using Spring Boot, DJL and Tensorflow.
Join the DZone community and get the full member experience.Join For Free
This is another post on Spring Boot that will show how to build a sample web application using Deep Java Library (DJL), an open-source Deep Learning library for Java to diagnose COVID-19 on X-ray images.
The sample app is a Spring Boot based version of DJL's similar COVID-19 example and it has a simple static HTML page built using Twitter Bootstrap and JQuery where users can submit an image URL to a REST api where the DJL library will download the image and predict if it's an X-ray image of lungs infected with COVID-19 or not.
The link to the source code is included at the end of this post.
Disclaimer: This is only a demo application based on the dataset at https://github.com/ieee8023/covid-chestxray-dataset and it SHOULD NOT be used for actual medical diagnosis.
Deep Java Library
As mentioned earlier, DJL is a Java-based library that supports multiple
Deep Learning frameworks like Apache MxNet, PyTorch and Tensorflow. Since most Deep Learning engines are built using Python and not in Java, DJL built engine adapters to access each of these engines’ native shared library.
DJL does it in an elegant way making it dead simple to switch from one framework to the other depending on the use case.
The app needs the Spring Boot web starter:
And the commons-io library for some basic I/O operations:
The Lombok library, too, as I'm too lazy to write the getters and setters:
And finally the DJL dependencies for this sample app:
Here's the list of Maven properties needed for the DJL dependency versions:
main() method will fire up the Spring Boot application and it looks like most other Application class files:
In order to configure the DJL library, let's create a
DjlConfig class with the
This class will define a
ZooModel Spring Bean that will help predicting if the submitted image URL belongs to a COVID-19 infected lung:
What this code says is that we create a ZooModel object with a
BufferedImage input and
Classifications (more on that later) output type and it uses an
XrayTranslator object to transform the input images to a format needed by the Deep Learning model to function properly.
Here's the code for the
XrayTranslator which is an inner class within
Covid19Service class will handle the business logic to diagnose the X-ray images and as you'll see, surprisingly, it's really just few lines of code:
ZooModel bean created in the
DjlConfig class is autowired and used in the
diagnose()method that has an
Within the method we create a
Predictorobject using the
try-resource block (as the predictor needs to be closed after execution) and use it to run the
BufferedImage(created using the
imageUrl parameter) through a pre-trained Tensorflow model.
For more details on the model visit: https://www.pyimagesearch.com/2020/03/16/detecting-covid-19-in-x-ray-images-with-keras-tensorflow-and-deep-learning/.
diagnose()method is run, the
Classificationsresult object will show if the lungs on the X-ray image were infected with COVID-19 or not and with what probability.
This controller class defines the REST API to diagnose X-ray images which will be consumed by our simple front-end app:
@RestControllerannotation tells Spring that in our MVC design this is a Controller bean that defines the REST api
@RequestMappingannotation tells Spring that paths of all REST apis within this class should be prefixed with
/api/v1and all REST apis will return
Covid19Servicethat we discussed earlier is autowired in the constructor and later on used by the
diagnose REST api at the
The diagnose api takes an
imageUrlrequest param and returns a JSON document with the String representation of the results.
The Spring Boot app has a simple static
index.html file as a front-end client for the diagnose REST API and it uses Twitter Bootstrap for the responsive design and JQuery to make the REST API call:
The file has an HTML form that can capture an X-ray image URL from the user:
Once the form is submitted, the REST API may take a while to respond. In the meantime the page will show a spinner and once the response is received, the text will be displayed within the
When the form's submit event is triggered, the code gets the
imageUrl value, show's the spinner, clears the content of the
diagnose div from previous runs and calls the diagnose REST api with the
In case of a successful response the code hides the spinner and displays the results within the
In case of an error the code also hides the spinner and displays a generic error message.
Running the app
The app needs Tensorflow to be downloaded first before it can be run.
Run the following command in the projects root folder:
Then visit http://localhost:8080/index.html to get diagnose on X-ray image URLs. Sample images to use:
In this tutorial, we reviewed how to create a sample Deep Learning Java app using Spring Boot, DJL, and Tensorflow.
The source code for the post is available at https://github.com/davidkiss/djl-spring-boot-xray.
Published at DZone with permission of David Kiss , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.