Image classification method based on sharpness perception minimization
By introducing a gradient correction mechanism into the sharpness-perceived minimization algorithm and using feature vectors to calibrate the gradient direction, the problem of inaccurate gradient updates in the SAM algorithm is solved, thereby improving the generalization ability and stability of the image classification model.
Patent Information
- Application Number
- CN202511271452.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-08
- Publication Date
- 2025-12-09
AI Technical Summary
The traditional Sharpness-Aware Minimization (SAM) algorithm suffers from inaccurate gradient update direction in image classification tasks, leading to unstable model optimization paths and insufficient generalization ability, especially in high-dimensional image data.
By periodically updating the maximum standard eigenvector, calculating the perturbation term and performing orthogonal decomposition, correcting the gradient direction, and using momentum SGD or Adam optimizer to update parameters, the gradient is ensured to point to a flat region.
It significantly improves the generalization performance and robustness of image classification models, especially the classification accuracy under non-ideal conditions, and reduces overfitting to training data.
Smart Images

Figure CN121095673A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of deep learning model training technology, and in particular to an image classification method based on sharpness perception minimization. Background Technology
[0002] In recent years, deep learning technology, represented by deep neural networks, has achieved significant breakthroughs in image classification tasks and has been widely applied in various fields such as computer vision, autonomous driving, and medical image recognition. With the rapid advancement of artificial intelligence technology, the parameter size and training data volume of deep learning models are increasing dramatically. However, the generalization performance of deep learning models on test sets still faces severe challenges. The problem of excellent performance on training sets but performance degradation in practical applications is becoming increasingly prominent, especially in application scenarios such as image classification.
[0003] To gain a deeper understanding of and improve the generalization ability of machine learning models, numerous studies have explored this topic from various perspectives, including Bayesian, informational, loss surface geometry, and kernel perspectives. Beyond analyzing the properties of the trained model, some studies have focused on the impact of the training and optimization processes on generalization performance, including implicit regularization in stochastic gradient descent (SGD), the regularization effect of the learning rate, and batch size. These studies have driven continuous improvements in the training process, enhancing the model's generalization ability. For example, Keskar proposed using the Adam optimizer in the early stages of training to achieve fast convergence, and switching to the SGD optimizer in the later stages to improve generalization performance. Izmailov et al. proposed obtaining wider local minima through average weights (expected to improve generalization ability compared to sharp minima), a similar approach later adopted by the Lookahead algorithm. Entropy-SGD avoids solutions falling into sharp valleys by deriving the gradient of local entropy, but its computational cost is significantly higher than conventional training methods due to the need for multiple Langevin iterations.
[0004] Among numerous methods for improving model generalization ability, the Sharpness-Aware Minimization (SAM) algorithm has gained widespread attention in recent years for tasks such as image classification due to its concise and effective optimization strategy. The goal of the SAM algorithm is to minimize a perturbation loss, which is defined as the maximum loss within a radius p centered on the model parameters w. Intuitively, traditional training methods aim to find a point with low loss, while the SAM algorithm seeks a flat region with low maximum loss. This strategy significantly enhances the model's robustness to input perturbations, thus exhibiting stronger generalization ability under non-ideal conditions commonly encountered in image recognition, such as illumination changes, viewpoint shifts, and noise interference.
[0005] However, despite SAM's significant performance improvements on multiple image classification benchmark tasks (such as CIFAR-10 / 100 and ImageNet), its method itself has key limitations. During actual training, the perturbated loss function may exhibit local minima in both flat and sharp regions, meaning that minimizing the perturbation function does not necessarily reduce sharpness, thus limiting its overall effectiveness in improving model generalization. This is particularly common for high-dimensional image classification models because the complex parameter space and highly nonlinear loss terrain can cause the optimization path to slip into sharp regions with poor generalization ability. Summary of the Invention
[0006] To address the shortcomings of existing technologies, this invention provides an image classification method based on sharpness perception minimization, which solves the technical problem of inaccurate gradient pointing when updating model parameters in the traditional SAM algorithm.
[0007] To address the aforementioned technical problems, this invention provides the following technical solution: an image classification method based on sharpness-perceived minimization, comprising the following steps: S1. Set up the training architecture for the image classification model and initialize the network parameters to obtain the raw parameters. ; S2. Periodically update the maximum standard eigenvector using the power method. ; S3. Sample from the image classification dataset, using the current original parameters. The gradient is calculated during forward and backward propagation. ; S4. Gradients obtained from the initial forward and backward propagation. Calculate the disturbance term ; S5, in the original parameters Add perturbation item To obtain the perturbation parameters introduced by the perturbation term. ; S6. Use disturbance parameters Gradients are obtained by performing forward and backward propagation. For the calculated gradient Normalize and along the largest standard eigenvector Perform orthogonal decomposition to obtain the decomposition gradient ; S7. Use gradient decomposition. The corrected gradient is obtained by correcting the original gradient. The network parameters are changed from perturbation parameters. Restore to original parameters Using corrected gradients based on the fundamental optimizer momentum SGD or Adam Update network parameters.
[0008] Furthermore, the image classification model is set to any one of ResNet, VGG, DenseNet, WideResNet, and PyramidNet.
[0009] Furthermore, in step S2, the specific process includes: Load training or validation data from an image classification dataset, standardize the images using mean-variance normalization, and construct a data loader. Before updating the feature vector, directly determine the feature vector. Whether it has been defined or stored in an array; if not, initialize an array with the original parameters from the image classification model. Consistent random vectors as feature vectors ; Then, the image classification model is set to evaluation mode to ensure that random operations during the training phase, such as Dropout / BatchNorm updates, are disabled to maintain computational consistency. The gradient is computed using the backward propagation function and the computation graph is preserved so that the gradient can be differentiated again later. Calculate the gradient in the current feature vector The dot product in the direction, and then the dot product result is used to adjust the original parameters. Differentiation yields the Hessian-vector product; After five iterations of the above process, normalization is performed to obtain the largest standard eigenvector estimated in this round. .
[0010] Furthermore, the disturbance term The calculation formula is: ; In the formula, The parameter value is equal to the original parameter. The gradient value at time; The second norm of the gradient value; The disturbance coefficient; This refers to the perturbation term applied to the parameters in the SAM optimization algorithm.
[0011] Furthermore, in step S5, the specific process includes: First, a mini-batch is sampled from the image classification dataset, denoted as the image inputs and their corresponding labels; Call zero_grad() to clear the original parameters The grad attribute, through enable_running_stats(model), switches the image classification model to training mode, activating the mean and variance updates of the BatchNorm layer in the image classification model; The image is input into the image classification model and forward propagation is performed to obtain the predicted probability logits; based on the loss value between the predicted output logits of the model and the true labels, the scalar loss value is calculated. The loss values from multiple batches are accumulated into a cumulative variable. After averaging the training loss, backpropagation is performed, and `loss.backward()` is called to calculate the current original parameters. gradient ; The `first_step` function in Sharpness-Aware Minimization is executed, iterating through all the original parameters. Extract its gradient p.grad, calculate the L2 norm of the overall gradient, and use normalization to obtain the gradient scaling factor to ensure that the perturbation falls exactly on the spherical boundary. ; Iterate through the parameter group and all parameter vectors within the group, saving the current original parameters. `old_p` is used for subsequent restoration of the original parameters. Calculate the perturbation vector Then, the parameter tensor is added to itself to obtain the perturbation parameters used for subsequent gradient calculations. .
[0012] Furthermore, the gradient scaling factor The calculation formula is: ; In the formula, This is a numerically stable term to prevent the denominator from being 0.
[0013] Furthermore, the orthogonal decomposition yields the decomposition gradient. The expression is: ; In the formula, The gradient component is parallel to the direction of the eigenvector. means parallel; The gradient component is perpendicular to the direction of the eigenvector. Indicates vertical.
[0014] Furthermore, in step S7, the specific process includes: Use torch.no_grad to disable automatic differentiation to ensure that the computation process does not affect the construction of the computation graph; iterate through each parameter w of the model and filter out the parameters with gradients; The calculated gradients of each parameter are flattened into one-dimensional vectors and stored in the gradient list grads_list. Each principal feature vector is also flattened into a one-dimensional vector and concatenated into a vector v_flat, so that the vector v_flat is aligned with the original gradient g_flat for element-wise operation. Calculate the dot product of the original gradient g_flat and the vector v_flat, and denote the sign of the dot product result as ±1 or 0, indicating whether the gradient and the feature vector are in the same direction. Here, 1 indicates that the angle is less than 90°; -1 indicates that the angle is greater than 90°; and 0 indicates that the angle is equal to 90°. Normalize the original gradient g_flat to a unit vector, and then calculate its dot product with the vector v_flat again. This dot product is the cosine similarity between the gradient and the feature. ; Calculate the projection of the gradient vector onto the direction of the eigenvector based on the dot product. ; Since the vector v_flat typically represents the steepest and most unstable direction, i.e., the local sharpness is maximized, the optimization process is flattened by reducing the gradient component in that direction. This yields the perturbation parameters, which are then updated to obtain the corrected gradient. The expression is: ; In the formula, To use gradient decomposition The corrected gradient after correcting the original gradient; To correct the overparameter of strength; This is a direction indicator; for ; When using the momentum SGD optimizer to learn model parameters, the network parameter values at time t are... ; When using the Adam optimizer to learn model parameters, the network parameter values at time t are... The calculation formula is: ; in, The network parameter values at time t; This is the momentum coefficient, used to control the proportion of historical gradients in SGD updates; The first moment estimate of the gradient; It is a first-order momentum decay factor; This is the second moment estimate of the gradient; It is the second-order momentum decay factor; and The bias correction for the moment estimate is used for the final parameter update; The learning rate is used to control the step size for parameter updates. A constant term is added to the denominator to prevent division by zero.
[0015] By employing the above technical solution, the present invention provides an image classification method based on sharpness perception minimization, which has at least the following beneficial effects: 1. This invention corrects the gradient after perturbation in the SAM algorithm by using feature vectors, effectively reducing the abnormal components of the gradient in the direction of the feature vectors, thereby preventing model parameter updates from pointing to sharp regions of the loss function. This correction of the optimization path significantly improves the accuracy of gradient updates during image classification model training, enabling the model to learn more discriminative visual feature representations, ultimately improving classification accuracy and the model's generalization performance on the test set.
[0016] 2. This invention implements gradient correction based on principal feature vectors, which is used to calibrate the update direction of the SAM algorithm in each training round, making it more closely match the real loss geometry. This correction mechanism uses eigenvalue analysis to extract the principal curvature direction corresponding to the current gradient, and projects and reconstructs the gradient direction, thereby improving the robustness and stability of the optimizer in image classification tasks.
[0017] 3. Compared with the traditional SAM algorithm, the image classification method proposed in this invention achieves superior test accuracy on several typical models. This indicates that the proposed correction mechanism has a significant effect on improving the generalization ability of image classification models, and is particularly suitable for the training and optimization process of convolutional neural networks on natural image datasets. Attached Figure Description
[0018] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings: Figure 1 This is a flowchart of the image classification method in this invention; Figure 2 The graph shows the change in validation set accuracy of the ResNet-18 model, which introduces the image classification method in this invention, on the CIFAR-10 image classification benchmark dataset. Figure 3 The graph shows the change in validation set accuracy of the AlexNet model, which introduces the image classification method in this invention, on the image classification benchmark dataset CIFAR-10. Figure 4The graph shows the change in validation set accuracy of the Wide-ResNet-28 model, which introduces the image classification method in this invention, on the CIFAR-10 image classification benchmark dataset. Detailed Implementation
[0019] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. This will allow for a full understanding of how the present application uses technical means to solve technical problems and achieve technical effects, and to facilitate its implementation.
[0020] The traditional Sharpness-Aware Minimization (SAM) algorithm, by finding adversarial directions in the parameter space and jointly minimizing the training loss and the maximum loss in its neighborhood, tends towards a flat minimum solution, demonstrating strong generalization ability in multiple image recognition tasks. However, its gradient update direction is often affected by local perturbations or feature noise in image data, leading to unstable optimization paths, especially when training deep convolutional neural networks. This embodiment, based on the original Sharpness-Aware Minimization (SAM) algorithm, introduces a gradient direction correction mechanism for image classification tasks to significantly improve the stability of the optimization process and the model's generalization ability on image recognition test data.
[0021] Because the sharpness-aware minimization algorithm, when seeking regions with lower loss, may find the minimum loss function value after perturbation in either flat or sharp areas, the model's parameter updates may not necessarily point to regions with theoretically lower loss values. To address the issue of inaccurate gradient pointing during model parameter updates in the traditional SAM algorithm, this embodiment proposes an image classification method based on sharpness-aware minimization to improve the accuracy of gradient updates, thereby enhancing the robustness and stability of the optimizer in image classification tasks. Figure 1 As shown, the method includes the following steps: S1. Set up the training architecture for the image classification model and initialize the network parameters to obtain the raw parameters. In this embodiment, a standard model for image classification, such as ResNet, VGG, DenseNet, WideResNet, PyramidNet, etc., is first selected, and then the network parameters are initialized.
[0022] S2. Periodically update the maximum standard eigenvector using the power method. The specific process includes: Load training or validation data from an image classification dataset, standardize the images (mean and variance normalization), and construct a data loader for batch data reading and transfer to the computing device. Before updating the feature vectors, directly determine the feature vectors. Whether it has been defined or stored in an array; if not, initialize an array with the original parameters from the image classification model. Consistent random vectors as feature vectors Then, the image classification model is set to evaluation mode to ensure that random operations during the training phase, such as Dropout / BatchNorm updates, are disabled, maintaining computational consistency. The gradient is computed using the backpropagation function `backward(create_graph=True)`, preserving the computation graph for subsequent differentiation of the gradient. The gradient is then computed on the current feature vector. The dot product in the direction, and then the dot product result is used to adjust the original parameters. Differentiation is performed to obtain the Hessian-vector product; after five iterations of the above process, normalization is performed to obtain the largest standard eigenvector estimated in this round. .
[0023] S3. Sample from the image classification dataset, using the current original parameters. The gradient is calculated during forward and backward propagation. This embodiment provides an example of forward and backward propagation. The forward propagation process of a neural network involves inputting samples and outputting the network's predicted values through a loss function. Backpropagation involves calculating the partial derivatives of the loss function with respect to the parameters to obtain the corresponding gradients. Since existing technologies can be directly used for this, detailed explanations are omitted. The expressions for forward and backward propagation are as follows: ; in, For input samples; This is a real label; The predicted value output by the network; and These are all weight matrices of the network, with subscripts indicating different weights in different layers; This is the output structure of the linear transformation layer; The output after activation; For activation functions; The cross-entropy loss function; This is the partial derivative of the loss function with respect to the activation values; This is the partial derivative of the activation function with respect to the output of the linear layer; The partial derivatives of the linear transformation with respect to the parameter matrix; superscript Indicates the first Layers are used to distinguish parameters or variables in different layers of a neural network; , As a bias term, in the linear transformation of a neural network, the bias term is used to adjust the reference position of the output value; This is the output of the hidden layer.
[0024] S4. Gradients obtained from the initial forward and backward propagation. Calculate the disturbance term The calculation formula is: ; In the formula, The parameter value is equal to the original parameter. The gradient value at time; The second norm of the gradient value is obtained by taking the square root of the sum of the squares of all components of the vector. The disturbance coefficient; This refers to the perturbation term applied to the parameters in the SAM optimization algorithm.
[0025] S5, in the original parameters Add the perturbation term calculated in step S3 above. To obtain the perturbation parameters introduced by the perturbation term. The expression is: ; In the formula, The parameter values obtained after adding the perturbation term.
[0026] In step S5, this embodiment first samples a mini-batch from the image classification dataset, denoted as image inputs and their corresponding labels. Then, zero_grad() is called to clear the original parameters. The `grad` attribute is used to prevent the gradient accumulation from the previous backpropagation from affecting the current calculation. The image classification model is switched to training mode via `enable_running_stats(model)`, activating the mean and variance updates of the BatchNorm layer in the model. An image is input into the model for forward propagation, yielding the predicted probability `logits`. Based on the loss value between the model's predicted output `logits` and the true labels, a scalar loss value `loss` is calculated. The loss values from multiple batches are accumulated into a cumulative variable, and after averaging the training loss, backpropagation is performed, calling `loss.backward()` to calculate the current original parameters. gradient .
[0027] The `first_step` function in Sharpness-Aware Minimization is executed, iterating through all the original parameters. Extract its gradient p.grad, calculate the L2 norm of the overall gradient, and to ensure that the perturbation falls exactly on the spherical boundary, enable normalization to obtain the result. ,in This is the gradient scaling factor. To ensure numerical stability and prevent the denominator from being zero, the parameter set and all parameter vectors within it are traversed, and the current original parameters are saved. `old_p` is used for subsequent restoration of the original parameters. Calculate the perturbation vector Then, the parameter tensor is added to itself to obtain the perturbation parameters used for subsequent gradient calculations. .
[0028] S6. Use disturbance parameters Gradients are obtained by performing forward and backward propagation. For the calculated gradient Normalize and along the largest standard eigenvector Perform orthogonal decomposition to obtain the decomposition gradient The expression is: ; In the formula, The gradient component is parallel to the direction of the eigenvector. means parallel; The gradient component is perpendicular to the direction of the eigenvector. Indicates vertical.
[0029] S7. Use gradient decomposition. The corrected gradient is obtained by correcting the original gradient. The network parameters are changed from perturbation parameters. Restore to original parameters Using corrected gradients based on the fundamental optimizer momentum SGD or Adam Update network parameters.
[0030] In this embodiment, `torch.no_grad` is used to disable the automatic differentiation mechanism to ensure that the calculation process does not affect the construction of the computation graph. Each parameter `w` of the model is traversed, and parameters with gradients are selected. The calculated gradients of each parameter are flattened into one-dimensional vectors and stored in the gradient list `grads_list`. Each principal feature vector is also flattened into a one-dimensional vector and concatenated into a vector `v_flat`, aligning `v_flat` with the original gradient `g_flat` for element-wise operations. The dot product of the original gradient `g_flat` and the vector `v_flat` is calculated, and the dot product result is marked with the sign `sign_gv` (±1 or 0), indicating whether the gradient and feature vector directions are consistent. 1 indicates an angle less than 90°; -1 indicates an angle greater than 90°; and 0 indicates an angle equal to 90°.
[0031] Normalize the original gradient g_flat to a unit vector, and then calculate its dot product with the vector v_flat again. This dot product is the cosine similarity between the gradient and the feature. ; Calculate the projection of the gradient vector onto the direction of the eigenvector based on the dot product. The expression is: Since the vector v_flat typically represents the steepest and most unstable direction, i.e., the local sharpness is maximized, the optimization process is flattened by reducing the gradient component in that direction, resulting in the perturbation parameters and updated gradients, expressed as: ; In the formula, To use gradient decomposition The corrected gradient after correcting the original gradient; To correct the overparameter of strength; This is a direction indicator; for .
[0032] Assuming that after calculating the corrected gradient, the gradient value is When using the momentum SGD optimizer to learn the model parameters, the network parameter values at time t are... .
[0033] Or, when using the Adam optimizer to learn model parameters, the network parameter values at time t. The calculation formula is: ; in, The network parameter values at time t; This is the momentum coefficient, used to control the proportion of historical gradients in SGD updates; The first moment estimate of the gradient; It is a first-order momentum decay factor; This is the second moment estimate of the gradient; It is the second-order momentum decay factor; and The bias correction for the moment estimate is used for the final parameter update; The learning rate is used to control the step size for parameter updates. A constant term is added to the denominator to prevent division by zero.
[0034] The method proposed in this embodiment can significantly improve the robustness and generalization ability of the model under non-ideal input conditions in image classification tasks. Its effectiveness has been verified, especially in experiments on datasets such as CIFAR-10 and CIFAR-100, as follows: Figures 2-4 The graphs show the changes in validation set accuracy obtained by the proposed XSAM method compared to the traditional SAM algorithm when optimizing ResNet-18, AlexNet, and Wide-ResNet-28 on the CIFAR-10 dataset. In benchmark tests on the CIFAR-10 dataset, compared to the SAM algorithm, the improved algorithm significantly improves the generalization performance of each model: ResNet-18 model shows a 0.93% improvement, AlexNet model a 1.48% improvement, and WideResNet-28 model a 0.63% improvement. By explicitly reducing the magnitude of gradient updates in sharp directions, the model training path tends to converge to a wide, flat loss region, reducing overfitting to the training data distribution and thus better adapting to changes in the test distribution.
[0035] Those skilled in the art will understand that all or part of the steps in the methods of the above embodiments can be implemented by a program instructing related hardware. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Moreover, this application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0036] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. Since the above embodiments are substantially similar to the method embodiments, their descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0037] The above embodiments provide a detailed description of the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. An image classification method based on sharpness-perceived minimization, characterized in that, The method includes the following steps: S1. Set up the training architecture for the image classification model and initialize the network parameters to obtain the raw parameters. ; S2. Periodically update the maximum standard eigenvector using the power method. ; S3. Sample from the image classification dataset, using the current original parameters. The gradient is calculated during forward and backward propagation. ; S4. Gradients obtained from the initial forward and backward propagation. Calculate the disturbance term ; S5, in the original parameters Add perturbation item To obtain the perturbation parameters introduced by the perturbation term. ; S6. Use disturbance parameters Gradients are obtained by performing forward and backward propagation. For the calculated gradient Normalize and along the largest standard eigenvector Perform orthogonal decomposition to obtain the decomposition gradient ; S7. Use gradient decomposition. The corrected gradient is obtained by correcting the original gradient. The network parameters are changed from perturbation parameters. Restore to original parameters Using corrected gradients based on the fundamental optimizer momentum SGD or Adam Update network parameters.
2. The image classification method according to claim 1, characterized in that, The image classification model is set to any one of ResNet, VGG, DenseNet, WideResNet, and PyramidNet.
3. The image classification method according to claim 1, characterized in that, In step S2, the specific process includes: Load training or validation data from an image classification dataset, standardize the images using mean-variance normalization, and construct a data loader. Before updating the feature vector, directly determine the feature vector. Whether it has been defined or stored in an array; if not, initialize an array with the original parameters from the image classification model. Consistent random vectors as feature vectors ; Then, the image classification model is set to evaluation mode to ensure that random operations during the training phase, such as Dropout / BatchNorm updates, are disabled to maintain computational consistency. The gradient is computed using the backward propagation function and the computation graph is preserved so that the gradient can be differentiated again later. Calculate the gradient in the current feature vector The dot product in the direction, and then the dot product result is used to adjust the original parameters. Differentiation yields the Hessian-vector product; After five iterations of the above process, normalization is performed to obtain the largest standard eigenvector estimated in this round. .
4. The image classification method according to claim 1, characterized in that, The disturbance term The calculation formula is: ; In the formula, The parameter value is equal to the original parameter. The gradient value at time; The second norm of the gradient value; The disturbance coefficient; This refers to the perturbation term applied to the parameters in the SAM optimization algorithm.
5. The image classification method according to claim 1, characterized in that, In step S5, the specific process includes: First, a mini-batch is sampled from the image classification dataset, denoted as the image inputs and their corresponding labels; Call zero_grad() to clear the original parameters The grad attribute, through enable_running_stats(model), switches the image classification model to training mode, activating the mean and variance updates of the BatchNorm layer in the image classification model; The image is input into the image classification model and forward propagation is performed to obtain the predicted probability logits; based on the loss value between the predicted output logits of the model and the true labels, the scalar loss value is calculated. The loss values from multiple batches are accumulated into a cumulative variable. After averaging the training loss, backpropagation is performed, and `loss.backward()` is called to calculate the current original parameters. gradient ; The `first_step` function in Sharpness-Aware Minimization is executed, iterating through all the original parameters. Extract its gradient p.grad, calculate the L2 norm of the overall gradient, and use normalization to obtain the gradient scaling factor to ensure that the perturbation falls exactly on the spherical boundary. ; Iterate through the parameter group and all parameter vectors within the group, saving the current original parameters. `old_p` is used for subsequent restoration of the original parameters. Calculate the perturbation vector Then, the parameter tensor is added to itself to obtain the perturbation parameters used for subsequent gradient calculations. .
6. The image classification method according to claim 5, characterized in that, The gradient scaling factor The calculation formula is: ; In the formula, This is a numerically stable term to prevent the denominator from being 0.
7. The image classification method according to claim 1, characterized in that, The orthogonal decomposition obtains the decomposition gradient. The expression is: ; In the formula, The gradient component is parallel to the direction of the eigenvector. means parallel; The gradient component is perpendicular to the direction of the eigenvector. Indicates vertical.
8. The image classification method according to claim 7, characterized in that, In step S7, the specific process includes: Use torch.no_grad to disable automatic differentiation to ensure that the computation process does not affect the construction of the computation graph; iterate through each parameter w of the model and filter out the parameters with gradients; The calculated gradients of each parameter are flattened into one-dimensional vectors and stored in the gradient list grads_list. Each principal feature vector is also flattened into a one-dimensional vector and concatenated into a vector v_flat, so that the vector v_flat is aligned with the original gradient g_flat for element-wise operation. Calculate the dot product of the original gradient g_flat and the vector v_flat, and denote the sign of the dot product result as ±1 or 0, indicating whether the gradient and the feature vector are in the same direction. Here, 1 indicates that the angle is less than 90°; -1 indicates that the angle is greater than 90°; and 0 indicates that the angle is equal to 90°. Normalize the original gradient g_flat to a unit vector, and then calculate its dot product with the vector v_flat again. This dot product is the cosine similarity between the gradient and the feature. ; Calculate the projection of the gradient vector onto the direction of the eigenvector based on the dot product. ; Since the vector v_flat typically represents the steepest and most unstable direction, i.e., the local sharpness is maximized, the optimization process is flattened by reducing the gradient component in that direction. This yields the perturbation parameters, which are then updated to obtain the corrected gradient. The expression is: ; In the formula, To use gradient decomposition The corrected gradient after correcting the original gradient; To correct the overparameter of strength; This is a direction indicator; for ; When using the momentum SGD optimizer to learn model parameters, the network parameter values at time t are... ; When using the Adam optimizer to learn model parameters, the network parameter values at time t are... The calculation formula is: ; in, The network parameter values at time t; This is the momentum coefficient, used to control the proportion of historical gradients in SGD updates; The first moment estimate of the gradient; It is a first-order momentum decay factor; This is the second moment estimate of the gradient; It is the second-order momentum decay factor; and The bias correction for the moment estimate is used for the final parameter update; The learning rate is used to control the step size for parameter updates. A constant term is added to the denominator to prevent division by zero.
Citation Information
Patent Citations
Calcareous sand unit test broken particle tracking and matching method based on digital image
CN120107651A
Image processing model training method and device, equipment and medium
CN120317288A
Morphological gradient region replacement method based on SAM semantic segmentation and user guidance
CN120471786A
Cited By
Image classification model training control method and device, equipment and medium
CN121459079A
A control method, apparatus, device, and medium for training an image classification model.
CN121459079B