Model compression method and device and readable storage medium

By combining model quantification and knowledge distillation technology, the deployment accuracy of deep neural network models on resource-limited hardware devices is improved, the problem of accuracy decline after model compression is solved, and efficient model deployment is achieved.

CN118586449AActive Publication Date: 2024-09-03SHENZHEN MICROBT ELECTRONICS TECH CO LTD

Patent Information

Application Number
CN202310237937.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-03-03
Publication Date
2024-09-03
Estimated Expiration
2043-03-03

AI Technical Summary

Technical Problem

When deep neural network models are deployed on hardware devices with limited resources, due to the huge amount of parameters and calculations, it is difficult to guarantee the accuracy of the model after compression, making it difficult to deploy on hardware such as mobile devices.

Method used

Through the combination of model quantification and knowledge distillation, the student network model is first quantified to obtain a smaller quantized model, and then the knowledge of the teacher network model is used to migrate to the quantized model, and parameters are adjusted to improve accuracy and ensure accuracy loss after compression. Minimum.

Benefits of technology

It achieves high-precision and lightweight deployment of deep neural network models on resource-limited hardware devices, solves the problem of significant drop in accuracy after model compression, and ensures efficient operation of the model during deployment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118586449A
    Figure CN118586449A
Patent Text Reader

Abstract

The embodiment of the invention provides a model compression method and device and a readable storage medium. The method comprises the following steps: acquiring a pre-trained teacher network model and a pre-trained student network model, wherein the teacher network model and the student network model are floating point models and have the same model type; performing model quantification on the student network model to obtain a quantified student network model; respectively inputting training data in a training set into the teacher network model and the quantized student network model in sequence, adjusting parameters of the quantized student network model according to a preset loss function, and obtaining a target network model when an iteration stop condition is satisfied; the preset loss function comprises a first distillation loss function, and the first distillation loss function is determined according to features output by corresponding segments in the quantified student network model and the teacher network model. According to the embodiment of the invention, the precision of the model can be improved while the model is compressed.
Need to check novelty before this filing date? Find Prior Art

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] The 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.

[0006] In a first aspect, an embodiment of the present invention discloses a model compression method, the method comprising:

[0007] Obtain a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type;

[0008] quantizing the student network model to obtain a quantized student network model;

[0009] The training data in the training set are input into the teacher network model and the quantized student network model respectively, and the parameters of the quantized student network model are adjusted according to the preset loss function. When the iteration stop condition is met, the target network model is obtained; the preset loss function includes a first distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

[0010] In a second aspect, an embodiment of the present invention discloses a model compression device, the device comprising:

[0011] A model acquisition module, used to acquire a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type;

[0012] A model quantization module, used for quantizing the student network model to obtain a quantized student network model;

[0013] A distillation training module is used to input the training data in the training set into the teacher network model and the quantized student network model respectively, adjust the parameters of the quantized 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 distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

[0014] 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.

[0015] The embodiments of the present invention include the following advantages:

[0016] The embodiment of the present invention jointly optimizes model quantization and knowledge distillation, first quantizes the student network model to obtain a quantized student network model with a smaller volume, and then uses knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model to improve the accuracy of the quantized student network model and obtain the target network model. The target network model has high accuracy (the accuracy of the teacher network model) and light volume (the volume of the quantized student network model), thereby achieving the minimum accuracy loss of the student network model after quantization under the supervision of the teacher network model, solving the problem of a significant decrease in accuracy after model compression. Further, in the process of using knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model, the embodiment of the present invention determines the first distillation loss function according to the features of the outputs of the corresponding stages in the quantized student network model and the teacher network model, so that the intermediate layer output distribution of the quantized student network model is constrained by the high-precision teacher network model in both space and feature dimensions based on the feature distillation of each stage, which can ensure that the feature expression ability of the quantized student network model is constrained, thereby significantly improving the final quantization accuracy. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] 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.

[0018] Figure 1 is a flow chart of steps of an embodiment of a model compression method of the present invention;

[0019] Figure 2 is a schematic diagram of calculating a final loss value of the present invention;

[0020] Figure 3 It is a structural block diagram of an embodiment of a model compression device of the present invention. DETAILED DESCRIPTION

[0021] 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.

[0022] 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.

[0023] Reference Figure 1 , shows a flow chart of a model compression method embodiment of the present invention, the method may include the following steps:

[0024] Step 101: Obtain a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type;

[0025] Step 102: quantizing the student network model to obtain a quantized student network model;

[0026] Step 103: input the training data in the training set into the teacher network model and the quantized student network model respectively, adjust the parameters of the quantized 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 distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

