SoFunction
Updated on 2025-03-03

Springboot Integrates Java DL4J to implement the recommended system for fashion outfits (example code)

Spring Boot integrates Java Deeplearning4j to realize the recommended system for fashion outfits

1. Introduction

In today's era of changing fashion trends, people's demand for personalized outfits is getting higher and higher. In order to meet this needs of users, we can use deep learning technology to recommend suitable clothing matching for users by analyzing photos uploaded by users. This article will introduce how to use Spring Boot to integrate Java Deeplearning4j to implement a fashionable outfit recommendation system.

2. Technical Overview

  • Spring Boot:Spring Boot is a framework for rapid development of Java applications. It simplifies the configuration and deployment of Spring applications, allowing developers to focus more on the implementation of business logic.
  • Deeplearning4j:Deeplearning4j is a Java library for deep learning. It supports a variety of deep learning algorithms,Including convolutional neural networks (CNNs), **Recurrent Neural Network (RNN)**, etc. In this case, we will use Deeplearning4j to implement image recognition.
  • Neural Network Selection: In this case, we chose to use **Convolutional Neural Network (CNN) to implement image recognition function. CNN is a neural network specially used to process image data, and it has good image recognition capabilities. The reasons for choosing CNN are as follows:
    • Local perceptionCNNLocal features in the image can be automatically learned to better identify objects in the image.
    • Weight sharingCNNThe convolutional layer in it can share weights, thereby reducing the number of parameters of the model and improving the training efficiency of the model.
    • Multi-layer structureCNNUsually composed of multiple convolutional layers and pooling layers, this multi-layer structure can extract features at different levels in the image, thereby improving the recognition accuracy of the model.

3. Dataset format

  • Dataset Source: We can collect pictures of fashionable outfits from fashion magazines, fashion blogs and other channels as our data set. Public fashion wear data sets, such as Fashion-MNIST data sets, can also be used.
  • Dataset format: We store the dataset as the form of image files, each image file represents an example of a fashionable outfit. The naming format of the image file is "dressing type_skin color_body.jpg", such as "casual wear_white_slim.jpg".
  • Dataset directory structure: We store the dataset in a directory, and the directory structure is as follows:
dataset/
    |--Casual wear/
    |   |--White/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Wheat color/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Bronze/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |--Formal wear/
    |   |--White/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Wheat color/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Bronze/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |--Sportswear/
    |   |--White/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Wheat color/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg
    |   |--Bronze/
    |   |   |--slim.jpg
    |   |   |--medium.jpg
    |   |   |--full.jpg

4. Maven dependency

Spring Boot Dependencies

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

This dependency containsSpring Boot's WebThe required components for development, such asSpring MVCTomcatwait.
2. Deeplearning4j Dependency

<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-core</artifactId>
    <version>1.0.0-beta7</version>
</dependency>
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-nn</artifactId>
    <version>1.0.0-beta7</version>
</dependency>
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-ui</artifactId>
    <version>1.0.0-beta7</version>
</dependency>

These dependencies include the core library, neural network library, and user interface library of Deeplearning4j.
3. Other dependencies

<dependency>
    <groupId></groupId>
    <artifactId>-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

These dependencies include the Servlet API and file upload components for handling photos uploaded by users.

V. Code examples

Model training code

import ;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org.;
import org..Nd4j;
import org.;
import ;
import ;
import ;
public class FashionRecommendationModel {
    private ComputationGraph model;
    public FashionRecommendationModel() {
        // Define neural network structure         builder = new ()
               .weightInit()
               .activation()
               .convolutionMode()
               .updater("adam")
               .l2(0.0005);
        // Input layer        int height = 224;
        int width = 224;
        int channels = 3;
        ()
               .addInputs("input")
               .setInputTypes((height, width, channels));
        // Convolutional layer 1        ("conv1", new (3, 3)
               .nIn(channels)
               .nOut(32)
               .stride(1, 1)
               .build(), "input");
        // Pooling layer 1        ("pool1", new (2, 2)
               .stride(2, 2)
               .build(), "conv1");
        // Convolutional layer 2        ("conv2", new (3, 3)
               .nOut(64)
               .stride(1, 1)
               .build(), "pool1");
        // Pooling layer 2        ("pool2", new (2, 2)
               .stride(2, 2)
               .build(), "conv2");
        // Convolutional layer 3        ("conv3", new (3, 3)
               .nOut(128)
               .stride(1, 1)
               .build(), "pool2");
        // Pooling layer 3        ("pool3", new (2, 2)
               .stride(2, 2)
               .build(), "conv3");
        // Full connection layer 1        int numClasses = 3; // Assume there are 3 types of wearable types        int numNodes = 1024;
        ("fc1", new ()
               .nOut(numNodes)
               .activation()
               .build(), "pool3");
        // Full connection layer 2        ("fc2", new ()
               .nOut(numClasses)
               .activation()
               .build(), "fc1");
        // Output layer        ("output", new ()
               .nOut(numClasses)
               .activation()
               .build(), "fc2");
        // Build a calculation diagram        model = new ComputationGraph(());
        ();
    }
    public void trainModel(String datasetPath) {
        // Load the dataset        List&lt;INDArray&gt; images = new ArrayList&lt;&gt;();
        List&lt;Integer&gt; labels = new ArrayList&lt;&gt;();
        File datasetDir = new File(datasetPath);
        for (File categoryDir : ()) {
            int label = (());
            for (File skinToneDir : ()) {
                for (File bodyShapeDir : ()) {
                    for (File imageFile : ()) {
                        NativeImageLoader loader = new NativeImageLoader(224, 224, 3);
                        INDArray image = (imageFile);
                        (image);
                        (label);
                    }
                }
            }
        }
        // Data normalization        DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
        for (INDArray image : images) {
            (image);
        }
        // Convert to ND4J dataset format        INDArray inputData = ((), 3, 224, 224);
        INDArray labelData = ((), 3);
        for (int i = 0; i &lt; (); i++) {
            (i, (i));
            (i, (i), 1.0);
        }
        // Training the model        (inputData, labelData);
    }
    public int predict(INDArray image) {
        // Data normalization        DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
        (image);
        // predict        INDArray output = (image);
        int prediction = (output, 1).getInt(0);
        return prediction;
    }
}

