Three minute experience: SpringBoot recognizes numbers with a deep learning model

Posted by angryjohnny on Tue, 07 Dec 2021 12:16:45 +0100

Overview of this article

  • Can the model trained with deep learning 4J be used in java applications?
  • Of course, today we spend three minutes to experience the SpringBoot application integrating AI capabilities
  • The function of the application is to recognize handwritten digits in black-and-white pictures (there is only one digit in each picture). As shown in the figure below, submit the picture through the http interface for SpringBoot application to recognize:
  • The following figure is a screenshot of the postman operation interface. The number in the red box is the recognition result of the SpringBoot application. The number 8 is consistent with the picture:
  • The AI model used in SpringBoot application is LeNet-5, which is a classic recognition model, which is often used in the introduction of convolutional network
  • This article focuses on experience and does not involve specific development. Later, an article will introduce the complete development process (including source code)

environmental information

  • In order to simplify the experience process, docker will be used next. The recommended environment information is as follows:
  1. Operating system: Ubuntu 16.04.1 LTS Server Version (MacBook Pro, version 11.2.3, macOS Big Sur)
  2. docker: 20.10.2 Community
  3. In order to speed up the download of docker images, it is recommended that you configure docker acceleration in advance
  4. http tool for submitting pictures. This is postman
  • The title of the article is called three minute speed experience. You don't have time to say too much. You can start quickly when you are ready for the environment

deploy

  1. Create a new directory named images to store the processed files. My full path here is / home/will/temp/202106/29/images
  2. Create a new directory named model to store the model files to be downloaded later. My full path here is / home/will/temp/202106/29/model
  3. To download the trained model file, I have prepared two download addresses. You can choose one, and the other is CSDN (no points required): https://download.csdn.net/download/boling_cavalry/19881160, the other is https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/minist-model.zip
  4. The downloaded model file is minist-model.zip. Do not unzip it, but directly put it into the newly created model directory
  5. Execute the following command to download the docker image file before creating the container:
docker run \
--rm \
-p 18080:8080 \
-v /home/will/temp/202106/29/images:/app/images \
-v /home/will/temp/202106/29/model:/app/model \
bolingcavalry/dl4j-model-app:0.0.3
  1. The image file is a little large (more than 900 megabytes...), please wait patiently, mainly because dl4j's dependent library is too large
  2. When the console outputs the following contents, it indicates that the startup is successful and the model is loaded successfully:
2021-06-29 10:51:55.744  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1748 ms
2021-06-29 10:51:55.852  INFO 1 --- [           main] c.b.p.service.impl.PredictServiceImpl    : load model from [/app/model/minist-model.zip]
2021-06-29 10:51:55.950  INFO 1 --- [           main] org.nd4j.linalg.factory.Nd4jBackend      : Loaded [CpuBackend] backend
2021-06-29 10:51:58.397  INFO 1 --- [           main] org.nd4j.nativeblas.NativeOpsHolder      : Number of threads used for linear algebra: 1
2021-06-29 10:51:58.399  WARN 1 --- [           main] o.n.l.cpu.nativecpu.CpuNDArrayFactory    : *********************************** CPU Feature Check Warning ***********************************
2021-06-29 10:51:58.399  WARN 1 --- [           main] o.n.l.cpu.nativecpu.CpuNDArrayFactory    : Warning: Initializing ND4J with Generic x86 binary on a CPU with AVX/AVX2 support
2021-06-29 10:51:58.399  WARN 1 --- [           main] o.n.l.cpu.nativecpu.CpuNDArrayFactory    : Using ND4J with AVX/AVX2 will improve performance. See deeplearning4j.org/cpu for more details
2021-06-29 10:51:58.399  WARN 1 --- [           main] o.n.l.cpu.nativecpu.CpuNDArrayFactory    : Or set environment variable ND4J_IGNORE_AVX=true to suppress this warning
2021-06-29 10:51:58.399  WARN 1 --- [           main] o.n.l.cpu.nativecpu.CpuNDArrayFactory    : *************************************************************************************************
2021-06-29 10:51:58.407  INFO 1 --- [           main] org.nd4j.nativeblas.Nd4jBlas             : Number of threads used for OpenMP BLAS: 1
2021-06-29 10:51:58.411  INFO 1 --- [           main] o.n.l.a.o.e.DefaultOpExecutioner         : Backend used: [CPU]; OS: [Linux]
2021-06-29 10:51:58.412  INFO 1 --- [           main] o.n.l.a.o.e.DefaultOpExecutioner         : Cores: [32]; Memory: [7.0GB];
2021-06-29 10:51:58.412  INFO 1 --- [           main] o.n.l.a.o.e.DefaultOpExecutioner         : Blas vendor: [OPENBLAS]
2021-06-29 10:51:59.076  INFO 1 --- [           main] o.d.nn.multilayer.MultiLayerNetwork      : Starting MultiLayerNetwork with WorkspaceModes set to [training: ENABLED; inference: ENABLED], cacheMode set to [NONE]
2021-06-29 10:51:59.658  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-29 10:51:59.671  INFO 1 --- [           main] c.b.p.PredictNumberApplication           : Started PredictNumberApplication in 6.474 seconds (JVM running for 7.235)
  • Now that the SpringBoot application has started successfully, let's try the function of identifying pictures. This application provides two interfaces, which can test black words on a white background and white words on a black background

Identify black words on white background

  • Prepare a png picture with black characters on a white background (drawing board and screenshot tool can be used), such as the following one:
  • The address of the black character recognition service on a white background is the IP address: 18080 / predict with white background. The postman operation is shown in the figure below. Please operate 1-6 in numerical order. It can be seen that the input is very simple. There is only one field, and the return value is the recognition result, which is in line with the expectation:

Recognize white words on black background

  • Next, try white characters on a black background and prepare a png picture similar to the following figure:
  • The address of white character recognition service on black background is IP address: 18080 / predict with black background. The operation with postman is as shown in the figure below. Please operate 1-6 in numerical order. The returned result is the number in red box 8, which is in line with the expectation:
  • So far, the experience of SpringBoot combined with the deep learning model has been completed. One minute overview, one minute deployment and one minute experience are efficient enough (the time to download more than 900 megabytes of images can't be counted, dare not be counted...)
  • Now you should feel the charm of deep learning. Of course, smart you will have many questions, such as:
  1. How is the model trained?
  2. How to use this model in java code?
  3. How do these things make a docker image?