[0027] The core idea of ​​the model compression method provided by the embodiment of the present invention is to jointly optimize model quantization and knowledge distillation. First, the student network model is quantized to obtain a smaller quantized student network model, and then the knowledge of the teacher network model is transferred to the quantized student network model by using knowledge distillation to improve the accuracy of the quantized student network model and obtain the target network model. The target network model has high accuracy (the accuracy of the teacher network model) and light volume (the volume of the quantized student network model), thereby achieving the minimum accuracy loss of the student network model after quantization under the supervision of the teacher network model, solving the problem of a significant decrease in accuracy after model compression. The target network model is a smaller model obtained by compression, which can be deployed on hardware devices with limited resources (such as mobile devices), can reduce the computing resources and storage resources occupied by the model, and can ensure the accuracy of the model.

[0028] In practical applications, the teacher network model and the student network model that need to perform knowledge distillation can be used to perform the same target task. For example, for input X, the teacher network model can output Y, and after Y is mapped by Softmax (normalized exponential function), the probability value of the corresponding category is output. Similarly, for input X, the student network model can output Y, and after Y is mapped by Softmax, the probability value of the corresponding category is output. The teacher network model can be a large model with high precision, and the student network model can be a small model with low precision obtained by model compression.

[0029] The target network model obtained by performing knowledge distillation on the quantized student network model using the teacher network model in the embodiment of the present invention can also perform the target task.

[0030] The embodiment of the present invention does not limit the application scenarios of the teacher network model and the student network model obtained in step 101. 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.

[0031] In an embodiment of the present invention, the teacher network model and the student network model are pre-trained neural network models, and the pre-training of the teacher network model and the student network model uses the same training set. 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 teacher network model and the student 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 teacher network model and the student network model trained with the training set can perform the target task of speech recognition.

[0032] The teacher network model is trained using the collected training set until the model converges, and a pre-trained teacher network model is obtained. Similarly, the student network model is trained using the same training set until the model converges, and a pre-trained student network model is obtained. The teacher network model obtained by pre-training is a large model with a complex structure, and the student network model obtained by pre-training is a small model with a relatively simple structure. The structural complexity of the teacher network model is higher than that of the student network model, and it has higher accuracy.

[0033] Furthermore, before using the training set to pre-train the teacher network model and the student network model, the training data in the training set can be enhanced, and the enhanced training set can be used for pre-training. The data enhancement methods include but are not limited to basic data enhancement methods such as scaling, random perturbation of color, introduction of random noise, and advanced data enhancement methods.

[0034] The teacher network model and the student network model obtained in step 101 are floating point models and have the same model type. Among them, the floating point model refers to the weights and activations of each layer in the model as floating point numbers. It should be noted that the model types of the teacher network model and the student network model obtained are the same, but the model depths may be different. Exemplarily, the network structure of the teacher network model is resnet34, and the network structure of the student network model may be resnet18; for another example, the network structure of the teacher network model is mobilenetv2-1.0, and the network structure of the student network model may be mobilenetv2-0.5.

[0035] 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, etc., it can improve the reasoning speed. The embodiment of the present invention performs model quantization on the student network model to obtain a quantized student network model, which can achieve model compression and obtain a lightweight neural network model.

[0036] 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 student network model to obtain a quantized student network model may include:

[0037] Step S11, inserting a pseudo-quantization node at the target node of the student network model;

[0038] Step S12, sequentially inputting the calibration data in the calibration set into the student network model into which the pseudo-quantization nodes have been inserted, and obtaining the quantization information of each pseudo-quantization node; the quantization information includes the maximum floating-point value and the minimum floating-point value flowing through each pseudo-quantization node, and the maximum fixed-point value and the minimum fixed-point value corresponding to each preset pseudo-quantization node; the calibration set is a subset of the training set;

[0039] Step S13: Initialize the quantization parameter of each pseudo quantization node according to the quantization information of each pseudo quantization node to obtain a quantized student network model.

[0040] Among them, 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 data to map it to low-precision data, and then dequantize the low-precision data to obtain the output high-precision 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 floating-point number with continuous values, while the output data is a discretized floating-point number. Among them, the floating-point number with continuous values ​​refers to any floating-point number within a preset continuous range. For example, if the preset continuous range is 0 to 1, the floating-point number with continuous values ​​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.

[0041] The embodiment of the present invention inserts a pseudo-quantization node at the target node of the student network model, and the target node refers to the network layer node in the student network model that supports quantization operation. For example, the target node includes but is not limited to nodes such as weights, activations, and input and output 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.

[0042] The embodiment of the present invention inserts a pseudo-quantization node at the target node of the student network model, inputs the calibration data in the calibration set into the student network model with the pseudo-quantization node inserted in sequence, and counts the quantization information of each pseudo-quantization node. According to the quantization information of each pseudo-quantization node, the parameters of each pseudo-quantization node are initialized to obtain a quantized student network model.

