Model compression method and device and readable storage medium
By quantizing the model and adding cross-layer connection modules in the deep neural network model, the problem of parameter volume and calculation volume when the deep neural network model is deployed on devices with limited resources is solved, and high-precision and fast inference effects are achieved.
Patent Information
- Application Number
- CN202310319361.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-22
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2043-03-22
AI Technical Summary
When deep neural network models are deployed on hardware devices with limited resources, they face deployment challenges caused by the huge amount of parameters and calculations, and it is difficult to guarantee the accuracy of the model after compression.
A lightweight second network model is obtained through model quantization, and a cross-layer connection module is added to the target network layer to make up for the error caused by quantization. Finally, the third network model is trained to improve accuracy.
It achieves the improvement of accuracy while compressing the model, and obtains a lightweight target network model, which can be deployed on hardware devices with limited resources and has the characteristics of high accuracy and fast reasoning.
Smart Images

Figure CN118690796A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a model compression method, device and readable storage medium. Background Art
[0002] With the development of deep learning technology, deep neural network models have been widely used in various application scenarios, such as image processing, speech recognition, reasoning / prediction, knowledge expression, and operation control.
[0003] In order to improve the performance of deep neural network models, the number of model parameters and the amount of computation have increased dramatically, which has brought great challenges to the training and deployment of the model. In particular, due to the huge number of parameters and computation of deep neural network models, it is difficult to deploy deep neural network models on hardware devices with limited resources (such as mobile devices).
[0004] In order to reduce the hardware consumption of deep neural network models and enable them to be deployed on hardware devices with limited resources, model compression technology is usually used to improve the operating efficiency of the model and reduce the computing and storage resources occupied by the model. However, the accuracy of the compressed neural network model is currently difficult to guarantee. Summary of the invention
[0005] Embodiments of the present invention provide a model compression method, device and readable storage medium, which can improve the accuracy of the model while compressing the model and reduce the computing resources and storage resources occupied by the model.
[0006] In a first aspect, an embodiment of the present invention discloses a model compression method, the method comprising:
[0007] Acquire a first network model, where the first network model is a floating-point model trained based on a training set;
[0008] Performing model quantization on the first network model to obtain a second network model;
[0009] Determining a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition;
[0010] Adding a corresponding cross-layer connection module at each of the target network layers to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer;
[0011] The third network model is trained to obtain a target network model.
[0012] In a second aspect, an embodiment of the present invention discloses a model compression device, the device comprising:
[0013] A model acquisition module, used to acquire a first network model, where the first network model is a floating-point model trained based on a training set;
[0014] A model quantization module, used to perform model quantization on the first network model to obtain a second network model;
[0015] A target determination module, used to determine a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition;
[0016] A model processing module, used for adding a corresponding cross-layer connection module at each of the target network layers to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer;
[0017] The model training module is used to train the third network model to obtain a target network model.
[0018] In a third aspect, an embodiment of the present invention discloses a machine-readable medium having instructions stored thereon, which, when executed by one or more processors of a device, causes the device to execute one or more of the model compression methods described above.
[0019] The embodiments of the present invention include the following advantages:
[0020] The model compression method provided by the embodiment of the present invention performs model quantization on the first network model to be compressed to obtain the second network model, determines the target network layer in the second network model, and adds a corresponding cross-layer connection module to each target network layer to obtain a third network model. The quantization loss of the target network layer meets the preset conditions, that is, the quantization loss of the target network layer exceeds the acceptable range, resulting in the accuracy of the entire model failing to meet the preset accuracy. The embodiment of the present invention adds a corresponding cross-layer connection module to each target network layer, and the cross-layer connection module can add a small amount of calculation on the basis of the second network model, thereby increasing the expression ability of the model, thereby compensating for the error caused by quantization of the target network layer. Finally, training the third network model can improve the model accuracy and obtain the target network model. The target network model is a lightweight model with the characteristics of high accuracy and high speed. It can be deployed on hardware devices with limited resources and can quickly infer and obtain correct prediction results under low power consumption. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0022] Figure 1 is a flow chart of steps of an embodiment of a model compression method of the present invention;
[0023] Figure 2 It is a schematic diagram of a model structure of adding a cross-layer connection module of the present invention;
[0024] Figure 3 It is a structural block diagram of an embodiment of a model compression device of the present invention. DETAILED DESCRIPTION
[0025] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0026] The terms "first", "second", etc. in the specification and claims of the present invention are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable when appropriate, so that the embodiments of the present invention can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, the term "and / or" in the specification and claims is used to describe the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. In the embodiments of the present invention, the term "multiple" refers to two or more, and other quantifiers are similar.
[0027] Reference Figure 1 , shows a flow chart of a model compression method embodiment of the present invention, the method may include the following steps:
[0028] Step 101: Obtain a first network model, where the first network model is a floating-point model trained based on a training set;
[0029] Step 102: quantize the first network model to obtain a second network model;
[0030] Step 103: determining a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition;
[0031] Step 104: adding a corresponding cross-layer connection module at each of the target network layers to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer;
[0032] Step 105: Train the third network model to obtain a target network model.
[0033] The model compression method provided by the embodiment of the present invention can reduce the accuracy loss of the model while compressing the model.
[0034] First, obtain the first network model, which is a floating-point model that has been trained based on the training set. The floating-point model refers to the weights and activations of each layer in the model as floating-point numbers. The first network model is a deep neural network model to be compressed. In practical applications, the first network model can be used to perform the target task. For example, for input X, the first network model can output Y, and after Y is mapped by Softmax (normalized exponential function), the probability value of the corresponding category is output.
[0035] The embodiment of the present invention does not limit the application scenario of the first network model. For example, the target tasks include but are not limited to face recognition, image classification, object detection, semantic segmentation, speech recognition, machine translation, natural language processing, and recommendation systems.
[0036] In an embodiment of the present invention, the first network model can be obtained by pre-training an existing neural network in a supervised or unsupervised manner according to a training set and a machine learning method. It should be noted that the embodiment of the present invention does not limit the network structure and training method of the first network model. The first network model can integrate multiple neural networks. The neural network includes but is not limited to a combination, superposition, and nesting of at least one or at least two of the following: CNN (Convolutional Neural Network), LSTM (Long Short-Term Memory) network, RNN (Recurrent Neural Network), attention neural network, etc.
[0037] In a specific implementation, for different application scenarios, the training data required for the corresponding target task can be collected to obtain a training set. For example, for the application scenario of image classification, a large number of pictures can be collected and manually annotated to obtain a training set. The first network model trained with the training set can perform the target task of image classification. For another example, for the application scenario of speech recognition, a large number of recordings can be collected and manually annotated to obtain a training set. The first network model trained with the training set can perform the target task of speech recognition.
[0038] Taking the scenario of image classification as an example, a training set is obtained, wherein the training set includes training data, and the training data includes multiple collected images. Each training data is labeled, and the category corresponding to each image is labeled to obtain the true label of each training data. Furthermore, before using the training set to train the first network model, the training data in the training set can also be enhanced, and the training set after data enhancement can be used for training to increase the diversity of training data and improve the accuracy of the first network model. Among them, the data enhancement methods include but are not limited to basic data enhancement methods such as scaling, rotation, segmentation, cutout, color enhancement, and adding noise, and can also include advanced data enhancement methods such as mixup and random erasing.
[0039] The embodiment of the present invention does not limit the model structure of the first network model. Exemplarily, the model structure may include convolutional neural networks of various sizes, such as one or more of 3×3 convolution, 5×5 convolution, dilated convolution, and grouped convolution; the model structure may also include a batch normalization layer; the activation function of the model structure may use any one of relu, swish, and sigmoid; the model structure may also include a residual structure, such as the residual structure of resnet or the inverted residual structure of mobilenent; the final output layer of the model structure may be a global pooling layer or a fully connected layer.
[0040] The first network model is trained using the collected training set, and the network parameters are updated using back propagation until the model converges to obtain the trained first network model. The model convergence means that the output result of the first network model meets the preset accuracy.
[0041] Next, the first network model is quantized to obtain a second network model. The second network model is a compressed model.
[0042] Model quantization refers to the process of approximating floating-point activations or weights (usually represented by 32-bit floating-point numbers) to low-bit integers (such as 16-bit or 8-bit), and then completing the calculation under the low-bit representation. Generally speaking, model quantization can compress model parameters, thereby reducing model storage overhead; and by reducing memory access and effectively utilizing low-bit computing instructions, the reasoning speed can be improved. The embodiment of the present invention performs model quantization on the first network model to obtain a second network model, which can achieve model compression, and the obtained second network model is a lightweight model.
[0043] The embodiment of the present invention does not limit the method used to quantize the student network model. In an optional embodiment of the present invention, quantizing the first network model to obtain the second network model may include:
[0044] Step S11, inserting a pseudo quantization node at the target node of the first network model;
[0045] Step S12, obtaining a calibration set, where the calibration set is a subset of the training set;
[0046] Step S13, sequentially inputting the calibration data in the calibration set into the first network model into which the pseudo quantization nodes have been inserted, and obtaining the quantization information of each pseudo quantization node;
[0047] Step S14: Initialize the quantization parameter of each pseudo quantization node according to the quantization information of each pseudo quantization node to obtain a second network model.
[0048] The pseudo quantization node includes a quantization node and an inverse quantization node. The quantization node quantizes the floating-point input according to the quantization parameter to obtain a quantized output, which is fixed-point data; the inverse quantization node inversely quantizes the quantized output according to the quantization parameter to obtain a floating-point output.
[0049] For a quantized node, the quantized output can be expressed as follows:
[0050]
[0051] Among them, Q represents quantized output, R represents floating-point input, and S and Z represent quantization parameters.
[0052] For the Dequantization node, the floating point output can be represented as follows:
[0053] R=(QZ)*S (2)
[0054] Among them, R represents floating-point output, Q represents quantized input, and S and Z represent quantization parameters.
[0055] The pseudo-quantization node is used to quantize and dequantize the data. The pseudo-quantization node inserted into the student network model can first quantize the input high-precision floating-point data to map it to low-precision fixed-point data, and then dequantize the low-precision fixed-point data to obtain the output high-precision floating-point data. The pseudo-quantization node can keep the input data and output data as floating-point numbers, but the difference is that the input data is a continuous floating-point number, while the output data is a discretized floating-point number. Among them, the continuous floating-point number refers to any floating-point number within a preset continuous range. For example, if the preset continuous range is 0 to 1, the continuous floating-point number can include any decimal between 0 and 1. The discretized floating-point number refers to any floating-point number within a preset discrete range. For example, if the preset discrete range includes the following three floating-point numbers: 0.33, 0.66, and 0.99, the discretized floating-point number can include any of these three floating-point numbers.
[0056] The embodiment of the present invention inserts a pseudo-quantization node at the target node of the first network model, and the target node refers to the network layer node that supports the quantization operation in the first network model. For example, the target node includes but is not limited to nodes such as weights, activations, and input and output nodes of operators. In a specific implementation, the deep learning algorithm is composed of computing units, which are also called operators. In the network model, the operator corresponds to the computing logic in the network layer, for example: the convolution layer that implements the convolution operation is an operator; the pooling layer that implements the pooling operation is an operator; the activation layer that implements the activation operation is an operator; the fully connected layer that implements the fully connected operation is an operator; and so on. For an operator that needs to be quantized, the embodiment of the present invention can insert a pseudo-quantization node before (input node) and after (output node) the operator.
[0057] In an example, assuming that the first network model includes two convolutional layers, a pseudo-quantization node can be inserted at the input node of the first convolutional layer, a pseudo-quantization node can be inserted at the weight node of the first convolutional layer, a pseudo-quantization node can be inserted at the output node of the first convolutional layer, a pseudo-quantization node can be inserted at the weight node of the second convolutional layer, and a pseudo-quantization node can be inserted at the output node of the second convolutional layer, for a total of 5 pseudo-quantization nodes. It should be noted that since the output of the first convolutional layer is the input of the second convolutional layer, there is no need to insert a pseudo-quantization node at the input node of the second convolutional layer in this example. Each pseudo-quantization node corresponds to its own quantization parameter.
[0058] It is understandable that the above examples are only for the convenience of explanation, and the embodiment of the present invention does not limit the position and number of inserted pseudo quantization nodes. For example, assuming that the output of a convolutional layer in the first network model is connected to the input of a batch normalization layer, and the output of the batch normalization layer is connected to the input of the activation function, then a pseudo quantization node can be inserted at the output node of the batch normalization layer, and a pseudo quantization node can be inserted at the output node of the activation function.
[0059] In the embodiment of the present invention, a pseudo-quantization node is inserted at the target node of the first network model, calibration data in the calibration set is sequentially input into the first network model into which the pseudo-quantization node has been inserted, and quantization information of each pseudo-quantization node is counted, and the parameters of each pseudo-quantization node are initialized according to the quantization information of each pseudo-quantization node, so as to obtain a second network model, that is, the quantized first network model. The calibration set may be a subset of the training set. For example, a portion of data may be extracted from the training set as the calibration set.
[0060] Optionally, the quantization information may include a maximum floating-point value and a minimum floating-point value flowing through each pseudo-quantization node, and a preset maximum fixed-point value and a minimum fixed-point value corresponding to each pseudo-quantization node.
[0061] In an embodiment of the present invention, the inserted pseudo-quantization node can be used to count the maximum floating-point value and the minimum floating-point value flowing through the node, and can also be used to simulate the precision loss caused by quantization, and reversely adjust the model parameters according to the precision loss to continuously reduce the precision loss caused by quantization, thereby achieving the purpose of improving the accuracy of the second network model.
[0062] According to the quantization information of each pseudo-quantization node obtained by statistics, the quantization parameters of each pseudo-quantization node can be initialized. The quantization parameters include a scaling factor S (scale) and a zero point Z (zero_point). The goal of model quantization is to calculate the scaling factor S and the zero point Z based on the numerical range of the floating-point input and the quantized output. Among them, the floating-point input refers to the floating-point input data. The quantized output refers to the pseudo-quantization node using the quantization parameter to operate the floating-point input, mapping the continuous numerical floating-point input to a discrete numerical value, and then mapping it to the output interval within a preset range to obtain a quantized output. The value of zero_point may be 0 (corresponding to symmetric quantization) or may not be 0 (corresponding to asymmetric quantization).
[0063] In an embodiment of the present invention, the maximum floating-point value and the minimum floating-point value flowing through each pseudo-quantization node refer to: the maximum floating-point value and the minimum floating-point value corresponding to the floating-point input of each pseudo-quantization node; the preset maximum fixed-point value and the minimum fixed-point value corresponding to each pseudo-quantization node refer to: the preset maximum fixed-point value and the minimum fixed-point value corresponding to the quantization output of each pseudo-quantization node.
[0064] After the quantization information of each pseudo-quantization node in the first network model is counted using the calibration set, the quantization parameter of each pseudo-quantization node can be calculated by the following formula:
[0065]
[0066]
[0067] Among them, Rmax and Rmin represent the maximum floating-point value and the minimum floating-point value obtained by statistics, respectively, and Qmax and Qmin represent the maximum fixed-point value and the minimum fixed-point value, respectively. The maximum fixed-point value and the minimum fixed-point value can be calculated according to the quantization bit width. For example, if the quantization bit width is 8 bits and the range of the signed number is -127 to 127, then Qmax is 127 and Qmin is 127; if the range of the unsigned number is 0 to 255, then Qmax is 0 and Qmin is 255.
[0068] In an example, assuming that a convolution layer in the first network model is quantized, a pseudo-quantization node can be inserted at each of the three nodes of the weight w, input x, and output y of the convolution layer, that is, three pseudo-quantization nodes are inserted. Assume that the pseudo-quantization node of the weight w is denoted as q1, the pseudo-quantization node of the input x is denoted as q2, and the pseudo-quantization node of the output y is denoted as q3. The Rmax and Rmin corresponding to the pseudo-quantization node q1 are the maximum and minimum values of the floating-point parameters of this convolution layer. The Rmax and Rmin corresponding to the pseudo-quantization node q2 and the pseudo-quantization node q3 need to be obtained through the calibration data in the calibration set. For example, assuming that the calibration set includes 100 calibration data, which are 100 pictures. These 100 pictures are input into the first network model into which the above three pseudo-quantization nodes have been inserted. By counting the maximum and minimum floating-point values flowing through the input x after inputting these 100 pictures, the Rmax and Rmin corresponding to the pseudo-quantization node q2 can be obtained. By counting the maximum and minimum floating-point values flowing through the output y after inputting these 100 pictures, the Rmax and Rmin corresponding to the pseudo-quantization node q3 can be obtained. It should be noted that the Rmax and Rmin corresponding to each pseudo-quantization node are the maximum and minimum values obtained by counting all the calibration data in the calibration set, that is, Rmax and Rmin are globally counted through the calibration data.
[0069] In an optional embodiment of the present invention, the method may further include: using the second network model to perform reasoning, and determining whether an output result of the second network model meets a preset accuracy.
[0070] After the first network model is quantized to obtain a lightweight second network model, the second network model can be used for reasoning to determine whether the output result of the second network model meets the preset accuracy; if the preset accuracy is met, it means that the accuracy loss caused by the model quantization process is small, and the second network model still has a high accuracy, then the subsequent steps 103 to 105 do not need to be performed. If the output result of the second network model does not meet the preset accuracy, it means that the accuracy loss caused by the model quantization process is large, and the accuracy of the second network model can no longer meet the preset requirements, then the accuracy of the second network model can be improved by performing subsequent steps 103 to 105, so that the accuracy of the second network model can reach the preset accuracy.
[0071] In one example, assuming that the first network model includes two convolutional layers, 5 pseudo-quantization nodes can be inserted into the first network model. First, the quantization parameters of each pseudo-quantization node in the first network model are initialized using the calibration set to obtain a second network model. Then, the second network model is used for reasoning to determine whether the output result of the second network model meets the preset accuracy. Specifically, the input data is input into the second network model, and the input data undergoes quantization and dequantization operations of the pseudo-quantization node at the input node of the first convolutional layer, and quantization and dequantization operations of the pseudo-quantization node at the weight node of the first convolutional layer to obtain the output data of the first convolutional layer; the output data undergoes quantization and dequantization operations of the pseudo-quantization node at the output node of the first convolutional layer to obtain the input data of the second convolutional layer; the input data undergoes quantization and dequantization operations of the pseudo-quantization node at the weight node of the second convolutional layer to obtain the output data of the second convolutional layer; the output data undergoes quantization and dequantization operations of the pseudo-quantization node at the output node of the second convolutional layer to obtain the output result.
[0072] The embodiment of the present invention determines whether the output result of the second network model meets the preset accuracy, that is, determines whether the accuracy loss of the second network model is within an acceptable range.
[0073] The preset accuracy can be set according to actual needs. Meeting the preset accuracy means that the accuracy of the output result of the second network model is greater than the preset accuracy. For example, assuming that the preset accuracy is 78, the accuracy of the output result of the first network model is 80, and the accuracy of the output result of the second network model obtained after quantization is 78.1, it is considered that the accuracy of the output result of the second network model meets the preset accuracy.
[0074] In an optional embodiment of the present invention, the network layer of the second network model may include weights of different bit widths.
[0075] The embodiment of the present invention can use a mixed precision quantization method to further improve the model accuracy. The mixed precision quantization method means that the network layer of the second network model obtained after the first network model is quantized can include weights of different bit widths. Exemplarily, a second network model includes convolution layer 1, convolution layer 2 and convolution layer 3; wherein the weight bit width of convolution layer 1 is 4 bits, the weight bit width of convolution layer 2 is 8 bits, and the weight bit width of convolution layer 3 is 2 bits, then the second network model is mixed precision.
[0076] Specifically, for a first network model to be quantized, the target bit width of each network layer to be quantized of the first network model can be determined first; then, the first network model is quantized according to the target bit width of each network layer to be quantized to obtain a second network model.
[0077] Optionally, the target bit width can be determined according to the quantization sensitivity of each network layer. The step of determining the target bit width to be quantized of each network layer of the first network model according to the quantization sensitivity of each network layer may include: calculating the single-layer quantization loss of each network layer of the first network model; determining the quantization sensitivity of each network layer according to the single-layer quantization loss of each network layer; determining the target bit width to be quantized of each network layer according to the quantization sensitivity of each network layer.
[0078] The quantization sensitivity of a network layer can be determined by the single-layer quantization loss of the network layer, which refers to the precision loss incurred in the quantization process of quantizing only the parameters (such as weights and / or activations) of this layer. The larger the single-layer quantization loss, the higher the quantization sensitivity of the network layer. Conversely, the smaller the single-layer quantization loss, the lower the quantization sensitivity of the network layer.
[0079] For network layers with high quantization sensitivity, a high bit width can be allocated; for network layers with low quantization sensitivity, a low bit width can be allocated. Furthermore, a sensitivity threshold can be set, and the target bit width to be quantized for each network layer can be determined based on the quantization sensitivity of each network layer and the sensitivity threshold. For example, for network layers greater than or equal to the sensitivity threshold, a first target bit width can be set; for network layers less than the sensitivity threshold, a second target bit width can be set.
[0080] Exemplarily, for a first network model to be quantized, a mixed precision quantization method is used for the first network model, and the mixed precision includes a first target bit width (such as 8 bits) and a second target bit width (such as 4 bits). A sensitivity threshold can be set. For network layers that are greater than or equal to the sensitivity threshold, the target bit width of the network layer to be quantized can be set to the first target bit width (such as 8 bits); for network layers that are less than the sensitivity threshold, the target bit width of the network layer to be quantized can be set to the second target bit width (such as 4 bits).
[0081] It should be noted that the embodiment of the present invention does not limit the method for determining the target bit width to be quantized for each network layer in the first network model. For example, the target bit width can be determined by a neural architecture search (NAS) method.
[0082] The basic idea of NAS is to give a set of candidate neural network structures called a search space, and use a certain search strategy to search for the optimal network structure therefrom. An embodiment of the present invention may set a search space, which includes convolutional layers of different bit widths, such as 8-bit convolutional layers and 4-bit convolutional layers. A neural network structure composed of convolutional layers of different bit widths is searched from the search space using a certain search strategy, and the neural network structure satisfies the preset constraints. The preset constraints may be the total bit width of the neural network structure, that is, the sum of the bit widths of all network layers of the neural network structure obtained by the search does not exceed the total bit width. The search strategy is not limited in the embodiment of the present invention, and any one of a random search strategy, a modular search strategy, and a continuous search strategy may be used.
[0083] After the first network model is quantized, a second network model is obtained. Compared with the first network model, the second network model reduces the size of the space occupied by the parameters and reduces the hardware memory occupied during operation. However, after the first network model is quantized to obtain the second network model, the second network model will cause a loss in accuracy compared with the first network model. In order to reduce the accuracy loss caused by quantization, an embodiment of the present invention determines a target network layer in the second network model; and adds a corresponding cross-layer connection module at each target network layer to obtain a third network model. Among them, the quantization loss of the target network layer meets a preset condition, and the preset condition is used to indicate that the quantization loss of the target network layer exceeds a preset threshold, that is, the quantization loss of the target network layer exceeds an acceptable range. The cross-layer connection module is used to compensate for the quantization loss generated by the target network layer.
[0084] The quantization loss refers to the precision loss caused by quantization. The target network layer is a network layer in the second network model that has a large precision loss due to quantization, and the precision loss of the target network layer due to quantization exceeds an acceptable range. The embodiment of the present invention determines the target network layer in the second network model, with the purpose of compensating for the precision loss caused by the target network layer, thereby improving the precision of the entire model.
[0085] In an optional embodiment of the present invention, determining the target network layer in the second network model may include:
[0086] Step S21, inputting the same input data into the first network model and the second network model respectively for reasoning;
[0087] Step S22: extracting output features of each network layer in the first network model and the second network model;
[0088] Step S23, calculating the similarity between the output features of the corresponding network layers in the first network model and the second network model;
[0089] Step S24: Determine in the second network model a network layer whose similarity is less than a preset value as a target network layer.
[0090] The embodiment of the present invention inputs the same input data into the first network model and the second network model for reasoning, and the input data is calculated by each network layer in the first network model in turn, and the embodiment of the present invention extracts the output features of each network layer in the first network model. Similarly, the input data is calculated by each network layer and each pseudo-quantization node in the second network model in turn, and the embodiment of the present invention extracts the output features of each network layer in the second network model. It should be noted that for each network layer in the second network model, if a pseudo-quantization node has been inserted at the output node of the network layer, extracting the output features of the network layer refers to extracting the output features of the pseudo-quantization node at the output node of the network layer.
[0091] Next, the similarity between the output features of the corresponding network layers in the first network model and the second network model is calculated. The embodiment of the present invention does not limit the method for calculating the similarity. For example, the similarity can be cosine similarity or KL divergence (relative entropy), etc. Taking cosine similarity as an example, if the cosine similarity between the output features of a certain network layer corresponding to the first network model and the second network model is less than a preset value, it means that there is a large error in the output features of the network layer in the second network model after quantization, and the precision loss after quantization is large, and the network layer can be determined as the target network layer.
[0092] The embodiment of the present invention determines the target network layer in the second network model, and adds a corresponding cross-layer connection module to each target network layer to obtain a third network model. The cross-layer connection module can add a small amount of calculation on the basis of the second network model, thereby increasing the expression ability of the model, thereby compensating for the error caused by quantization of the target network layer. Compared with the second network model, the third network model can improve a certain degree of accuracy through the cross-layer connection module.
[0093] The embodiment of the present invention does not limit the structure of the cross-layer connection module, and the cross-layer connection module can be defined as a lightweight network layer. In an optional embodiment of the present invention, the cross-layer connection module can include a convolution layer, a normalization layer, and an activation function layer. The input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer.
[0094] Reference Figure 2 , shows a schematic diagram of a model structure in which a cross-layer connection module is added according to an embodiment of the present invention. Figure 2 The third network model shown includes network layer 1, network layer 2 and network layer 3, network layer 2 is the target network layer, and a corresponding cross-layer connection module is added to network layer 2. The input of the cross-layer connection module is connected to the input of network layer 2, and the output of the cross-layer connection module is connected to the output of network layer 2. Figure 2 As shown, the output of network layer 1 is input to network layer 2 and the cross-layer connection module for calculation respectively. The output of the cross-layer connection module is added to the output of network layer 2 and then input to network layer 3 for calculation. Through the cross-layer connection module, network layer 3 can receive richer feature information, thereby compensating for the error caused by quantization of network layer 2, thereby improving the accuracy of the model.
[0095] In an optional embodiment of the present invention, the cross-layer connection module may include a pseudo-quantization node. In an embodiment of the present invention, the cross-layer connection module is used to compensate for the quantization loss generated by the target network layer. After the training is completed, the cross-layer connection module still needs to be retained to participate in the reasoning process in actual use. Therefore, the added cross-layer connection module also needs to be quantized. The target network model obtained after the training is completed can be a fully quantized model, that is, each network layer of the target network model is quantized. It should be noted that the method of inserting a pseudo-quantization node in the cross-layer connection module can be the same as the method of inserting a pseudo-quantization node in the first network model. For example, a pseudo-quantization node is inserted at the target node of the cross-layer connection module.
[0096] In an optional embodiment of the present invention, the number of channels of the convolutional layer in the cross-layer connection module is less than a preset ratio of the number of channels of the convolutional layer in the second network model. In the embodiment of the present invention, adding a cross-layer connection module at the target network layer will increase the number of parameters and the amount of calculation of the model. If the number of parameters and the amount of calculation increased by the cross-layer connection module are too large, the calculation speed of the entire model will be affected, and the purpose of improving the calculation speed of the model through quantization will not be achieved. Therefore, in order to avoid this problem, the embodiment of the present invention controls the number of channels of the convolutional layer in the cross-layer connection module to minimize the number of parameters and the amount of calculation increased by the cross-layer connection module while meeting the model accuracy requirements. For example, the number of channels of the convolutional layer in the cross-layer connection module is controlled to be less than a preset ratio of the number of channels of the convolutional layer in the second network model. The preset ratio can be set according to actual needs. For example, the preset ratio can be one tenth, that is, the number of channels of the convolutional layer in the cross-layer connection module should be less than one tenth of the number of channels of the convolutional layer in the second network model.
[0097] After adding a cross-layer connection module to the target network layer in the second network model, a third network model is obtained. The third network model including the cross-layer connection module is then trained to improve the model accuracy, and the target network model is obtained when the training is completed. The embodiment of the present invention does not limit the method for training the third network model. For example, the third network model can be calibrated again using a calibration set to further optimize the quantization parameters of each pseudo-quantization node, or the teacher network model can be used to perform knowledge distillation on the third network model.
[0098] In an optional embodiment of the present invention, the training of the third network model to obtain the target network model may include: using the first network model as a teacher network model, and using the third network model as a student network model to perform knowledge distillation training to obtain the target network model.
[0099] The core idea of knowledge distillation (KD) is to improve the performance of the student network model without changing its structure by guiding the lightweight student network model to "imitate" the teacher network model with better performance and more complex structure.
[0100] In an embodiment of the present invention, the first network model is used as a teacher network model, and the third network model is used as a student network model for knowledge distillation training, so that the output of the third network model approaches the output of the first network model. When the training is completed, the obtained target network model has the same or similar accuracy as the first network model.
[0101] In an optional embodiment of the present invention, the step of using the first network model as a teacher network model and the third network model as a student network model for knowledge distillation training to obtain a target network model may include:
[0102] The training data in the training set are input into the teacher network model and the student network model in turn for inference, and the parameters of the student network model are adjusted according to a preset loss function. When the iteration stop condition is met, the target network model is obtained; the preset loss function includes a first loss function and a second loss function, the first loss function is determined according to the output results of the network output layer of the student network model and the network output layer of the teacher network model for the same training data; the second loss function is determined according to the prediction results of the student network model for the training data and the true labels of the training data.
[0103] The purpose of knowledge distillation is to improve the fitting ability of the student network model (the third network model), that is, the learning ability. Since the small model (student network model) has relatively few parameters, its learning ability is relatively weak. It is difficult to converge to the same level as the teacher network model (the first network model) through direct training, that is, the error between the output result of the student network model and the actual value is difficult to reduce to the same level as the teacher network model. Therefore, in the distillation training process, the output result of the teacher network model when the same input data is input is used as the label of the input data, and the output result of the student network model is used to fit the output of the large model (teacher network model) to converge to a level close to the teacher network model, which can improve the learning ability of the student network model.
[0104] The embodiment of the present invention inputs the training data in the training set into the teacher network model and the student network model in sequence, and the same training data is input into the teacher network model and the student network model each time. Taking the image recognition scenario as an example, the training data is input into the teacher network model and the student network model respectively, and is calculated layer by layer through each network layer until the output layer, and then the probability distribution of the recognition category is output through the fully connected layer. For example, if the recognition category includes N categories, the feature output by the fully connected layer is an N-dimensional feature, which represents the output probability of each category. That is, the fully connected layer of the teacher network model will output N-dimensional features, and the fully connected layer of the student network model will also output N-dimensional features.
[0105] The N-dimensional features output by the fully connected layer of the teacher network model are called soft labels. The goal of knowledge distillation is to use the output of the teacher network model to guide the learning of the student network model, and to complete the migration of the knowledge of the high-precision model (teacher network model) to the low-precision model (student network model), so as to achieve the purpose of improving the accuracy of the low-precision model. In the process of using the teacher network model to perform knowledge distillation training on the student network model, the embodiment of the present invention adjusts the parameters of the student network model according to the preset loss function in each round of training. Specifically, the network parameters of the student network model and the quantization parameters of each pseudo-quantization node are adjusted to keep the parameters of the teacher network model unchanged. Wherein, the preset loss function includes a first loss function and a second loss function.
[0106] The first loss function is determined according to the output results of the network output layer of the student network model and the network output layer of the teacher network model for the same training data. The network output layer refers to the last layer of the model network layer, such as a fully connected layer. For example, in the above example, the first loss function can be determined according to the N-dimensional features output by the fully connected layer of the student network model and the N-dimensional features output by the fully connected layer of the teacher network model. The output of the fully connected layer is also called Logits, and the first loss function is a loss function based on Logits, which can be used to measure the degree of difference between the probability distributions output by the student network model and the teacher network model. The role of the first loss function is to make the output result (Logits) of the network output layer of the student network model as close as possible to the output result of the network output layer of the teacher network model. Compared with the prediction results and positive and negative labels, Logits contains richer information, which can achieve more accurate reverse parameter adjustment and further improve the model accuracy.
[0107] The second loss function is determined according to the prediction result of the student network model for the training data and the true label of the training data. For example, the second loss function is used to calculate the cross entropy loss determined according to the prediction result of the student network model for the training data and the true label of the training data. The second loss function is used to make the prediction result of the student network model as close to the true value as possible.
[0108] It should be noted that the embodiment of the present invention does not limit the loss function used to calculate the first loss and the second loss. For example, any loss function such as cross entropy loss, KL divergence loss, L2loss, MGDloss, FGDloss, etc. can be used.
[0109] The preset loss function adopts a combination of the above two loss functions, that is, the loss values calculated by the first loss function and the second loss function are added (or weighted addition) to obtain a final loss value, and the network parameters of the student network model and the quantization parameters of each pseudo-quantization node are reversely adjusted using the final loss value. The student network model is iteratively trained by continuously inputting the same training data to the student network model and the teacher network model until the student network model converges, that is, until the accuracy of the student network model meets the preset accuracy and no longer increases, the iterative training is stopped, the quantization parameters of the student network model are saved, and the target network model is obtained.
[0110] In an optional embodiment of the present invention, the method may further include: removing pseudo-quantization nodes in the target network model, and using the target network model to perform the target task.
[0111] After the target network model is obtained through training, the cross-layer connection module can be retained and the pseudo-quantization nodes can be removed, and the target network model can be deployed on the target device, and the target network model can be used to perform the target task. The input of the target network model is fixed-point data, and reasoning is performed based on fixed-point parameters. The target network model can be deployed on hardware devices with limited resources, and correct prediction results can be obtained through rapid reasoning at low power consumption. For example, in image recognition application scenarios, the target network model for image recognition can be deployed in an embedded camera, so that images captured in real time can be recognized and the classification of the recognized objects can be output.
[0112] The target devices include but are not limited to: home smart terminals (including air conditioners, refrigerators, rice cookers, water heaters, etc.), business smart terminals (including videophones, conference desktop smart terminals, etc.), wearable devices (including smart watches, smart glasses, etc.), financial smart terminals, as well as smart phones, tablet computers, personal digital assistants (PDAs), vehicle-mounted devices, computers, embedded devices, etc.
[0113] In summary, the model compression method provided by the embodiment of the present invention, after quantizing the first network model to be compressed to obtain the second network model, determines the target network layer in the second network model, and adds a corresponding cross-layer connection module to each target network layer to obtain a third network model. The quantization loss of the target network layer meets the preset conditions, that is, the quantization loss of the target network layer exceeds the acceptable range, resulting in the accuracy of the entire model failing to meet the preset accuracy. The embodiment of the present invention adds a corresponding cross-layer connection module to each target network layer, and the cross-layer connection module can add a small amount of calculation on the basis of the second network model, thereby increasing the expression ability of the model, thereby compensating for the error caused by quantization of the target network layer. Finally, training the third network model can improve the model accuracy and obtain the target network model. The target network model is a lightweight model with the characteristics of high precision and high speed. It can be deployed on hardware devices with limited resources and can quickly infer and obtain correct prediction results under low power consumption.
[0114] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0115] Reference Figure 3 , shows a structural block diagram of an embodiment of a model compression device of the present invention, the device may include:
[0116] The model acquisition module 301 is used to acquire a first network model, where the first network model is a floating-point model trained based on a training set;
[0117] A model quantization module 302, configured to perform model quantization on the first network model to obtain a second network model;
[0118] A target determination module 303 is used to determine a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition;
[0119] A model processing module 304 is used to add a corresponding cross-layer connection module at each target network layer to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer;
[0120] The model training module 305 is used to train the third network model to obtain a target network model.
[0121] Optionally, the target determination module includes:
[0122] An input reasoning submodule, used for inputting the same input data into the first network model and the second network model for reasoning;
[0123] A feature extraction submodule, used to extract output features of each network layer in the first network model and the second network model;
[0124] A feature comparison submodule, used to calculate the similarity between the output features of the corresponding network layers in the first network model and the second network model;
[0125] The target determination submodule is used to determine, in the second network model, a network layer whose similarity is less than a preset value as a target network layer.
[0126] Optionally, the model training module is specifically used to perform knowledge distillation training using the first network model as a teacher network model and the third network model as a student network model to obtain a target network model.
[0127] Optionally, the model training module is specifically used to input the training data in the training set into the teacher network model and the student network model in sequence for inference, adjust the parameters of the student network model according to a preset loss function, and obtain the target network model when the iteration stop condition is met; the preset loss function includes a first loss function and a second loss function, the first loss function is determined according to the output results of the network output layer of the student network model and the network output layer of the teacher network model for the same training data; the second loss function is determined according to the prediction results of the student network model for the training data and the true label of the training data.
[0128] Optionally, the model quantization module includes:
[0129] A node insertion submodule, used for inserting a pseudo-quantization node at a target node of the first network model;
[0130] A calibration set acquisition submodule, used to acquire a calibration set, wherein the calibration set is a subset of the training set;
[0131] An information acquisition submodule, used to sequentially input the calibration data in the calibration set into the first network model into which the pseudo quantization nodes have been inserted, and to acquire quantization information of each pseudo quantization node;
[0132] The initialization submodule is used to initialize the quantization parameter of each pseudo quantization node according to the quantization information of each pseudo quantization node to obtain a second network model.
[0133] Optionally, the cross-layer connection module includes a pseudo quantization node.
[0134] Optionally, the cross-layer connection module includes a convolution layer, a normalization layer and an activation function layer.
[0135] Optionally, the number of channels of the convolutional layer in the cross-layer connection module is smaller than a preset ratio of the number of channels of the convolutional layer in the second network model.
[0136] The model compression device provided by the embodiment of the present invention performs model quantization on the first network model to be compressed to obtain the second network model, determines the target network layer in the second network model, and adds a corresponding cross-layer connection module to each target network layer to obtain a third network model. The quantization loss of the target network layer meets the preset conditions, that is, the quantization loss of the target network layer exceeds the acceptable range, resulting in the accuracy of the entire model failing to meet the preset accuracy. The embodiment of the present invention adds a corresponding cross-layer connection module to each target network layer, and the cross-layer connection module can add a small amount of calculation on the basis of the second network model, thereby increasing the expression ability of the model, thereby compensating for the error caused by quantization of the target network layer. Finally, training the third network model can improve the model accuracy and obtain the target network model. The target network model is a lightweight model with the characteristics of high accuracy and high speed. It can be deployed on hardware devices with limited resources and can quickly infer and obtain correct prediction results under low power consumption.
[0137] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0138] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0139] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0140] The embodiment of the present invention also provides a non-temporary computer-readable storage medium, when the instructions in the storage medium are executed by a processor of a device (server or terminal), the device can execute the above Figure 1The description of the model compression method in the corresponding embodiment will not be repeated here. In addition, the description of the beneficial effects of the same method will not be repeated. For technical details not disclosed in the computer program product or computer program embodiment involved in this application, please refer to the description of the method embodiment of this application.
[0141] Those skilled in the art will readily appreciate other embodiments of the present invention after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses or adaptations of the present invention that follow the general principles of the present invention and include common knowledge or customary techniques in the art not disclosed by the present invention. The description and examples are to be regarded as exemplary only, and the true scope and spirit of the present invention is indicated by the following claims.
[0142] It should be understood that the present invention is not limited to the exact construction that has been described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present invention is limited only by the appended claims.
[0143] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
[0144] The above is a detailed introduction to a model compression method, device and machine-readable storage medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A model compression method, characterized in that: The method comprises: Acquire a first network model, where the first network model is a floating-point model trained based on a training set; Performing model quantization on the first network model to obtain a second network model; Determining a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition; Adding a corresponding cross-layer connection module at each of the target network layers to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer; The third network model is trained to obtain a target network model.
2. The method according to claim 1, characterized in that The determining of the target network layer in the second network model includes: Inputting the same input data into the first network model and the second network model respectively for reasoning; Extracting output features of each network layer in the first network model and the second network model; Calculating the similarity between output features of corresponding network layers in the first network model and the second network model; In the second network model, a network layer whose similarity is less than a preset value is determined as a target network layer.
3. The method according to claim 1, characterized in that The step of training the third network model to obtain a target network model includes: The first network model is used as a teacher network model, and the third network model is used as a student network model for knowledge distillation training to obtain a target network model.
4. The method according to claim 3, characterized in that The method of using the first network model as a teacher network model and the third network model as a student network model for knowledge distillation training to obtain a target network model includes: The training data in the training set are input into the teacher network model and the student network model in turn for inference, and the parameters of the student network model are adjusted according to a preset loss function. When the iteration stop condition is met, the target network model is obtained; the preset loss function includes a first loss function and a second loss function, the first loss function is determined according to the output results of the network output layer of the student network model and the network output layer of the teacher network model for the same training data; the second loss function is determined according to the prediction results of the student network model for the training data and the true labels of the training data.
5. The method according to claim 1, characterized in that The step of performing model quantization on the first network model to obtain a second network model includes: Inserting a pseudo quantization node at a target node of the first network model; Acquire a calibration set, where the calibration set is a subset of the training set; Inputting the calibration data in the calibration set into the first network model into which the pseudo quantization nodes have been inserted in sequence, and obtaining the quantization information of each pseudo quantization node; According to the quantization information of each pseudo quantization node, the quantization parameter of each pseudo quantization node is initialized to obtain a second network model.
6. The method according to claim 1, characterized in that The cross-layer connection module includes a pseudo quantization node.
7. The method according to claim 1, characterized in that The cross-layer connection module includes a convolution layer, a normalization layer and an activation function layer.
8. The method according to claim 1, characterized in that The number of channels of the convolutional layer in the cross-layer connection module is less than a preset ratio of the number of channels of the convolutional layer in the second network model.
9. A model compression device, characterized in that: The device comprises: A model acquisition module, used to acquire a first network model, where the first network model is a floating-point model trained based on a training set; A model quantization module, used to perform model quantization on the first network model to obtain a second network model; A target determination module, used to determine a target network layer in the second network model; the quantization loss of the target network layer meets a preset condition; A model processing module, used for adding a corresponding cross-layer connection module at each of the target network layers to obtain a third network model; the input of the cross-layer connection module is connected to the input of the target network layer, and the output of the cross-layer connection module is connected to the output of the target network layer; The model training module is used to train the third network model to obtain a target network model.
10. A machine-readable storage medium having instructions stored thereon, which, when executed by one or more processors of a device, causes the device to execute the model compression method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Convolutional neural network model compression method combining pruning and knowledge distillation
CN113159173A
Image processing model training method, image processing method and related equipment
CN113705317A
Data processing method and device, equipment and computer readable storage medium
CN113762503A
Cited By
Model compression method and device, deployment method and device, equipment and storage medium
CN121390160A
