A method for pruning a pre-trained neural network model
By analyzing the gradient values of the convolution kernel weight parameters of the pre-trained neural network model to determine the important parameters, and pruning the target network layer, the problem of low pruning accuracy and low efficiency in the existing technology is solved, and an efficient model is realized Compression and performance guaranteed.
Patent Information
- Application Number
- CN202110263342.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-11
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-03-11
AI Technical Summary
The existing structured pruning algorithm has low pruning accuracy for convolution kernels and channel numbers, making it difficult to effectively reduce the calculation amount and parameter amount of the neural network model. At the same time, users need to manually observe the impact of pruning ratio on model performance layer by layer. Low efficiency.
By analyzing the gradient values of the convolution kernel weight parameters of the pre-trained neural network model, the number of important weight parameters is determined, and based on this, the target network layer is pruned, and retraining is performed after pruning to obtain the compression model.
The pruning accuracy and efficiency are improved. Users do not need to manually adjust the pruning ratio layer by layer, and can quickly obtain a compression model that meets their needs while ensuring model performance.
Smart Images

Figure CN115081580B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of neural network processing, in particular to the field of compression technology of neural network models, and more particularly to a method for pruning a pre-trained neural network model. Background Art
[0002] Deep learning has brought great improvements to computer vision tasks, but complex network models require high storage space and computing resources. In computer vision tasks, whether it is image classification or target recognition, convolutional layers are usually required as the basic network structure when extracting image features, and fully connected layers are required as the basic network structure when downstream tasks involve classification or regression. In target recognition, other network structures are also used due to the complexity of identifying targets.
[0003] With the introduction of Residual Networks (ResNet), the training degradation problem of deep neural networks has been alleviated, making it possible to train networks with thousands of layers. However, the more layers there are in the network and the more complex the network structure is, the more memory and computing resources the model requires for deployment. Especially for target detection networks, other network structures are introduced to improve detection accuracy in order to achieve small target detection, occluded target recognition and other problems. In order to reduce the amount of computation and parameters of the model while ensuring accuracy, many methods for compressing neural networks have been proposed. Common compression methods include: low-rank decomposition, pruning, quantization, knowledge distillation, compact network design, etc., among which pruning and quantization are research hotspots in this field.
[0004] Pruning algorithms can be divided into unstructured pruning and structured pruning algorithms. Unstructured pruning algorithms have less precision loss, but are limited by specific algorithm libraries and hardware platforms, so they are relatively rarely used in practice. Structured pruning algorithms prune convolution kernels and the number of channels. The pruning strategy is very effective, but the pruning accuracy is relatively low. Summary of the invention
[0005] Therefore, the purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a method for pruning a pre-trained neural network model.
[0006] The objective of the present invention is achieved through the following technical solutions:
[0007] According to a first aspect of the present invention, a method for pruning a pre-trained neural network model is provided, wherein the pre-trained neural network model is used as a baseline model, comprising: S1, selecting a network layer in the baseline model that needs to be pruned as a target network layer; S2, obtaining gradient values corresponding to weight parameters of multiple convolution kernels in the target network layer; S3, for the target network layer, determining the number of important weight parameters in the convolution kernel based on the gradient values corresponding to the weight parameters of each convolution kernel; S4, for the target network layer, pruning the target network layer according to the number of important weight parameters in all its convolution kernels; S5, retraining the pruned model to obtain a compressed model.
[0008] In some embodiments of the present invention, the network layer that needs to be pruned is determined in one or more of the following ways: selecting a specific network layer as the network layer that needs to be pruned by layer number or network layer name, or determining a network layer whose computational amount is greater than or equal to a preset computational amount threshold value as the network layer that needs to be pruned, or determining a network layer whose parameter amount of a weight parameter of the network layer is greater than or equal to a preset parameter amount threshold value as the network layer that needs to be pruned, or determining a network layer whose computational amount ratio is greater than or equal to a preset computational amount ratio threshold value as the network layer that needs to be pruned, or determining a network layer whose parameter amount ratio of a weight parameter of the network layer is greater than or equal to a preset parameter amount ratio threshold value as the network layer that needs to be pruned.
[0009] In some embodiments of the present invention, the S3 includes: S31, calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set upper limit of the search step number and the absolute value of the gradient value with the largest absolute value; S32, in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is greater than or equal to the search threshold, until the absolute difference in the number of weight parameters searched out in two adjacent steps is less than the set stop threshold, wherein the closer the current search step number is to the upper limit of the search step number, the closer its corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33, taking the number of weight parameters whose gradient absolute value corresponding to the weight parameter is greater than or equal to the corresponding search threshold when the search is stopped as the number of important weight parameters in the convolution kernel.
[0010] In some embodiments of the present invention, the S3 includes: S31', calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set search step upper limit and the absolute value of the gradient value with the largest absolute value; S32', in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is less than the search threshold, and determining the remaining parameter amount of each search step, until the absolute difference between the remaining parameter amounts after two adjacent search steps is less than the set stop threshold, the search is stopped, wherein the closer the current search step is to the search step upper limit, the closer its corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33', for each convolution kernel, determining the search threshold used when stopping the search for the convolution kernel, and setting the weight parameter whose absolute value of the gradient value is less than the search threshold to zero; S34', calculating the number of non-zero weight parameters in each convolution kernel, and using it as the number of important weight parameters in the convolution kernel.
[0011] In some embodiments of the present invention, multiple search thresholds of each convolution kernel are determined according to the following formula: in, represents the search threshold of the ith step search of the tth convolution kernel of the lth network layer, The absolute value of the gradient of the t-th convolution kernel of the l-th network layer with the largest absolute value, m i represents the i-th search step, and m represents the upper limit of the number of search steps set by the user.
[0012] Preferably, the setting range of the upper limit of the search step number is [50, 300].
[0013] In some embodiments of the present invention, the stopping threshold is determined according to the following formula: Among them, γ represents the adjustment coefficient set by the user, It represents the number of weight parameters p of the t-th convolution kernel in the l-th network layer, and m represents the upper limit of the search steps set by the user.
[0014] In some embodiments of the present invention, S4 includes: S41, multiplying the maximum value of the number of important weight parameters in all convolution kernels of each target network layer by a preset coefficient as the pruning index of the target network layer; S42, removing the convolution kernels whose number of important weight parameters in the target network layer is less than the pruning index, or resetting the number of convolution kernels of the target network layer according to the number of convolution kernels whose number of important weight parameters in the target network layer is greater than or equal to the pruning index.
[0015] According to a second aspect of the present invention, an electronic device is provided, comprising: one or more processors; and a memory, wherein the memory is used to store one or more executable instructions; the one or more processors are configured to implement the steps of the method described in the first aspect by executing the one or more executable instructions.
[0016] Compared with the prior art, the advantages of the present invention are:
[0017] The present invention determines the number of important weight parameters in the target network layer based on the gradient value corresponding to each weight parameter of each convolution kernel, and prunes the target network layer according to the number of important weight parameters in all its convolution kernels, thereby improving the accuracy of pruning. Moreover, the present invention does not require the user to manually observe the impact of the pruning ratio of the current layer on the model performance layer by layer to complete pruning. The user only needs to observe whether the performance and size of the final compressed model meet the requirements, thereby improving the efficiency of pruning. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:
[0019] Figure 1 A schematic diagram of a flow chart of a method for pruning a neural network model according to an embodiment of the present invention;
[0020] Figure 2 A schematic diagram of a schematic residual network before and after pruning according to a method for pruning a neural network model according to an embodiment of the present invention;
[0021] Figure 3 A flowchart of an example of a method for pruning a neural network model according to an embodiment of the present invention;
[0022] Figure 4 The figure is a statistical histogram drawn according to the number of non-zero weight parameters of the convolution kernel of the network layer when Faster R-CNN is pruned according to the method for pruning a neural network model according to an embodiment of the present invention. DETAILED DESCRIPTION
[0023] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below through specific embodiments in conjunction with the accompanying drawings. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0024] As mentioned in the background technology section, the existing structured pruning algorithm prunes the convolution kernel and the number of channels, and the pruning strategy is very effective, but the pruning accuracy is relatively low. The present invention determines the number of important weight parameters in the convolution kernel based on the gradient values corresponding to the weight parameters of each convolution kernel for the target network layer, and prunes the target network layer according to the number of important weight parameters in all its convolution kernels, thereby improving the pruning accuracy. Moreover, the present invention does not require the user to manually observe the impact of the pruning ratio of the current layer on the model performance layer by layer to complete pruning. The user only needs to observe whether the performance and size of the final compressed model meet the requirements, thereby improving the efficiency of pruning.
[0025] Before specifically introducing the embodiments of the present invention, some of the terms used therein are explained as follows:
[0026] The baseline model (also called the benchmark model) is a pre-trained neural network model to be pruned.
[0027] Gradient, in machine learning, refers to the vector of partial derivatives of the loss function with respect to the network weights.
[0028] See also Figure 1 The present invention provides a method for pruning a pre-trained neural network model, wherein the pre-trained neural network model is used as a baseline model, and the method includes steps S1, S2, S3, S4, and S5. In order to better understand the present invention, each step is described in detail below in conjunction with specific embodiments.
[0029] Step S1: Select the network layer that needs to be pruned in the baseline model as the target network layer.
[0030] According to one embodiment of the present invention, the baseline model is, for example, a neural network model for image classification, recommendation system, target detection or autonomous driving. For example, the baseline model is a model that has been trained using image data and corresponding label instances (image classification data set). The user can pre-train a neural network model with certain reasoning capabilities as the baseline model of the present invention. The present invention is mainly used to adjust the number of convolution kernels in a neural network. For example, some commonly used convolutional network models including convolutional layers, pooling layers, BN layers, etc., such as LeNet, VGG, AlexNet, ResNet and other models commonly used in backbone networks, and Faster RCNN series, Yolo series and other models commonly used in target detection. After the baseline model is determined, the user can select the network layer of the baseline model that needs to be pruned. At this time, part or all of the network layers of the baseline model can be selected as the target network layers to be pruned according to the needs of the user, and only these target network layers are optimized later to complete the pruning operation more efficiently. Preferably, the network layer that needs to be pruned is determined in one or more of the following ways: selecting a specific network layer as the network layer that needs to be pruned by layer number or network layer name, or taking the network layer whose computation amount is greater than or equal to the preset computation amount threshold as the network layer that needs to be pruned, or taking the network layer whose parameter amount of the weight parameter of the network layer is greater than or equal to the preset parameter amount threshold as the network layer that needs to be pruned, or taking the network layer whose computation amount ratio is greater than or equal to the preset computation amount ratio threshold as the network layer that needs to be pruned, or taking the network layer whose parameter amount ratio of the weight parameter of the network layer is greater than or equal to the preset parameter amount ratio threshold as the network layer that needs to be pruned. For example, assuming a detection network based on Faster RCNN as a baseline model, the detection network has a total of 63 layers, and the user can select network layers between 20-63 layers, and further select network layers whose computation amount ratio is greater than 0.5% as target network layers to ensure the accuracy after pruning. The method also includes: providing the user with a user interface and / or operation interface for determining the network layer that needs to be pruned based on these methods. The technical solution of this embodiment can at least achieve the following beneficial technical effects: provide users with one or more methods for determining the network layers that need to be pruned, and users can quickly determine the network layers that they need to optimize based on these methods, so as to efficiently complete the pruning operation and obtain the required model.
[0031] Step S2: Obtain the gradient values corresponding to the weight parameters of multiple convolution kernels in the target network layer.
[0032] According to one embodiment of the present invention, a neural network model is a computing model, which is composed of a large number of nodes (or neurons) connected to each other. Each node represents a specific output function, called an activation function. The connection between each two nodes represents a weighted value for the signal passing through the connection, called a weight parameter. In a neural network model, it usually includes multiple network layers, each network layer includes one or more convolution kernels, and each convolution kernel includes one or more weight parameters. The size of the convolution kernel is equal to length × width × number of input channels. The size of the convolution kernel is the number of weight parameters of the convolution kernel. Among them, length × width represents the size of the kernel. If the length and width of the convolution kernel are both greater than 1 and the number of kernels is 1 (such as 5×5×1), the convolution kernel is a two-dimensional convolution kernel. If the length, width and kernel are all greater than 1 (such as 5×5×128), the convolution kernel is a three-dimensional convolution kernel. Each weight parameter corresponds to a gradient value, which is used to update the value of its corresponding weight parameter according to the corresponding gradient value calculated by back propagation during the training process. The larger the absolute value of the gradient value, the more important the weight parameter corresponding to the gradient value. Therefore, the absolute value of the parameter gradient value can be used as a statistic to measure the importance of the parameter.
[0033] Step S3: For the target network layer, determine the number of important weight parameters in each convolution kernel based on the gradient values corresponding to the weight parameters of the convolution kernel.
[0034] According to an optional embodiment of the present invention, step S3 includes: S31, calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set upper limit of the search step number and the absolute value of the gradient value with the largest absolute value; S32, in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is greater than or equal to the search threshold, until the absolute difference in the number of weight parameters searched out in two adjacent steps is less than the set stop threshold, wherein the closer the current search step number is to the upper limit of the search step number, the closer its corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33, taking the number of weight parameters whose gradient absolute value corresponding to the weight parameter is greater than or equal to the corresponding search threshold when the search is stopped as the number of important weight parameters in the convolution kernel.
[0035] According to an alternative embodiment of the present invention, step S3 includes: S31', calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set search step upper limit and the absolute value of the gradient value with the largest absolute value; S32', in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is less than the search threshold, and determining the remaining parameter amount of each search step, until the absolute difference between the remaining parameter amounts after two adjacent search steps is less than the set stop threshold, the search is stopped, wherein the closer the current search step is to the search step upper limit, the closer its corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33', for each convolution kernel, determining the search threshold used when stopping the search for the convolution kernel, and setting the weight parameter whose absolute value of the gradient value is less than the search threshold to zero; S34', calculating the number of non-zero weight parameters in each convolution kernel, and using it as the number of important weight parameters in the convolution kernel.
[0036] According to another optional embodiment of the present invention, step S3 includes: S31", calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set search step upper limit and the absolute value of the gradient value with the largest absolute value; S32", in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is less than the search threshold, and determining the remaining parameter amount of each search step, until the absolute difference between the remaining parameter amounts after two adjacent search steps is less than the set stop threshold, the search is stopped, wherein the closer the current search step number is to the search step upper limit, the closer its corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33", calculating the stop search remaining parameter amount corresponding to the last step of the search when stopping the search for each convolution kernel, and using the stop search remaining parameter amount as the number of important weight parameters in the convolution kernel. The remaining parameter amount of each search step = the total number of weight parameters of the convolution kernel - the number of weight parameters whose gradient absolute value corresponding to the weight parameter currently searched out is less than the search threshold corresponding to the step.
[0037] In the above three optional embodiments, multiple search thresholds of the convolution kernel can be determined by the upper limit of the search step number set by the user and the absolute value of the gradient value with the largest absolute value. The number of important weight parameters in each convolution kernel is adaptively analyzed and counted according to the search threshold and the stop threshold, so as to serve as a reference for pruning. In this case, there is no need to manually analyze the impact of the pruning ratio on the accuracy of the pruned model for each layer to determine the final pruning scheme as in the existing technology. In neural networks with a large number of layers, such as deep neural networks, the pruning speed can be increased, the required model can be obtained quickly, and the efficiency of pruning can be improved. Moreover, the accuracy of the model can also be better guaranteed.
[0038] Preferably, multiple search thresholds for each convolution kernel are determined according to the following formula:
[0039]
[0040] in, represents the search threshold of the ith step search of the tth convolution kernel of the lth network layer, The absolute value of the gradient of the t-th convolution kernel of the l-th network layer with the largest absolute value, m i represents the i-th step search, and m represents the upper limit of the number of search steps set by the user. In the present invention, the user can indirectly adjust the compression ratio of the model by setting the upper limit of the number of search steps.
[0041] The user sets the upper limit of the search steps according to the accuracy and search efficiency of pruning. Based on the upper limit of the search steps set by the user, the present invention automatically searches for the pruning schemes of each target network layer by means of grid search, thereby improving the efficiency of pruning. In order to balance the accuracy and search efficiency of pruning, preferably, the setting range of the upper limit of the search steps is [50,300]. For example, the user sets the upper limit of the search steps to 100, 150 or 200.
[0042] According to one embodiment of the present invention, the stop threshold is determined according to the following formula:
[0043]
[0044] Among them, γ represents the adjustment coefficient set by the user, Indicates the number of weight parameters p of the tth convolution kernel of the lth network layer, and m indicates the upper limit of the search steps set by the user. In order to achieve better accuracy of the compressed model, preferably, the value range of γ is set to [0.7, 1.5]. For example, γ is set to 0.8, 0.9, 1 or 1.2.
[0045] According to the above two formulas, the present invention uses the absolute value of the gradient value that has the greatest impact on the reduction of the loss function to perform a fine-grained evaluation of the weight value, measure the importance of each weight in the convolution kernel, and adaptively determine the stopping threshold based on the upper limit of the search step number set by the user and the total number of parameters of each network layer, which can avoid manual layer-by-layer adjustment and ensure pruning accuracy.
[0046] Step S4: for the target network layer, pruning the target network layer according to the number of important weight parameters in all its convolution kernels.
[0047] According to an embodiment of the present invention, step S4 includes: S41, multiplying a preset coefficient by the maximum value of the number of important weight parameters in all convolution kernels of each target network layer as the pruning index of the target network layer, and the preset coefficient can be specified by the user; S42, removing the convolution kernels whose number of important weight parameters in the target network layer is less than the pruning index, or resetting the number of convolution kernels of the target network layer according to the number of convolution kernels whose number of important weight parameters in the target network layer is greater than or equal to the pruning index. For example, α represents the preset coefficient, Represents the maximum value of the number of important weight parameters in all convolution kernels of the lth target network layer. The value range of α is (0,1). For example, assuming that a target network layer originally has 256 convolution kernels, among which the number of important weight parameters of 114 convolution kernels is less than the pruning index, then these 114 convolution kernels are removed from the target network layer. For another example, according to another method, the number of convolution kernels whose number of important weight parameters in the aforementioned target network layer is greater than or equal to the pruning index is 142, then the number of convolution kernels of the target network layer is reset to 142.
[0048] Step S5: Retrain the pruned model to obtain a compressed model.
[0049] According to one embodiment of the present invention, in the previous step (i.e., S4), pruning the target network layer is to remove the convolution kernels whose number of important weight parameters in the target network layer is less than the pruning index and the original weight parameters of the unpruned convolution kernels are randomly reinitialized, or the user prunes by resetting the number of convolution kernels of the target network layer. The accuracy of model recognition after pruning will decrease and cannot be used directly. Then, in step S5, the pruned model is retrained using the data set selected by the user to obtain a compressed model. The user can use the compressed model to deploy it to electronic devices such as mobile devices with limited computing resources and storage resources. Thereby, the storage space occupied by the mobile device is reduced, and the desired results can be obtained on the mobile device at a faster speed.
[0050] According to one embodiment of the present invention, when adjusting the number of convolution kernels of different neural network models, for example, when pruning a directly superimposed neural network (Plain Network, such as LeNet, AlexNet, VGG, etc.), it is only necessary to change the number of convolution kernels of the target network layer; when pruning a residual network, since the residual network includes a residual branch and a shortcut branch (Identity Mapping Branch), multiple target network layers that need to be pruned are located in the residual branch, and it is also necessary to adjust the number of convolution kernels in the shortcut branch (Identity Mapping Branch) to be consistent with the number of convolution kernels in the last layer of the residual branch, so that the dimensions of the feature maps generated by the two match. Taking a ResNet network with more than 50 layers as an example, if Figure 2 As shown, the residual network includes a residual branch and a shortcut branch. Figure 2 The left figure is a schematic residual network structure before pruning. Its convolution process is a residual branch processing method. The feature map of n×n size and 256 channels (corresponding to [n, n, 256]) is used as input. The residual branch uses 128 1×1 convolution kernels to compress the number of feature map channels to half of the original by setting the stride (Stride) = 1 (corresponding to stride: 1, Conv1: 1×1, 128), and then uses 128 3×3 convolution kernels to set the stride = 2 (corresponding to stride: 2, Conv2: 3×3, 128) to reduce the feature map size to 1 / 2 of the original. Finally, 512 1×1 convolution kernels are used to obtain the feature map and add it to the feature map from the shortcut branch. The size of the output feature map is: [n / 2, n / 2, 512]. Figure 2 The relu in the residual branch represents the activation function. The network layer in the residual branch is the target network layer to be adjusted by the present invention. Assuming that after the pruning process of the target network layer by the present invention, the number of convolution kernels of the three target network layers of the residual branch is respectively increased from Figure 2 The 128, 128, and 512 in the left picture are adjusted to Figure 2 M1, M2, and M3 in the right figure. Since the number of channels in the last layer of the residual branch is changed to M3, in order to match the final feature map addition operation, when processing the shortcut branch, the number of 1×1 convolution kernels in the shortcut branch is directly set to be consistent with the number of channels in the last layer of the residual branch, that is, adjusted to M3. At the same time, the step size of the shortcut branch is set to 2 to ensure that the size of the feature map output by the shortcut branch is consistent with that of the residual branch. Finally, the size of the feature map output by the adjusted model is: [n / 2, n / 2, M3].
[0051] According to one embodiment of the present invention, the trained baseline model is read, and the parameter amount and calculation amount of each layer are compared to select the name of the network layer that needs to be pruned. The absolute value of the gradient is used as the basis for judging the importance of the weight parameters in the convolution kernel. The difference between the remaining parameter amounts after two adjacent steps of searching determines whether to stop searching the convolution kernel. The remaining weight parameters are considered to be important weight parameters for the convolution kernel. For the number of non-zero weights in each convolution kernel, the convolution kernels with a certain proportion of non-zero weights are pruned, and the remaining number of convolution kernels is retained. Finally, the number of channels generated by the next layer is affected by modifying the convolution kernel, and the model accuracy is finally restored by retraining.
[0052] In an experiment conducted by the applicant, the more accurate Faster R-CNN was selected as the basic detection network (baseline model), ResNet50 was used as the backbone network of the detection model in the entire network structure, and the Feature Pyramid Networks (FPN) was used to fuse feature maps of different layers for the Region Proposal Networks (RPN) to generate candidate regions. The schematic steps of this example are as follows Figure 3 As shown, it includes steps T1, T2, T3, T4, T5, T6, T7, T8, T9, and T10. In this example, the parameters correspond to the weight parameters described above. The explanation of each sub-step is as follows:
[0053] Step T1: Load the baseline model. For example, read the trained Faster-RCNN (baseline model), obtain the parameter name, parameter value and corresponding gradient value of each layer of the network, and select the network layer to be pruned by comparing the network parameter amount and the proportion of calculation. For example, suppose the user selects the 11 network layers shown in Table 1 from Faster-RCNN as the target network layers.
[0054] Step T2: Calculate the lth layer Assume that there are L target network layers. Starting from the lth layer of the target network layer (initial value is 1), count the number of all convolution kernels in this layer. The superscript l represents the current network layer number, l=1, 2, 3, ..., L.
[0055] Step T3: Get m and calculate For example, calculate the parameters of each convolution kernel in the lth layer All gradient values And the number of all parameters Among them l t represents the t-th convolution kernel of the l-th layer, t = 1, 2, .... Then, calculate The absolute maximum value in Gets the upper limit m of the search step number to be set for calculating multiple search thresholds. That is, the absolute value of the gradient with the largest absolute value. Assuming m = 100, the method for calculating the search threshold corresponding to each step of the search is: Where mi=1, 2, 3, ..., 100.
[0056] Step T4: Statistics calculate Record and the corresponding parameter positions (Indicates the position of the weight parameter whose corresponding gradient value is less than the search threshold). That is: according to the threshold Statistical Gradient Matrix Less than threshold The number of parameters Calculate the remaining parameters of step mi Calculate the remaining parameter difference between two search intervals based on the remaining parameter amount of each search step
[0057] Step T5: Determine whether the conditions are met: If yes, go to step T6, if no, m i +1. That is, if yes, it means that the stop condition is reached and the search for the convolution kernel is completed. If no, it means that the mth convolution kernel of the current target network layer is searched. i Step search completed, m i +1(m i Increment by 1), and return to step T4. Search each convolution kernel of the lth layer. Thus, different thresholds are obtained. All the convolution kernels of the next lth layer and tth layer when Less than the set threshold Stop searching and record and the corresponding parameter positions Thresholds at different network layers according to Calculations were performed, where γ was set to 0.9 in the experiment to ensure model accuracy.
[0058] Step T6: Get the corresponding search result in step i and Pass Threshold The number of steps m at which the search stops is determined i ,according to Calculated in m i The corresponding absolute value of the gradient The absolute value of the recorded gradient is less than Parameter position will be recorded The corresponding parameter value is set to 0.
[0059] Step T7: According to Reset the number of convolution kernels in the first layer. In this step, get the number of non-zero parameters in the convolution kernel Calculate all convolution kernels in layer l The maximum value of For convolution kernels with a number less than The convolution kernel is cropped using Reset the number of convolution kernels in the lth layer. The value range of α is (0, 1). For the detection model used in this experiment, experiments were conducted with α values of 0.1, 0.2, 0.3, 0.4, 0.5, and 0.6, and finally α = 0.4 was determined while ensuring that the accuracy of the model used in this example was not lost. Figure 4 The figure shows a statistical histogram of the number of non-zero weight parameters of the convolution kernel of the network layer when pruning Faster R-CNN. The horizontal axis is the number of non-zero weight parameters in the convolution kernel, and the vertical axis is the number of corresponding convolution kernels. Some of the convolution kernels are the convolution kernels to be retained.
[0060] Step T8: Determine whether each layer has completed pruning. If so, go to step T10. If not, then l+1 (l is incremented by 1) and go to step T2. For example, repeat steps T2-T8 to adjust the number of convolution kernels and the number of fully connected neurons of all network layers read. When processing the residual network, the number of convolution kernels of the shortcut branch (Identity Mapping Branch) and the number of convolution kernels of the last layer of the residual branch (Residual Branch) are changed to match the dimensions.
[0061] Step T9: Model channel adjustment.
[0062] Step T10: Retrain the model. Retrain the model after adjusting the number of convolution kernels to restore the accuracy of the model.
[0063] In order to better evaluate the accuracy of the model detection results, the average precision (AP) with an intersection over union (Iou) threshold of 0.5 between the target box and the detection box is used for measurement. AP is obtained by calculating the area of the precision-recall curve. In actual calculation, the numerical integration method is used to obtain the approximate value:
[0064]
[0065] Among them, P represents precision, r represents recall, and P is a function with r as a parameter. mAP is the average of multiple categories (Mean Average Precision), that is, the average of the AP of each category is calculated. Table 1 shows the change in the number of convolution kernels before and after pruning, and Table 2 is a comparison of the mAP, parameter amount, and computational complexity of the network before and after pruning.
[0066] Table 1
[0067] Layer number Target network layer name Original channel number Number of channels after pruning 39 backbone.body.layer3.5.conv3 1024 69 40 backbone.body.layer4.0.conv1 512 85 43 backbone.body.layer4.1.conv1 512 190 46 backbone.body.layer4.2.conv1 512 261 48 backbone.body.layer4.2.conv3 2048 136 49 backbone.fpn.inner_blocks.0 256 19 52 backbone.fpn.inner_blocks.3 256 66 53 backbone.fpn.layer_blocks.0 256 141 54 backbone.fpn.layer_blocks.1 256 129 56 backbone.fpn.layer_blocks.3 256 142 60 roi_heads.box_head.fc6.weight (1024,12544) (256,2304)
[0068] Table 2
[0069]
[0070] In general, the present invention measures the number of important weight parameters in the convolution kernel by the absolute value of the gradient of the weight parameters in the convolution kernel, and performs pruning operations on the target network layer according to the number of important weight parameters in each convolution kernel of each target network layer, without introducing other additional statistical parameters (such as the accuracy of the model after pruning). After pruning, the model can be effectively compressed, and the compressed model also has higher accuracy.
[0071] It should be noted that although the above describes the various steps in a specific order, it does not mean that the various steps must be executed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.
[0072] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.
[0073] Computer readable storage medium can be a tangible device that holds and stores instructions used by an instruction execution device. Computer readable storage medium can include, for example, but is not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer readable storage medium include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, for example, a punch card or a protruding structure in a groove on which instructions are stored, and any suitable combination thereof.
[0074] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A method for pruning a pre-trained neural network model, wherein the pre-trained neural network model is used as a baseline model, wherein: The baseline model is a neural network model used for image classification, recommendation system, target detection or autonomous driving, and is characterized by comprising: S1. Select the network layer that needs to be pruned in the baseline model as the target network layer; S2, obtain the gradient values corresponding to the weight parameters of multiple convolution kernels in the target network layer; S3. For the target network layer, determine the number of important weight parameters in each convolution kernel based on the gradient values corresponding to the weight parameters of the convolution kernel, where: The S3 includes: S31, calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set upper limit of the number of search steps and the absolute value of the gradient value with the largest absolute value; S32, in each step of the search, one of the multiple search thresholds corresponding to each convolution kernel is used to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is greater than or equal to the search threshold, until the absolute difference in the number of weight parameters searched out in two adjacent steps is less than the set stop threshold, wherein the search is stopped when the current search step number is closer to the upper limit of the search step number and the corresponding search threshold is closer to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33, taking the number of weight parameters whose gradient absolute values corresponding to the weight parameters are greater than or equal to the search threshold corresponding to when the search is stopped as the number of important weight parameters in the convolution kernel; Alternatively, the S3 includes: S31', calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set upper limit of the number of search steps and the absolute value of the gradient value with the largest absolute value; S32', in each search step, one of the multiple search thresholds corresponding to each convolution kernel is used to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is less than the search threshold, and the remaining parameter amount of each search step is determined until the absolute difference between the remaining parameter amounts after two adjacent search steps is less than the set stop threshold, wherein the search is stopped when the current search step number is closer to the search step number upper limit and the corresponding search threshold is closer to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33', for each convolution kernel, determine the search threshold used when stopping the search for the convolution kernel, and set the weight parameter whose absolute value of the gradient value is less than the search threshold to zero; S34', calculating the number of non-zero weight parameters in each convolution kernel, and taking it as the number of important weight parameters in the convolution kernel; Alternatively, the S3 includes: S31”, calculating the gradient value with the largest absolute value among the gradient values corresponding to all weight parameters of each convolution kernel, and determining multiple search thresholds of the convolution kernel according to the set upper limit of the number of search steps and the absolute value of the gradient value with the largest absolute value; S32”, in each search step, using one of the multiple search thresholds corresponding to each convolution kernel to search for the number of weight parameters whose gradient absolute value corresponding to the weight parameter is less than the search threshold, and determining the amount of remaining parameters for each search step, until the absolute difference between the amounts of remaining parameters after two adjacent search steps is less than the set stop threshold, the search is stopped, wherein the closer the current search step number is to the upper limit of the search step number, the closer the corresponding search threshold is to the absolute value of the gradient value with the largest absolute value in the current target network layer; S33”, calculating the stop search remaining parameter amount corresponding to the last step of the search when stopping the search for each convolution kernel, and using the stop search remaining parameter amount as the number of important weight parameters in the convolution kernel; S4. Pruning the target network layer according to the number of important weight parameters in all convolution kernels of the target network layer; S5. Retrain the pruned model to obtain a compressed model.
2. The method according to claim 1, characterized in that The network layer to be pruned is determined in one or more of the following ways: Select a specific network layer as the network layer to be pruned by layer number or network layer name, or The network layers whose computation amount is greater than or equal to the preset computation amount threshold are regarded as the network layers that need to be pruned, or The network layers whose weight parameters are greater than or equal to the preset parameter value threshold are regarded as the network layers that need to be pruned, or The network layers whose computational load ratio is greater than or equal to the preset computational load ratio threshold are regarded as the network layers that need to be pruned, or The network layers whose parameter ratio of weight parameters is greater than or equal to a preset parameter ratio threshold are regarded as the network layers that need to be pruned.
3. The method according to claim 1, characterized in that The multiple search thresholds for each convolution kernel are determined according to the following formula: in, represents the search threshold of the ith step search of the tth convolution kernel of the lth network layer, The absolute value of the gradient of the t-th convolution kernel of the l-th network layer with the largest absolute value, m i represents the i-th search step, and m represents the upper limit of the number of search steps set by the user.
4. The method according to claim 1, characterized in that: The setting range of the upper limit of the search step number is [50,300].
5. The method according to claim 1, characterized in that The stopping threshold is determined according to the following formula: Among them, γ represents the adjustment coefficient set by the user, It represents the number of weight parameters p of the t-th convolution kernel in the l-th network layer, and m represents the upper limit of the search steps set by the user.
6. The method according to claim 1, characterized in that The S4 includes: S41, multiplying the maximum value of the number of important weight parameters in all convolution kernels of each target network layer by a preset coefficient as a pruning index of the target network layer; S42. Remove the convolution kernels whose number of important weight parameters in the target network layer is less than the pruning index, or reset the number of convolution kernels of the target network layer according to the number of important weight parameters in the target network layer being greater than or equal to the pruning index.
7. A computer-readable storage medium, characterized in that: A computer program is included thereon, and the computer program can be executed by a processor to implement the steps of the method according to any one of claims 1 to 6.
8. An electronic device, characterized in that: include: one or more processors; as well as A memory, wherein the memory is used to store one or more executable instructions; The one or more processors are configured to implement the steps of the method of any one of claims 1 to 6 by executing the one or more executable instructions.