[0043] The calibration set is a subset of the training set. For example, a portion of data can be extracted from the training set as a calibration set. The quantization information includes the maximum floating-point value and the minimum floating-point value flowing through each pseudo-quantization node, and the maximum fixed-point value and the minimum fixed-point value corresponding to each preset pseudo-quantization node. 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 during training, and can also be used to simulate the precision loss caused by quantization, and adjust the model parameters in reverse according to the precision loss to continuously reduce the precision loss caused by quantization, thereby achieving the purpose of improving the precision of the quantized student network model. It should be noted that the embodiment of the present invention does not limit the position and number of inserted pseudo-quantization nodes.

[0044] 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).

[0045] 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.

[0046] The embodiment of the present invention can calculate the quantization output of a pseudo quantization node by the following formula (1).

[0047]

[0048] In the above formula (1), f is the floating point input of the pseudo quantization node, is the quantized output of f after passing through the pseudo quantization node, q max is the maximum fixed-point value corresponding to the quantized output of the pseudo-quantization node, q min The minimum fixed-point value corresponding to the quantization output of the pseudo quantization node. It should be noted that the maximum fixed-point value and the minimum fixed-point value corresponding to the quantization output of each pseudo quantization node are preset. The maximum fixed-point value and the minimum fixed-point value corresponding to the quantization output of each pseudo quantization node can be set according to actual needs, and the embodiment of the present invention does not limit this.

[0049] After obtaining the maximum floating-point value and the minimum floating-point value corresponding to the floating-point input of each pseudo-quantization node, and obtaining the maximum fixed-point value and the minimum fixed-point value corresponding to the quantization output of each pseudo-quantization node, the quantization parameters s and z of each pseudo-quantization node can be calculated according to the following equations (2) and (3).

[0050]

[0051]

[0052] For a pseudo-quantization node, s is the scaling factor (scale) and z is the zero point (zero_point). max is the maximum fixed-point value corresponding to the quantized output of the pseudo-quantization node, q min The minimum fixed-point value corresponding to the quantized output of the pseudo-quantization node. max and q min The value of is preset. For example, for 8-bit quantization, the preset range of the signed number is -127 to 127. max Should be 127, q min should be 127; the default range of unsigned numbers is 0 to 255, so q max Should be 0, q min Should be 255.

[0053] In an example, assuming that a convolutional layer in the student network model is quantized, a pseudo-quantization node can be inserted at each of the weight w, input x, and output y nodes of the convolutional layer, that is, three pseudo-quantization nodes are inserted. Assume that the pseudo-quantization node of weight w is denoted as q1, the pseudo-quantization node of input x is denoted as q2, and the pseudo-quantization node of output y is denoted as q3. The pseudo-quantization node q1 corresponds to f max and f min The maximum and minimum values ​​of the floating-point parameters of this convolution layer. The f corresponding to the pseudo-quantization node q2 and pseudo-quantization node q3 max and f min It needs to be obtained through the calibration data in the calibration set. For example, the calibration set includes 100 calibration data, which are 100 pictures. These 100 pictures are input into the student network model that has been inserted into the above three pseudo-quantization nodes. By counting the maximum and minimum floating-point values ​​of the input x after inputting these 100 pictures, the f corresponding to the pseudo-quantization node q2 can be obtained. max and f min By counting the maximum and minimum floating-point values ​​of the output y after inputting these 100 images, we can get the f corresponding to the pseudo-quantization node q3 max and f min It should be noted that each pseudo-quantization node corresponds to f max and f min is the maximum and minimum value obtained by counting all calibration data in the calibration set, that is, f max and f min It is globally counted through calibration data.

[0054] It should be noted that the embodiments of the present invention do not limit the specific method used for model quantization.

[0055] In post-training quantization, the calibration set is used to infer the student network model with pseudo-quantization nodes inserted. During the inference process, the maximum and minimum floating-point values ​​flowing through each pseudo-quantization node are recorded, and then scale and zero_point are calculated. In the quantization-aware training method, the maximum and minimum floating-point values ​​flowing through each pseudo-quantization node are recorded during the training process, and then scale and zero_point are calculated. The main difference between quantization-aware training and post-training quantization is that quantization-aware training simulates quantization operations on activations and weights. The advantage of this is that the actual operation process of quantization can be simulated, and the error generated in the quantization process can also be provided as a feature to the network learning. Therefore, quantization-aware training will obtain better accuracy than post-training quantization.

[0056] Preferably, an embodiment of the present invention can adopt a quantization-aware training method. Specifically, pseudo-quantization nodes are inserted into the student network model, and the quantization parameters corresponding to each pseudo-quantization node are counted. Based on the quantization parameters, quantization-aware training is performed on the student network model with the pseudo-quantization nodes inserted to obtain a quantized student network model.

