Method and device for optimizing network model, electronic equipment and storage medium
By identifying and correcting dropout layers in the quantization network model, the problem of decreased accuracy of the quantization model on edge devices was solved, thereby improving model quality and efficiency.
Patent Information
- Application Number
- CN202210289198.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-22
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2042-03-22
AI Technical Summary
When deploying deep learning models on edge devices, due to limited computing power and storage resources, quantization errors may be introduced into the model after quantization, leading to a decrease in accuracy, which existing technologies have not been able to effectively solve.
By obtaining the output of the target network model, missing layers are identified and corrected, including grouping feature parameters, calculating distribution feature values, and correcting outlier feature values, thereby optimizing the quantized network model.
It improves the quality and efficiency of the quantized network model, reduces the model size, and enhances the model's inference speed and accuracy.
Smart Images

Figure CN114676820B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of information technology, and in particular to a method, apparatus, electronic device, and storage medium for optimizing a network model. Background Technology
[0002] Currently, due to the limited computing power and storage resources of edge devices, deep learning models typically need to be quantized when deployed for inference computation to reduce storage requirements and improve the inference speed of the network model. Offline quantization, with its relatively low hardware requirements, is more suitable for industrial applications and is the quantization method supported by most hardware devices.
[0003] However, quantization often introduces quantization errors into the model, which may lead to a decrease in accuracy (i.e., dropped points). Summary of the Invention
[0004] The purpose of this application is to provide a method, apparatus, electronic device, and storage medium for optimizing a network model, so as to optimize the quantized network model. The specific technical solution is as follows:
[0005] A first aspect of this application provides a method for optimizing a network model, comprising:
[0006] Obtain a pre-trained target network model and a quantized network model obtained by quantizing the target network model, wherein both the quantized network model and the target network model include multiple network layers;
[0007] The sample data is acquired and input into the target network model for calculation to obtain the output results of each network layer in the target network model, wherein the output results of each network layer in the target network model include the target output result of the last layer;
[0008] Based on the output results of each network layer in the target network model, determine the drop-point layer in the quantized network model;
[0009] The target network layer corresponding to the drop point layer in each network layer of the target network model is modified to obtain the modified target network model.
[0010] Optionally, determining the drop-point layers in the quantized network model based on the output results of each network layer in the target network model includes:
[0011] From the output results of each network layer in the target network model, the output result of the i-th layer is selected as the currently selected output result, where i is an integer from 1 to N, and N is the total number of network layers in the quantized network model;
[0012] The currently selected output result is input into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model.
[0013] The output of the quantized network model is compared with the target output. Based on the comparison result, an output result is selected from the output results of each network layer in the target network model as the current selected result. The process of inputting the current selected result into the (i+1)th network layer of the quantized network model and calculating the output result of the quantized network model continues until the difference between the output result of the current network layer and the target output result meets the preset error and the difference between the output result of the previous layer of the current network layer and the target output result does not meet the preset error. Then, the previous layer of the current network layer is determined as the drop point layer.
[0014] Optionally, the step of comparing the output of the quantized network model with the target output, selecting an output from each network layer of the target network model as the current selected result based on the comparison result, and returning to the step of inputting the current selected result into the (i+1)th network layer of the quantized network model to calculate the output of the quantized network model continues until the difference between the output of the current network layer and the target output meets the preset error and the difference between the output of the previous layer and the target output does not meet the preset error, includes:
[0015] The output of the quantized network model is compared with the target output.
[0016] If the difference between the output of the quantized network model and the target output meets the preset error, then an output result is selected from the output results of the first to the i-th layers of the target network model as the currently selected output result; if the difference between the output of the quantized network model and the target output does not meet the preset error, then an output result is selected from the output results of the i-th to the N-th layers of the target network model as the currently selected output result, and the step of inputting the currently selected result into the (i+1)-th network layer of the quantized network model to calculate the output result of the quantized network model continues to be executed until the difference between the output result corresponding to the current network layer and the target output meets the preset error and the difference between the output result corresponding to the previous layer of the current network layer and the target output does not meet the preset error, then the previous layer of the current network layer is determined as the drop point layer.
[0017] Optionally, the step of modifying the target network layer corresponding to the dropped layer in each network layer of the target network model to obtain the modified target network model includes:
[0018] The feature parameters of the target network layer are grouped to obtain multiple feature value groups;
[0019] Calculate the distribution characteristic values of the characteristic parameters in each characteristic value group;
[0020] Identify outlier distributional features in each feature set;
[0021] The feature value group containing the outlier distribution feature value is corrected to obtain the corrected target network model.
[0022] Optionally, the feature parameters of the target network layer are grouped to obtain multiple feature value groups, including:
[0023] When the feature parameter is a feature parameter of a convolution kernel, the feature parameter is grouped according to the convolution kernel corresponding to the feature parameter to obtain multiple feature value groups; when the feature parameter is a feature parameter of an input or output channel, the feature parameter is classified according to the channel corresponding to the feature parameter to obtain multiple feature value groups.
[0024] Optionally, the step of modifying the target network layer to obtain the modified target network model includes:
[0025] Calculate the mean of the distributional characteristic values of the characteristic parameters in the plurality of characteristic value groups;
[0026] Calculate the ratio of the mean to the distribution characteristic value of the outliers to obtain the proportionality coefficient;
[0027] The feature parameters in the feature value group containing the outlier distribution feature value are scaled according to the scaling factor to obtain the corrected target network model.
[0028] Optionally, the step of correcting the feature set containing the outlier distribution feature values to obtain the corrected target network model includes:
[0029] The feature parameters in the feature value group containing the outlier distribution feature value are amplified or reduced, and one or more revision layers are added before or after the target network layer. The revision layers are used to reduce the input data when the feature parameters are amplified, and to amplify the input data when the feature parameters are reduced.
[0030] Optionally, the step of correcting the feature set containing the outlier distribution feature values to obtain the corrected quantized network model includes:
[0031] The feature parameters in the feature value group containing the outlier distribution feature value are amplified or reduced, and one or more network layers before or after the target network layer are modified. The modification of one or more network layers before or after the target network layer includes: when amplifying the feature parameters, modifying one or more network layers before the target network layer to reduce the output data, or modifying one or more network layers after the target network layer to reduce the output data; when reducing the feature parameters, modifying one or more network layers before the target network layer to amplify the output data, or modifying one or more network layers after the target network layer to amplify the output data.
[0032] A second aspect of this application provides a network model tuning apparatus, comprising:
[0033] The model acquisition module is used to acquire a pre-trained target network model and a quantized network model obtained by quantizing the target network model, wherein both the quantized network model and the target network model include multiple network layers.
[0034] The model calculation module is used to acquire and input sample data into the target network model for calculation, and obtain the output results of each network layer in the target network model, wherein the output results of each network layer in the target network model include the target output result of the last layer;
[0035] The drop point identification module is used to determine the drop point layer in the quantized network model based on the output results of each network layer in the target network model.
[0036] The model correction module is used to correct the target network layer corresponding to the dropped layer in each network layer of the target network model to obtain the corrected target network model.
[0037] Optionally, the drop point layer identification module includes:
[0038] The output result selection submodule is used to select the output result of the i-th layer from the output results of each network layer in the target network model as the currently selected output result, where i is an integer from 1 to N, and N is the total number of network layers in the quantized network model.
[0039] The output result calculation submodule is used to input the currently selected output result into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model.
[0040] The output result comparison submodule is used to compare the output result of the quantized network model with the target output result. Based on the comparison result, it reselects an output result from the output results of each network layer in the target network model as the current selected result, and returns to the step of inputting the current selected result into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model. This process continues until the difference between the output result corresponding to the current network layer and the target output result meets the preset error, and the difference between the output result corresponding to the previous layer of the current network layer and the target output result does not meet the preset error. Then, the previous layer of the current network layer is determined as the drop point layer.
[0041] Optionally, the output result comparison submodule includes:
[0042] The result comparison unit is used to compare the output result of the quantized network model with the target output result;
[0043] The result selection unit is used to select an output result from the output results of the first to the i-th layers of the target network model as the currently selected output result if the difference between the output result of the quantized network model and the target output result meets the preset error; if the difference between the output result of the quantized network model and the target output result does not meet the preset error, then select an output result from the output results of the i-th to the N-th layers of the target network model as the currently selected output result, and return to the step of inputting the currently selected result into the (i+1)-th network layer of the quantized network model to calculate the output result of the quantized network model, until the difference between the output result corresponding to the current network layer and the target output result meets the preset error and the difference between the output result corresponding to the previous layer of the current network layer and the target output result does not meet the preset error, then the previous layer of the current network layer is determined as the drop point layer.
[0044] Optionally, the model correction module includes:
[0045] The parameter grouping submodule is used to group the feature parameters of the target network layer to obtain multiple feature value groups;
[0046] The eigenvalue calculation submodule is used to calculate the distribution eigenvalues of the eigenparameters in each eigenvalue group.
[0047] The outlier feature value identification submodule is used to identify outlier distribution feature values in each feature value group;
[0048] The outlier feature value correction submodule is used to correct the feature value group to which the outlier distribution feature value belongs, so as to obtain the corrected target network model.
[0049] Optionally, the parameter grouping submodule is specifically used to group the feature parameters according to the convolution kernel corresponding to the feature parameters when the feature parameters are feature parameters of the convolution kernel, to obtain multiple feature value groups; and to classify the feature parameters according to the channel corresponding to the feature parameters when the feature parameters are feature parameters of the input or output channel, to obtain multiple feature value groups.
[0050] Optionally, the outlier eigenvalue correction submodule includes:
[0051] The mean calculation unit is used to calculate the mean of the distribution characteristic values of the feature parameters in the plurality of feature value groups;
[0052] The proportionality coefficient calculation unit is used to calculate the ratio of the mean to the distribution characteristic value of the outliers to obtain the proportionality coefficient;
[0053] The feature parameter scaling unit is used to scale the feature parameters in the feature value group where the outlier distribution feature value is located according to the scaling coefficient, so as to obtain the corrected target network model.
[0054] Optionally, the outlier feature value correction submodule is specifically used to amplify or reduce the feature parameters in the feature value group where the outlier distribution feature value is located, and to correct one or more network layers before or after the target network layer. Specifically, when amplifying the feature parameters, the output data is reduced by correcting one or more network layers before the target network layer, or by correcting one or more network layers after the target network layer. Conversely, when reducing the feature parameters, the output data is amplified by correcting one or more network layers before the target network layer, or by correcting one or more network layers after the target network layer.
[0055] Optionally, the outlier feature value correction submodule is specifically used to amplify or reduce the feature parameters in the feature value group where the outlier distribution feature value is located, and to correct one or more network layers before or after the target network layer. Correcting one or more network layers before or after the target network layer includes: when amplifying the feature parameters, correcting one or more network layers before the target network layer to reduce the output data, or correcting one or more network layers after the target network layer to reduce the output data; when reducing the feature parameters, correcting one or more network layers before the target network layer to amplify the output data, or correcting one or more network layers after the target network layer to amplify the output data.
[0056] Another aspect of the embodiments of this application also provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0057] Memory, used to store computer programs;
[0058] The processor, when executing programs stored in memory, implements the tuning methods for any of the above network models.
[0059] In another aspect of the embodiments of this application, a computer-readable storage medium is provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements the tuning method of any of the above-described network models.
[0060] In another aspect of the embodiments of this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute the tuning method of any of the above-described network models.
[0061] Beneficial effects of the embodiments in this application:
[0062] The network model tuning method, apparatus, electronic device, and storage medium provided in this application can acquire a pre-trained target network model and a quantized network model obtained after quantizing the target network model. Both the quantized network model and the target network model include multiple network layers. Sample data is acquired and input into the target network model for calculation to obtain the output results of each network layer in the target network model, including the target output result of the last layer. Based on the output results of each network layer in the target network model, the dropout layers in the quantized network model are determined. The target network layers corresponding to the dropout layers in each network layer of the target network model are then corrected to obtain a corrected target network model. Through the method of this application, by inputting the input data of the target network model into the corresponding network layer of the quantized network model, comparing the final output results, determining the dropout layers of the quantized network model, and correcting the target network model based on the dropout layers, the network model can be tuned, improving the quality of the quantized network model and increasing quantization efficiency.
[0063] Of course, implementing any product or method of this application does not necessarily require achieving all of the advantages described above at the same time. Attached Figure Description
[0064] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other embodiments can be obtained based on these drawings.
[0065] Figure 1 A flowchart illustrating a network model tuning method provided in an embodiment of this application;
[0066] Figure 2 A flowchart illustrating the selection of the current result provided in an embodiment of this application;
[0067] Figure 3 An example diagram illustrating the network model tuning method provided in this application embodiment;
[0068] Figure 4 Another example diagram illustrating the network model tuning method provided in this application embodiment;
[0069] Figure 5 A schematic diagram illustrating the process of modifying the network model provided in an embodiment of this application;
[0070] Figure 6 This is a schematic diagram illustrating the process of correcting the feature value group containing the outlier distribution feature value provided in an embodiment of this application;
[0071] Figure 7 A schematic diagram of a network model tuning device provided in an embodiment of this application;
[0072] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0073] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art based on this application are within the scope of protection of this application.
[0074] First, the technical terms that may be used in the embodiments of this application will be explained:
[0075] Quantization: When deep learning models perform inference on edge devices, compression is necessary to address power consumption and performance concerns. Quantization is a common model compression method. It converts the weights or calculated values of a deep learning model from floating-point (Float32) to fixed-point (int16 / int8 / int4, etc.) representations, enabling fixed-point inference computation. This achieves goals such as reducing model size, minimizing memory consumption, and accelerating inference speed while maintaining model accuracy. Essentially, quantization establishes a mapping between floating-point and fixed-point numbers. This mapping can be constructed using different algorithms and parameters—the quantization algorithm and quantization parameters. The objects of quantization include the weights, inputs, and outputs of each layer of the model.
[0076] Offline quantization: Quantization typically includes two types: post-training quantization and training-aware quantization. Post-training quantization, also known as offline quantization, directly quantizes a trained floating-point model, saving float data as int data according to a specific quantization algorithm to obtain the quantized model. This method is relatively simple and is currently the most commonly used quantization method. Online quantization, on the other hand, introduces quantization operations during model training, making the trained model a quantized model. This application primarily focuses on the accuracy analysis and tuning of offline quantized models.
[0077] Accuracy drop: For the same test samples, when the accuracy of the quantized model decreases significantly compared to the original floating-point model, it is considered that the quantized model has experienced an accuracy drop, i.e., an accuracy drop. The degree of accuracy drop that qualifies as an accuracy drop depends on the actual application scenario, but is commonly defined as 2% or higher.
[0078] Dropped-point layers: When a deep learning model drops points due to one or more layers in the model, these layers that cause the model to drop points are called dropped-point layers.
[0079] Custom Layers: Neural network models contain various network layers, broadly categorized into public layers and custom layers. Public layers are general-purpose, functionally defined layers, such as Convolution, ReLU, and Batchnorm. The logic, functionality, and parameters of public layers are consistent across different models. Different deep learning frameworks (Caffe, PyTorch, TensorFlow, etc.) pre-support common public layers, eliminating the need for user development. In contrast, custom layers have their functionality and logic entirely defined by the developer, requiring user development. Typically, custom layers are not quantized.
[0080] A first aspect of this application provides a method for optimizing a network model, comprising:
[0081] Obtain a pre-trained target network model and a quantized network model obtained by quantizing the target network model. Both the quantized network model and the target network model include multiple network layers.
[0082] The sample data is acquired and input into the target network model for calculation to obtain the output results of each network layer in the target network model. The output results of each network layer in the target network model include the target output result of the last layer.
[0083] Based on the output results of each network layer in the target network model, determine the drop-point layers in the quantization network model;
[0084] The target network layer corresponding to the missing layer in each network layer of the target network model is modified to obtain the modified target network model.
[0085] As can be seen, the method of this application embodiment can be used to input the input data of the target network model into the network layer of the corresponding quantized network model, determine the drop-point layer of the quantized network model by comparing the final output results, and modify the target network model according to the drop-point layer, thereby achieving network model optimization. When the adjusted network model is quantized, the quality of the quantized network model can be improved and the quantization efficiency can be improved.
[0086] For details, see Figure 1 , Figure 1 A flowchart illustrating a network model tuning method provided in this application embodiment includes:
[0087] Step S11: Obtain the pre-trained target network model and the quantized network model obtained after quantizing the target network model.
[0088] Both the quantized network model and the target network model include multiple network layers. In practical applications, quantizing the network model can compress it, reducing model size, memory consumption, and inference speed while maintaining accuracy. However, the number of network layers remains unchanged during quantization. For example, when the target network model in this embodiment is a face recognition model, which includes nine network layers, quantizing it also results in a model with nine network layers.
[0089] Step S12: Obtain and input the sample data into the target network model for calculation to obtain the output results of each network layer in the target network model.
[0090] The output of each network layer in the target network model includes the target output of the last layer. Acquiring and inputting sample data into the target network model for calculation can be done by obtaining the corresponding sample data based on the function of the target network model. For example, when the target network model is a face recognition model, a face sample to be recognized is acquired and input into the face recognition model for recognition, and the corresponding recognition result is calculated, such as whether the face to be recognized is the target face. The output of each network layer in the target network model includes the target output of the last layer. This last layer's output is the calculation result of the network model in actual use. For example, when the target network model is a face recognition model, the output of the last layer is whether the face to be recognized is the target face.
[0091] For example, select certain test samples as model input, such as a face recognition model, where the test samples are a batch of face images to be processed. Perform forward inference computation on the unquantized model (i.e., the original floating-point model) in float format within the original framework. When the input images are different, the output value of layer 1, float_layer1, and the final output value of the model, float_out, will be different. Record the float_layer1 value and float_out value corresponding to each input sample.
[0092] Step S13: Determine the dropout layer in the quantization network model based on the output results of each network layer in the target network model.
[0093] Based on the output results of each network layer in the target network model, the drop point layer in the quantization network model can be determined. This can be done by inputting the output results of each network layer in the target network model into the corresponding network layer in the quantization network model to obtain multiple output results. These multiple output results are then compared with the output results of the target network model to determine the drop point layer in the quantization network model.
[0094] Optionally, based on the output results of each network layer in the target network model, the drop point layer in the quantization network model is determined, including: selecting the output result of the i-th layer from the output results of each network layer in the target network model as the currently selected output result, where i is an integer from 1 to N, and N is the total number of network layers in the quantization network model; inputting the currently selected output result into the (i+1)-th network layer of the quantization network model to calculate the output result of the quantization network model; comparing the output result of the quantization network model with the target output result, and based on the comparison result, selecting an output result from the output results of each network layer in the target network model as the currently selected result, and returning to the step of inputting the currently selected result into the (i+1)-th network layer of the quantization network model to calculate the output result of the quantization network model, until the difference between the output result corresponding to the current network layer and the target output result meets the preset error and the difference between the output result corresponding to the previous layer of the current network layer and the target output result does not meet the preset error, then the previous layer of the current network layer is determined as the drop point layer.
[0095] In this context, when selecting the output of layer i from the outputs of each network layer in the target network model, i is an integer from 1 to N, and N is the total number of network layers in the quantized network model. In practical use, selecting the output of layer i involves multiple loops. It can be done in a preset order, such as from front to back, or from back to front. Alternatively, a binary search method can be used; the specific binary search method can be found in subsequent embodiments. A random order can also be used; for example, after randomly selecting the output of one network layer for calculation, a new network layer can be randomly selected from the remaining unselected layers in the next loop. In practical use, the above floating-point model can be deployed on an edge device, quantized according to hardware requirements, and the same test samples used as model input to complete the forward inference calculation of the quantized model on the device.
[0096] The process involves inputting the currently selected output result into the (i+1)th layer of the quantized network model to calculate its output. Alternatively, the output of a specific layer in the target network model can be input into the next iteration of that layer in the corresponding quantized network model to obtain the final calculation result. For example, if both the target network model and the corresponding quantized network model have nine layers, the output of the fifth layer in the target network model can be selected and input into the sixth layer of the quantized network model. Then, calculations are performed through layers 6 through 9 of the quantized network model to obtain the final result. For instance, if the target network model is a face recognition model, the final calculation result is whether the face to be recognized is the target face. In practical applications, this floating-point model can be deployed on edge devices, quantized according to hardware requirements, and the same test samples used as model input to complete the forward inference calculation of the quantized network model on the device. During the forward computation of the quantized network model, the input of layer2 should originally be the computation result of layer1 after quantization, i.e., quant_layer1. In order to locate the position of the quantized dropout layer, the input of layer2 is now replaced with the corresponding float_layer1, and then the subsequent inference computation of the quantized model continues to obtain the final output result quant_out of the model.
[0097] The process involves comparing the output of the quantized network model with the target output. Based on the comparison, a new output is selected from the outputs of each layer in the target network model. This selected output is then input into the (i+1)th layer of the quantized network model to calculate the quantized network model's output. This process continues until the difference between the current layer's output and the target output meets a preset error, while the difference between the layer preceding the current layer and the target output does not. In this case, the layer preceding the current layer is designated as the "dropped layer." Similarly, the difference between the layer following the dropped layer and the target output meets the preset error, but not the preset error. The output of the layer following the dropped layer is obtained by inputting the output of the target network model's layer corresponding to the dropped layer into the quantized network model. For example, if the dropped layer in the quantized network model is layer 5, the output of layer 5 in the target network model is input into layer 6 of the quantized network model to obtain the calculated quantized network model. The output of the dropout layer is the output of the previous layer of the target network model. The output of the dropout layer is input into the quantized network model to calculate the output of the dropout layer. For example, if the dropout layer in the quantized network model is the 5th layer, the output of the 4th layer of the target network model is selected and input into the 5th layer of the quantized network model to obtain the calculation result of the quantized network model.
[0098] Step S14: Correct the target network layer corresponding to the missing layer in each network layer of the target network model to obtain the corrected target network model.
[0099] Specifically, the target network layer corresponding to the dropped layer in each network layer of the target network model is modified. This involves adjusting the parameters of the network layers in the original network model corresponding to the quantized network model, i.e., the target network model. In practice, after adjustment, the modified target network model can be quantized and the error calculated. If the error exceeds the required limit, further adjustments can be made until a modified target network model that meets the requirements is obtained.
[0100] The method in this application embodiment is applied to the optimization of a quantized network model. Specifically, the quantized network model can be a network model obtained by quantizing various types of network models. Optionally, the network model can be a network model that processes images, videos, audio, text, etc., and then quantized to obtain the quantized network model. For example, the quantized network model can be a quantized face recognition model, a quantized object classification model, a quantized license plate recognition model, etc. Since quantizing a network model may lead to a decrease in the accuracy and efficiency of the network model, the method in this application embodiment, after identifying and correcting the missing layers of the quantized network model, can improve the quality of the quantized network model. Therefore, when performing face recognition, object classification, and license plate recognition using the quantized face recognition model, quantized object classification model, quantized license plate recognition model, etc., the accuracy and efficiency of recognition can be improved.
[0101] As can be seen, the method of this application embodiment can be used to input the input data of the target network model into the network layer of the corresponding quantized network model, determine the drop-point layer of the quantized network model by comparing the final output results, and modify the target network model according to the drop-point layer, thereby achieving network model optimization. When the adjusted network model is quantized, the quality of the quantized network model can be improved and the quantization efficiency can be improved.
[0102] In this embodiment, when selecting an output result from each network layer in the target network model as the current selection result based on the comparison results, a binary search method can be used to identify the efficiency of the dropout layer. For details, see [link to relevant documentation]. Figure 2 The process involves comparing the output of the quantized network model with the target output. Based on the comparison, a new output is selected from the outputs of each layer in the target network model. This selected output is then input into the (i+1)th layer of the quantized network model to calculate the output. This process continues until the difference between the current layer's output and the target output meets a preset error, and the difference between the output of the layer preceding the current layer and the target output does not meet a preset error. This includes:
[0103] Step S21: Compare the output of the quantized network model with the target output.
[0104] Step S22: If the difference between the output of the quantized network model and the target output meets the preset error, then select an output from the output of the first to the i-th layer of the target network model as the currently selected output; if the difference between the output of the quantized network model and the target output does not meet the preset error, then select an output from the output of the i-th to the N-th layer of the target network model as the currently selected output, and return to input the currently selected result into the (i+1)-th network layer of the quantized network model to calculate the output of the quantized network model. Continue executing until the difference between the output of the current network layer and the target output meets the preset error and the difference between the output of the previous layer of the current network layer and the target output does not meet the preset error. Then, determine the previous layer of the current network layer as the drop point layer.
[0105] During the calculation process, for a specific layer in the quantized network model, if the output of the previous layer in the target network model is input into that layer of the quantized network model to obtain the calculation result of the quantized network model, and the difference between the calculated result and the calculation result of the target network model does not meet a preset difference, the dropped layer should be that layer or a subsequent layer. Therefore, a layer can be selected from the subsequent layers as the currently selected layer to continue the calculation. Conversely, if the output of the previous layer in the target network model is input into that layer of the quantized network model to obtain the calculation result of the quantized network model, and the difference between the calculated result and the calculation result of the target network model meets a preset difference, the dropped layer should be a layer preceding that layer. Therefore, a layer can be selected from the preceding layers as the currently selected layer to continue the calculation. The output result of the quantized network model can be the last output result of the quantized network model. For example, inputting the output result of a layer in the target network model into a layer in the quantized network model yields the final calculation result of the quantized network model, i.e., the output result of the quantized network model.
[0106] See Figure 3The model accuracy is calculated based on the output of the original model (float_out) and the output of the quantized network model (quant_out). If the quantized network model produces dropouts, the dropout layer is located in layer 2 or later; otherwise, it is located before layer 2. This is because the input to layer 2 is the output of layer 1 in the original model. Since none of the layers in the float model are quantized, the float_layer1 data contains no error, meaning the input to layer 2 in the quantized model has no error. Based on this premise, if the quantized network model still produces dropouts, the dropout layer is located in layer 2 or later, and vice versa. The next test layer is selected based on the range of the dropout layer. Different methods can be used to select the next test layer, such as layer-by-layer selection or binary selection. Binary selection is recommended to improve localization speed. Through the selection and verification of each test layer, the range of the dropout layer is gradually narrowed down until the dropout layer is finally located. The input of layer 2 during the quantized model inference process is replaced with float_layer1 from quant_layer1. Optional, see [link to relevant documentation]. Figure 4 When the original floating-point model performs forward inference calculations, the output of layer1 is saved sequentially as binary files in a specified path. A custom layer, custom_layer, is inserted into the quantized model. The output of this layer is layer2. Its function is to read the binary files in the specified path and pass the data in the binary files as the output of custom_layer to layer2. In this way, the overall inference of the quantized network model can be completed, and the float data can be replaced.
[0107] The method in this application embodiment can be used to compare the output of the quantized network model with the target output using the binary search method, and select the output based on the comparison result, thereby identifying the efficiency of the dropout layer.
[0108] Optionally, during correction, the characteristic parameters causing the error can be identified and corrected, thereby improving the efficiency of the correction. For details, see [link to documentation]. Figure 5 Step S14 corrects the target network layer corresponding to the missing layer in each network layer of the target network model to obtain the corrected target network model, including:
[0109] Step S141: Group the feature parameters of the target network layer to obtain multiple feature value groups;
[0110] Step S142: Calculate the distribution characteristic value of the characteristic parameter in each characteristic value group;
[0111] Step S143: Identify outlier distribution feature values in each feature value group;
[0112] Step S144: Correct the feature value group containing the outlier distribution feature values to obtain the corrected target network model.
[0113] When grouping the feature parameters of the target network layer corresponding to the dropped layer in each network layer of the target network model, they can be classified according to the type of feature parameters. Specifically, the type of feature parameters can include the feature parameters of convolution kernels and the feature parameters of input or output channels. Optionally, the feature parameters of the target network layer can be grouped to obtain multiple feature value groups, including: when the feature parameter is a feature parameter of a convolution kernel, the feature parameter is grouped according to the convolution kernel corresponding to the feature parameter to obtain multiple feature value groups; when the feature parameter is a feature parameter of an input or output channel, the feature parameter is classified according to the channel corresponding to the feature parameter to obtain multiple feature value groups. By grouping the feature parameters, the correction process can be carried out on a group-by-group basis. For example, for the weight parameters of the convolutional layer, one convolution kernel is one group; for the input and output, one channel of data is one group. In practical use, since different platforms quantize different feature parameters, corresponding feature parameters can be selected for grouping based on different platforms. For example, platform 1 only quantizes weight parameters, while platform 2 not only quantizes weight parameters but also quantifies the feature parameters of input and output channels. Therefore, when grouping according to platform characteristics, for platform 1, only weight parameters can be selected and grouped, while for platform 2, weight parameters and feature parameters of output and output channels can be selected and grouped.
[0114] When calculating the distribution characteristics of the characteristic parameters in each characteristic value group, parameters can be calculated to characterize the individual characteristics of each characteristic parameter in each characteristic value group. For example, the mean, median, and extreme values of the characteristic parameters in each characteristic value group can be calculated.
[0115] Identifying outlier distributional features within each feature value group involves identifying feature parameters that differ significantly from other feature parameters within each group. Specifically, this can be done by calculating the mean of the feature parameters and then identifying outlier features based on the difference between each feature parameter and the mean. For example, the standard deviation of each feature parameter in each group can be calculated, and the difference between each feature parameter and the mean can be compared to the standard deviation; if the difference is greater, it is considered an outlier distributional feature. For example, x... out For the selected feature parameter group, s represents the mean of the feature parameters in the selected feature parameter set, and s represents the standard deviation of the selected feature parameter set, calculated using a preset formula:
[0116]
[0117] Make a judgment; if it meets the criteria, it is an outlier distribution characteristic value.
[0118] The method described in this application allows for the grouping of feature parameters into multiple feature value groups. The distributional feature values of the feature parameters in each feature value group are then calculated. Outlier distributional feature values within each feature value group are identified, and the feature value groups containing these outlier distributional feature values are corrected to obtain a corrected quantized network model. This allows for the correction of feature parameters on a group-by-group basis, reducing the correction range and improving correction efficiency.
[0119] When correcting the eigenvalues in a set of eigenvalues, there are several methods. Optionally, scaling can be used. See [link to relevant documentation]. Figure 6 The target network model is modified by correcting the target network layers corresponding to the layers with dropped nodes, resulting in the modified target network model, including:
[0120] Step S61: Calculate the mean of the distribution eigenvalues of the eigenvalues of the eigenvalues in multiple eigenvalue groups;
[0121] Step S62: Calculate the ratio of the mean to the distribution characteristic value of the outliers to obtain the proportionality coefficient;
[0122] Step S63: Scale the feature parameters in the feature value group containing the outlier distribution feature value according to the scaling factor to obtain the corrected target network model.
[0123] Specifically, the ratio of the mean to the outlier distribution characteristic value is calculated to obtain the proportionality coefficient, which can be obtained through a preset formula:
[0124]
[0125] Perform the calculation.
[0126] In practical use, scaling the feature parameters often affects the final output of the model. Therefore, data recovery can be achieved by adding one or more correction layers after the target network layer, thereby ensuring the consistency of the model results before and after the modification.
[0127] Optionally, the feature value group containing the outlier distribution feature value is modified to obtain the modified target network model, including: amplifying or reducing the feature parameters in the feature value group containing the outlier distribution feature value, and adding one or more revision layers before or after the target network layer, wherein the revision layer is used to reduce the input data when the feature parameters are amplified, and to amplify the input data when the feature parameters are reduced.
[0128] For example, if the first convolutional kernel (kernel1) in the weights of a certain convolutional layer is judged to be an outlier distribution feature value, then after reducing the weight coefficients in kernel1 by a factor of n according to the above calculation method, the data of channel 1 in the convolution output will also be reduced by a factor of n accordingly. In order to ensure that the overall convolution result remains unchanged, a slice+scale layer is added after the convolution to amplify the data of channel 1 by a factor of n, thereby ensuring that the out data is consistent with the original convolution data before adjustment.
[0129] In practical use, the problem can also be corrected by adjusting adjacent network layers.
[0130] Optionally, the feature value group containing the outlier distribution feature values is modified to obtain a modified quantized network model. This includes: amplifying or reducing the feature parameters in the feature value group containing the outlier distribution feature values, and modifying one or more network layers before or after the target network layer. Modifying one or more network layers before or after the target network layer includes: when amplifying the feature parameters, modifying one or more network layers before the target network layer to reduce the output data, or modifying one or more network layers after the target network layer to reduce the output data; when reducing the feature parameters, modifying one or more network layers before the target network layer to amplify the output data, or modifying one or more network layers after the target network layer to amplify the output data. Specifically, when amplifying or reducing the feature parameters in the feature value group containing the outlier distribution feature values, this can be done using preset weights, coefficients, input or output parameters, etc.
[0131] As can be seen, the method of this application embodiment can scale the feature parameters in the feature value group where the outlier distribution feature value is located to obtain a corrected quantized network model, thereby improving the accuracy of the quantized network model.
[0132] To illustrate the methods of the embodiments of this application, the following description is provided in conjunction with specific implementation methods:
[0133] 1. Quantitative accuracy analysis
[0134] Typically, quantization accuracy drops in models are caused by one or a few layers, and tuning all quantization layers would be very time-consuming. This solution proposes a quantization accuracy analysis method. Before performing specific tuning comparisons, it uses data insertion and search to locate the dropping layers, thus determining their positions. Subsequent tuning can then focus on these dropping layers.
[0135] The steps for quantitative accuracy analysis are as follows:
[0136] Select a quantized layer in the model as the test layer layer2, and its input layer is layer1;
[0137] Select a certain number of test samples as model input, such as a face recognition model, where the test samples are a batch of face images. Perform forward inference calculations on the unquantized model (i.e., the original floating-point model) in float format within the original framework. When the input images are different, the output value of layer 1, float_layer1, and the final output value of the model, float_out, will be different. Record the float_layer1 value and float_out value corresponding to each input sample.
[0138] Deploy the floating-point model to an edge device, quantize it according to hardware requirements, use the same test samples as model input, and complete the forward inference calculation of the quantized model on the device.
[0139] During the forward computation of the quantization model, the input of layer2 should originally be the computation result of layer1 after quantization, i.e., quant_layer1. In order to locate the position of the quantization dropout layer, the input of layer2 is now replaced with the corresponding float_layer1, and then the subsequent inference computation of the quantization model continues to obtain the final output result quant_out of the model.
[0140] The model accuracy is calculated based on the output `float_out` of the original model and the output `quant_out` of the quantized model. If the quantized model produces dropped data, it indicates that the dropped layer is located in layer 2 or later; otherwise, it indicates that the dropped layer is located before layer 2. This is because the input to layer 2 is the output of layer 1 in the original model. Since none of the layers in the float model have been quantized, the `float_layer1` data contains no error, meaning the input to layer 2 in the quantized model has no error. Based on this premise, if the quantized model still produces dropped data, it indicates that the dropped layer is located in layer 2 or later; otherwise, it is located before layer 2.
[0141] Based on the scope of the dropout layer, select the next test layer. Different methods can be used to select the next test layer, such as layer-by-layer selection or binary search selection. Binary search is recommended to improve the localization speed. Through the selection and verification of each test layer, the scope of the dropout layer is gradually narrowed down, and the dropout layer is finally located.
[0142] In step 4), the input of layer 2 during the quantization model inference process needs to be replaced from quant_layer1 to float_layer1. One possible solution is as follows: When the original floating-point model performs forward inference calculations, the output of layer 1 is saved sequentially as binary files in a specified path. A custom layer, custom_layer, is inserted into the quantization model. The output of this layer is layer 2. Its function is to read the binary files in the specified path and pass the data in the binary files as the output of custom_layer to layer 2. This method can complete the overall inference of the quantization model and replace the float data.
[0143] 2. Quantitative optimization
[0144] Regardless of the quantization algorithm or parameters used, the essence of quantization is to represent floating-point numbers using fixed-point numbers. However, due to the difference in precision between floating-point and fixed-point numbers, there is an inherent error, thus generating quantization error. But even with the same quantization method, some models maintain essentially the same precision after quantization, while others experience a significant decrease. The main difference lies in the distribution range and characteristics of the quantized data. When the data distribution range is too large, the quantized data cannot fully represent the original data values, leading to a larger quantization error and more severe model drops. Therefore, to achieve quantization optimization, this solution adjusts the distribution of the quantized data in the drop layer to better suit quantization, thereby reducing quantization error and enabling optimization. The specific steps are as follows:
[0145] Based on the drop point analysis results, the drop point layer is selected.
[0146] Different hardware platforms quantize different objects. For example, platform 1 only quantizes weight parameters, while platform 2 quantizes not only weight parameters but also inputs and outputs. Therefore, it is necessary to select a specific quantization object (weight parameters, inputs, or outputs) in the dropout layer based on the platform characteristics.
[0147] All data in the quantized object is grouped, and adjustments are made on a group-by-group basis. For the weight parameters of the convolutional layer, one convolutional kernel constitutes one group; for the input and output, one channel of data constitutes one group.
[0148] Each set of values in the quantified object is scored separately. The score represents the overall distribution of this set of data. The calculation methods include mean, median, extreme value, etc.
[0149] Obtain the scores for each group, observe the distribution of scores, and filter out outliers. The method for identifying outliers is as follows, where x out For a certain group's score, Let s be the mean of all group scores, and s be the standard deviation of each group score. A group is considered an outlier if its score meets the above calculation formula; otherwise, it is not. This is one method for outlier screening, but it is not limited to this method.
[0150] Once outliers in the score values are identified, it indicates that the set of data corresponding to the score values belongs to an anomalous distribution and needs to be adjusted. After adjustment, the score values of each set of data are guaranteed to be within a similar range, thereby reducing the overall distribution range of the data and improving the quantification accuracy.
[0151] One method for adjusting outlier data is scaling, which involves adjusting a set of parameters corresponding to outlier scores according to a certain proportional coefficient t. The formula for calculating sparsity by scaling data is as follows:
[0152]
[0153] After data adjustments are completed, the modifications to the model will inevitably affect the final results, potentially leading to errors. To compensate for the anomalies in model results caused by data adjustments, it is necessary to modify the model to restore the data, thereby ensuring consistency between the model's results before and after the modification.
[0154] Taking convolutional layer adjustment as an example, the data recovery method is explained. Assuming the first convolutional kernel (kernel1) in the convolutional layer's weights is identified as an outlier, then after reducing the weight coefficients in kernel1 by a factor of n according to the calculation method described above, the data in channel 1 of the convolutional output will also be reduced by a factor of n. To ensure the overall convolutional result remains unchanged, a slice+scale layer is added after the convolution to amplify the data in channel 1 by a factor of n. The resulting out data is then consistent with the original convolutional data before adjustment. This achieves the goal of result recovery.
[0155] When different adjustment layers are used, different model recovery strategies are employed, including but not limited to adding slice+scale layers and adjusting the parameters of adjacent convolutional layers.
[0156] A second aspect of the embodiments of this application, see [link to embodiment]. Figure 7 A network model tuning device is provided, comprising:
[0157] The model acquisition module 701 is used to acquire a pre-trained target network model and a quantized network model obtained after quantizing the target network model. Both the quantized network model and the target network model include multiple network layers.
[0158] The model calculation module 702 is used to acquire and input sample data into the target network model for calculation, and obtain the output results of each network layer in the target network model, wherein the output results of each network layer in the target network model include the target output result of the last layer.
[0159] The drop point identification module 703 is used to determine the drop point layer in the quantized network model based on the output results of each network layer in the target network model.
[0160] The model correction module 704 is used to correct the target network layer corresponding to the missing layer in each network layer of the target network model, so as to obtain the corrected target network model.
[0161] Optionally, the drop point layer recognition module 703 includes:
[0162] The output result selection submodule is used to select the output result of the i-th layer from the output results of each network layer in the target network model as the currently selected output result, where i is an integer from 1 to N, and N is the total number of network layers in the quantized network model;
[0163] The output result calculation submodule is used to input the currently selected output result into the (i+1)th network layer of the quantized network model and calculate the output result of the quantized network model.
[0164] The output result comparison submodule is used to compare the output result of the quantized network model with the target output result. Based on the comparison result, it selects an output result from the output results of each network layer in the target network model as the current selected result, and returns to input the current selected result into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model. The process continues until the difference between the output result corresponding to the current network layer and the target output result meets the preset error, and the difference between the output result corresponding to the previous layer of the current network layer and the target output result does not meet the preset error. Then, the previous layer of the current network layer is determined as the drop point layer.
[0165] Optional, the output comparison submodule includes:
[0166] The result comparison unit is used to compare the output of the quantized network model with the target output.
[0167] The result selection unit is used to select an output result from the output results of the target network model from the 1st to the ith layer of the target network model if the difference between the output result of the quantized network model and the target output result meets the preset error; if the difference between the output result of the quantized network model and the target output result does not meet the preset error, then an output result is selected from the output results of the target network model from the ith to the Nth layer of the target network model as the currently selected output result, and the process of inputting the currently selected result into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model continues until the difference between the output result corresponding to the current network layer and the target output result meets the preset error and the difference between the output result corresponding to the previous layer of the current network layer and the target output result does not meet the preset error, then the previous layer of the current network layer is determined as the drop point layer.
[0168] Optionally, the model correction module 704 includes:
[0169] The parameter grouping submodule is used to group the feature parameters of the target network layer to obtain multiple feature value groups;
[0170] The eigenvalue calculation submodule is used to calculate the distribution eigenvalues of the eigenparameters in each eigenvalue group.
[0171] The outlier feature value identification submodule is used to identify outlier distribution feature values in each feature value group;
[0172] The outlier feature value correction submodule is used to correct the feature value group containing the outlier distribution feature values to obtain the corrected target network model.
[0173] Optionally, a parameter grouping submodule is used to group the feature parameters according to the convolution kernel when the feature parameters are the feature parameters of the convolution kernel, to obtain multiple feature value groups; and to classify the feature parameters according to the channel when the feature parameters are the feature parameters of the input or output channel, to obtain multiple feature value groups.
[0174] Optional outlier eigenvalue correction submodule, including:
[0175] The mean calculation unit is used to calculate the mean of the distribution characteristic values of the characteristic parameters in multiple characteristic value groups;
[0176] The proportionality coefficient calculation unit is used to calculate the ratio of the mean to the distribution characteristic value of the outliers to obtain the proportionality coefficient;
[0177] The feature parameter scaling unit is used to scale the feature parameters in the feature value group containing the outlier distribution feature value according to the scaling factor, so as to obtain the corrected target network model.
[0178] Optionally, an outlier feature value correction submodule is used to amplify or reduce the feature parameters in the feature value group containing the outlier distribution feature values, and to correct one or more network layers before or after the target network layer. Specifically, when amplifying the feature parameters, the output data is reduced by correcting one or more network layers before the target network layer, or by correcting one or more network layers after the target network layer. When reducing the feature parameters, the output data is amplified by correcting one or more network layers before the target network layer, or by correcting one or more network layers after the target network layer.
[0179] Optionally, an outlier feature value correction submodule is specifically used to amplify or reduce the feature parameters in the feature value group containing the outlier distribution feature values, and to correct one or more network layers before or after the target network layer. Correcting one or more network layers before or after the target network layer includes: when amplifying the feature parameters, correcting one or more network layers before the target network layer to reduce the output data, or correcting one or more network layers after the target network layer to reduce the output data; when reducing the feature parameters, correcting one or more network layers before the target network layer to amplify the output data, or correcting one or more network layers after the target network layer to amplify the output data.
[0180] As can be seen, the method of this application embodiment can be used to input the input data of the target network layer into the network layer of the corresponding quantized network model, and by comparing the final output results, the drop-point layer of the quantized network model can be determined. The target network model can then be corrected based on the drop-point layer, thereby achieving network model optimization. When the adjusted network model is quantized, the quality of the quantized network model can be improved, and the quantization efficiency can be increased.
[0181] This application also provides an electronic device, such as... Figure 8 As shown, it includes a processor 801, a communication interface 802, a memory 803, and a communication bus 804. The processor 801, communication interface 802, and memory 803 communicate with each other via the communication bus 804.
[0182] Memory 803 is used to store computer programs;
[0183] When processor 801 executes a program stored in memory 803, it performs the following steps:
[0184] Obtain a pre-trained target network model and a quantized network model obtained by quantizing the target network model. Both the quantized network model and the target network model include multiple network layers.
[0185] The sample data is acquired and input into the target network model for calculation to obtain the output results of each network layer in the target network model. The output results of each network layer in the target network model include the target output result of the last layer.
[0186] Based on the output results of each network layer in the target network model, determine the drop-point layers in the quantization network model;
[0187] The target network layer corresponding to the missing layer in each network layer of the target network model is modified to obtain the modified target network model.
[0188] The communication bus mentioned in the above electronic devices can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0189] The communication interface is used for communication between the aforementioned electronic devices and other devices.
[0190] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0191] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0192] In another embodiment provided in this application, a computer-readable storage medium is also provided, which stores a computer program that, when executed by a processor, implements the steps of the tuning method for any of the above-described network models.
[0193] In another embodiment provided in this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute the tuning method of any of the network models in the above embodiments.
[0194] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0195] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0196] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, electronic devices, storage media, and computer program products are basically similar to the method embodiments, so the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0197] The above description is merely a preferred embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application are included within the scope of protection of this application.
Claims
1. A method for tuning a network model, the method comprising: The method comprises the following steps: obtaining a pre-trained target network model and a quantized network model obtained after a quantization operation is performed on the target network model, wherein the target network model and the quantized network model both comprise a plurality of network layers, and the target network model is a face recognition model; obtaining sample data and inputting the sample data into the target network model to perform calculation, to obtain output results of the network layers in the target network model, wherein the output results of the network layers in the target network model comprise target output results output by a last layer, the sample data is a to-be-recognized face picture, and the output results represent whether a to-be-recognized face in the to-be-recognized face picture is a target face; determining a drop point layer in the quantized network model according to the output results of the network layers in the target network model; performing correction on a target network layer corresponding to the drop point layer among the network layers of the target network model, to obtain a corrected target network model, and performing quantization on the corrected target network model; the step of performing correction on the target network layer corresponding to the drop point layer among the network layers of the target network model to obtain the corrected target network model comprises the following steps: grouping feature parameters of the target network layer to obtain a plurality of feature value groups; calculating distribution feature values of the feature parameters in each feature value group; identifying outlying distribution feature values in each feature value group; calculating a mean value of the distribution feature values of the feature parameters in the plurality of feature value groups; calculating a ratio of the mean value and the outlying distribution feature values to obtain a proportion coefficient; amplifying or reducing the feature parameters in a feature value group in which the outlying distribution feature values are located according to the proportion coefficient, and adding one or more revision layers before or after the target network layer, wherein the revision layers are used to reduce input data when the feature parameters are amplified, and are used to amplify input data when the feature parameters are reduced.
2. The method of claim 1, wherein, the step of determining the drop point layer in the quantized network model according to the output results of the network layers in the target network model comprises the following steps: selecting an output result of an i-th layer as a currently selected output result from the output results of the network layers in the target network model, wherein i is an integer belonging to 1 to N, and N is a total number of network layers in the quantized network model; inputting the currently selected output result into an i+1-th network layer of the quantized network model to calculate an output result of the quantized network model; comparing the output result of the quantized network model with the target output result, reselecting an output result from the output results of the network layers in the target network model as a currently selected result according to a comparison result, and continuing to execute the step of inputting the currently selected result into the i+1-th network layer of the quantized network model to calculate the output result of the quantized network model, until a difference between a current network layer corresponding output result and the target output result meets a preset error and a difference between a previous layer corresponding output result of the current network layer and the target output result does not meet the preset error, and then determining a previous layer of the current network layer as the drop point layer.
3. The method of claim 2, wherein, comparing the output result of the quantized network model with the target output result, and reselecting an output result from the output results of the network layers of the target network model as a current selected result according to a comparison result, and returning the step of inputting the current selected result into an (i+1)th network layer of the quantized network model to calculate an output result of the quantized network model continues to be executed until a difference between the output result of the current network layer and the target output result meets a preset error and a difference between an output result of a previous layer of the current network layer and the target output result does not meet the preset error, comprising: comparing the output result of the quantized network model with the target output result; if the difference between the output result of the quantized network model and the target output result meets the preset error, selecting an output result from the output results of the 1st layer to the i-th layer of the target network model as a current selected output result; if the difference between the output result of the quantized network model and the target output result does not meet the preset error, selecting an output result from the output results of the i-th layer to the Nth layer of the target network model as a current selected output result, and returning the step of inputting the current selected result into the (i+1)th network layer of the quantized network model to calculate the output result of the quantized network model continues to be executed until the difference between the output result of the current network layer and the target output result meets the preset error and the difference between the output result of the previous layer of the current network layer and the target output result does not meet the preset error, and then determining the previous layer of the current network layer as the drop point layer.
4. The method of claim 1, wherein, the feature parameters of the target network layer are grouped to obtain a plurality of feature value groups, comprising: when the feature parameters are feature parameters of a convolution kernel, the feature parameters are grouped according to the convolution kernel corresponding to the feature parameters to obtain a plurality of feature value groups; when the feature parameters are feature parameters of an input or output channel, the feature parameters are classified according to the channel corresponding to the feature parameters to obtain a plurality of feature value groups.
5. An apparatus for tuning a network model, the apparatus comprising: comprising: a model acquisition module configured to acquire a pre-trained target network model and a quantized network model obtained by performing a quantization operation on the target network model, wherein the quantized network model and the target network model each comprise a plurality of network layers, and the target network model is a face recognition model; a model calculation module configured to acquire sample data and input the sample data into the target network model to calculate output results of the network layers of the target network model, wherein the output results of the network layers of the target network model include a target output result output by a last layer, the sample data is a to-be-recognized face picture, and the output results represent whether a to-be-recognized face in the to-be-recognized face picture is a target face; a drop point layer identification module configured to determine a drop point layer in the quantized network model according to the output results of the network layers of the target network model; The model correction module is configured to correct a target network layer corresponding to the dropout layer in each network layer of the target network model to obtain a corrected target network model, and to quantize the corrected target network model. The model correction module comprises: The parameter grouping sub-module is configured to group feature parameters of the target network layer to obtain a plurality of feature value groups. The feature value calculation sub-module is configured to calculate a distribution feature value of each feature parameter in each feature value group. The outlier feature value identification sub-module is configured to identify an outlier distribution feature value in each feature value group. The outlier feature value correction sub-module is configured to correct a feature value group in which the outlier distribution feature value is located to obtain a corrected target network model. The outlier feature value correction sub-module comprises: The mean value calculation unit is configured to calculate a mean value of the distribution feature values of the feature parameters in the plurality of feature value groups. The proportion coefficient calculation unit is configured to calculate a proportion of the mean value and the outlier distribution feature value to obtain a proportion coefficient. The feature parameter scaling unit is configured to scale the feature parameters in the feature value group in which the outlier distribution feature value is located according to the proportion coefficient, and to add one or more revision layers before or after the target network layer, wherein the revision layer is configured to scale down input data when the feature parameters are scaled up, and to scale up input data when the feature parameters are scaled down.
6. An electronic device, comprising: The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method steps of any one of claims 1-4. The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method steps of any one of claims 1-4. 7. A computer-readable storage medium, characterized in that,
Citation Information
Patent Citations
Abnormal network layer determination method and a related product
CN109918237A
Quantization processing method and device and quantization processing chip
CN113282535A