Method, device, computer equipment and storage medium for deep neural network compression
By combining local structured sparse processing and product quantization methods, the problem of high computational complexity of deep neural network models in edge devices is solved, higher compression rates and computational efficiency are achieved, and it is suitable for real-time applications of edge devices.
Patent Information
- Application Number
- CN202410584793.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-13
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-05-13
AI Technical Summary
Existing deep neural network models have high computational complexity and long inference time in edge computing devices, and model acceleration and compression methods such as pruning and quantization have problems such as low sparsity rate and unfriendly hardware computing.
Using a combination of local structured sparse processing and product quantization method, the weight matrix is divided into multiple blocks for structured pruning and quantization to generate regular sparse weights, and random pruning is used between different blocks to improve the overall sparsity rate and compression rate.
It achieves higher model compression rate and computing efficiency, reduces storage space requirements, while maintaining model accuracy, and is suitable for real-time applications on edge devices.
Smart Images

Figure CN118171697B
Abstract
Description
Technical Field
[0001] The present invention relates to a method, device, computer equipment and storage medium for deep neural network compression, and belongs to the technical field of convolutional neural networks. Background Art
[0002] The patent with application number 202311174427.9 and patent name "method, device, computer equipment and storage medium for power transmission and transformation defect identification" uses convolutional neural network to establish a power transmission and transformation defect identification model, and performs power transmission and transformation defect detection based on real-time collection of power transmission and transformation defect monitoring images, which improves the accuracy of defect identification, and can improve the efficiency and quality of inspection work, improve the level of power transmission and transformation operation and maintenance automation and power grid operation safety. However, the substation defect identification model it uses has high computational complexity and long inference time. The edge computing device has limited memory and storage, and the model needs to be accelerated and compressed.
[0003] Commonly used model acceleration and compression methods are pruning and quantization. The goal of pruning is to remove unimportant weights from a neural network to achieve storage efficiency, memory efficiency, and sometimes computational efficiency and time efficiency, while maintaining model performance. There are two key elements in pruning methods: (1) The pruned unit is the atomic unit removed from the model; it can be a single weight (unstructured pruning), an attention head, or even a Transformer layer (structured pruning). (2) The significance score is the criterion for making pruning decisions. Based on whether it uses gradients and the order of the gradients used, we can classify pruning methods into zero-order (only considering the size of weights), first-order, and second-order methods. Unstructured pruning removes "unimportant" connections in a neural network by setting the corresponding weights to 0. After pruning, the weight matrix usually becomes sparse. In order to exploit the characteristics of sparse matrices to achieve computational and memory efficiency, specialized hardware (such as the sparse tensor cores in Nvidia A100) and software (such as the PyTorch Sparse API) are required. Structured pruning is achieved by removing weight blocks, rows, attention heads, or layers from the model. Compared with unstructured pruning, it can usually achieve acceleration and memory reduction without dedicated hardware or software. Model quantization is an important model optimization technology in the field of deep learning. It aims to reduce the computational and storage requirements of the model and improve the efficiency of the model in resource-constrained environments by reducing the representation accuracy of weights and activation values in neural networks. In traditional deep learning models, 32-bit floating-point numbers are usually used to represent each weight and activation value, which ensures the high expressiveness of the model for various complex tasks, but also causes a lot of waste of computing and storage resources. The core idea of model quantization is to convert these 32-bit floating-point representations into lower-bit numbers, such as 8-bit or less integers or fixed-point numbers, to achieve sparsity and simplicity of model parameters. The advantage of model quantization is not only that it reduces the storage and computing resources required for the model, but also helps to improve the inference speed of the model, making the deep learning model more suitable for real-time applications and edge devices. In practical applications, model quantization has become one of the important means of optimizing deep learning models and is widely used in many fields such as image recognition and natural language processing.
[0004] The combination of model pruning and quantization can achieve a higher model compression ratio. Although the random pruning method has a high compression ratio, it is not hardware-friendly, and the structured pruning is hardware-friendly, but the sparsity rate is not high. Therefore, the present invention proposes a deep neural network compression method that improves the compression ratio and realizes a computationally friendly hybrid pruning and quantization. Summary of the invention
[0005] In order to solve the above problems, the present invention proposes a method, device, computer equipment and storage medium for deep neural network compression, which can improve the compression multiple of the neural network and is hardware-computing friendly.
[0006] The technical solution adopted by the present invention to solve the technical problem is:
[0007] In a first aspect, an embodiment of the present invention provides a method for compressing a deep neural network, comprising the following steps:
[0008] Get the weight matrix of the original deep neural network;
[0009] The weight matrix is sparsely pruned through local structured sparse processing;
[0010] The weight matrix after sparse pruning is compressed by product quantization method to obtain compressed weight;
[0011] The compression weights are placed in the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model.
[0012] As a possible implementation of this embodiment, the sparse pruning of the weight matrix by local structured sparse processing includes:
[0013] Group the weight matrix and divide the input channels into K groups, each of which is represented as a pruning unit;
[0014] Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix;
[0015] The pruned loss function is expanded using Taylor's formula to obtain the importance measure of each pruned unit;
[0016] According to the importance metric of each pruned unit, the pruned units below a certain threshold are deleted, that is, the weight of the pruned unit is reset to 0.
[0017] As a possible implementation of this embodiment, the formula for performing the pruning operation on the sub-matrix of each group is as follows:
[0018] (1)
[0019] in, represents the cross entropy loss function, represents the threshold for limiting the number of weights, is the original weight, is the weight that needs to be pruned, l 0 The norm restricts W i The number of non-zero parameters B in ; Indicates weight W pruning .
[0020] As a possible implementation of this embodiment, the pruned loss function is expanded using Taylor's formula:
[0021] (2)
[0022] in, is the original weight, is the weight that needs to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative, Represents the loss function right Find the second-order partial derivative;
[0023] The importance metric of each pruning unit is expressed as:
[0024] (3)
[0025] in, represents the importance measure of pruning weights, represents the weight to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0026] As a possible implementation of this embodiment, the method of compressing the weight matrix after sparse pruning by a product quantization method to obtain the compressed weight includes:
[0027] Divide the weight matrix after sparse pruning into K groups, and each group size is represented as a quantization unit as in the sparse pruning process, and the group size of the product quantization method is the same as the pruning unit size in the sparse pruning of the weight matrix by local structured sparse processing;
[0028] Perform quantization operations on the sub-matrices of each group, and perform clustering calculations on the non-empty subsets in each sub-matrix to obtain the cluster centers and labels;
[0029] Assign cluster centers and codebooks to each component in the submatrix, combine the obtained cluster centers and labels, use Euclidean distance to calculate which cluster center each component is closer to, and save the corresponding labels.
[0030] As a possible implementation of this embodiment, in the process of performing matrix weight compression on the sparse pruned weight matrix by the product quantization method, for the parameter matrix W, T is the output channel, S is the input channel; the input channel is divided into K groups, each group is represented as a quantized unit, and each quantization is performed with a group of weights W i Quantize the non-zero parameters in:
[0031] (6)
[0032] in represents the quantization formula, represents the weight, ; Kmeans is used for quantification, and the cluster centers quantified by Kmeans are not shared among the groups.
[0033] As a possible implementation of this embodiment, in the process of performing matrix weight compression on the sparse pruned weight matrix by the product quantization method,
[0034] For the vector x of the output channel of weight W, divide the vector x into k different sub-vectors u j , where 1≤j≤k, and the dimension of each subvector is T ∗ =T / k, where T is a multiple of k;
[0035] Using k different quantizers to quantize the sub-vectors separately, a given vector x is mapped as follows:
[0036] (7)
[0037] in, represents the i-th component of vector x, q j is the quantization associated with the jth subvector;
[0038] For quantization q j , associated index set I j , Codebook C j and the corresponding subscript c ji ;
[0039] The vector x recursively represents the value of the index set I=I 1 ×...×I k The codebook is defined as the Cartesian product:
[0040] (8)
[0041] in represents the i-th cluster center, the cluster center of this set is the concatenation of the cluster centers of m sub-quantizers;
[0042] Assuming that all subquantizers have the same finite number m* of cluster centers, the total number of cluster centers is given by:
[0043] (9)
[0044] In the extreme case where k=T, the components of vector x are quantized individually.
[0045] As a possible implementation method of this embodiment, when putting the compression weight into the adam module for fine-tuning, a fixed cluster label B is used, and then the cluster center D of each group is gradient updated through back propagation, thereby achieving fine-tuning of the model.
[0046] As a possible implementation of this embodiment, in the process of putting the compression weight into the adam module for fine-tuning, the calculation target is:
[0047] (10)
[0048] in is the quantized weight, represents the original weight, X is the input, represents the two-norm, represents the Frobenius inner product of two matrices, the matrix is precomputed; this objective is optimized via iterative (non-stochastic) full-batch gradient descent.
[0049] As a possible implementation of this embodiment, in the process of putting the compression weight into the adam module for fine-tuning, the learning unit scale s∈R h , which is initialized to s i :=||W i || 2 , and updated together with the cluster centers using the same Adam optimizer:
[0050] (11)
[0051] Where W is the weight and X is the input.
[0052] As a possible implementation of this embodiment, the method for deep neural network compression further includes the following steps:
[0053] Each layer of feature maps of the original deep neural network is obtained, and each layer of feature maps is sparsely pruned through local structured sparse processing.
[0054] As a possible implementation of this embodiment, the sparse pruning of each layer of feature maps by local structured sparse processing includes:
[0055] Group the feature maps and divide the input channels into K groups, each of which is represented as a pruning unit;
[0056] Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix;
[0057] The following formula is used to expand the loss function after pruning using Taylor's formula:
[0058] (4)
[0059] in, Indicates the feature graph that needs to be pruned. represents the importance measure of the pruned feature map, represents the loss function;
[0060] Taylor expansion is used to obtain the importance measure of each pruned unit:
[0061] (5)
[0062] in, represents the importance measure of the pruned feature map, represents the feature map to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0063] In a second aspect, an embodiment of the present invention provides a device for compressing a deep neural network, comprising:
[0064] The weight matrix acquisition module is used to obtain the weight matrix of the original deep neural network;
[0065] A first sparse pruning module, used for sparse pruning the weight matrix through local structured sparse processing;
[0066] A weight compression module is used to compress the weight matrix after sparse pruning by a product quantization method to obtain a compressed weight;
[0067] The adam fine-tuning module is used to put the compression weight into the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model.
[0068] As a possible implementation of this embodiment, the apparatus for deep neural network compression further includes:
[0069] The second sparse pruning module is used to obtain the feature map of each layer of the original deep neural network and perform sparse pruning on each layer of the feature map through local structured sparse processing.
[0070] In a third aspect, an embodiment of the present invention provides a computer device, comprising a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor. When the computer device is running, the processor communicates with the memory through the bus, and the processor executes the machine-readable instructions to perform the steps of any deep neural network compression method as described above.
[0071] In a fourth aspect, an embodiment of the present invention provides a storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of any of the above-mentioned deep neural network compression methods are executed.
[0072] The beneficial effects of the technical solution of the embodiment of the present invention are as follows:
[0073] The present invention combines a local structured sparse method with a quantization method, divides the model weight into multiple blocks, and uses a structured pruning method in each block to generate regular sparse weights. Random pruning can be used between different blocks to improve the overall sparse rate, thus taking into account both computational efficiency and compression rate. The present invention combines the sparse method with the quantization technology, and uses a product quantization method to reduce the storage space of the model parameters without significantly affecting the accuracy, which can greatly reduce the storage space. The group size of the product quantization of the present invention is the same as the basic pruning unit size of the local structured sparse method, so that the zero elements generated by the structured pruning can directly skip quantization and do not occupy any storage space. The other non-zero elements are compressed by the quantization method; through this combination, a higher model compression rate is achieved. The present invention improves computational efficiency through structured sparseness, reduces storage overhead through product quantization, and the structured sparseness and product quantization work together to greatly improve the compression rate of the deep neural network. BRIEF DESCRIPTION OF THE DRAWINGS
[0074] Figure 1 is a flow chart of a method for deep neural network compression according to an exemplary embodiment;
[0075] Figure 2 is a schematic structural diagram of a device for deep neural network compression according to an exemplary embodiment;
[0076] Figure 3 is a flow chart of another method for deep neural network compression according to an exemplary embodiment;
[0077] Figure 4 It is a schematic diagram of the structure of another apparatus for deep neural network compression according to an exemplary embodiment. DETAILED DESCRIPTION
[0078] In order to more clearly illustrate the technical features of the solution of the present invention, the present invention is described in detail below through specific implementation methods and in conjunction with the accompanying drawings.
[0079] The random pruning method can obtain a very high sparsity rate, thereby achieving a very high model compression rate. However, random pruning will produce an unstructured sparse pattern, which brings great difficulties to hardware implementation. The existence of a large number of zero elements and their random distribution will seriously affect the efficiency of calculation. In contrast, the structured pruning method can generate a regular sparse pattern, such as pruning only certain filters or channels, which is very friendly to hardware implementation. However, the sparsity rate of structured pruning is relatively low. In order to resolve this contradiction, the present invention proposes a compromise local structured sparse method. The method divides the model weights into multiple blocks, and uses a structured pruning method in each block to generate regular sparse weights. Random pruning can be used between different blocks to improve the overall sparsity rate. In this way, both computational efficiency and compression rate are taken into account. In addition, the present invention combines the sparse method with quantization technology. Common quantization methods include bit width compression, fixed-point quantization, etc. The present invention adopts a product quantization method here, which can reduce the storage space of model parameters without significantly affecting the accuracy. The basic idea of product quantization is to split a high-dimensional vector into multiple low-dimensional subspaces, each subspace adopts a common quantization method, and then splices the quantization results of all subspaces together to approximately represent the original high-dimensional vector. This can greatly reduce the storage space. The group size of the product quantization designed by the present invention is the same as the basic pruning unit size of the local structured sparse method. In this way, the zero elements generated by structured pruning can directly skip quantization and do not take up any storage space. Other non-zero elements are compressed using a quantization method. Through this combination, the present invention achieves a higher model compression rate. Structured sparsity improves computational efficiency, and product quantization reduces storage overhead. The present invention makes structured sparsity and product quantization work together to make deep neural networks with ultra-high compression rates possible.
[0080] Example 1
[0081] like Figure 1 As shown, a method for compressing a deep neural network provided by an embodiment of the present invention includes the following steps:
[0082] Get the weight matrix of the original deep neural network;
[0083] The weight matrix is sparsely pruned through local structured sparse processing;
[0084] The weight matrix after sparse pruning is compressed by product quantization method to obtain compressed weight;
[0085] The compression weights are placed in the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model.
[0086] As a possible implementation of this embodiment, the sparse pruning of the weight matrix by local structured sparse processing includes:
[0087] Group the weight matrix and divide the input channels into K groups, each of which is represented as a pruning unit;
[0088] Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix;
[0089] The pruned loss function is expanded using Taylor's formula to obtain the importance measure of each pruned unit;
[0090] According to the importance metric of each pruned unit, the pruned units below a certain threshold are deleted, that is, the weight of the pruned unit is reset to 0.
[0091] As a possible implementation of this embodiment, the formula for performing the pruning operation on the sub-matrix of each group is as follows:
[0092] (1)
[0093] in, represents the cross entropy loss function, represents the threshold for limiting the number of weights, is the original weight, is the weight that needs to be pruned, l 0 The norm restricts W i The number of non-zero parameters B in ; Indicates weight W pruning .
[0094] As a possible implementation of this embodiment, the pruned loss function is expanded using Taylor's formula:
[0095] (2)
[0096] in, is the original weight, is the weight that needs to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative, Represents the loss function right Find the second-order partial derivative;
[0097] The importance metric of each pruning unit is expressed as:
[0098] (3)
[0099] in, represents the importance measure of pruning weights, represents the weight to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0100] As a possible implementation of this embodiment, the method of compressing the weight matrix after sparse pruning by a product quantization method to obtain the compressed weight includes:
[0101] Divide the weight matrix after sparse pruning into K groups, and each group size is represented as a quantization unit as in the sparse pruning process, and the group size of the product quantization method is the same as the pruning unit size in the sparse pruning of the weight matrix by local structured sparse processing;
[0102] Perform quantization operations on the sub-matrices of each group, and perform clustering calculations on the non-empty subsets in each sub-matrix to obtain the cluster centers and labels;
[0103] Assign cluster centers and codebooks to each component in the submatrix, combine the obtained cluster centers and labels, use Euclidean distance to calculate which cluster center each component is closer to, and save the corresponding labels.
[0104] As a possible implementation of this embodiment, in the process of performing matrix weight compression on the sparse pruned weight matrix by the product quantization method, for the parameter matrix W, T is the output channel, S is the input channel; the input channel is divided into K groups, each group is represented as a quantized unit, and each quantization is performed with a group of weights W i Quantize the non-zero parameters in:
[0105] (6)
[0106] in represents the quantization formula, represents the weight, ; Kmeans is used for quantification, and the cluster centers quantified by Kmeans are not shared among the groups.
[0107] As a possible implementation of this embodiment, in the process of performing matrix weight compression on the sparse pruned weight matrix by the product quantization method,
[0108] For the vector x of the output channel of weight W, divide the vector x into k different sub-vectors u j, where 1≤j≤k, and the dimension of each subvector is T ∗ =T / k, where T is a multiple of k;
[0109] Using k different quantizers to quantize the sub-vectors separately, a given vector x is mapped as follows:
[0110] (7)
[0111] in, represents the i-th component of vector x, q j is the quantization associated with the jth subvector;
[0112] For quantization q j , associated index set I j , Codebook C j and the corresponding subscript c ji ;
[0113] The vector x recursively represents the value of the index set I=I 1 ×...×I k The codebook is defined as the Cartesian product:
[0114] (8)
[0115] in represents the i-th cluster center, the cluster center of this set is the concatenation of the cluster centers of m sub-quantizers;
[0116] Assuming that all subquantizers have the same finite number m* of cluster centers, the total number of cluster centers is given by:
[0117] (9)
[0118] In the extreme case where k=T, the components of vector x are quantized individually.
[0119] The present invention prunes the weight matrix through local structured sparsity to form local structured pruning; further improves the matrix weight compression through the product quantization method, further improves the matrix weight compression, makes up for the defects of structured pruning, achieves the compression multiple with random pruning, and overcomes the disadvantage of random pruning, that is, computationally unfriendly. The product quantization method is combined with the local structured pruning method and fine-tuned for training, which improves the compression multiple and achieves computationally friendly.
[0120] As a possible implementation method of this embodiment, when putting the compression weight into the adam module for fine-tuning, a fixed cluster label B is used, and then the cluster center D of each group is gradient updated through back propagation, thereby achieving fine-tuning of the model.
[0121] As a possible implementation of this embodiment, in the process of putting the compression weight into the adam module for fine-tuning, the calculation target is:
[0122] (10)
[0123] in is the quantized weight, represents the original weight, X is the input, represents the two-norm, represents the Frobenius inner product of two matrices, the matrix is precomputed; this objective is optimized via iterative (non-stochastic) full-batch gradient descent.
[0124] As a possible implementation of this embodiment, in the process of putting the compression weight into the adam module for fine-tuning, the learning unit scale s∈R h , which is initialized to s i :=||W i || 2 , and updated together with the cluster centers using the same Adam optimizer:
[0125] (11)
[0126] Where W is the weight and X is the input.
[0127] like Figure 2 As shown, an embodiment of the present invention provides a device for compressing a deep neural network, including:
[0128] The weight matrix acquisition module is used to obtain the weight matrix of the original deep neural network;
[0129] A first sparse pruning module, used for sparse pruning the weight matrix through local structured sparse processing;
[0130] A weight compression module is used to compress the weight matrix after sparse pruning by a product quantization method to obtain a compressed weight;
[0131] The adam fine-tuning module is used to put the compression weight into the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model.
[0132] Example 2
[0133] like Figure 3 As shown, as a possible implementation of this embodiment, the method for deep neural network compression described in Embodiment 1 further includes the following steps:
[0134] Each layer of feature maps of the original deep neural network is obtained, and each layer of feature maps is sparsely pruned through local structured sparse processing.
[0135] As a possible implementation of this embodiment, the sparse pruning of each layer of feature maps by local structured sparse processing includes:
[0136] Group the feature maps and divide the input channels into K groups, each of which is represented as a pruning unit;
[0137] Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix;
[0138] The following formula is used to expand the loss function after pruning using Taylor's formula:
[0139] (4)
[0140] in, Indicates the feature graph that needs to be pruned. represents the importance measure of the pruned feature map, represents the loss function;
[0141] Taylor expansion is used to obtain the importance measure of each pruned unit:
[0142] (5)
[0143] in, represents the importance measure of the pruned feature map, represents the feature map to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0144] like Figure 4 As shown, as a possible implementation of this embodiment, the apparatus for deep neural network compression described in Embodiment 1 further includes:
[0145] The second sparse pruning module is used to obtain the feature map of each layer of the original deep neural network and perform sparse pruning on each layer of the feature map through local structured sparse processing.
[0146] The specific process of deep neural network compression in the present invention includes:
[0147] Step 1: Prune the weight matrix through local structured sparsity. For the weight matrix W, T is the output channel and S is the input channel; divide the input channel into K groups, each group is represented as a pruning unit, and prune each time with a group of sparse W_i to form local structured pruning.
[0148] The principle of local structured sparsity is the same as that of structured sparsity, except that local structured sparsity groups the weight update matrices. Local structured sparsity can be implemented as follows:
[0149] Step 11: Group the weight update matrix. The weight of the fully connected layer is W=T*S, where T is the output channel and S is the input channel. Divide the input channel into K groups, and each group is represented as a pruning unit.
[0150] Step 12: Prune the sub-matrices of each group. For each of the K sub-matrices obtained in step S1, calculate the loss function of the model after removing a certain subset;
[0151] Step 13: The pruned loss function obtained in step 12 is expanded using Taylor's formula to obtain the importance measure of each pruned unit;
[0152] Step 14: The importance measure of each pruned unit obtained in step 13 is used to delete the units below a certain threshold, that is, to reset the weight of this part to 0;
[0153] The specific implementation of the above local structured sparse steps is as follows:
[0154] (1)
[0155] in represents the cross entropy loss function, represents the threshold for limiting the number of weights, is the original weight, is the weight that needs to be pruned, l 0 The norm restricts W i The number of non-zero parameters B in . Indicates weight W pruning Then Performing Taylor expansion yields:
[0156] (2)
[0157] in, is the original weight, is the weight that needs to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative, Represents the loss function right Find the second-order partial derivative.
[0158] Then the importance metric of each pruning can be expressed as:
[0159] (3)
[0160] in, represents the importance measure of pruning weights, represents the weight to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0161] Similarly, the local structured sparse method can also be applied to the sparseness of feature maps, and the impact of feature map pruning on network loss is used as an evaluation indicator:
[0162] (4)
[0163] in, Indicates the feature graph that needs to be pruned. represents the importance measure of the pruned feature map, Represents the loss function.
[0164] Taylor expansion yields:
[0165] (5)
[0166] in, represents the importance measure of the pruned feature map, represents the feature map to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
[0167] According to the importance of pruning , , the unimportant branches in the weights can be removed to achieve the purpose of structured sparse model.
[0168] Step 2: Further improve matrix weight compression through product quantization method. The sparse data representation obtained in step 1 is placed in the product quantization branch, and the input channel is also divided into K groups. At this stage, the units of the product quantization grouping match the size of the pruned units of the local structure pruning, so that they can be effectively combined together. For each group, K-means quantization is performed on the non-zero parameter group. It is worth noting that the K-means quantization clustering centers between groups are not shared. The introduction of this step helps to further optimize the model structure, further improve the compression of the matrix weights, make up for the defects of structured pruning, achieve the compression multiples of random pruning, and overcome the disadvantage of random pruning, that is, computational unfriendliness. Therefore, the combination of the product quantization method and the local structured pruning method not only improves the compression multiple, but also achieves computational friendliness.
[0169] In the process of executing model compression, the present invention first uses local structured sparsity. Although this structure is hardware-friendly, the compression ratio is smaller than that of the random pruning method. In order to solve this problem, on the basis of local structured sparsity, the pruned weights are further multiplied and quantized. Through this combination, the present invention not only achieves hardware friendliness, but also achieves a higher model compression rate.
[0170] Product quantization can be implemented as follows:
[0171] Step 21: Group the weight updated matrix. Here, the weight matrix is also divided into K groups according to the method in step 11 of local structured sparsity, and the size of each group is represented as a quantization unit as before;
[0172] Step 22: Quantize each group of sub-matrices. For the K sub-matrices obtained in step 21, perform clustering on the non-empty subsets in each sub-matrix to obtain the cluster center and label;
[0173] Step 23: Assign a cluster center and a codebook to each component in the submatrix. Based on the cluster centers and labels obtained in step 22, use the Euclidean distance to calculate which cluster center each component is closer to, and save the corresponding label.
[0174] Specifically, for the parameter matrix W, T is the output channel, S is the input channel; the input channel is divided into K groups, each group is represented as a quantized unit, and each quantization is based on a set of weights W i The non-zero parameters in are quantized. The quantization method used is:
[0175] (6)
[0176] in represents the quantization formula, represents the weight, . Kmeans is used for quantification, and the cluster centers quantified by Kmeans are not shared between groups. The details are as follows:
[0177] For the vector x (i.e. any row vector) of the output channel of weight W, divide x into k different sub-vectors u j , where 1≤j≤k, and the dimension of each subvector is T ∗ =T / k, where T is a multiple of k. Then, the sub-vectors are quantized using k different quantizers. Therefore, a given vector x is mapped as follows:
[0178] (7)
[0179] in, represents the i-th component of vector x, q j is the quantization associated with the jth subvector. j , the present invention associates the index set I j , Codebook C j and the corresponding subscript c ji .
[0180] The vector x recursively represents the value of the index set I=I 1 ×...×I k Therefore, the codebook is defined as the Cartesian product:
[0181] (8)
[0182] in Denotes the i-th cluster center, the cluster center of this set is the concatenation of the cluster centers of the m sub-quantizers. From now on, the present invention assumes that all sub-quantizers have the same finite number m* of cluster centers. In this case, the total number of cluster centers is given by:
[0183] (9)
[0184] Note that in the extreme case where k = T, the components of vector x are all quantized individually. The product quantizer is then a scalar quantizer where the quantization function associated with each component may be different. The advantage of the product quantizer is that it generates a large set of cluster centers from a few small sets of cluster centers (the ones associated with the subquantizers).
[0185] Step 3: The compressed weights obtained in step 2 are put into the adam module for fine-tuning. After pruning and quantization, the accuracy of the model may be lost, so fine-tuning training is required. In this stage, we fix the cluster label B and perform gradient updates on the cluster center D of each group through back propagation to achieve fine-tuning.
[0186] After pruning and quantization, although the size and computational overhead of the model can be significantly reduced, it is often accompanied by a loss of accuracy. In order to compensate for this loss, fine-tuning training is required to ensure that the model can still maintain superior performance in practical applications. This process aims to gradually restore the model to its original level of accuracy by making small adjustments to the parameters of the pruned and quantized model. In fine-tuning training, a fixed cluster label B is used, and then the cluster center D of each group is gradient updated through back propagation to achieve fine-tuning of the model. By adjusting the cluster center, the model can better adapt to the pruned and quantized parameters, which helps to improve the performance of the model in practical tasks. This fine-tuning method can not only maintain the overall structure of the model, but also restore the classification accuracy of the model to a certain extent.
[0187] For simplicity, Adam is used to approximate this minimization problem, and the calculation target is as follows:
[0188] (10)
[0189] in is the quantized weight, represents the original weight, X is the input, represents the two-norm, represents the Frobenius inner product of two matrices, the matrix is calculated in advance. This objective is optimized by iterative (non-stochastic) full-batch gradient descent.
[0190] For each cluster center weight update stage, 100 Adam steps are run with a learning rate of 1e-4. However, the final results are not sensitive to these parameters: training with fewer steps or learning rates can achieve the same loss, but takes longer to converge.
[0191] In the fine-tuning phase, similar to most other quantized algorithms, the unit scale s∈R is learned h , which is initialized to s i :=||W i || 2 , and updated together with the cluster centers using the same Adam optimizer:
[0192] (11)
[0193] Where W is the weight and X is the input.
[0194] Example 3
[0195] A computer device provided by an embodiment of the present invention includes a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor. When the device is running, the processor communicates with the memory through the bus, and the processor executes the machine-readable instructions to perform the steps of any deep neural network compression method as described above.
[0196] Specifically, the above-mentioned memory and processor are general-purpose memories and processors, and when the processor runs the computer program stored in the memory, the above-mentioned deep neural network compression method can be executed.
[0197] Corresponding to the method for starting the above-mentioned application, an embodiment of the present invention further provides a storage medium, on which a computer program is stored, and when the computer program is run by a processor, the steps of any of the above-mentioned deep neural network compression methods are executed.
[0198] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, ordinary technicians in the relevant field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for deep neural network compression, It is characterized in that The steps include: Obtaining a weight matrix of an original power transmission and transformation defect recognition model, wherein the original power transmission and transformation defect recognition model performs power transmission and transformation defect detection according to a power transmission and transformation defect monitoring image collected in real time; The weight matrix is sparsely pruned through local structured sparse processing; Obtain each layer of feature graphs of the original power transmission and transformation defect recognition model, and perform sparse pruning on each layer of feature graphs through local structured sparse processing; The weight matrix after sparse pruning is compressed by product quantization method to obtain compressed weight; The compression weights are put into the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model; The sparse pruning of the weight matrix by local structured sparse processing includes: Group the weight matrix and divide the input channels into K groups, each of which is represented as a pruning unit; Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix; The pruned loss function is expanded using Taylor's formula to obtain the importance measure of each pruned unit; According to the importance measurement of each pruned unit, the pruned units below a certain threshold are deleted, that is, the weight of the pruned unit is reset to 0; The method of compressing the weight matrix after sparse pruning by a product quantization method to obtain a compressed weight includes: Divide the weight matrix after sparse pruning into K groups, and each group size is represented as a quantization unit as in the sparse pruning process, and the group size of the product quantization method is the same as the pruning unit size in the sparse pruning of the weight matrix by local structured sparse processing; Perform quantization operations on the sub-matrices of each group, and perform clustering calculations on the non-empty subsets in each sub-matrix to obtain the cluster centers and labels; Assign cluster centers and codebooks to each component in the submatrix, combine the obtained cluster centers and labels, use Euclidean distance to calculate which cluster center each component is closer to, and save the corresponding labels; The sparse pruning of each layer of feature maps by local structured sparse processing includes: Group the feature maps and divide the input channels into K groups, each of which is represented as a pruning unit; Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix; The following formula is used to expand the loss function after pruning using Taylor's formula: (4), in, Indicates the feature graph that needs to be pruned. represents the importance measure of the pruned feature map, represents the loss function; Taylor expansion is used to obtain the importance measure of each pruned unit: (5), in, represents the importance measure of the pruned feature map, represents the feature map to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
2. The method for deep neural network compression according to claim 1, It is characterized in that The formula for pruning the sub-matrix of each group is as follows: (1), in, represents the cross entropy loss function, represents the threshold for limiting the number of weights, is the original weight, is the weight that needs to be pruned, l 0 The norm restricts W i The number of non-zero parameters B in ; Indicates weight W pruning .
3. The method for deep neural network compression according to claim 2, It is characterized in that The loss function after pruning is expanded using Taylor's formula: (2), in, is the original weight, is the weight that needs to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative, Represents the loss function right Find the second-order partial derivative; The importance metric of each pruning unit is expressed as: (3), in, represents the importance measure of pruning weights, represents the weight to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
4. The method for deep neural network compression according to claim 1, It is characterized in that In the process of matrix weight compression of the sparse pruned weight matrix by the product quantization method, for the parameter matrix W, T is the output channel and S is the input channel; The input channels are divided into K groups, each group is represented as a quantized unit, and each quantization is performed with a set of weights W i Quantize the non-zero parameters in: (6), in represents the quantization formula, represents the weight, ; Kmeans is used for quantification, and the cluster centers quantified by Kmeans are not shared among the groups.
5. The method for deep neural network compression according to claim 4, It is characterized in that In the process of matrix weight compression of the sparse pruned weight matrix by product quantization method, For the vector x of the output channel of weight W, divide the vector x into k different sub-vectors u j , where 1≤j≤k, and the dimension of each subvector is T ∗ =T / k, where T is a multiple of k; Using k different quantizers to quantize the sub-vectors separately, a given vector x is mapped as follows: (7), in, represents the i-th component of vector x, q j is the quantization associated with the jth subvector; For quantization q j , associated index set I j , Codebook C j and the corresponding subscript c ji ; The vector x recursively represents the value of the index set I=I 1 ×...×I k The codebook is defined as the Cartesian product: (8), in, represents the i-th cluster center, the cluster center of this set is the concatenation of the cluster centers of m sub-quantizers; Assuming that all subquantizers have the same finite number m* of cluster centers, the total number of cluster centers is given by: (9), In the extreme case where k=T, the components of vector x are quantized individually.
6. The method for deep neural network compression according to claim 1, It is characterized in that In the process of putting the compressed weights into the adam module for fine-tuning, a fixed cluster label B is used, and then the cluster center D of each group is gradient updated through back propagation to achieve fine-tuning of the model.
7. The method for deep neural network compression according to claim 6, It is characterized in that When putting the compressed weights into the adam module for fine-tuning, the calculation target is: (10), in is the quantized weight, represents the weight, X is the input, represents the two-norm, represents the Frobenius inner product of two matrices, the matrix is calculated in advance; this objective is optimized by iterative full-batch gradient descent.
8. The method for deep neural network compression according to claim 6, It is characterized in that During fine-tuning by putting the compressed weights into the adam module, the unit scale s∈R is learned h , which is initialized to s i =||W i || 2 , and updated together with the cluster centers using the same Adam optimizer: (11), Where W is the weight and X is the input.
9. A device for compressing a deep neural network, It is characterized in that include: A weight matrix acquisition module is used to acquire a weight matrix of an original power transmission and transformation defect recognition model, wherein the original power transmission and transformation defect recognition model performs power transmission and transformation defect detection based on power transmission and transformation defect monitoring images collected in real time; A first sparse pruning module, used for sparse pruning the weight matrix through local structured sparse processing; The second sparse pruning module is used to obtain each layer of feature maps of the original power transmission and transformation defect recognition model, and perform sparse pruning on each layer of feature maps through local structured sparse processing; A weight compression module is used to compress the weight matrix after sparse pruning by a product quantization method to obtain a compressed weight; The adam fine-tuning module is used to put the compression weight into the adam module for fine-tuning to obtain the compressed power transmission and transformation defect recognition model; The sparse pruning of the weight matrix by local structured sparse processing includes: Group the weight matrix and divide the input channels into K groups, each of which is represented as a pruning unit; Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix; The pruned loss function is expanded using Taylor's formula to obtain the importance measure of each pruned unit; According to the importance measurement of each pruned unit, the pruned units below a certain threshold are deleted, that is, the weight of the pruned unit is reset to 0; The method of compressing the weight matrix after sparse pruning by a product quantization method to obtain a compressed weight includes: Divide the weight matrix after sparse pruning into K groups, and each group size is represented as a quantization unit as in the sparse pruning process, and the group size of the product quantization method is the same as the pruning unit size in the sparse pruning of the weight matrix by local structured sparse processing; Perform quantization operations on the sub-matrices of each group, and perform clustering calculations on the non-empty subsets in each sub-matrix to obtain the cluster centers and labels; Assign cluster centers and codebooks to each component in the submatrix, combine the obtained cluster centers and labels, use Euclidean distance to calculate which cluster center each component is closer to, and save the corresponding labels; The sparse pruning of each layer of feature maps by local structured sparse processing includes: Group the feature maps and divide the input channels into K groups, each of which is represented as a pruning unit; Perform pruning operations on the sub-matrices of each group, and calculate the loss function of the deep neural network after removing a certain subset for each sub-matrix; The following formula is used to expand the loss function after pruning using Taylor's formula: (4), in, Indicates the feature graph that needs to be pruned. represents the importance measure of the pruned feature map, represents the loss function; Taylor expansion is used to obtain the importance measure of each pruned unit: (5), in, represents the importance measure of the pruned feature map, represents the feature map to be pruned, Represents the loss function right Find the transpose of the first-order partial derivative.
10. A computer device, It is characterized in that It includes a processor, a memory and a bus, the memory stores machine-readable instructions executable by the processor, when the computer device is running, the processor and the memory communicate through the bus, and the processor executes the machine-readable instructions to perform the steps of the deep neural network compression method as described in any one of claims 1-8.
11. A storage medium, It is characterized in that The storage medium stores a computer program, which, when executed by a processor, executes the steps of the method for deep neural network compression as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Power transmission and transformation defect identification method and device, computer equipment and storage medium
CN116912637A
Convolutional neural network processing method and device, equipment and storage medium
CN111382854A
Compression method and system for deep convolutional neural network
CN111612143A
Cited By
Efficient collaborative reasoning method and system for privacy protection
CN119886325A