[0057] After the student network model is quantized, the size of the space occupied by the parameters can be reduced, and the hardware memory occupied during runtime can be reduced.

[0058] In an optional embodiment of the present invention, the network layer of the quantized student network model may include weights of different bit widths.

[0059] 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 quantized student network model obtained after the student network model is quantized can include weights of different bit widths. Exemplarily, a quantized student 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 quantized student network model is mixed precision.

[0060] Specifically, for a student network model to be quantized, the target bit width of each network layer of the student network model to be quantized can be determined first, and the target bit width can be determined according to the quantization sensitivity of each network layer; after determining the target bit width of each network layer to be quantized, the student network model is quantized according to the target bit width of each network layer to be quantized. Furthermore, for network layers with high quantization sensitivity, high bit width can be allocated; for network layers with low quantization sensitivity, low bit width can be allocated.

[0061] Among them, the quantization sensitivity of a network layer can be determined by the single-layer quantization loss of the network layer, and the single-layer quantization loss refers to the precision loss generated in the quantization process of quantizing only the parameters (such as weights and / or activations) of this layer of the network layer. The larger the single-layer quantization loss, the higher the quantization sensitivity of the network layer, and conversely, the smaller the single-layer quantization loss, the lower the quantization sensitivity of the network layer.

[0062] Furthermore, a sensitivity threshold may be set, and a target bit width to be quantized for each network layer may be determined according to the quantization sensitivity of each network layer and the sensitivity threshold.

[0063] For example, for a network layer greater than or equal to the sensitivity threshold, a first target bit width may be set; and for a network layer less than the sensitivity threshold, a second target bit width may be set.

[0064] Exemplarily, for a student network model to be quantized, a mixed precision quantization method is used for the student 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).

[0065] After the student network model is quantized, it will cause a loss in accuracy. In order to reduce the accuracy loss caused by quantization, the embodiment of the present invention uses knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model.

[0066] Specifically, the training data in the training set are respectively input into the teacher network model and the quantized student network model in turn, and the parameters of the quantized student network model are adjusted according to a preset loss function, and the target network model is obtained when the iteration stop condition is met; the preset loss function includes a first distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

[0067] It should be noted that the training data input into the teacher network model and the quantized student network model may be data-enhanced training data, and the data enhancement method is consistent with the data enhancement method used in pre-training.

[0068] Furthermore, the iteration stopping condition may include: for the same training data, the difference between the output results of the quantized student network model and the teacher network model is less than a preset threshold.

[0069] Taking the image classification scenario as an example, the output result of the quantized student network model is the classification result output for a certain input image, and the output result of the teacher network model is the classification result output for the same input image.

[0070] In the training process of the quantized student network model, the teacher network model is used to guide the quantized student network model. Specifically, in the training process of the quantized student network model, the output result of the quantized student network model is no longer fitted to the label of the training set, but to the output result of the teacher network model when the same input data is input. The above process is called knowledge distillation. The purpose of knowledge distillation is to improve the fitting ability of the small model (quantized student network model), that is, the learning ability. Since the number of parameters of the small model is relatively small, the learning ability is relatively weak, and it is difficult to converge to the same degree as the large model through direct training, that is, the error between the output result of the small model and the actual value is difficult to drop to the same degree as the large model (teacher network model). Therefore, in the training process, the output result of the large model when the same input data is input is used as the label of the input data, and the output result of the small model is used to fit the output of the large model, and converge to a degree close to the large model, which can improve the learning ability of the small model.

[0071] In the process of performing knowledge distillation on the quantized student network model using the teacher network model, the embodiment of the present invention adjusts the parameters of the quantized student network model according to a preset loss function, specifically, adjusts the network parameters of the quantized student network model and the quantization parameters of the pseudo-quantized nodes, and keeps the parameters of the teacher network model unchanged. The preset loss function includes a first distillation loss function, which is determined according to the features of the corresponding outputs of each segment in the quantized student network model and the teacher network model.

[0072] In the embodiment of the present invention, the student network model and the teacher network model have the same network structure. The student network model includes N stages, and the teacher network model also includes N stages. The stage in the embodiment of the present invention refers to the part from the input of the model to the output of the classification layer. A stage may include one or more network layers. The embodiment of the present invention may divide the stage according to the number of downsampling, and each stage may perform downsampling once.

[0073] After defining the preset loss function, the teacher network model is used to perform knowledge distillation training on the quantized student network model according to the preset loss function until the iteration stop condition is met to obtain the target network model. During the knowledge distillation training process, the parameters of the teacher network model remain fixed, and only the network parameters and quantization parameters in the quantized student network model are optimized. The teacher network model participates in the reasoning process of the training data during the knowledge distillation training process, and the features output by each stage of the teacher network model participate in the calculation of the first distillation loss function.

