{"id":3574,"date":"2025-09-22T10:00:41","date_gmt":"2025-09-22T14:00:41","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3574"},"modified":"2025-09-22T10:00:41","modified_gmt":"2025-09-22T14:00:41","slug":"deeplearning4j-and-spring-boot-a-powerful-duo-for-ai-powered-applications","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/deeplearning4j-and-spring-boot-a-powerful-duo-for-ai-powered-applications\/","title":{"rendered":"Deeplearning4J and Spring Boot: A Powerful Duo for AI-Powered Applications"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Deeplearning4J (DL4J) offers a comprehensive Java framework for deep learning, while Spring Boot streamlines the development of production-ready applications.  By combining these two technologies, you unlock a flexible platform for building intelligent services that can handle various types of data. In this guide, we\u2019ll explore how to integrate DL4J into your Spring Boot project and demonstrate its usage with both structured numerical data and images.<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\n<ul>\n<li>Basic knowledge of Spring Boot<\/li>\n<li>Familiarity with Maven or Gradle<\/li>\n<li>A pre-trained DL4J model (or the intention to train one)<\/li>\n<\/ul>\n<p><strong>Steps<\/strong><\/p>\n<ol>\n<li>\n<p><strong>Project Setup<\/strong><\/p>\n<ul>\n<li>Start by creating a new Spring Boot project.<\/li>\n<li>Add the DL4J and ND4J dependencies to your project\u2019s build configuration (e.g., in your <code>pom.xml<\/code> for Maven or <code>build.gradle<\/code> for Gradle):<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.deeplearning4j&lt;\/groupId&gt;\n    &lt;artifactId&gt;deeplearning4j-core&lt;\/artifactId&gt;\n    &lt;version&gt;1.0.0-M2&lt;\/version&gt; \n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.nd4j&lt;\/groupId&gt;\n    &lt;artifactId&gt;nd4j-native-platform&lt;\/artifactId&gt;\n    &lt;version&gt;1.0.0-M2&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<ul>\n<li>For image processing, include the JavaCV library and DL4J\u2019s image processing tools:<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.bytedeco&lt;\/groupId&gt;\n    &lt;artifactId&gt;javacv-platform&lt;\/artifactId&gt;\n    &lt;version&gt;1.5.7&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.deeplearning4j&lt;\/groupId&gt;\n    &lt;artifactId&gt;deeplearning4j-zoo&lt;\/artifactId&gt; &lt;version&gt;1.0.0-M2&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.deeplearning4j&lt;\/groupId&gt;\n    &lt;artifactId&gt;deeplearning4j-datavec-image&lt;\/artifactId&gt; &lt;version&gt;1.0.0-M2&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\n<p><strong>Model Loading<\/strong><\/p>\n<ul>\n<li>Place your pre-trained model(s) (usually with a <code>.zip<\/code> extension) in a suitable location within your Spring Boot project, such as the <code>resources<\/code> directory.<\/li>\n<li>Create a Spring component (<code>ModelLoader<\/code>) to load and manage your models:<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><code class=\"language-java\">@Component\npublic class ModelLoader {\n\n    private MultiLayerNetwork numericalModel;\n    private MultiLayerNetwork imageModel; \/\/ If you have a separate image model\n\n    @PostConstruct\n    public void init() throws IOException {\n        numericalModel = ModelSerializer.restoreMultiLayerNetwork(\n            new ClassPathResource(&quot;numerical_model.zip&quot;).getFile());\n        \n        \/\/ If using a separate model for image processing:\n        imageModel = ModelSerializer.restoreMultiLayerNetwork(\n            new ClassPathResource(&quot;image_model.zip&quot;).getFile()); \n    }\n\n    \/\/ ... (Getters for models)\n}\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>Handling Structured Data<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-java\">@RestController\npublic class PredictionController {\n\n    @Autowired\n    private ModelLoader modelLoader;\n\n    @PostMapping(&quot;\/predict&quot;)\n    public Map&lt;String, Double&gt; predict(@RequestBody Map&lt;String, Double&gt; inputData) {\n        INDArray input = Nd4j.create(inputData.values().stream().mapToDouble(d -&gt; d).toArray());\n        INDArray output = modelLoader.getNumericalModel().output(input);\n        \/\/ ... (Process output and return results)\n    }\n}\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>Handling Images<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-java\">@RestController\npublic class ImageController {\n\n    @Autowired\n    private ModelLoader modelLoader;\n    \n    private static final NativeImageLoader LOADER = new NativeImageLoader(224, 224, 3); \/\/ Example dimensions\n\n    @PostMapping(&quot;\/classify&quot;)\n    public Map&lt;String, Double&gt; classifyImage(@RequestParam(&quot;image&quot;) MultipartFile file) throws IOException {\n        \n        \/\/ Load the image\n        Image image = ImageIO.read(file.getInputStream());\n\n        \/\/ Convert to INDArray \n        INDArray input = LOADER.asMatrix(image);\n\n        \/\/ Preprocess (normalize, etc.) - Adjust as needed for your model\n        \/\/ DataNormalization scaler = new ImagePreProcessingScaler(0, 1);\n        \/\/ scaler.transform(input);\n\n        \/\/ Perform prediction using the model\n        INDArray output = modelLoader.getImageModel().output(input); \n        \/\/ ... Convert output to probabilities (e.g., using Softmax)\n        \/\/ ... Return probabilities\n    }\n}\n<\/code><\/pre>\n<p><strong>DL4J Model Formats: A Deep Dive<\/strong><\/p>\n<p>Deeplearning4j (DL4J) is a versatile deep learning library for Java, supporting various model formats:<\/p>\n<ul>\n<li><strong>Keras:<\/strong> Import models trained with Keras (TensorFlow backend) seamlessly.<pre><code class=\"language-java\">ComputationGraph model = KerasModelImport.importKerasModelAndWeights(&quot;path\/to\/model.h5&quot;);\n<\/code><\/pre>\n<\/li>\n<li><strong>TensorFlow:<\/strong> Directly load TensorFlow models.<pre><code class=\"language-java\">ComputationGraph model = SavedModelBundle.load(&quot;path\/to\/model\/directory&quot;).getComputationGraph();\n<\/code><\/pre>\n<\/li>\n<li><strong>ONNX:<\/strong> Utilize the Open Neural Network Exchange format for interoperability.<pre><code class=\"language-java\">ComputationGraph model = ModelImporter.importModel(&quot;path\/to\/model.onnx&quot;);\n<\/code><\/pre>\n<\/li>\n<li><strong>SavedModel:<\/strong> Work with TensorFlow\u2019s SavedModel format. (Same as TensorFlow instructions above)<\/li>\n<li><strong>DL4J Model:<\/strong> Load a pre-trained or saved DL4J model.<pre><code class=\"language-java\">MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(&quot;path\/to\/model.zip&quot;);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><strong>Performance:<\/strong><\/p>\n<p>DL4J is designed for Java environments, often showing comparable or better performance than other Java-based libraries. Benchmarking deep learning models is complex and depends heavily on hardware, model architecture, and dataset.<\/p>\n<p><strong>Key Points &amp; Best Practices<\/strong><\/p>\n<ul>\n<li><strong>Model Optimization:<\/strong> Consider techniques like model quantization or pruning for production deployment to improve performance.<\/li>\n<li><strong>Model Type:<\/strong> Ensure your DL4J model is designed for the type of image analysis you want to perform (e.g., classification, object detection, segmentation).<\/li>\n<li><strong>Image Preprocessing:<\/strong> Pay careful attention to preprocessing steps. Image resizing, normalization, and other transformations are often crucial for good model performance.<\/li>\n<li><strong>Error Handling:<\/strong> Implement robust error handling for cases where the image upload fails, the file format is incorrect, or the model encounters issues.<\/li>\n<li><strong>Deployment:<\/strong> Package your application into an executable JAR and leverage containerization (e.g., Docker) for scalability.<\/li>\n<li><strong>Security:<\/strong> Always prioritize security when exposing AI models through APIs.  If your API allows image uploads from untrusted sources, consider adding security measures to prevent malicious uploads.<\/li>\n<li><strong>Testing:<\/strong> Thoroughly test your model integration with various input types and edge cases.<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3490,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[443],"tags":[69,319],"series":[420,397],"class_list":["post-3574","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_ai","tag-java-2","tag-spring","series-ai-ml-dl","series-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8453379_1280.jpg?fit=853%2C1280&ssl=1","jetpack-related-posts":[{"id":3951,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/scaling-streams-mastering-virtual-threads-in-spring-boot-4-and-java-25\/","url_meta":{"origin":3574,"position":0},"title":"Scaling Streams: Mastering Virtual Threads in Spring Boot 4 and Java 25","author":"Jeffery Miller","date":"December 22, 2025","format":false,"excerpt":"As a software architect, I\u2019ve seen the industry shift from heavy platform threads to reactive streams, and finally to the \"best of both worlds\": Virtual Threads. With the recent release of Spring Boot 4.0 and Java 25 (LTS), Project Loom's innovations have officially become the bedrock of high-concurrency enterprise Java.\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/12\/Gemini_Generated_Image_wqijejwqijejwqij-scaled.avif 3x"},"classes":[]},{"id":3935,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/%f0%9f%9a%80-dl4j-and-spring-boot-real-time-anomaly-detection-in-time-series-data\/","url_meta":{"origin":3574,"position":1},"title":"\ud83d\ude80 DL4J and Spring Boot: Real-Time Anomaly Detection in Time-Series Data","author":"Jeffery Miller","date":"November 25, 2025","format":false,"excerpt":"As a Software Architect, you understand that an excellent solution requires not just a powerful model, but a robust, scalable, and performant architecture for deployment. The combination of DL4J (Deeplearning4j) and Spring Boot is perfectly suited for building real-time, high-performance services, especially for a critical task like time-series anomaly detection.\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/Gemini_Generated_Image_ek9cohek9cohek9c.avif 2x"},"classes":[]},{"id":3912,"url":"https:\/\/www.mymiller.name\/wordpress\/uncategorized\/spring-boot-4-0-whats-next-for-the-modern-java-architect\/","url_meta":{"origin":3574,"position":2},"title":"Spring Boot 4.0: What&#8217;s Next for the Modern Java Architect?","author":"Jeffery Miller","date":"September 24, 2025","format":false,"excerpt":"A Forward-Looking Comparison of Spring Boot 3.x and 4.0 Staying on top of the rapidly evolving Java ecosystem is paramount for any software architect. The shift from Spring Boot 2.x to 3.x brought significant changes, notably the move to Jakarta EE. Now, with the horizon of Spring Boot 4.0 and\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/09\/per-2056740_1280.avif 3x"},"classes":[]},{"id":3569,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/integrating-prolog-with-spring-boot\/","url_meta":{"origin":3574,"position":3},"title":"Integrating Prolog with Spring Boot","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"Prolog, a declarative logic programming language, shines in solving specific types of problems that require knowledge representation and logical inference. Integrating Prolog with Spring Boot can bring the power of logic programming to your Java applications. 1. Setting Up Your Environment Add JPL Dependency: Include the Java Prolog Interface (JPL)\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/Gemini_Generated_Image_avkkoeavkkoeavkk.jpg?fit=1200%2C1200&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3557,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/spring-ai-simplifying-ai-development\/","url_meta":{"origin":3574,"position":4},"title":"Spring AI: Simplifying AI Development","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"Spring AI is a new framework aimed at making AI development easier for Java developers. It provides a simple, Spring-friendly way to integrate AI models into your applications, including the use of templates for more structured prompts. It currently supports models from various providers. Why Spring AI? Simplified Integration: Easily\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8686301_640.jpg?fit=640%2C640&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8686301_640.jpg?fit=640%2C640&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8686301_640.jpg?fit=640%2C640&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3893,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_ai\/building-intelligent-apps-with-spring-ai\/","url_meta":{"origin":3574,"position":5},"title":"Building Intelligent Apps with Spring AI","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"In today's fast-paced world of software development, integrating artificial intelligence into applications is no longer just a trend\u2014it's a necessity. At the heart of this revolution is Generative AI, a type of artificial intelligence that can create new content, such as text, images, and code, in response to prompts. It's\u2026","rel":"","context":"In &quot;Spring AI&quot;","block_context":{"text":"Spring AI","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_ai\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/08\/ai-generated-8273796_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/08\/ai-generated-8273796_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/08\/ai-generated-8273796_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/08\/ai-generated-8273796_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/08\/ai-generated-8273796_1280.avif 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3574"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3574\/revisions"}],"predecessor-version":[{"id":3575,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3574\/revisions\/3575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3490"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3574"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}