This code defines aFashionRecommendationModelClass, used to train and predict fashion outfit types. In the constructor, a convolutional neural network structure is defined, including the input layer, the convolutional layer, the pooling layer, the fully connected layer and the output layer. existtrainModelIn the method, the data set is loaded and data normalized, and then the data is converted into the data set format of ND4J, and finally trained using a computational graph. existpredictIn the method, the input image is normalized by data, and then the trained model is used to predict, returning the predicted wear type.

Boot service code

import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootApplication
@RestController
public class FashionRecommendationApp {
    private FashionRecommendationModel model;
    public FashionRecommendationApp() {
        model = new FashionRecommendationModel();
        ("dataset");
    }
    @PostMapping("/recommend")
    public String recommend(@RequestParam("image") MultipartFile imageFile) throws IOException {
        // Read the uploaded image file        InputStream inputStream = new ByteArrayInputStream(());
        BufferedImage image = (inputStream);
        // Convert image to ND4J array format         loader = new (224, 224, 3);
        INDArray imageArray = (image);
        // Use the model to predict        int prediction = (imageArray);
        // Return to the predicted outfit suggestions        switch (prediction) {
            case 0:
                return "Casual wear";
            case 1:
                return "Formal clothing";
            case 2:
                return "Sportswear";
            default:
                return "Unrecognized";
        }
    }
    public static void main(String[] args) {
        (, args);
    }
}

This code defines a Spring Boot application for providing fashion outfit recommendations. In the constructor, aFashionRecommendationModelObjects and trained using datasets. existrecommendIn the method, the image file uploaded by the user is processed, converted to an ND4J array format, and then used the trained model to make predictions, and finally returned the predicted outfit suggestions.

VI. Unit Test

Model training test

import org.;
import ;
import ;
import org.;
import static ;
class FashionRecommendationModelTest {
    private FashionRecommendationModel model;
    @BeforeEach
    void setUp() {
        model = new FashionRecommendationModel();
    }
    @Test
    void testTrainModel() {
        ("dataset");
        ComputationGraph trainedModel = ();
        assertNotNull(trainedModel);
    }
    @Test
    void testPredict() {
        ("dataset");
        // Load the test image         loader = new (224, 224, 3);
        INDArray testImage = (new File("test_image.jpg"));
        int prediction = (testImage);
        // Assert based on the actual wear type of the test image        assertEquals(0, prediction);
    }
}

This code is correctFashionRecommendationModelThe class was unit tested. existtestTrainModelIn the method, the training method of the model is tested to ensure that the trained model is not empty. existtestPredictIn the method, a test image is loaded, a trained model is used to predict, and an assertion is made based on the actual wear type of the test image.

Service Testing

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@SpringBootTest
class FashionRecommendationAppTest {
    private MockMvc mockMvc;
    @Test
    void testRecommend() throws Exception {
        FashionRecommendationApp app = new FashionRecommendationApp();
        mockMvc = (app).build();
        // Load the test image        FileInputStream fis = new FileInputStream("test_image.jpg");
        MockMultipartFile imageFile = new MockMultipartFile("image", "test_image.jpg", "image/jpeg", fis);
        // Send POST request for testing        (("/recommend")
                       .file(imageFile))
               .andExpect(().isOk())
               .andExpect(().string("Casual wear"));
    }
}

This code is correctFashionRecommendationAppThe class was unit tested. existtestRecommendIn the method, useMockMvcSimulate sending a POST request, uploading a test image, and asserting that the returned outfit suggestion is correct.

7. Expected output

  • After the model is successfully trained, the console will output information such as loss value during the training process.
  • When a user uploads a photo, the service returns a dressing suggestion, such as "casual wear", "formal wear" or "sports wear".

8. Reference materials

Deeplearning4j official documentation

Spring Boot Official Documentationroll

Introduction to the Ji Neural Network

This is the article about Springboot integrating Java DL4J to implement a fashionable dressing recommendation system. For more related Springboot Java DL4J fashion dressing recommendation system, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!