[0074] In the embodiment of the present invention, the teacher network model does not update the gradient but only provides knowledge transfer. The feature distillation based on each stage makes the intermediate layer output distribution of the quantized student network model constrained by the high-precision teacher network model in both space and feature dimensions, which can ensure that the feature expression ability of the quantized student network model is constrained, thereby significantly improving the final quantization accuracy.

[0075] In an optional embodiment of the present invention, the first distillation loss function may be determined by the following steps:

[0076] Step S21: for the same training data, extracting features of each output segment from the N segments of the teacher network model and the quantized student network model respectively;

[0077] Step S22, based on the features output by the i-th segment of the teacher network model and the features output by the i-th segment of the quantized student network model, calculate the feature distillation loss corresponding to the i-th segment, where the value of i ranges from 1 to N;

[0078] Step S23: Determine a first distillation loss function according to the feature distillation losses corresponding to the N segments.

[0079] Specifically, the training data in the training set are input into the teacher network model and the quantized student network model in sequence, respectively, to ensure that the training data input into the two models are consistent each time.

[0080] The student network model and the teacher network model have the same network structure. The student network model includes N stages, and the teacher network model also includes N stages. A segment may include at least one network layer. For the same training data, the features of the last layer output of each segment are extracted from the N segments of the teacher network model and the quantized student network model. For the teacher network model, the features of the N segments can be extracted; similarly, for the quantized student network model, the features of the N segments can be extracted.

[0081] Based on the features output by the i-th segment of the teacher network model and the features output by the i-th segment of the quantized student network model, the feature distillation loss corresponding to the i-th segment is calculated, and the value of i is 1 to N, and N feature distillation losses can be obtained. Based on these N feature distillation losses, a first distillation loss function can be determined. For example, the first distillation loss function can add these N feature distillation losses, or perform weighted addition, etc.

[0082] The purpose of the first distillation loss function is to make the features of each stage output of the quantized student network model as close as possible to the features of the corresponding stage output in the teacher network model, so that the intermediate layer output distribution of the quantized student network model is constrained by the high-precision teacher network model in both spatial and feature dimensions, further improving the model accuracy.

[0083] In an optional embodiment of the present invention, the feature distillation loss corresponding to the i-th segment is calculated based on the features output by the i-th segment of the teacher network model and the features output by the i-th segment of the quantized student network model, and may include: for the same training data, obtaining the spatial pooling results and channel-dimensional pooling results of the features output by the i-th segment of the teacher network model after pooling, and obtaining the spatial pooling results and channel-dimensional pooling results of the features output by the i-th segment of the quantized student network model after pooling; based on the spatial pooling results and channel-dimensional pooling results corresponding to the i-th segment of the teacher network model and the spatial pooling results and channel-dimensional pooling results corresponding to the i-th segment of the student network model, calculating the feature distillation loss corresponding to the i-th segment.

[0084] Specifically, the characteristic distillation loss corresponding to the i-th segment can be calculated by the following formula.

[0085]

[0086] The first distillation loss function is defined as follows:

[0087]

[0088] In the above formula (4), sp represents the spatial pooling result, cp represents the channel-wise pooling result, ||x|| represents the calculation of the bi-norm of x, O i is the feature output by the i-th stage of the teacher network model, is the feature output by the quantized student network model at the i-th stage. In the embodiment of the present invention, the first distillation loss function is also referred to as a feature-based distillation loss function.

[0089] Among them, spatial pooling can retain most of the important information while reducing the feature dimension, and there are several ways of spatial pooling: maximum pooling, average pooling, weighted pooling, etc. In the embodiment of the present invention, maximum pooling is mainly used as an example for description.

[0090] Taking the image classification application scenario as an example, the features output by the last layer of the teacher network model and the quantized student network model at the i-th stage are feature maps. Assume that the size of the feature maps output by the last layer of the i-th stage of the teacher network model and the quantized student network model are both W*H*C, where W is the width, H is the height, and C is the number of channels, that is, C feature maps are output.

[0091] In the embodiment of the present invention, the spatial pooling result sp refers to: for the C feature maps output by the i-th stage, the maximum pooling value is calculated for each feature map in the C feature maps, and C maximum pooling values ​​can be obtained. These C maximum pooling values ​​are combined into a vector, which is sp in the above formula.

[0092] The channel dimension pooling result cp means: for the C feature maps output by the i-th stage, extract the maximum pooling value at each corresponding position in the C feature maps to obtain W*H maximum pooling values, and form these W*H maximum pooling values ​​into a vector, which is cp in the above formula. For example, for C feature maps, the size of each feature map is W*H, select the maximum pooling value at the position of the 1st row and 1st column in the C feature map, select the maximum pooling value at the position of the 1st row and 2nd column in the C feature map, and so on, select the maximum pooling value at the position of the Wth row and the Hth column in the C feature map, and obtain W*H maximum pooling values. Among them, sp is a C-dimensional vector, and cp is a W*H vector.

