AI deep learning model training method and device based on transfer learning
By using transfer learning methods on the browser side, selecting pre-trained models and adding shallow classifiers, the problem of complex training and high resource consumption in the browser is solved, efficient training and high accuracy are achieved, and the model is ensured. reusability.
Patent Information
- Application Number
- CN202411440882.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-16
- Publication Date
- 2025-01-17
AI Technical Summary
Deep learning models in browsers are complex and resource-consuming, and lack effective solutions.
Using a transfer learning-based method, select the pre-trained model, remove its last layer of classifier to obtain the truncated model, and add a shallow fully connected classifier to the truncated model, use training data to train the shallow classifier to realize the browser side Efficient training of deep learning models.
Transfer learning reduces training complexity, reduces resource consumption, ensures high accuracy and efficiency of the model, and achieves high reusability of the model in different devices and environments.
Smart Images

Figure CN119323247A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence, and in particular to a method and device for training an AI deep learning model based on transfer learning. Background Art
[0002] Machine learning plays an important role in modern computing and is widely used in image recognition, natural language processing and other fields. In recent years, with the widespread use of JavaScript in the Web front-end, JavaScript-based machine learning frameworks (such as TensorFlow.js) have been developed, making it possible to perform machine learning in the browser. However, due to differences in different browser versions and device hardware, machine learning in the JavaScript environment has compatibility issues, such as different backend support and large performance differences. This poses a great challenge to developers and is also a problem that needs to be solved in practical applications.
[0003] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0004] The embodiments of the present invention provide an AI deep learning model training method and device based on transfer learning, so as to at least solve the technical problem that training a deep learning model from scratch in a browser is complex and resource-intensive.
[0005] According to one aspect of an embodiment of the present invention, a browser-side deep learning model training method based on transfer learning is provided, comprising: selecting a pre-trained model, and obtaining a truncated model by removing the last layer classifier of the pre-trained model; adding a shallow fully-connected classifier to the truncated model to obtain a browser-side deep learning model; and training the shallow fully-connected classifier of the browser-side deep learning model using training data.
[0006] According to another aspect of an embodiment of the present invention, a method for graphical programming based on a browser-side deep learning model is also provided, comprising: embedding the above-mentioned browser-side deep learning model into a web page or a web application; during graphical programming, calling the browser-side deep learning model, wherein the browser-side deep learning model is used to classify images.
[0007] According to another aspect of an embodiment of the present invention, a browser-side deep learning model training device based on transfer learning is also provided, including: a selection module, configured to select a pre-trained model, and obtain a truncated model by removing the last layer classifier of the pre-trained model; an adding module, configured to add a shallow fully connected classifier to the truncated model to obtain a browser-side deep learning model; and a training module, configured to train the shallow fully connected classifier of the browser-side deep learning model using training data.
[0008] According to another aspect of an embodiment of the present invention, there is also provided a device for graphical programming based on a browser-side deep learning model, comprising: an embedding module, configured to embed the above-mentioned browser-side deep learning model into a web page or a web application; and a calling module, configured to call the browser-side deep learning model during graphical programming, wherein the browser-side deep learning model is used to classify images.
[0009] In an embodiment of the present invention, a pre-trained model is selected, and a truncated model is obtained by removing the last layer classifier of the pre-trained model; a shallow fully connected classifier is added to the truncated model to obtain a browser-side deep learning model; and the shallow fully connected classifier of the browser-side deep learning model is trained using training data. Through the above solution, the technical problem that the training of deep learning models in browsers from scratch is complex and resource-intensive is solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0011] Figure 1 A browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention;
[0012] Figure 2 is a flowchart of another browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention;
[0013] Figure 3 is a flowchart of another browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention;
[0014] Figure 4 is a flowchart of a method for selecting an Adam optimizer and a cross entropy loss function to train a model according to an embodiment of the present invention;
[0015] Figure 5It is a structural diagram of a browser-side deep learning model training device based on transfer learning according to an embodiment of the present invention. DETAILED DESCRIPTION
[0016] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0017] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0018] According to an embodiment of the present invention, a method embodiment of a browser-side deep learning model training method based on transfer learning is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in an order different from that shown here.
[0019] Figure 1 is a browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention, such as Figure 1 As shown, the method comprises the following steps:
[0020] Step S102, selecting a pre-trained model, and obtaining a truncated model by removing the last layer classifier of the pre-trained model.
[0021] A pre-trained model is a model that has been pre-trained on a large-scale general dataset. This model can be further fine-tuned to adapt to specific tasks in actual applications. The main purpose of pre-training is to use the knowledge learned from general data to enable the model to converge faster and achieve better results when facing specific tasks.
[0022] First, a pre-trained model is selected. A model with a number of parameters less than a preset number threshold, a computational overhead lower than a preset overhead threshold, and suitable for browser-side operation is selected as the pre-trained model, wherein the version of the pre-trained model is a lightweight model version.
[0023] Next, truncation is performed. The last layer of classifiers of the pre-trained model is removed to obtain a truncated model. The early and middle layers of the pre-trained model usually learn low-level and mid-level features of the input data, such as edges, textures, shapes, etc. In image processing tasks, these features are irrelevant to the specific classification task and can be well generalized to different tasks. The last layer of classifiers is usually optimized specifically for the specific tasks and datasets used during pre-training. For example, the last layer of ResNet trained on ImageNet is for a specific 1000-category image classification task, and these categories may be irrelevant to the new task. Therefore, removing the last layer of classifiers can retain common features while avoiding the limitations of specific task classifiers.
[0024] Step S104, adding a shallow fully connected classifier to the truncated model to obtain a browser-side deep learning model.
[0025] Step S106, using the training data to train the shallow fully connected classifier of the browser-side deep learning model.
[0026] First, preprocessing is performed. The input data is preprocessed, and the preprocessing includes cropping, scaling and normalization; the preprocessed input data is captured and marked as the training data, and the truncation model is used to extract features from the training data to obtain feature data.
[0027] Then, training is performed: selecting an optimizer and a loss function for the shallow fully connected classifier, and setting training parameters; and training the shallow fully connected classifier using the extracted feature data based on the optimizer, the loss function, and the training parameters.
[0028] Finally, the trained browser-side deep learning model and the weight of the browser-side deep learning model are saved as a file, wherein the file contains the model topology structure and weight values; and the classification label information of the browser-side deep learning model is saved in a metadata file.
[0029] Through the above steps, the problem of complex and resource-intensive training of deep learning models from scratch in browsers is solved. Transfer learning reduces the complexity of training while ensuring that the model has high accuracy and efficiency.
[0030] Figure 2Another browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention is as follows: Figure 2 As shown, the method comprises the following steps:
[0031] Step S202: pre-training model selection.
[0032] Choose a pre-trained model (such as MobileNet) with small parameters, low computational overhead, and suitable for running on the browser side. Use a lightweight model version (such as MobileNetV2 alpha 0.35) to ensure efficient operation in the browser environment.
[0033] Specifically, for browser applications, it is very important to choose a model with small parameters and low computational overhead. The MobileNet series of models, especially MobileNetV2, is a commonly used lightweight convolutional neural network that uses layered depthwise separable convolutions as its core, significantly reducing computational complexity and parameters. To further reduce the computational burden, you can choose a lighter version, such as MobileNetV2 alpha 0.35, which reduces the width of the network and makes the model more suitable for low-resource environments. When choosing a pre-trained model, you also need to consider its compatibility with browser-side technologies such as JavaScript or WebAssembly. Frameworks such as TensorFlow.js support converting TensorFlow or Keras models into a format suitable for browser operation.
[0034] Step S204, truncating the pre-trained model.
[0035] By removing the last classifier layer (i.e., the output layer) of the pre-trained model, a truncated model is obtained. The truncated model retains the feature extraction capability of the original model and reduces the training burden.
[0036] Pre-trained models are usually trained on large-scale datasets (such as ImageNet), and their last layer of classifiers is specifically optimized for the categories of these datasets. Since the categories of the target task may be different from the original training task, it is necessary to remove this layer of classifiers to obtain a "truncated model". This truncated model retains the feature extraction capabilities of the previous layers, but is not constrained by the original classification task. The truncated model, i.e. the truncated model, is able to extract common visual features such as edges, textures, and shapes, which can be well suited for different tasks. By truncation, the burden of retraining the entire model is reduced, and only the training of the new classifier needs to be focused, thereby improving training efficiency.
[0037] Step S206: adding a custom classifier.
[0038] Based on the truncated model, a new shallow fully connected classifier is added. The new classifier has a simple structure and fewer parameters, making it suitable as a custom classification head.
[0039] Specifically, the newly added classifier needs to be designed according to the needs of the specific task. Usually, a shallow fully connected classifier has a simple structure, consisting of one or two fully connected layers, and can efficiently perform task-specific classification. Choosing a classifier with fewer parameters not only maintains the lightweight nature of the model, but also runs efficiently on the browser side. The output layer of the custom classifier needs to be set according to the number of new task categories. For example, if the task is binary classification, one output unit can be used in conjunction with the Sigmoid activation function; the design of the shallow classifier should ensure that it can effectively utilize the features extracted by the truncated model while having good generalization capabilities.
[0040] Step S208: data preprocessing and sample addition.
[0041] Preprocess the input data (e.g., crop, scale, and normalize). Capture and label sample images, pass the samples through the truncated model for feature extraction, and save intermediate activation values.
[0042] The input data needs to be preprocessed, such as cropping, scaling, and normalizing the image. These operations are designed to adjust the input data to a format and range suitable for model input, while eliminating unnecessary variability in the image. For browser-side applications, it is very important to ensure that the preprocessing steps are lightweight and efficient. The preprocessed samples are input into the truncated model to extract intermediate features. These features are extracted through the convolutional layers of the pre-trained model to describe the high-dimensional feature representation of the input data. These features are then fed into a custom fully connected classifier for training. By saving these features, they can be reused in subsequent training to improve training efficiency.
[0043] Step S210: model training.
[0044] Use the feature data extracted by the truncated model to train a new shallow classifier. Select an appropriate optimizer (such as Adam) and loss function (such as cross entropy loss) for model compilation and training. Set training parameters (such as learning rate, batch size, and training cycle), and monitor the training process through the callback function. Specifically:
[0045] First, select the optimizer and loss function. The choice of optimizer has an important impact on the convergence speed and effect of the model. Adam optimizer is a preferred choice, which combines the advantages of momentum and adaptive learning rate and is suitable for processing large amounts of training data. In terms of loss function, cross entropy loss is usually used for classification tasks, which can effectively measure the difference between the model output and the true label.
[0046] Next, set the training parameters. Some key parameters in the training process need to be carefully set, such as learning rate, batch size, and training cycle. The learning rate controls the pace of model parameter updates, the batch size determines the number of samples used in each iteration, and the training cycle determines the number of times the model iterates on the entire dataset. By adjusting these parameters, you can find the best balance for model training.
[0047] Finally, monitor the training process. During the training process, monitor the performance of the model by setting callback functions, such as checking the loss and accuracy of the model on the validation set. Based on these monitoring indicators, adjustments can be made at appropriate times to avoid overfitting or undertraining.
[0048] Step S212: model saving and loading.
[0049] Save the trained model and its weights as a file, including the model topology and weight values. Save the classification label information to the metadata file to ensure the consistency and reusability of the model in different environments.
[0050] Specifically, after training is completed, the structure and weights of the model are saved as files. These files may include the model's topology, weight parameters, and related configuration information. The saved model can be loaded into different environments, such as for use on the browser side. In order to ensure the consistency of the model across different devices and environments, the classification label information also needs to be saved in the metadata file. This information describes the category labels output by the model, and together with the model structure and weights, ensures the reusability of the model in different application scenarios.
[0051] Step S214: model reasoning and application.
[0052] Use the trained model to classify and predict new input images and output the classification results. In practical applications, the model can be embedded in a web page or web application to achieve efficient inference on the browser side. Specifically, the new input image or data is processed through the preprocessing step and then input into the trained model for inference. The model will extract features through the truncation model, and the custom classifier will generate the prediction results and output the classification label or probability distribution.
[0053] In order to achieve efficient model inference on the browser side, the trained model can be embedded in a web page or web application. Using frameworks such as TensorFlow.js, the saved model can be loaded and executed, and inference can be performed directly on the client side to provide real-time classification results.
[0054] Through the above technical scheme, the present invention achieves the following technical effects: using pre-trained models for transfer learning, greatly reducing the amount of data and computing resources required for model training; using lightweight pre-trained models and custom shallow classifiers to ensure high efficiency and low resource consumption when running on the browser side; through transfer learning, the feature extraction ability of the pre-trained model can be retained to improve classification accuracy; by saving the model and classification label information, it is ensured that the model has high reusability and consistency in different devices and environments.
[0055] Figure 3 is another browser-side deep learning model training method based on transfer learning according to an embodiment of the present invention, such as Figure 3 As shown, the method comprises the following steps:
[0056] Step S302, select a pre-trained model.
[0057] In this embodiment, a lightweight pre-trained model suitable for browser-side operation is first selected. Taking into account the resource-constrained environment of the browser side, the present invention selects the MobileNetV2 alpha 0.35 version, which has a low parameter amount and computational overhead while maintaining good accuracy, and is very suitable for execution in a browser environment. MobileNetV2 is an efficient convolutional neural network model based on deep separable convolution. The alpha parameter is used to control the network width. A lower alpha value means that the network is more compact, which is beneficial to reducing computational costs. After loading the pre-trained model, the model can be directly used to process new tasks.
[0058] Step S304, truncating the pre-trained model.
[0059] In a specific embodiment of the present invention, a method of truncating a pre-trained model is adopted. The method removes the last layer classifier (i.e., the output layer) of the pre-trained model to obtain a "truncated model". In this way, the feature extraction capability of the pre-trained model can be retained while reducing the training burden of the model under new tasks.
[0060] Typically, pre-trained models are trained on large-scale data sets (such as ImageNet), which contain a large amount of labeled data and cover a wide range of categories. The last layer of classifiers in the pre-trained model is optimized specifically for these categories, and its parameter tuning is also fully based on the features of these data sets. However, when the category of the target task is inconsistent with the category of the original training task of the pre-trained model, directly using the classifier layer of the pre-trained model will cause a decline in performance. In order to solve this problem, the strategy of removing the output layer is adopted in the embodiment, and by removing this specific classification layer, the retained part is "truncated model".
[0061] The truncated model retains the first few layers of the pre-trained model in terms of structure, including convolutional layers, pooling layers, and some fully connected layers. These layers have been trained on large-scale data sets and have strong feature extraction capabilities, and can recognize common visual features in images, such as edges, textures, shapes, and colors. By truncating the pre-trained model, these trained feature extractors can continue to work without the need for re-learning, significantly saving computing resources and time costs.
[0062] The truncated model is no longer limited to the specific classification requirements of the original training task, which means that the truncated model can adapt to new task requirements more flexibly. For example, in object detection, image segmentation, or new image classification tasks, the truncated model provides a powerful feature extraction foundation, and only a new classifier or task-specific output layer needs to be added on top of it for training. Since the parameters of the first few layers have been fully trained through large-scale datasets, the model only needs to fine-tune the newly added classifier layer, which greatly reduces the burden of training and also improves the training speed.
[0063] In addition, the truncated model not only reduces the training time of the model through this structural adjustment, but also significantly reduces the demand for computing resources. Through the truncation operation, the complexity of the model is controlled, while retaining the feature extraction capability, making the model more universal and adaptable.
[0064] Step S306, adding a custom classifier.
[0065] In order to adapt the model to new classification tasks, a new shallow fully connected classifier is added on top of the truncated model, which will serve as a custom classification head to provide more accurate output for specific tasks.
[0066] The newly added shallow fully connected classifier has a relatively simple structure, usually consisting of one or two fully connected layers (FC). This can maintain the lightweight characteristics of the model, reduce computational complexity, and is suitable for running in resource-constrained environments, such as real-time prediction on the browser side. These fully connected layers receive high-dimensional feature vectors output by truncated models, and map these features to task-specific category spaces through a combination of matrix multiplication and activation functions.
[0067] Specifically, a shallow classifier can be composed of the following components: input layer, hidden layer, and output layer. The input layer receives the feature vector from the truncated model, which has been processed by the convolution layer and the pooling layer and contains rich image information. The hidden layer usually selects one or two fully connected layers according to the complexity of the task and the number of features. The number of neurons in each layer can be adjusted according to the requirements of the task. In order to prevent overfitting, a Dropout layer can be added after the hidden layer to randomly ignore some neurons, thereby enhancing the generalization ability of the model. The number of neurons in the output layer depends on the number of new task categories. If the target task is binary classification (for example, judging whether an image contains a certain target object), the output layer only needs one neuron, and the Sigmoid activation function is used to map the output to the [0,1] interval, indicating the probability of belonging to the target category. If the task is multi-classification, the number of neurons in the output layer is equal to the number of categories, and the Softmax activation function is used to convert the output vector into a probability distribution. Selecting a classifier with fewer parameters can minimize computational overhead and memory usage while ensuring model performance. This is especially important when running the model on the browser or mobile device, because the computing power of these devices is limited. By reducing the complexity of the model, the operating efficiency can be significantly improved and the goal of real-time processing can be achieved.
[0068] Step S308: data preprocessing and sample addition.
[0069] Before model training, the input data needs to be properly preprocessed. The data preprocessing in this embodiment includes the following steps:
[0070] 1) Image cropping. Crop the image to a specified size to fit the model's input requirements. The main goal of image cropping is to focus the image on the main target object by removing unnecessary edge areas, thereby retaining the most representative feature information. This process not only helps reduce data noise, but also improves the learning efficiency and accuracy of the model. In addition, the cropping operation helps to enhance the consistency of the input data and ensure that all input images have the same size and structure, which is crucial for both the training and reasoning stages of the model. In the specific implementation process, the target size of the cropping is first determined (for example, 224x224 pixels). Then, according to the size of the original image and the position of the target object, a suitable cropping area is selected. If the aspect ratio of the original image does not match the target size, a center cropping or a cropping strategy based on target detection is required to ensure that the cropped image not only meets the size requirements, but also effectively retains the key features of the target object. Finally, the cropped image is scaled and normalized to adapt to the requirements of the model input layer, and the processed image is input into the model for feature extraction and classification prediction.
[0071] 2) Image scaling.
[0072] Scale the image to the input size of the model (e.g., 224x224 pixels). In the present invention, image scaling is a key step in data preprocessing, which aims to adjust the input image to the fixed size required by the model to ensure that the input data can be effectively processed by the model. Specifically, many deep learning models (such as MobileNetV2 alpha0.35) have strict requirements on the size of the input image, such as a three-channel color image of 224x224 pixels. By scaling the original image to the specified target size, the computational efficiency of the model can be improved and the accuracy of the classification results can be ensured.
[0073] First, after determining the target size, calculate the image scaling ratio, which is obtained by comparing the target size with the size of the original image. The specific calculation method is: the scaling factor is equal to the smaller value of the ratio of the target width to the original width and the ratio of the target height to the original height. This calculation method ensures that the image maintains its original aspect ratio during the scaling process and avoids image deformation.
[0074] Next, the image is adjusted using the calculated scaling factor. First, the image is resampled and interpolated using bilinear interpolation or nearest neighbor interpolation. This effectively preserves the image details while avoiding blur or artifacts introduced during the scaling process. After the scaling operation is completed, if the aspect ratio of the original image does not exactly match the target size, the image edges need to be further processed.
[0075] Strategies for dealing with mismatches include padding and cropping. The padding method adds extra pixels (such as black pixels) around the short sides of the image to resize the image to the target size. This method is suitable for keeping the original image content intact. Another method is center cropping, which is to crop the target size portion from the center area of the scaled image. This method helps to remove unimportant edge areas in the image and focus on the core content of the image.
[0076] 3) Normalization processing.
[0077] After image scaling and cropping, normalization is performed. The pixel values are adjusted from the original range (such as 0 to 255) to the range required by the model (such as 0 to 1) to better adapt to the input requirements of the model. Normalization helps improve the training efficiency and stability of the model and ensures that the model can converge quickly. Finally, the scaled, cropped and normalized images can be input into the deep learning model for feature extraction and classification prediction. Through this image scaling solution, it can be ensured that all input images meet the size requirements of the model, thereby improving the computational efficiency and classification accuracy of the model.
[0078] After normalization, the generated Gaussian kernel is used to calculate the offset value of each pixel in the image in combination with the spatial information, horizontal and vertical gradients of each pixel. The offset value is used to describe the grayscale changes and texture details of the image in the local area. In addition, in some embodiments, the noise intensity parameter can also be considered as a regulating factor to control the sensitivity in the denoising process. Specifically, the width of the Gaussian distribution can be determined based on the standard deviation, and the center of the Gaussian kernel is constructed based on the width of the Gaussian distribution, the coordinates of the pixels in the image relative to the center of the kernel, and the size of the Gaussian kernel. Then, based on the pixel value of the original image at position (i, j), the offset of the horizontal coordinate position and the offset of the vertical coordinate position relative to the center of the Gaussian kernel, the horizontal gradient of the image at each pixel position, the vertical gradient of the image at each pixel position, the gradient weight, and the noise intensity parameter used to control the sensitivity to the difference in pixel values of the image during the denoising process, the denoising process is performed, wherein the noise intensity parameter is based on the noise intensity, the pixel value of the image at position (i, j), and the pixel value of the image at position (i, j) after the offset. By using the generated Gaussian kernel combined with the spatial information of each pixel and the gradient to calculate the offset value, the local grayscale changes and texture details of the image can be effectively described. At the same time, the noise intensity parameter is considered to adjust the denoising sensitivity, making the denoising process more adaptive, which can effectively remove noise while retaining the detailed features of the image, improving the quality and accuracy of image processing.
[0079] Step S310: model training.
[0080] After completing data preprocessing and feature extraction, this embodiment trains the custom classifier. During the training process, the Adam optimizer and the cross entropy loss function are selected to compile the model to ensure fast convergence and good performance of the model. The settings of training parameters include learning rate, batch size, and training cycle, and the performance changes during training are monitored through the callback function.
[0081] Specific steps are as follows Figure 4 As shown, the following steps are included:
[0082] Step S3102, initialize the optimizer and loss function.
[0083] The optimizer is responsible for updating the weights of the model to reduce the value of the loss function. In this embodiment, the Adam optimizer (Adaptive Moment Estimation) is selected. The Adam optimizer combines the advantages of the momentum and RMSProp (Root Mean Square Propagation) algorithms and can dynamically adjust the learning rate according to the changes in different gradients. It corrects the learning rate by calculating the first-order moment (momentum) and the second-order moment (root mean square), thereby achieving a smoother optimization path during the training process.
[0084] In order to measure the difference between the predicted output of the model and the actual label, this embodiment adopts the cross entropy loss function, which is a commonly used loss function in classification problems. It can effectively quantify the difference between the probability distribution of the model output and the true distribution. By minimizing the cross entropy loss, the output probability distribution of the model gradually approaches the true distribution, thereby improving the classification accuracy. Specifically, the cross entropy loss function can punish wrong classification results, especially for predictions with high confidence but errors. This feature prompts the model to update weights more carefully during training in order to make classification predictions more accurately.
[0085] Step S3104, setting training parameters.
[0086] After initializing the optimizer and loss function, the next step is to set the training parameters, which have a direct impact on the training process and effect of the model. In this embodiment, the training parameters include learning rate, batch size and training cycle (epoch).
[0087] First, the learning rate is set to 0.001. The learning rate determines the step size of each weight update, which directly affects the convergence speed and training stability of the model. A higher learning rate may cause the training process to oscillate or even diverge, while a too low learning rate will make the training process too slow and may fall into a local optimal solution. Therefore, choosing an appropriate learning rate such as 0.001 can achieve a better balance in the training process and make the model gradually approach the global optimal solution.
[0088] Secondly, the batch size is set to 32. The batch size determines the number of samples used for each weight update. Choosing an appropriate batch size can balance the training speed and memory usage of the model. A batch size of 32 means that 32 samples are used for calculation each time the model parameters are updated. This setting helps to improve the stability of gradient estimation and can better utilize the parallel computing capabilities of modern computing devices (such as GPUs).
[0089] Finally, the training epoch is set to 10. The training epoch refers to the number of times the entire training dataset is used to train the model. Setting the training epoch to 10 means that the entire training dataset will be reused 10 times for model training. This setting helps ensure that the model can fully learn the features in the data while avoiding overfitting.
[0090] Step S3106, model training.
[0091] After completing the initialization of the optimizer and loss function, and setting the training parameters, the model training process officially begins. Using the feature data extracted from the truncated model (i.e., the model obtained by removing the output layer from the pre-trained model), the newly added shallow classifier begins to be trained. Through transfer learning, the shallow classifier uses the feature extraction capability of the truncated model and only needs to train the classification head for the current task, which significantly reduces the computational overhead and time of training. During the training process, the set Adam optimizer and cross entropy loss function are used for back propagation and weight update. The Adam optimizer automatically adjusts the learning rate after each round of training to ensure that the model can maintain a stable optimization effect at different training stages. The cross entropy loss function calculates the loss value based on the prediction results of the current model and the true label, and uses it for back propagation to adjust the model parameters.
[0092] In addition, in order to monitor the training progress of the model in real time, this embodiment adopts a callback function (CallbackFunctions) mechanism. At the end of each training cycle, the callback function will record key indicators such as the loss value and classification accuracy of the current model. By observing the changing trends of these indicators, the training strategy can be dynamically adjusted, such as changing the learning rate, modifying the batch size, or terminating the training early to prevent the model from overfitting or the training time from being too long.
[0093] In general, by initializing the optimizer and loss function, reasonably setting the training parameters, and combining the dynamic monitoring mechanism of the callback function, the present invention provides an efficient and reliable model training method, which is particularly suitable for deep learning model training in resource-constrained browser environments. This method not only ensures the high-precision classification ability of the model, but also significantly reduces the training complexity and computing resource consumption.
[0094] In summary, this embodiment uses transfer learning technology to achieve efficient training and reasoning of deep learning models on the browser side. The use of lightweight pre-trained models and custom shallow classifiers effectively reduces the complexity of training and resource consumption, while ensuring the accuracy and efficiency of the model, providing a reliable technical solution for the application of deep learning on the browser side.
[0095] Step S312: model saving and loading.
[0096] Save the trained model as a file for calling by the WEB browser.
[0097] This application also provides a browser-side deep learning model training device based on transfer learning, such as Figure 5 As shown, it includes: a selection module 52, configured to select a pre-trained model, and obtain a truncated model by removing the last layer classifier of the pre-trained model; an adding module 54, configured to add a shallow fully connected classifier to the truncated model to obtain a browser-side deep learning model; a training module 56, configured to train the shallow fully connected classifier of the browser-side deep learning model using training data.
[0098] It should be noted that the browser-side deep learning model training device based on transfer learning provided in the above embodiment is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the browser-side deep learning model training device based on transfer learning provided in the above embodiment and the browser-side deep learning model training method embodiment based on transfer learning belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0099] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A method for training an AI deep learning model based on transfer learning, characterized in that: include: Select a pre-trained model, and obtain a truncated model by removing the last layer classifier of the pre-trained model; Adding a shallow fully connected classifier to the truncated model to obtain a browser-side deep learning model; The shallow fully connected classifier of the browser-side deep learning model is trained using the training data.
2. The method according to claim 1, characterized in that Choose from pre-trained models, including: A model with a number of parameters less than a preset number threshold, a computational overhead lower than a preset overhead threshold, and suitable for running on a browser is selected as the pre-trained model, wherein the version of the pre-trained model is a lightweight model version.
3. The method according to claim 1, characterized in that Before using the training data to train the shallow fully connected classifier of the browser-side deep learning model, the method further includes: Preprocessing the input data, including cropping, scaling, and normalization; The preprocessed input data is captured and marked as the training data, and the truncation model is used to extract features from the training data to obtain feature data.
4. The method according to claim 3, characterized in that: Using training data to train the shallow fully connected classifier of the browser-side deep learning model includes: Selecting an optimizer and a loss function for the shallow fully connected classifier, and setting training parameters; Based on the optimizer, the loss function, and the training parameters, the shallow fully connected classifier is trained using the extracted feature data.
5. The method according to claim 1, characterized in that After training the shallow fully connected classifier of the browser-side deep learning model using training data, the method includes: Saving the trained browser-side deep learning model and the weights of the browser-side deep learning model as a file, wherein the file includes a model topology structure and weight values; The classification label information of the browser-side deep learning model is saved in a metadata file.
6. A method for graphical programming based on a browser-side deep learning model, characterized in that: include: Embedding the browser-side deep learning model described in any one of claims 1 to 5 into a web page or a web application; During graphical programming, the browser-side deep learning model is called, wherein the browser-side deep learning model is used to classify images.
7. A browser-side deep learning model training device based on transfer learning, characterized in that: include: A selection module is configured to select a pre-trained model and obtain a truncated model by removing the last layer classifier of the pre-trained model; An adding module is configured to add a shallow fully connected classifier to the truncated model to obtain a browser-side deep learning model; The training module is configured to train the shallow fully connected classifier of the browser-side deep learning model using training data.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the computer-readable storage medium is located is controlled to execute the method according to any one of claims 1 to 6.
9. A computer device, characterized in that: include: Memory and processor, The memory stores a computer program; The processor is used to execute the computer program stored in the memory, and when the computer program is run, the processor is enabled to execute the method according to any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.