[0093] In an optional embodiment of the present invention, the preset loss function may also include a second distillation loss function, which is determined according to the output results of the network output layer of the quantized student network model and the network output layer of the teacher network model for the same training data.

[0094] The output results of the network output layer of the quantized student network model and the network output layer of the teacher network model for the same training data can be called Logits in the knowledge distillation process, that is, Logits is the last layer output of the network layer.

[0095] For general classification problems, such as image classification, after inputting an image, it will go through various nonlinear transformations in the DNN network. Before the final Softmax layer of the network, the size value z of each category of the image will be obtained. i , the value z of a category i The larger the value, the more likely the model thinks the input image belongs to this category. After summarizing various information within the network, the summary score z belonging to each category is obtained. i , which is Logits, i represents the i-th category, z iRepresents the possibility of belonging to the i-th category. Because Logits are not probability values, the Softmax function is generally used to transform the Logits values, and the resulting probability value is used as the final classification result probability. Softmax normalizes the Logits values ​​between categories so that the values ​​belonging to each category meet the probability distribution.

[0096] In this embodiment of the present invention, the second distillation loss function is also called a Logits-based distillation loss function.

[0097] In the embodiment of the present invention, the second distillation loss function is defined by the following equations (6) and (7).

[0098]

[0099]

[0100] Among them, p i is the probability of each category output, z i is the logits output for each category, and T is the temperature of knowledge distillation. KL divergence (Kullback–Leibler divergence) is also called KL distance or relative entropy. L KD This is the second distillation loss function.

[0101] The purpose of the second distillation loss function is to make the output results (Logits) of the network output layer of the quantized student network model as close as possible to the output results 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.

[0102] In an optional embodiment of the present invention, the preset loss function may also include a task loss function, which is determined according to the prediction results of the quantized student network model for the training data and the true labels of the training data.

[0103] For example, the task loss function is used to calculate the cross entropy loss determined based on the prediction results of the quantized student network model for the training data and the true labels of the training data.

[0104] In this embodiment of the present invention, the task loss function is defined as follows:

[0105]

[0106] Wherein, y is the prediction result output by the quantized student network model for a certain training data, is the true label of the training data, and m is the number of training data.

[0107] The role of the task loss function is to make the prediction results of the quantized student network model as close to the true value as possible.

[0108] Optionally, the preset loss function can adopt a combination of three loss functions, that is, the loss values ​​calculated by the first distillation loss function, the second distillation loss function, and the task loss function are added (or weighted addition) to obtain the final loss value, and the final loss value is used to reversely adjust the network parameters and quantization parameters of the quantized student network model.

[0109] In an embodiment of the present invention, the preset loss function can be defined as follows:

[0110]

[0111] in, is the first distillation loss function, which is used to calculate the feature distillation loss of each stage and sum the feature distillation losses of each stage; L kd is the second distillation loss function, used to calculate the Logits distillation loss; L ce is the task loss function, which is used to calculate the cross entropy loss.

[0112] Reference Figure 2 , shows a schematic diagram of calculating the final loss value according to an embodiment of the present invention. Figure 2 As shown, T_stage is the stage in the teacher network model, and S_stage is the stage in the quantized student network model.

[0113] In a specific implementation, an embodiment of the present invention first performs model quantization on a floating-point student network model to obtain a quantized student network model, which includes initialized quantization parameters; and then uses a high-precision teacher network model to guide further optimization of the network parameters and quantization parameters of the quantized student network model through knowledge distillation, so as to achieve the purpose of further improving the accuracy.

[0114] In one example, taking an image classification scenario as an example, the model compression method of an embodiment of the present invention may include the following steps:

[0115] Step A1, obtaining a training set, wherein the training set includes training data, wherein the training data includes a plurality of collected images, and each training data is annotated to obtain a true label of each training data. Data enhancement is performed on the training data, including but not limited to basic data enhancement methods such as scaling, cutting out, color enhancement, and adding noise, and may also include advanced data enhancement methods such as mixup and random erasure.

[0116] Step A2: input the training data in the training set into the teacher network model and the quantized student network model in sequence, wherein pseudo quantized nodes have been inserted into the quantized student network model, and the quantized parameters of each pseudo quantized node are initialized.

[0117] Step A3: Define hyperparameters such as optimizer, learning rate, and batch size for distillation training.

[0118] Step A4, extracting the feature graphs output by each stage of the teacher network model, and extracting the feature graphs output by each stage of the quantized student network model, so as to calculate the feature distillation loss corresponding to each stage; calculating the Logits distillation loss according to the output results of the network output layer of the quantized student network model and the network output layer of the teacher network model for the same training data; and calculating the cross entropy loss according to the predicted results of the training data and the true labels of the training data of the quantized student network model, and adding these three losses to obtain the final loss value.

[0119] Step A5: According to the final loss value, the optimizer is used to update the parameters of the quantized student network model until the verification accuracy of the quantized student network model converges, and the target network model is obtained, that is, the optimal quantization parameters are obtained. The optimizer is a tool for updating model parameters, such as SGD (Stochastic Gradient Descent), ADAM (Adaptive Moment Estimation) and other optimizers.

[0120] Step A6: remove the pseudo-quantization nodes in the target network model, deploy the target network model on the target device to perform the target task, and quickly infer and obtain the correct prediction results under low power consumption.

[0121] 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.

[0122] After the target network model is obtained through training, the pseudo-quantized nodes in the target network model can be removed and the target network model can be deployed on the target device to perform the target task. The target network model can be deployed on a hardware device with limited resources.

[0123] 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 equipment, computers, etc.

[0124] In summary, the embodiment of the present invention jointly optimizes model quantization and knowledge distillation, first quantizes the student network model to obtain a smaller quantized student network model, and then uses knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model to improve the accuracy of the quantized student network model and obtain the target network model. The target network model has high accuracy (the accuracy of the teacher network model) and light volume (the volume of the quantized student network model), thereby achieving the minimum accuracy loss of the student network model after quantization under the supervision of the teacher network model, solving the problem of a significant decrease in accuracy after model compression. Further, in the process of using knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model, the embodiment of the present invention determines the first distillation loss function according to the features of the outputs of the corresponding stages in the quantized student network model and the teacher network model, so that the intermediate layer output distribution of the quantized student network model is constrained by the high-precision teacher network model in both space and feature dimensions based on the feature distillation of each stage, which can ensure that the feature expression ability of the quantized student network model is constrained, thereby significantly improving the final quantization accuracy.

[0125] 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.

[0126] Reference Figure 3 , shows a structural block diagram of an embodiment of a model compression device of the present invention, the device may include:

[0127] A model acquisition module 301 is used to acquire a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type;

[0128] A model quantization module 302 is used to quantize the student network model to obtain a quantized student network model;

[0129] The distillation training module 303 is used to input the training data in the training set into the teacher network model and the quantized student network model respectively, adjust the parameters of the quantized student network model according to the preset loss function, and obtain the target network model when the iteration stop condition is met; the preset loss function includes a first distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

[0130] Optionally, the distillation training module includes:

[0131] A feature extraction submodule, for extracting features of each output segment from the N segments of the teacher network model and the quantized student network model respectively for the same training data;

[0132] The loss determination submodule is used to calculate the feature distillation loss corresponding to the i-th segment based on the features output by the i-th segment of the teacher network model and the features output by the i-th segment of the quantized student network model, where i is 1 to N; and determine the first distillation loss function according to the feature distillation losses corresponding to the N segments.

[0133] Optionally, the loss determination submodule includes:

[0134] A pooling result acquisition unit, for obtaining, for the same training data, the spatial pooling result and the channel dimension pooling result of the features output by the i-th segment of the teacher network model after pooling, and obtaining the spatial pooling result and the channel dimension pooling result of the features output by the i-th segment of the quantized student network model after pooling;

[0135] The feature loss calculation unit is used to calculate the feature distillation loss corresponding to the i-th segment based on the spatial pooling results and channel dimension pooling results corresponding to the i-th segment of the teacher network model and the spatial pooling results and channel dimension pooling results corresponding to the i-th segment of the student network model.

[0136] Optionally, the preset loss function also includes a second distillation loss function, which is determined according to the output results of the network output layer of the quantized student network model and the network output layer of the teacher network model for the same training data.

[0137] Optionally, the preset loss function also includes a task loss function, and the task loss function is determined according to the prediction result of the quantized student network model for the training data and the true label of the training data.

[0138] Optionally, the model quantization module includes:

[0139] A pseudo-quantization node insertion submodule, used for inserting a pseudo-quantization node at a target node of the student network model;

[0140] A quantization information statistics submodule is used to sequentially input the calibration data in the calibration set into the student network model into which the pseudo-quantization nodes have been inserted, and obtain the quantization information of each pseudo-quantization node; the quantization information includes the maximum floating-point value and the minimum floating-point value flowing through each pseudo-quantization node, and the maximum fixed-point value and the minimum fixed-point value corresponding to each preset pseudo-quantization node; the calibration set is a subset of the training set;

[0141] The parameter 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 quantized student network model.

[0142] Optionally, the network layers of the quantized student network model include weights of different bit widths.

[0143] Optionally, the iteration stopping condition includes: for the same training data, the difference between the output results of the quantized student network model and the teacher network model is less than a preset threshold.

[0144] The embodiment of the present invention jointly optimizes model quantization and knowledge distillation, first quantizes the student network model to obtain a quantized student network model with a smaller volume, and then uses knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model to improve the accuracy of the quantized student network model and obtain the target network model. The target network model has high accuracy (the accuracy of the teacher network model) and light volume (the volume of the quantized student network model), thereby achieving the minimum accuracy loss of the student network model after quantization under the supervision of the teacher network model, solving the problem of a significant decrease in accuracy after model compression. Further, in the process of using knowledge distillation to transfer the knowledge of the teacher network model to the quantized student network model, the embodiment of the present invention determines the first distillation loss function according to the features of the outputs of the corresponding stages in the quantized student network model and the teacher network model, so that the intermediate layer output distribution of the quantized student network model is constrained by the high-precision teacher network model in both space and feature dimensions based on the feature distillation of each stage, which can ensure that the feature expression ability of the quantized student network model is constrained, thereby significantly improving the final quantization accuracy.

[0145] 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.

[0146] 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.

[0147] 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.

[0148] 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 1 The 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.

[0149] 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.

[0150] 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.

[0151] 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.

[0152] 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: Obtain a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type; quantizing the student network model to obtain a quantized student network model; The training data in the training set are input into the teacher network model and the quantized student network model respectively, and the parameters of the quantized student network model are adjusted according to the preset loss function. When the iteration stop condition is met, the target network model is obtained; the preset loss function includes a first distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher network model.

2. The method according to claim 1, characterized in that: The first distillation loss function is determined by the following steps: For the same training data, extracting features of each output segment from the N segments of the teacher network model and the quantized student network model respectively; Based on the features output by the i-th segment of the teacher network model and the features output by the i-th segment of the quantized student network model, calculate the feature distillation loss corresponding to the i-th segment, where the value of i ranges from 1 to N; According to the feature distillation loss corresponding to the N segments, the first distillation loss function is determined.

3. The method according to claim 2, characterized in that The calculating the feature distillation loss corresponding to the i-th segment based on the feature output by the i-th segment of the teacher network model and the feature output by the i-th segment of the quantized student network model includes: For the same training data, obtain the spatial pooling result and channel dimension pooling result of the features output by the i-th segment of the teacher network model after pooling, and obtain the spatial pooling result and channel dimension pooling result of the features output by the i-th segment of the quantized student network model after pooling; Based on the spatial pooling results and channel-dimensional pooling results corresponding to the i-th segment of the teacher network model and the spatial pooling results and channel-dimensional pooling results corresponding to the i-th segment of the student network model, the feature distillation loss corresponding to the i-th segment is calculated.

4. The method according to claim 1, characterized in that: The preset loss function also includes a second distillation loss function, which is determined according to the output results of the network output layer of the quantized student network model and the network output layer of the teacher network model for the same training data.

5. The method according to claim 1, characterized in that The preset loss function also includes a task loss function, which is determined according to the prediction results of the quantized student network model for the training data and the true labels of the training data.

6. The method according to claim 1, characterized in that The step of quantizing the student network model to obtain a quantized student network model includes: Inserting a pseudo-quantization node at a target node of the student network model; The calibration data in the calibration set are sequentially input into the student network model into which the pseudo-quantization nodes have been inserted, and the quantization information of each pseudo-quantization node is obtained; the quantization information includes the maximum floating point value and the minimum floating point value flowing through each pseudo-quantization node, and the maximum fixed point value and the minimum fixed point value corresponding to each preset pseudo-quantization node; the calibration set is a subset of the training set; According to the quantization information of each pseudo-quantization node, the quantization parameter of each pseudo-quantization node is initialized to obtain a quantized student network model.

7. The method according to claim 1, characterized in that The network layers of the quantized student network model include weights of different bit widths.

8. The method according to claim 1, characterized in that The iteration stopping condition includes: for the same training data, the difference between the output results of the quantized student network model and the teacher network model is less than a preset threshold.

9. A model compression device, characterized in that: The device comprises: A model acquisition module, used to acquire a pre-trained teacher network model and a student network model, wherein the teacher network model and the student network model are floating point models and have the same model type; A model quantization module, used for quantizing the student network model to obtain a quantized student network model; A distillation training module is used to input the training data in the training set into the teacher network model and the quantized student network model respectively, adjust the parameters of the quantized 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 distillation loss function, which is determined according to the characteristics of the corresponding output segments in the quantized student network model and the teacher 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

Cited By

  • Construction method, training method and use method of segmentation model

    CN122066959A

  • A method for constructing, training and using a segmentation model

    CN122066959B