This application is based on and claims priority under 35 U.S.C. § 119 from Chinese Patent Application No. 201910228917.X, filed on Mar. 25, 2019, in the Chinese Intellectual Property Office, the disclosure of which is incorporated by reference herein in its entirety.
Example embodiments of the present disclosure relate to an electronic device, an electronic device controlling method, and a computer program product including instructions for performing the electronic device controlling method.
In the field of artificial intelligence, neural network technologies are widely used, and performance thereof is greatly improved as compared with conventional algorithms. With the popularity of portable devices such as mobile phones, there is an increasing demand for operating neural network models on a device side.
At present, on a device side, the neural network model is mainly applied in two manners: Cloud (which is based on cloud technologies and relies on a communication network) and On Device (which directly uses computing resources of a terminal device and does not need a network). The On Device manner has advantages as compared with the Cloud manner. Firstly, the On Device manner has better real-time performance while the Cloud manner is influenced by the network speed, which may be unable to achieve real-time effects. Secondly, under the current situation that people attach importance to privacy protection, the Cloud manner needs to upload data to a cloud, which will bring risks of leakage of private user data. However, since all calculations in the On Device manner are performed on the device side, the On Device manner has advantages in privacy protection. Thirdly, the On Device manner may provide better autonomy, as all operations are completed on the device side to make decisions without relying on operation such as external communication. Finally, the Cloud manner needs to respond to the increase of the number of devices and services, and will increase more costs by the needs to introduce better scheduling algorithms and more devices.
It is not difficult to foresee that the On Device manner will become an important development direction, but at present, the neural network model often occupies a large amount of hard disk space and memory of the terminal device, and the operation speed is slow, which cannot be simply deployed on a device side.
Example embodiments provide an electronic device, an electronic device controlling method, and a computer program product including instructions for performing the electronic device controlling method.
Additional aspects will be set forth in part in the description which follows and, in part, will be apparent from the description, or may be learned by practice of the presented example embodiments of the disclosure.
According to an aspect of an example embodiment of the disclosure, there is provided a method for compressing a machine learning model by an electronic device, comprising determining a compression parameter of a set hidden layer in a model based on a pruning number of respective channels included in the set hidden layer and a pruning loss of each hidden layer of the model; and compressing the model based on the compression parameter of the set hidden layer, wherein the compression parameter is related to a pruning of the model.
The compression parameter comprises a pruning number of each hidden layer, and the determining a compression parameter of the set hidden layer in the model, comprises determining a relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss of each hidden layer in the set hidden layer; determining the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels and corresponding pruning loss of each hidden layer.
The determining a relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss of each hidden layer, comprises determining a relationship between the pruning number of respective channels of a current hidden layer and the corresponding pruning loss of the current hidden layer, based on training data of at least one next hidden layer to the current hidden layer, wherein the training data comprises a relationship between an output channel of the at least one next hidden layer and each input channel of the at least one next hidden layer.
The determining the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss of each hidden layer, comprises determining the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss of each hidden layer, and a weight corresponding to each hidden layer.
The determining the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each set hidden layer and the corresponding pruning loss of each hidden layer, and a weight corresponding to each hidden layer, comprises: determining a relationship between the pruning number of respective channels of each hidden layer and corresponding weighted pruning loss of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss of each hidden layer, and a weight corresponding to each hidden layer; determining the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss of each hidden layer.
The determining a relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss of each hidden layer, comprises calculating the pruning loss of each current candidate channel to be pruned respectively, wherein any current candidate channel to be pruned comprises pruned channels determined by a previous channel pruning number and at least one unpruned channel, or any current candidate channel to be pruned comprises remaining pruned channels after removing at least one channel from pruned channels determined by a previous channel pruning number; determining a current candidate channel to be pruned with the minimum pruning loss as the pruned channel corresponding to the current channel determined by the previous channel pruning number, to obtain the relationship between the current channel pruning number and the corresponding pruning loss of each hidden layer.
The compression parameter comprises a quantization rate, and the determining the compression parameter of the set hidden layer in the model, comprises determining a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding quantization loss of each hidden layer; and determining a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer.
The determining a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, comprises determining the relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, based on training data of a current hidden layer; and wherein, the training data of the current hidden layer comprises a relationship between an output channel of the current hidden layer and each input channel of the current hidden layer.
The determining a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, comprises determining a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, and a weight corresponding to each hidden layer.
The determining a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, and a weight corresponding to each hidden layer, comprises determining a relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss, based on the relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss, and a weight corresponding to each hidden layer; and determining a quantization rate of each hidden layer, based on the relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss of each hidden layer.
If a current hidden layer corresponds one-to-one to a next hidden layer, the weight of the current hidden layer is the same as the weight of the next hidden layer; if the current hidden layer corresponds to at least two next hidden layers by a multi-out structure, the weight of the current hidden layer is the sum of the weights of at least two next hidden layers; and if at least two current hidden layers correspond to one next hidden layer by a multi-in structure, the weight of each current hidden layer is the weight of the next hidden layer allocated according to a channel proportion of each current hidden layer.
The determining a compression parameter of the set hidden layer in the model, comprises determining the compression parameter of the set hidden layer, based on a loss relationship and an overall compression target parameter of the model; wherein, if the compression parameter is a pruning number, the loss relationship comprises a relationship between the pruning number of respective channels of each hidden layer in the set hidden layer in the model and the corresponding weighted pruning loss of each hidden layer; if the compression parameter is a quantization rate, the loss relationship comprises a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding weighted quantization loss of each hidden layer; and if the compression parameter comprises a pruning number and a quantization rate, then the loss relationship comprises a relationship between the pruning number of respective channels of each hidden layer in the set hidden layer and the corresponding weighted pruning loss, and a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding weighted quantization loss of each hidden layer.
The overall compression target parameter comprises at least one of an overall compression rate of the model or an overall loss of the model.
The determining the compression parameter of the set hidden layer, based on a loss relationship and an overall compression target parameter of the model, comprises any one of the following: calculating a compression parameter of each set hidden layer that minimizes the overall loss of the model based on the loss relationship and the overall compression rate of the model; and calculating a compression parameter of each set hidden layer that maximizes the overall compression rate of the model based on the loss relationship and the overall loss of the model.
The method may further comprise selecting at least one of the following models as a learning model: a model before compression; a model obtained after compressing at least one hidden layer; a model after historical fine-tuning; and fine-tuning the compressed model based on the learning model to obtain an optimized model.
The fine-tuning the compressed model based on the learning model to obtain the optimized model, comprises, based on determining that the fine-tuned model does not satisfy a preset condition, repeatedly performing the step of determining the compression parameter of the set hidden layer in the model to be optimized, compressing the model to be optimized based on the compression parameter of the set hidden layer, and the step of fine-tuning the compressed model based on the learning model, until the fine-tuned model satisfies the preset condition, to obtain the optimized model.
After obtaining the optimized model, the method may further comprises splitting channels of at least one hidden layer of the optimized model into at least two groups of sub-channels respectively, and determining a network parameter of each group of sub-channels, wherein each group of sub-channels comprises corresponding input channels and output channels after grouping; adding a combination layer to obtain a group compressed model, wherein the input of the combination layer is connected to the output channel of each group of sub-channels.
In accordance with another aspect of the disclosure, provided is an electronic device, comprising at least one processor; and a memory storing at least one instruction, wherein the at least one processor configured to, by executing the at least one instruction, determine a compression parameter of a set hidden layer in a model based on a pruning number of respective channels included in the set hidden layer and a pruning loss of each hidden layer of the model; compress the model based on the compression parameter of the set hidden layer, wherein the compression parameter is related to a pruning of the model.
In accordance with another aspect of the disclosure, provided is a computer readable storage medium comprising a non-transitory computer-readable storage medium storing computer program codes for performing the method for compressing a machine learning model.
In accordance with an aspect of the disclosure, provided is a method for compressing a machine learning model by an electronic device, the method comprising: determining a pruning number for each of a plurality of channels included in a hidden layer of the machine learning model; determining a pruning loss of the hidden layer based on the determined pruning numbers of the hidden layer; and determining a compression parameter of the hidden layer based on the pruning loss of hidden layer of the machine learning model; and compressing the machine learning model based on the determined compression parameter of the hidden layer. According to an aspect of an example embodiment of the disclosure, the compression parameter is related to a pruning of the machine learning model.
According to an aspect of an example embodiment of the disclosure, the machine learning model comprises a plurality of hidden layers, including the hidden layer, the compression parameter comprises a pruning number of each of the hidden layers, and the determining the compression parameter of the hidden layer in the machine learning model, comprises: determining a relationship between the pruning number of respective channels of each of the hidden layers and the corresponding pruning loss of each of the hidden layers; determining the pruning number of each of the hidden layers, based on the relationship between the pruning number of the respective channels and the corresponding pruning loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the determining the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding pruning loss of each of the hidden layers, comprises: determining a relationship between the pruning number of the respective channels of a current hidden layer and the corresponding pruning loss of the current hidden layer, based on training data of at least one next hidden layer next to the current hidden layer; wherein the training data comprises a relationship between an output channel of the at least one next hidden layer and each input channel of the at least one next hidden layer.
According to an aspect of an example embodiment of the disclosure, the determining the pruning number of each of the hidden layers, based on the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding pruning loss of each of the hidden layers, comprises: determining the pruning number of each of the hidden layers, based on the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding pruning loss of each of the hidden layers, and a respective weight of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the determining the pruning number of each of the hidden layers, based on the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding pruning losses of each of the hidden layers, and the respective weight of each of the hidden layers, comprises: determining a relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding weighted pruning loss of each of the hidden layers, based on the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding pruning loss of each of the hidden layers, and the weight of each of the hidden layers; and determining the pruning number of each hidden layer, based on the relationship between the pruning number of the respective channels of each of the hidden layers and the corresponding weighted pruning loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the determining the relationship between the pruning number of the respective channels of each hidden layer and the corresponding pruning loss of each of the hidden layers, comprises: calculating, using an incremental manner or a decreasing manner, the pruning loss of each current candidate channel to be pruned respectively, wherein the incremental manner includes any current candidate channel to be pruned comprising a pruning number that includes the pruning numbers for each of the pruned channels determined by a previous channel pruning number and at least one unpruned channel, and the decreasing manner includes any current candidate channel to be pruned comprising a pruning number that corresponds to remaining pruned channels after removing at least one channel from pruned channels determined by the previous channel pruning number; and determining a current candidate channel to be pruned with a minimum pruning loss as the pruned channel corresponding to the current channel determined by the previous channel pruning number, to obtain the relationship between the current channel pruning number and the corresponding pruning loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the compression parameter comprises a quantization rate, and the determining the compression parameter of the hidden layer in the machine learning model, comprises: determining a relationship between respective candidate quantization rates of each of the hidden layers in the machine learning model and corresponding quantization loss of each of the hidden layers; and determining a quantization rate of each of the hidden layers based on a relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the determining the relationship between respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss of each of the hidden layers, comprises: determining the relationship between respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss of each of the hidden layers, based on training data of a current hidden layer; and wherein the training data of the current hidden layer comprises a relationship between an output channel of the current hidden layer and each input channel of the current hidden layer.
According to an aspect of an example embodiment of the disclosure, the determining the quantization rate of each of the hidden layers based on the relationship between the respective candidate quantization rates of each of the hidden layer and the corresponding quantization loss of each of the hidden layers, comprises: determining the quantization rate of each of the hidden layers based on the relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss of each of the hidden layers, and the weight of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, wherein the determining the quantization rate of each of the hidden layers based on the relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss of each of the hidden layers, and the weight of each of the hidden layers, comprises: determining a relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding weighted quantization loss, based on the relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding quantization loss, and the weight of each of the hidden layers; and determining the quantization rate of each of the hidden layers, based on the relationship between the respective candidate quantization rates of each of the hidden layers and the corresponding weighted quantization loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, based on a current hidden layer corresponding one-to-one to a next hidden layer, the weight of the current hidden layer is the same as the weight of the next hidden layer; based on the current hidden layer corresponding to at least two next hidden layers by a multi-out structure, the weight of the current hidden layer is the sum of the weights of the at least two next hidden layers; and based on at least two current hidden layers corresponding to one next hidden layer by a multi-in structure, the weight of each current hidden layer is the weight of the next hidden layer allocated according to a channel proportion of each current hidden layer.
According to an aspect of an example embodiment of the disclosure, the determining the compression parameter of the hidden layer in the machine learning model, comprises: determining the compression parameter of the hidden layer, based on a loss relationship and an overall compression target parameter of the machine learning model; wherein, based on the compression parameter being a pruning number, the loss relationship comprises a relationship between the pruning number of the respective channels of each hidden layer in the hidden layer in the machine learning model and the corresponding weighted pruning loss of each of a plurality of hidden layers; based on the compression parameter being a quantization rate, the loss relationship comprises a relationship between respective candidate quantization rates of each of the hidden layers in the machine learning model and a corresponding weighted quantization loss of each of the hidden layers; and based on the compression parameter comprising a pruning number and a quantization rate, then the loss relationship comprises the relationship between the pruning number of respective channels of each of the hidden layers in the machine learning model and the corresponding weighted pruning loss, and the relationship between respective candidate quantization rates of each of the hidden layers in the machine learning model and the corresponding weighted quantization loss of each of the hidden layers.
According to an aspect of an example embodiment of the disclosure, the overall compression target parameter comprises at least one of an overall compression rate of the machine learning model or an overall loss of the machine learning model.
According to an aspect of an example embodiment of the disclosure, the determining the compression parameter of the hidden layer, based on the loss relationship and the overall compression target parameter of the machine learning model, comprises any one of the following: calculating a compression parameter of each of the hidden layers that minimizes an overall loss of the machine learning model based on the loss relationship and the overall compression rate of the machine learning model; and calculating a compression parameter of each of the hidden layers that maximizes the overall compression rate of the machine learning model based on the loss relationship and the overall loss of the machine learning model.
According to an aspect of an example embodiment of the disclosure, further comprising: selecting at least one of the following machine learning models as the machine learning model: a machine learning model before compression; a machine learning model obtained after compressing at least one hidden layer; a machine learning model after historical fine-tuning; and fine-tuning the compressed model based on the selected machine learning model to obtain an optimized model.
According to an aspect of an example embodiment of the disclosure, the fine-tuning the compressed model based on the selected machine learning model to obtain the optimized model, comprises: based on determining that the fine-tuned model does not satisfy a preset condition, repeatedly performing the operation of determining the compression parameter of the hidden layer in the machine learning model to be optimized, the operation of compressing the machine learning model to be optimized based on the compression parameter of the hidden layer, and the operation of fine-tuning the compressed model based on the learning model, until the fine-tuned model satisfies the preset condition, to obtain the optimized model.
According to an aspect of an example embodiment of the disclosure, after obtaining the optimized model, the method further comprises: splitting channels of at least one hidden layer of the optimized model into at least two groups of sub-channels respectively, and determining a network parameter of each group of the at least two groups of sub-channels, wherein each group of the at least two groups of sub-channels comprises corresponding input channels and output channels after grouping; and adding a combination layer to obtain a group compressed model, wherein the input of the combination layer is connected to the output channel of each group of the at least two sub-channels.
According to an aspect of an example embodiment of the disclosure, provided is an electronic device comprising: at least one processor; and a memory storing at least one instruction, wherein the at least one processor is configured to execute the at least one instruction, which causes the processor to: determine a pruning number for each of a plurality of channels included in a hidden layer of the machine learning model; determine a pruning loss of the hidden layer based on the determined pruning numbers of the hidden layer; determine a compression parameter of the hidden layer based on the pruning loss of the hidden layer of the machine learning model; and compress the machine learning model based on the determined compression parameter of the hidden layer, wherein the compression parameter is related to a pruning of the machine learning model.
In accordance with an aspect of the disclosure, provided is a method comprising: determining a relationship between a pruning number of respective channels of each hidden layer and a corresponding pruning loss in a set hidden layer; determining the pruning number of each hidden layer according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss; and optimizing the machine learning model by pruning at least a portion of a layer of the machine learning model based on the determined pruning number of each hidden layer.
The pruning the at least the portion of the layer of the machine learning mode may include subtracting one output feature map of the hidden layer, which is a previous convolutional layer.
The method may further comprise deleting a corresponding part in a kernel matrix of a current layer and a subsequent layer.
The above and other aspects, features, and advantages of certain example embodiments of the disclosure will be more apparent from the following description taken in conjunction with the accompanying drawings, in which:
Example embodiments of the present disclosure will be described in detail hereafter. The example embodiments have been illustrated in the drawings throughout which same or similar reference numerals refer to same or similar elements or elements having same or similar functions. The example embodiments described hereafter with reference to the drawings are illustrative, merely used for explaining the present disclosure and should not be regarded as any limitations thereto.
It should be understood by those skill in the art that singular forms “a”, “an”, “the”, and “said” may be intended to include plural forms as well, unless otherwise stated. It should be further understood that terms “include/including” used in this specification specify the presence of the stated features, integers, steps, operations, elements and/or components, but not exclusive of the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or combinations thereof. It should be understood that when a component is referred to as being “connected to” or “coupled to” another component, it may be directly connected or coupled to other elements or provided with intervening elements therebetween. In addition, “connected to” or “coupled to” as used herein may include wireless connection or coupling. As used herein, term “and/or” includes all or any of one or more associated listed items or combinations thereof.
In order to make the purpose, technical solution and advantage of the disclosure more clearly, the embodiments of the disclosure will be further described in detail below with reference to the accompanying drawings.
The embodiment of the disclosure provides a method for compressing a machine learning model, as shown in
In Operation S101, the electronic device may determine a compression parameter of a set hidden layer in the machine learning model to be optimized. According to an embodiment of the disclosure, the machine learning model may be performed by an on device manner. The machine learning model by the on device manner may be embodied by resources of the electronic device, and may process an input of the machine learning model by using the resources of the electronic device. For example, nodes of the machine learning model may be embodied by a register or a storage of the electronic device (e.g., a memory), and parameter values of the machine learning model may be managed by the electronic device. The electronic device may process an input value by the machine learning model substantially without communication with an external device. Alternatively, the electronic device may mostly perform the operation of the machine learning model with only a minimal amount of communication with another device.
The machine learning model may correspond to an artificial intelligence model, and may be a model generated by machine learning based on training data. The model may be defined by nodes, branches between the nodes, at least one layer, functions of the at least one layer, and weight values of the branches. Computer program instructions and data sets for the model may be stored or recorded on the electronic device (e.g., in a memory of the electronic device), and the electronic device may perform the model by executing the computer program instructions based on the data sets.
The machine learning model may be embodied by various model structures. For example, the machine learning model may comprise a deep neural network structure, a convolution neural network, a recurrent neural network, or a combination thereof. Also, the machine learning model may comprise a plurality of nodes, a plurality of layers, or a plurality of channels. The plurality of layers of the machine learning model may comprise an input layer, at least one hidden layer, and an output layer. The at least one hidden layer may process input values from the input layer by a non-linear function, and may output the processed input value to another hidden layer or the output layer.
The compression parameter of a set hidden layer may be a parameter for reducing a size of the set hidden layer. The electronic device may reduce the size of the hidden layer by various methods. For example, the electronic device may reduce the size of the hidden layer by pruning a part of branches in the set hidden layer, or grouping channels in the set hidden layer.
After determining the compression parameter of the set hidden layer, the electronic device may compress the model to be optimized based on the compression parameter of the set hidden layer, to obtain an optimized model in Operation S102. The electronic device may compress the model to optimize the model. The updating of the model may comprise compressing the model. The compressing of the model may comprise operations of updating the compression parameter of the set hidden layer or pruning branches of the model.
The technical solution provided by the embodiments of the disclosure, by compressing the model, may reduce the space and memory occupied by the model, reduce the calculation amount of the model, and achieve the purpose of simplifying the model, thereby improving operating speed of the model, which may be able to implement better deployment on the terminal device side.
The model optimization method proposed by the embodiment of the disclosure may be applied to neural network models with various structures, including but not limited to a deep learning model, a deep neural network model, a deep network model, a conventional three-layer neural network model, and the like.
In order to better interpret the working principle of model compression involved in one or more of the example embodiments of the disclosure, related structures in the neural network are introduced and explained herein: in a neural network, the influence on the size and operating speed of the model is concentrated in a network layer such as a convolutional layer or a fully connected layer. Therefore, if the number of parameters and/or the calculation amount of the network layer such as the convolutional layer and the fully connected layer in the model are reduced, the size of the model may be reduced accordingly, and the operation speed of the model may be improved, to achieve the purpose of simplifying the model. The model compression in the embodiment of the disclosure may process a network layer such as the convolutional layer and the fully connected layer in the model, and certainly, may also process other network layers. For convenience of description, in the embodiment of the disclosure, a network layer (for example, a convolutional layer) other than an input layer and an output layer in each network layer in the model to be optimized is referred to as a hidden layer.
In the embodiment of the disclosure, the adopted model compression technology includes pruning, quantization, and the like. That is, in Operation S101, the compression parameters of any hidden layer in the model to be optimized include pruning number, quantization rate, or a combination thereof.
The pruning technology may achieve the purpose of simplifying the model by discarding unimportant parameters in the neural network. For example, the pruning at the channel level may be adopted, which specifically refers to reducing the number of channels corresponding to the hidden layer when pruning a certain hidden layer. In a neural network, based on a certain channel of the hidden layer being removed, the corresponding parameters in subsequent layers associated with the channel may also be removed, thereby reducing the calculation amount and the required number of parameters. The pruning technology prunes a large model into a small model, and the final effect is often better than directly training the same small model. In other words, in the embodiment, a model that is small as a result of pruning may be more accurate than a small model that is directly trained to be a small model.
The quantization technology achieves the purpose of reducing the size of the model by representing parameters with fewer number of bits. Meanwhile, if using a hardware acceleration technology corresponding to the quantized data, the operating speed of the model may be improved.
In the embodiment of the disclosure, the model compression manner using the pruning technology is introduced, and the compression parameter is the pruning number in Operation S101.
The inventors of the present application have found through research that in order to minimize the loss of accuracy in the process of compressing the model, during the pruning, a channel with lower “importance” may be selected for pruning. However, in practical applications, the high participation of human experts is usually required. For example, an experienced expert may determine a pruning rate of each layer (hidden layer) according to experience or experimental results. For example, for one hidden layer, the result of the pruning number being divided by the total number of channels may be the pruning rate of the layer. The experienced expert may observe the pruning rate of each layer, which may be proportional to the pruning number. However, the experienced expert approach will greatly reduce the flexibility of pruning, and will often take more time, but achieve lower efficiency. In addition, artificially setting the pruning rate may result in improperly setting the pruning rate. If the pruning rate is not set properly, it may cause a large loss in the model operation.
Regarding this, an aspect of the example embodiment of the disclosure provides a possible implementation manner which performs a model compression method based on the pruning technology by using a fully automated process. Specifically, Operation S101 may include the following operations:
Operation S1011: determining a relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss in the set hidden layer;
Operation S1012: determining the pruning number of each hidden layer according to the relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss.
In the embodiment of the disclosure, the set hidden layer may be a hidden layer of the model to be optimized, or may be one or more hidden layers set in respective hidden layers. According to an embodiment of the disclosure, and the set hidden layer may preset for which hidden layers are compressed, thereby selecting the above set hidden layer. For example, all of the convolutional layers and/or the fully connected layers in the model may be used as the above set hidden layer, or a part of convolutional layers or fully connected layers may be used as the above set hidden layer.
According to the embodiment of the disclosure, the set hidden layer may be selected according to the overall compression target of the model (which may be manually selected or automatically selected), for example, the set hidden layer may be selected to compress which hidden layers according to at least one of the overall compression rate of the model or the overall loss of the model. Based on the overall compression rate being equal to or greater than a threshold, more hidden layers may be selected to be compressed, or based on the overall compression rate being smaller than the threshold, fewer hidden layers may be selected to be compressed, for example, only one or more hidden layers with a large number of parameters and large calculation amount may be selected to be compressed. The model compression method proposed by the embodiment of the disclosure has a higher flexibility and is suitable for compression processing on various targets, since a set hidden layer may be flexibly selected to be compressed.
Each hidden layer of the model may include an input channel and an output channel. In the embodiment of the disclosure, pruning the model may refer to pruning the output channel of each layer of the model.
In practical applications, when pruning a layer of the model, if some output channels of the layer are pruned, the input channels of adjacent subsequent layers of this layer are correspondingly reduced, thereby influencing the calculation amount of adjacent subsequent layers.
As an example, taking an example that the hidden layer i is a convolutional layer, as shown in
In the calculation process for the convolutional layer, as shown in
In the embodiment of the disclosure, the number of channels of the ith hidden layer (i.e., the hidden layer i) refers to the number c+i of output channels of this hidden layer.
In the convolutional layer, a kernel (k(i)w×k(i)h) may be convoluted with an input feature map for one time, and the required calculation amount is k(i)w×k(i)h×Wi+1×Hi+1 (there are Wi+1×Hi+1 groups of pixels in each input feature map that need to be computed for k(i)w×k(i)h times), and the calculation amount may be but not limited to be represented by Multiply-Accumulate Operation (MAC). Then, an output feature map may need to perform convolution calculation for ci times, so the calculation amount thereof is ci×k(i)w×k(i)h×Wi+1×Hi+1. Then total required calculation amount of ci+1 output feature maps may be Ci=ci+1×ci×k(i)w×k(i)h×Wi+1×Hi+1.
When the output channel of the convolutional layer (that is, the number of output feature maps) is pruned, it not only influences the calculation amount of the current convolutional layer, but also influences the calculation amount of an adjacent subsequent convolutional layer. Specifically, referring to
Therefore, deleting the output channel of the current layer will influence the current layer and the subsequent layer, and accelerate the speed for the both layers. For example, if the number of output channels of each layer decreases to 50% of the previous number, then the calculation amount of almost all layers (except the first convolutional layer) will become ¼ of the previous calculation amount:
In the embodiment of the disclosure, the influence of the current layer pruning on the subsequent layers may be used to determine the importance of each output channel of the current layer.
Specifically, the relationship between the pruning number of respective channels and the corresponding pruning loss of the current hidden layer may be determined according to the training data of the next hidden layer. The training data may include the relationship between the output channel and each input channel.
This is because, in the calculation process of the training data of the next layer, a combination process of different input channels in the next layer will occur, which may indicate the importance of all input channels. In the calculation process of one output channel as shown in
Specifically, as shown in
Wherein, xk represents the value of x corresponding to the kth channel, that is, the value of x corresponding to the position of y in the result map after convoluting the previous kth input channel, and at this time, (xi, x2, . . . xc
In the embodiment of the disclosure, for the data collection of each hidden layer of the model to be optimized, some training samples may be selected to input into the model to be optimized, and forward propagation is performed according to the above processes. During the forward propagation, a certain number of samples may be collected from the feature map of each layer until obtaining enough data for each layer to complete the data sampling process.
Considering that the network structure is constantly changing and developing nowadays, the network is more and more complex, and there are a large number of multi-branch networks. In order to improve the adaptability of various networks such as single-branch and multi-branch networks, the embodiments of the disclosure provide a possible implementation manner, which may accurately allocate training data to respective hidden layers of respective networks such as single-branch and multi-branch networks.
Firstly, respective branch structures are introduced: in early neural network models, for example, a VGG-16 network, each hidden layer has only one pre-order layer and one subsequent layer. The overall network structure is a “straight line” from beginning to end, and such network is referred to as a “single-branch network”. Subsequently, a new structure was proposed. In a network similar to GoogLeNet, some layers have more than one pre-order layer, and some layers have more than one subsequent layer. Such network is referred to as a “multi-branch network”.
Wherein, a conventional multi-branch structure includes the following three types, as shown in
In the embodiment of the disclosure, the process of allocating training data may be different depending on whether the network has multiple branches. Specifically:
For a single-branch network, since the output channel of the previous layer is the input channel of the next layer, it is only necessary to allocate the training data collected by this layer to the corresponding previous layer.
For a multi-branch network, it is necessary to allocate the collected data needs to respective layers in a targeted manner, as shown in
For the Multi-out structure, the pre-order layer has multiple training data since there are multiple subsequent layers. The training data collected from multiple subsequent layers may be combined as the training data of the pre-order layer. For example, if a layer has 3 subsequent layers and 10,000 pieces of training data are collected on each subsequent layer, then the data on these subsequent layers are combined, and the pre-order layer obtains 30,000 pieces of training data. For the Multi-out structure, each subsequent layer may separately reflect the weights of all channels of the pre-order layer. Therefore, it is not necessary to separately determine the training data of the pre-order layer for each training data, and only all the data of the subsequent layers are needed to be combined.
For the Multi-in structure, since there are multiple pre-order layers sharing the training data of a subsequent layer, the training data of the subsequent layers may be split according to sources of input channels, and the training data related to the input channels is respectively allocated to the corresponding pre-order layers as training data. For example, there are 4 pre-order layers (each layer has 64 output channels) that are aggregated into one subsequent layer (correspondingly, the layer has 256 input channels), and 10,000 pieces of training data are collected on subsequent layers, wherein in each piece of training data, y is obtained by adding 256 xk. Then, based on that the input channel splitting data is provided to the corresponding pre-order layer, each pre-order layer obtains 10,000 pieces of training data, wherein each training data y′ is obtained by adding 64 xk.
For the Element-wise structure, since the channels of the previous and next layers correspond one-to-one and the pruning should be consistent, all layers in this structure may be regarded as an overall layer, and the obtained pruning results are equally applied to each layer. For training data, data collected on subsequent layers of all layers in this overall layer needs to be allocated to this overall layer. In other words, in the embodiment of the disclosure, any hidden layer may contain at least one Element-wise structure, and the same process may be performed according to a hidden layer that does not contain the Element-wise structure, which details will not be further described below.
In this manner, the corresponding training data may be obtained by each hidden layer of the model to be optimized, and the training data of the ith layer has the form (x(i)j,k, y(i)j), k=1, 2, . . . , ci+1, wherein ci+1 is the number of output channels of the ith layer. j=1, 2, . . . , Ni, Ni is the number of training samples in the ith layer. The data may be expressed as:
Next, the training data may be used to calculate the importance of each channel in each hidden layer, thereby pruning based on the importance. Taking the current layer as an example, the embodiment of the disclosure provides a possible implementation manner to indicate the importance of each channel in the current layer. That is, in Operation S1011, the relationship between the pruning number (in piratical applications, the pruning number may also be replaced by a pruning rate, which the pruning number is used as an example to introduce herein, and the same part will not be described again) of respective channels of each hidden layer and the corresponding pruning loss may be determined.
Specifically, the relationship may be expressed in the form of a relationship map of the pruning number and the pruning loss. As shown in
For example, the channel with the minimum influence on performance in all channels (for convenience of description, hereinafter referred to as the first channel) may be first determined, to calculate the pruning loss (i.e., the loss of the layer after pruning compared to before pruning) when the first channel (the pruning number is 1) is pruned. Then, on the basis of pruning the first channel, the influence on the performance of respectively pruning any one of the remaining channels may be calculated, and the pruning loss of the channel with the minimum influence on performance (i.e., the pruning number is 2, and the 2 pruned channel contains the first channel) may be determined, and analogously, the relationship map of the pruning number and the pruning loss as shown in
The relationship map of the pruning number and the pruning loss is drawn in the incremental manner of the pruning number, and similarly, may be drawn in the decreasing manner of the pruning number, and may be a similar relationship map of the pruning number and the pruning loss as shown in
From this relationship map, it may be seen the loss corresponding to respective pruning numbers in each layer, that is, the relationship map may reflect the relationship between the pruning number of respective channels and the corresponding pruning loss, that is, the importance of each channel. The calculating formula for the pruning loss is:
Wherein loss(i)(p) represents the information loss after pruning p channels in the ith layer, Ni is the number of collected samples corresponding to the current layer, p represents the pruning number, and S(i) represents a set of current remaining channels after pruning p channels in the ith layer, x(i)j,k represents the data related to the kth input channel in the jth sample data of the ith layer, and y(i)j is the sum of the data of the jth sample in the ith layer.
After obtaining the relationship between the pruning number of each hidden layer and the corresponding pruning loss, in Operation S1012, the pruning number of each hidden layer may be determined according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss.
The inventors of the disclosure have considered that for different hidden layers, the influence on the final result of the model output are different, and the determined relationship between the pruning number of respective channels and the corresponding pruning loss is respectively represented (the pruning loss is the loss of the current layer, rather than the influence on the entire network) in each hidden layer. If the pruning rate of each layer is respectively determined by the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss, the importance of the layer in the network may not be referenced, and the importance of different channels between different layers cannot be compared, and the respectively determined pruning rate of each layer may not be appropriate, thereby influencing the accuracy of the network.
Based on this, in order to enable the pruning losses between different layers to be compared with each other, in the embodiment of the disclosure, a weight is added to each layer to describe the importance of the layer, thereby obtaining a pruning solution with the minimum influence on the overall accuracy in all layers.
Similarly, in order to obtain the weight of each layer in the network, weights may be passed from back to the front in the network structure. As shown in
In the embodiment of the disclosure, the allocation of weights may also be different depending on whether the network has multiple branches. Specifically:
For a single-branch structure, since the network will no longer be connected if any layer is removed, then each layer may be considered equally important, and the same weight should be allocated to each layer, and all weights may be passed forward (for example, 1.0), that is, if a current hidden layer corresponds to a next hidden layer, the weight of the current hidden layer is the same as the weight of the next hidden layer. Alternatively, a relatively complex processing manner may be adopted for a single-branch network, and the weight of each layer may be different. For example, the weight of each layer may be determined according to a task type, a task target, and an input content of the neural network. As an example, for some tasks, the network layer close to the output side has a greater influence on the output result, and the weight closer to the output end may be set larger; for some tasks, the network layer close to the input side has a greater influence on the output result, and the weight closer to the input side may be set larger. The weight of each layer may also be obtained from prior knowledge.
For the Multi-out structure, the weights of all subsequent layers are added to the pre-order layer, that is, if one current hidden layer corresponds to at least two next hidden layers, the weight of the current hidden layer is the sum of weights of at least two next hidden layers.
For the Multi-in structure, the weights are allocated to any pre-order layer according to the following ratio: allocating according to the ratio of the sum of the data after convolution through all channels of any pre-order layer to the sum of the data after convolution through all channels of the pre-order layer. For example, according to the ratio of the sum of the data of the channel corresponding to the pre-order layer to the sum of all the data in all training data collected by the subsequent layer, the weight of the subsequent layer is split and is allocated to the pre-order layer, that is, at least two current hidden layer corresponds to one next hidden layer, and the weight of each current hidden layer is the weight of the next hidden layer allocated according to a channel proportion of each current hidden layer.
For the Element-wise structure, as a whole, it shares one weight. However, since the Element-wise structure is a structure in which multiple branches are combined into a single-branch structure, the weights need to be forward allocated to the respective input branches during the forward propagation of the weight. This allocation manner may be implemented in multiple ways, such as artificially specified, or evenly distributed, or may be determined by sampling according to the sample data size ratio.
In this manner, the weight of each layer may be calculated simultaneously, and with these weights, comparisons may be made between channels of different hidden layers. In combination with the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss, in Operation S1012, the pruning numbers of each hidden layer are determined, according to the relationship between the pruning number of respective channels of each hidden layer in the set hidden layer and corresponding pruning loss, and the weight corresponding to each hidden layer in the set hidden layer.
Wherein, after obtaining the weights of respective layers of the model, the weights corresponding to each hidden layer in the set hidden layer may also be obtained.
Specifically, the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss is determined, according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss, and a weight corresponding to each hidden layer; the pruning number of each hidden layer is determined, according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss. That is, as shown in
In practical applications, the model may be pruned by combining the weights of each layer with the importance of other defined channels. For example, the norm of the relevant parameters of different channels is used as the basis for importance, or a regularization term may be added; those skilled in the art may make appropriately changes on the above examples, which should also belong to the spirit and scope of the disclosure.
In the embodiment of the disclosure, a possible implementation manner is provided for how to provide a pruning strategy for each layer in a global scope: in Operation S1012, the compression parameter of the set hidden layer is determined according to the loss relationship and the overall compression target parameter of the model, wherein the overall compression target parameter includes an overall compression rate of the model and/or an overall loss of the model; in other words, the compression parameter of the set hidden layer is determined according to the loss relationship and at least one of the following: the overall compression rate of the model and the overall loss of the model; that is, when the compression parameter is the pruning number, the pruning number of each hidden layer is determined, by a combination optimization algorithm, according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss, as well as the overall compression target parameter (at least one of the following: the overall compression rate of the model and/or the overall loss of the model). Wherein, the loss relationship may also be the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss. Specifically, the overall pruning rate may be controlled, and the overall loss may also be controlled, and the task may be approximated into a group knapsack problem, which may be solved by dynamic programming.
If the overall pruning rate is controlled (equivalent to controlling the overall calculation amount or the size occupied by the overall parameter), that is, according to the loss relationship and the overall compression rate of the model (i.e., the overall pruning rate), the compression parameter (i.e., the pruning number) of each set hidden layer that minimizes the overall loss of the model is calculated, which the solution process is as follows:
Wherein xi is the number of channels retained after pruning in the ith layer, ci+1 is the total number of channels in the ith layer, and lossw(i)(p) represents the weighted pruning loss after pruning p channels in the ith layer, which is obtained by loss(i)(p) multiplied by the weight of the ith layer (it should be noted that ci+1-x is the pruning number of ith layer, and may also be represented by p and may be used as the independent variable of lossw(i)), f0 is the calculation amount calculated according to the given overall pruning rate, qi1,i2 is the calculation amount of performing a convolution operation on one input feature map of the layer, and qi1,i2xi1xi2 is the calculation amount of this layer calculated based on the number of channels xi1 and Xi2 retained in the previous and next layers.
If the overall loss is controlled (equivalent to controlling the accuracy of the model), that is, according to the loss relationship and the overall loss of the model, the compression parameter (i.e., the pruning number) of each set hidden layer that maximizes the overall compression rate (i.e., the overall pruning rate) of the model (correspondingly, the calculation amount of the compressed model is the smallest) is calculated, which the solution process is as follows:
Where l0 is the given overall loss, and the definitions of other variables are consistent with the previous solution.
In summary, the overall flow of the above technical solution for automatically determining the compression parameter (the pruning number or the pruning rate) of each layer is as shown in
Further, in Operation S102, the model to be optimized is compressed according to the pruning number of the set hidden layer, to obtain an optimized model.
The foregoing technical solution provided by the embodiment of the disclosure may automatically determine the optimal pruning number or pruning rate of each layer in a global scope with a given overall compression target, and may simultaneously determine the pruning number or pruning rate of each layer, which does not require artificially setting the compression rate of each layer, thereby greatly improving the flexibility of compression, saving the time cost of model compression, and improving the compression efficiency. In addition, the automatically determined compression rate is more accurate, thereby avoiding the large loss caused by compression.
Moreover, the technical solution may be applied to a commonly used multi-branch network structure with good adaptability, and can accurately determine the importance of each layer in the multi-branch network, and further automatically determine optimal pruning rate for each layer in the multi-branch network according to the importance of each layer, and in addition, it may accurately allocate the training data to the respective layers in the multi-branch network.
In addition, different from the method for calculating the pruning number or pruning rate layer by layer, in the above technical solution according to the embodiment of the disclosure, each pruning refers to calculating the optimal pruning number and the pruning rate of each layer in the global scope. In other words, it is determined that the determination of the pruning number or the pruning rate of each layer considers not only the current layer but also the importance of each layer in the network. Therefore, the pruning number or the pruning rate corresponding to each layer respectively is determined comprehensively, and then the pruning is performing on each layer according to the pruning number or the pruning rate of each layer respectively, such that the accuracy of compression rate of each layer is higher, and the accuracy of the compressed model is improved.
In the embodiment of the disclosure, a model compression method that uses both a pruning technology and a quantization technology is introduced, that is, a model compression manner in which the compression parameter includes a pruning number and a quantization rate in Operation S101.
The inventors of the disclosure have found that the quantization method and the pruning method are complementary. If using a common compression method that first performs pruning and then performs quantization, even if the quantification is similar to the pruning, fine-tuning (when further training on the data set, often set the learning rate and other parameters to a lower value, and only a small adjustment is processed compared with the previous training) is required to be repeated, which makes more operations in the overall compression process for the model and cost more time.
In fact, the quantization and the pruning are compressions on the model at different levels, which the pruning is to reduce the number of network parameters, and the quantization is to reduce the number of bits required to represent a single parameter. It is observed that there is a similarity between the quantization process and the pruning process: both have compression rates (the compression rate here may correspond to the compression rate of the model calculation amount, and may also correspond to the compression rate of the storage space required by the model), and meanwhile, both of the above compression processes will cause loss. Therefore, the two have certain complementarity, and the quantization process and the pruning process may be combined, and it may achieve better compression effect while using the both.
Therefore, the embodiment of the disclosure proposes to combine the quantization and pruning processes, and determines the pruning number and the quantization rate of each layer simultaneously, and performs a corresponding process according to the pruning number and the quantization rate of each layer to improve the overall optimization speed. Specifically, Operation S101 may include the operations of:
Step S1013: determining the relationship between each pruning number of each hidden layer in the set hidden layer and the corresponding pruning loss, and determining a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and the corresponding quantization loss;
Step S1014: determining the pruning number and the quantization loss of each hidden layer according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss and the determined relationship between respective candidate quantization rates of each hidden layer and the corresponding quantization loss.
Wherein, for the manner of determining the relationship between pruning number of respective channels of each hidden layer in the set hidden layer and the corresponding pruning loss in Step S1013, it may be referred to the description of Step S1011 in the above, and details are not described herein.
In the embodiment of the disclosure, the quantization rate of each layer may refer to the quantization rate corresponding to the convolution kernel at each convolution operation in the layer.
Specifically, consistent with the pruning process, the quantization process also needs to first collect relevant training data, which is not limited to a specific collecting manner in the disclosure. Different from the pruning process, the collection of the quantized data may be completed in a single layer, that is, for each layer, according to the training data of the current hidden layer, the relationship between the respective candidate quantization rates and the corresponding quantization loss of the current hidden layer is determined; wherein, the training data includes the relationship between the output channel and each input channel. This is because the change of quantization for a single layer does not influence the structure of the previous and next layers. Then, according to the collected data, the relationship between the respective candidate quantization rates of each hidden layer and the corresponding quantization loss may be calculated.
Similarly, the relationship may be represented in the form of a relationship map of a candidate quantization rate and a quantization loss, which is similar to the process shown in
Moreover, in order to enable the quantization losses between different layers to be compared with each other, it is also possible to add a weight to each layer to describe the importance of the layer, thereby obtaining a quantization solution having the minimum influence on the overall accuracy in all layers. That is, in Step S1014, the quantization rate of each hidden layer is determined according to the relationship between the respective candidate quantization rates of each hidden layer in the set hidden layer and the corresponding quantization loss, as well as the weight corresponding to each hidden layer.
In the embodiment of the disclosure, the weight of each layer obtained by the pruning process may be directly reused, thereby reducing steps of calculating the weight.
After that, the relationship map of the candidate quantization rate and the weighted quantization loss may be obtained, which is similar to the process shown in
Then, the pruning strategy and the quantization strategy of each layer are also given from the global scope, that is, in Step S1014, the compression parameter of the set hidden layer is determined according to the loss relationship and the overall compression target parameter of the model, wherein the overall compression target parameter includes an overall compression rate of the model and/or an overall loss of the model; that is, the compression parameter of the set hidden layer is determined according to the loss relationship and at least one of the following: the overall compression rate of the model and the overall loss of the model; that is, when the compression parameter includes the pruning number and the quantization rate, the pruning number of each hidden layer is determined, by a combination optimization algorithm, according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss, the relationship between respective candidate quantization rate of each hidden layer and the corresponding weighted pruning loss as well as the overall compression target parameter (at least one of the following: the overall compression rate of the model and/or the overall loss of the model). Wherein, the loss relationship may also be the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss, as well as the relationship between the respective candidate quantization rates of each hidden layer and the corresponding quantization loss. Specifically, the overall pruning rate may be controlled, and the overall loss may also be controlled, and the task may be approximated into a group knapsack problem, which may be solved by dynamic programming.
If the overall compression rate is controlled (equivalent to controlling the overall calculation amount or the size occupied by the overall parameter), that is, according to the loss relationship and the overall compression rate of the model, the compression parameter (i.e., the pruning number and the quantization rate) of each set hidden layer that minimizes the overall loss of the model is calculated, which the solution process is as follows:
Wherein, zi is the quantization rate of the ith layer, Zi is the set of all possible quantization rates of the ith layer, and lossqw(i)(zi) is the quantization loss corresponding to the quantization rate z adopted by the ith layer, p(i, zi3) represents the total calculation amount (or the space occupied by the layer parameter) of the ith layer after using the quantization rate zi, and f0 is the calculation amount (or the model size, which needs to correspond to the meaning represented by p, that is, when p represents the calculation amount, f0 also represents the calculation amount or the computation amount, p represents the size of the space occupied by the parameter, and f0 represents the model size) calculated according to the given pruning rate and the quantization rate, which other variables may be referred to the calculation method for determining the pruning number and are not described here.
If the overall loss is controlled (equivalent to controlling the accuracy of the model), that is, according to the loss relationship and the overall loss of the model, the compression parameter (i.e., the pruning number and the quantization rate) of each hidden layer that maximizes the overall compression rate of the model (correspondingly, the calculated amount of the model is the smallest) is calculated, which the solution process is as follows:
The definition of respective variables, may refer to the various solutions in the above, and further details are not provided here.
In the embodiment of the disclosure, when performing model compression, if the quantization methods are different, the process of determining the quantization rate may be different. The following specifically gives two existing quantization methods to determine the quantization rate, including how to collect data, how to determine the loss relationship, etc.
Selecting Incremental Network Quantization (INQ, which converts the parameter to 2 order power, and the last number of bits may be set) as a quantization method. When sampling the data, training data y(i)j,k, (t=1, 2, . . . , T; j=1, 2, . . . , Mi) at different quantization rates t may be collected; wherein T is the total level of quantization, and Mi is the number of overall samples in the ith layer. y(i)j represents the data collected at the jth unquantified. Then the corresponding loss lossq(i)(t) of the ith layer at the given quantization rate t may be calculated as follows:
Meanwhile, the influence on the calculation amount (or model size) at different quantization rates may be directly calculated according to the quantization rate.
(2) Selecting Integer-Arithmetic-Only-Inference (IAOI, which requires only 8-bit integer arithmetic after quantization) as the quantization method. For each layer, there may be two choices: quantization or non-quantization. Therefore, when collecting the data, two forward conductions may be performed for each layer, in which one is quantized, and the other is not quantized; for the quantization rate: the quantization rate is 0% when not quantized, and 100% when quantized, and meanwhile the loss may also be calculated by the change of the feature maps before and after quantization. Therefore, the obtained relationship map of the quantization rate and loss has values only in two positions: 0% (not quantized) and 100% (quantized).
Furthermore, in addition to the manners proposed in the foregoing embodiment of the disclosure to determine the pruning number and the quantization rate, the pruning rate and the quantization rate may also be determined according to other methods, which are not specifically limited herein. As long as the quantization process and the pruning process are combined, the process will result in better compression effect.
In summary, the overall process of the above technical solution for automatically determining the pruning number and the quantification rate of each layer is shown in
Further, in Step S102, according to the pruning number and the quantization rate of the set hidden layer, the model to be optimized is compressed to obtain an optimized model.
In the embodiment of the disclosure, a model compression manner using the quantization technology is introduced, that is, a model compression manner in which the compression parameter includes a quantization rate in Step S101.
Similarly, in order to solve that the artificially setting a quantization rate will reduce the flexibility of the model compression and the loss from improper setting the quantization rate, the embodiment of the disclosure adopts a fully automated process to execute a model compression method based on the quantization technology, and specifically Step S101 may include the steps of:
Step S1015: determining a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding quantization loss;
Step S1016: determining a quantization rate of each hidden layer according to a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss.
Wherein, in Step S1015, it is necessary to determine the relationship between respective candidate quantization rates and corresponding quantization loss of the current hidden layer according to the training data of the current hidden layer, which the specific executing process may refer to the description of Step S1013 and will not be repeated herein.
Similarly, in order to enable the pruning losses between different layers to be compared with each other, a weight is added to each layer to describe the importance of the layer, thereby obtaining a pruning solution with the minimum influence on the overall accuracy in all layers. That is, in Step S1016, the quantization rate of each hidden layer is determined according to a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss, and a weight corresponding to each hidden layer. Specifically, the relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss is determined according to the relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss, and a weight corresponding to each hidden layer; the quantization rate of each hidden layer is determined according to the relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss. The manner of determining the weight may refer to the manner of determining the weight of each layer in the pruning process, and details will not be described herein.
Further, the quantization strategy of each layer are also given from the global scope, that is, in Step S1016, the compression parameter of the set hidden layer is determined according to the loss relationship and the overall compression target parameter; that is, the compression parameter of the set hidden layer is determined according to the loss relationship and at least one of the following: the overall compression rate of the model and the overall loss of the model; that is, when the compression parameter includes the quantization rate, the quantization rate of each hidden layer is determined, by a combination optimization algorithm, according to the relationship between respective candidate quantization rates of each hidden layer and the corresponding weighted quantization loss. Wherein, the loss relationship may also be the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss. Specifically, the overall pruning rate may be controlled, and the overall loss may also be controlled, and the task may be approximated into a group knapsack problem, which may be solved by dynamic programming.
If the overall compression rate is controlled (equivalent to controlling the overall calculation amount or the size occupied by the overall parameter), that is, according to the loss relationship and the overall compression rate of the model, the quantization rate of each set hidden layer that minimizes the overall loss of the model is calculated, which the specific solution process may be referred to above and will not be described herein.
If the overall loss is controlled (equivalent to controlling the accuracy of the model), that is, according to the loss relationship and the overall loss of the model, the quantization rate of each set hidden layer that maximizes the overall compression rate of the model is calculated, which the specific solution process may be referred to above and will not be described herein.
In this manner, the quantization rate of each set hidden layer may be obtained. Further, in Step S102, according to the quantization rate of the set hidden layer, the model to be optimized is compressed to obtain an optimized model.
The foregoing technical solution provided by the embodiment of the disclosure may automatically determine the optimal quantization rate of each layer in a global scope with a given overall compression target, which does not require artificially setting the compression rate of each layer, thereby greatly improving the flexibility of the compression, saving the time cost of model compression, and improving the compression efficiency. In addition, the automatically determined compression rate is more accurate, thereby avoiding the large loss caused by compression.
In addition, different from the method for calculating the quantization rate layer by layer, in the above technical solution provided by the embodiment of the disclosure, each quantization refers to calculating the optimal quantization rate of each layer in the global scope. In other words, the determination of the quantization rate of each layer considers not only the current layer but also the importance of each layer in the network. Therefore, the quantization rate corresponding to each layer is determined respectively, and then the quantization is performed on the each layer according to the quantization rate of each layer respectively, such that the accuracy of quantization rate of each layer is higher, and the accuracy of the compressed model is improved.
The inventors of the disclosure also believe that in the model compression process, whether it is the pruning and/or quantization, the information that is pruned or quantized is relatively unimportant, but also includes certain information that may influence the expression capability of the parameters and the accuracy of the model, so the effect of the model will decrease after compression.
Conventionally, it is expected that the lost information will be adjusted back through the fine-tuning process. But the “fine-tuning” network only trains the network on a small data set, and sets the learning rate to a lower value. Since the previous information is discarded and it is more difficult to learn back, the influence of the training process on the network is smaller than that of the larger learning rate.
In the embodiment of the disclosure, fine-tuning is performed by means of knowledge distillation technology, and the knowledge of the larger model is used to guide the training of the small model, thereby improving the performance of the small model. In general, a large model is easier to obtain a good training effect, while a small model is limited to model capacity and is often not easy to be trained. By the small model “learning” and “simulating” the performance of the large model, the effect of the small model will be improved.
The embodiment of the disclosure proposes that after the model is subjected to a compression process (such as pruning and/or quantization), the compressed model may be fine-tuned by a knowledge distillation method. Specifically, at least one of the following models is selected as a learning model: a model before compression, a model after compressing at least one hidden layer, and a model after historical fine-tuning; the compressed model is fine-tuned according to the learning model to obtain an optimized model.
Since the model before compression is larger than the compressed model and has more information in the process of compressing a model, the model before compression may be selected as the learning model (also referred to as a teacher model). It is also possible to select a model at any stage of this compression process as a teacher model, such as a model in which some hidden layers is compressed by pruning processing and/or quantization processing, and the model at this time is larger than later compressed model and has more information. In addition, since the model obtained after the fine-tuning process (that is, the model after the historical fine-tuning processing) also has relatively more information, which may also be used as a learning model. The compressed model is used as a student model and is fine-tuned by means of knowledge distillation to improve the performance of the student model.
A fully connected layer may be added to the same key positions of the teacher model and the student model (for example, at the intersection of important modules, etc., which may be artificially set), to aggregate them into a loss layer. The student model is trained by the distribution difference between the student model and the teacher model as the target. When the difference between the two models is less than a certain degree, the fine-tuning process may be terminated early. Due to the guidance from the teacher model, the entire fine-tuning process will be more efficient and faster.
In the embodiment of the disclosure, if the model after the fine-tuning does not satisfy a preset condition, the step of determining the compression parameter of the set hidden layer in the model to be optimized and compressing the model to be optimized (i.e., pruning and/or quantization process) according to the compression parameter of the set hidden layer, and the step of fine-tuning the compressed model according to the learning model (i.e., the step of fine-tuning the compressed model according to the learning model) are repeatedly performed, until the fine-tuned model satisfies the preset condition, to obtain the optimized model.
Wherein, the preset condition may be determined according to a given compression target, which may specifically refer to the overall compression rate and/or the preset condition related to the overall compression rate and the loss, for example, whether the overall compression rate is reduced to 50% of the original target, or whether the overall loss reaches 50% of the original target, etc. The preset threshold may be set by those skilled in the art according to the actual situation, which is not limited herein.
It may be understood that the model to be optimized in Step S101 may be an original model to be optimized, and for the case where the fine-tuned model does not satisfy the preset condition, the model to be optimized may also be a fine-tuned model, and the execution processes are consistent with each of the above embodiments and will not be described again here.
It should be noted that the teacher model selected in the fine-tuning process may be the original model to be optimized, which has the most information, or a model with key steps in previous compression processes, for example, a model with the previous compression step, and the number of teacher models may be one or more, which is not limited herein.
This application uses knowledge distillation technology to fine-tune the pruned and/or quantized model, makes full use of the information lost during the model compression process, and makes full use of the previously learned results, so that the fine-tuning process may be more targeted and the accuracy of the compressed model is improved, thereby accelerating the fine-tuning process and making the fine-tuning process faster and more efficient.
The inventors of the disclosure have also considered that different compression methods or overall compression levels may be suitable for different models. This process, if given by human experts based on experience, also influences the flexibility of model compression. Based on this, the embodiment of the disclosure proposes that the overall compression target is controlled by reinforcement learning to achieve the full automation. In other words, the overall compression target parameter of the model is determined by the reinforcement learning algorithm.
In combination with the above, as long as the overall compression target is given, it is possible to automatically solve the optimal compression parameters of respective layers. In other words, a fully automated process may be achieved as long as the overall compression target is automatically determined.
Specifically, the overall compression rate of the model and the overall loss of the model are determined by a reinforcement learning algorithm (for example, A3C or the like). The Action Space is given by discrete compression parameters, such as 10%, 20%, and the like. In addition to the basic state of the network, the state space includes the model size, the current pruning process, etc., and also includes the relevant statistics of the previously calculated relationship map of compression parameters (e.g., the pruning number) and the loss (e.g., average loss, loss variance, etc.). The reward function not only considers the compression rate and compression loss, but also the speed of compression. If the total compression rate is less than a given value within a certain steps, the reward function returns a negative value.
In the embodiment of the disclosure, in the model compression stage, the model to be optimized and the final optimization target (or termination condition) are input, and the reinforcement learning algorithm sequentially gives an overall compression target parameter (overall compression rate or overall loss) of the current iteration. Wherein, the MAC may also be output, that is, the calculation amount after compression, and the overall compression rate may be further determined by the MAC; according to the overall compression target parameter and the above method for determining the compression parameter of the hidden layer, compression parameters of the set hidden layer may be automatically calculated, and which hidden layers are compressed may also be automatically selected, that is, automatically determining the set hidden layer. In practical applications, when the compressed model does not satisfy the termination condition, the model compression process of each of the above embodiments may be repeated until the termination condition is satisfied and the optimized model is obtained. Wherein, the foregoing termination condition may be set, for example, setting to confirm that the termination condition is satisfied when the MAC of the compressed model is reduced to 50% of the MAC of the original model to be optimized, or setting to confirm that the termination condition is satisfied when the loss of the compressed model has reached to 50% of the loss of the original model to be optimized.
In the embodiment of the disclosure, in order to obtain a lightweight small learning network with superior performance, a group compression method (also referred to as a group pruning method) for a model is also proposed, as shown in
Step S201: splitting channels of each hidden layer in a set hidden layer of the model to be compressed into at least two groups of sub-channels, and determining a network parameter of each group of sub-channels, wherein each group of sub-channels includes corresponding input channels and output channels after grouping;
Step S202: adding a combination layer to obtain a group compressed model, wherein the input of the combination layer is connected to the output channel of each group of sub-channels.
With the group pruning method of the embodiment of the disclosure, a hidden layer may be split into multiple small hidden layers (which may be referred to as a group hidden layer), for example, splitting a convolutional layer or a fully connected layer into multiple group convolutional layers or group fully connected layers, which may prune associations between groups, that is, pruning connections between some channels (the connections between these channels have little influence on network performance), which greatly reduces the calculation amount and the number of parameters of the model.
According to an embodiment of the disclosure, the network parameters of the hidden layer include the kernel matrix of the layer. Taking that a convolutional layer is split into two groups as an example, as shown in
As an example, in an Xception network, by grouping the convolutional layer (one group with 3*3 convolutional kernels), each convolutional kernel acts on only one channel, thereby reducing a large number of inter-group connections. The embodiment of the disclosure proposes that, in the model optimization, network parameters of the convolutional layer or the fully connected layer in the model may be grouped, such that some connections between the groups that have little influence on network performance may be pruned, and is possible to achieve a model optimization with finer particle granularity.
As shown in
In particularly, Step S201 specifically includes the following steps:
Step S2011: determining a contribution between each pair of input channels and output channels of each hidden layer in the set hidden layer;
Step S2012: determining a splitting manner with minimum loss for splitting the channels of each hidden layer into at least two groups of sub-channels, according to the contribution between each pair of input channels and the output channels, and splitting the channels of each hidden layer into at least two groups of sub-channels according to the splitting manner.
The complete operation flow is shown in
In the embodiment of the disclosure, a possible implementation manner is provided for Step S2011, and a contribution between each pair of input channels and output channels of each hidden layer is determined, according to a norm of a calculation parameter respectively associated with each pair of input channels and output channels of each hidden layer.
A norm (e.g., L1 norm) of a parameter (a calculation parameter when the output channel is calculated by the input channel, that is, a kernel) associated with each input channel and each output channel is used to represent the contribution, and a larger norm represents a greater contribution.
In the embodiment of the disclosure, a possible implementation manner is further provided for Step S2011: determining a contribution between each pair of input channels and output channels of each hidden layer according to the relationship between the pruning number and the weighted pruning loss related to each output channel and each input channel of each hidden layer respectively by determining a relationship between a pruning number and weighted pruning loss related to each output channel and each input channel of each hidden layer respectively.
Similar to the above method for automatically determining the pruning number, by sampling the data and drawing the relationship map of the pruning number and the weighted pruning loss related to all the input channels on the single output channel n of the ith layer, as shown in
Wherein, contrib(i)(n, k) is the contributions (normalized value) from the nth input channel to the kth output channel on the ith layer, and lossc(i, k)(n) represents the loss corresponding to pruning n output channels on the kth output channel on the ith layer, the calculation method is similar to the calculation of the previous loss (just sampling on the kth output channel), and W(i)n,k is the contribution from the nth input channel to the kth output channel before normalization.
In the embodiment of the disclosure, a possible implementation manner is provided for the Step S2012, and a splitting manner with minimum loss for splitting the channels to be grouped into two groups of sub-channels is determined, wherein the channel to be grouped is a channel of each set hidden layer or at least any one of sub-channels after grouping; that is, as shown in
At each grouping, through the graph theory algorithm, a splitting manner with minimum loss for splitting the channels to be grouped into two groups of sub-channels is determined, as shown in
Further, after the group compressed model is obtained, the group compressed model may be fine-tuned by a knowledge distillation algorithm. Specifically, the model to be compressed before group compression is selected as a learning model, and the group compressed model is fine-tuned, which the specific implementation may refer to the above description and details are not described herein again.
As compared with a convolutional layer of a general model (that is, the convolutional layer in which each output channel is associated with all input channels), for partial convolutional layers, the group-compressed model obtained by the embodiment of the disclosure prunes association information between the groups by grouping, such that each output channel only connects to a part of the input channels, thereby achieving group convolution, and greatly reducing the calculation amount and the number of parameters of the model.
As compared with the channel-based pruning technology, the technical solution provided by the embodiment of the disclosure may implement a pruning method with finer granularity, and further improve the effect of model compression.
In the embodiment of the disclosure, the group compression method for the model shown in
Wherein, if performing group compression on the optimized model after the model optimization method as shown in
In combination with the above embodiments, the embodiment of the disclosure further proposes an implementation process. As shown in
Specifically, by giving an original model and the overall compression target, such as a given MAC, that is, the calculation amount after compression, the MAC may further determine the overall compression rate, or by giving the overall loss (representing available information loss), the final compression result is obtain through two stages successively: a channel-level-based compression stage (pruning and quantization), and a group pruning compression stage.
Firstly, in the channel-level-based compression stage S1700, the current neural network model (corresponding to the input model in
The second stage is the group pruning compression stage S1720. The model after the previous stage compression 1722 and the overall compression target 1724 are input, and the model is further compressed by the group pruning technology S1726 proposed by the disclosure, and finally the final model 1730 is obtained through the fine-tuning process S1728.
The model compression or optimization method provided by the embodiments of the disclosure may be used in the following application scenes.
The Scene Text Detection (STD) is to detect text visible in different scenes in the life through a camera 1802, and the detected text 1832 may be handed over to a subsequent recognition module 1850 for practical functions such as searching/recommending 1860, or translating 1870. In order to improve the user experience, it is necessary to increase the detection speed, and the STD should be able to be used in some environments where the network signal is poor. The detection module needs to be deployed on a device side.
An original STD model has a larger size and slow operation speed. Through the technical solution provided by the embodiment of the disclosure, as shown in
Since the experience is related to the accuracy of the detection for a user, it is also directly related to the speed of the detection. Different strategies may be adopted depending on an actual usage scene. For example, for a simpler scene, the speed of the detection is very important, and the overall compression rate of the model may be set smaller (that is, the calculation amount of the compressed model is smaller and the calculation speed is faster), and then the model compression algorithm 1820 proposed by this solution is used for compression, thereby improving the speed of the detection; for another example, in a more complex scene (scene having rich details and more light and dark changes, etc.), the accuracy of the detection is more important, so the overall loss of the model may be set smaller (that is, the accuracy of the compressed model is higher, and the information loss caused by compression is less than a certain degree), and then the model compression algorithm (1820) proposed by this solution is used for compression, thereby ensuring the accuracy of the detection.
Human skeleton keypoints detection refers to detecting multiple key positions (pre-set) of a person in the scene, which is the basis of many other computer vision tasks, such as behavior analysis, motion classification, etc., and may also provide assistance for human-computer interaction, auxiliary training, automatic pilot, etc. Every application related to the human skeleton keypoints detection often requires real-time. Meanwhile, the model may be compressed by the technical solution provided by the embodiment of the disclosure, which ensure a certain accuracy while effectively reducing the operating time.
When the original keypoints detection model 1922 is deployed directly on the terminal device, the real-time performance may not satisfy requirements. As shown in
In applications of the human skeleton keypoints detection, it is critical to be able to achieve real-time performance. For the device side, especially for devices with weak chip processing capabilities, how to operate the relevant model in real time is the key. The overall compression rate of the model may be set smaller (that is, the calculation amount of the compressed model is smaller, and the calculation speed is faster), and the model is compressed through the model compression algorithm provided by the embodiment of the disclosure, thereby improving the detection speed, and finally a simplified model obtained is deployed on a terminal device (i.e., a mobile phone).
The foregoing model optimization method proposed by the embodiment of the disclosure may be applied to an On-device intelligent compression platform, and the platform optimizes the model to be deployed in a terminal device, so that the compressed lightweight model may be normally operated in the terminal device.
According to an embodiment of the disclosure, an electronic device (2000) is provided. The electronic device 2000 comprises an input interface 2010, a processor 2020, a memory 2030, and an output interface 2040. At least one of the input interface 2010 or the output interface 2040 may be omitted according to an embodiment.
The input interface 2010 receives input data for a machine learning model which is performed by the processor 2020 by on-device method. According to an embodiment of the disclosure, the input interface 2010 may correspond to a camera 1810 in
According to an embodiment of the disclosure, the input interface 2010 receives an original machine learning model to be compressed or to be optimized. The original machine learning model is input in the form of computer program instructions and parameter sets.
The processor 2020 controls overall operation of the electronic device 2000. The processor 2020 comprises one or more processor. The processor 2020 performs the machine learning model according to embodiments of the disclosure. Also, the processor 2020 performs a model compression algorithm or a model optimization algorithm according to embodiments of the disclosure as explained above. The processor 2020 updates an original model by the model compression algorithm or the model optimization algorithm, generates an updated model, and performs operations based on the updated model. The processor 2020 input an input data from the input interface 2010 into the updated model, and obtains an output data from the updated model.
According to an embodiment of the disclosure, the processor 2020 performs at least one operation of the model compression algorithm 1820, the STD module 1830, the hardware related acceleration 1840, the text recognition 1850, the searching/recommending 1860, or the translation 1870 in
The processor 2020 may be a CPU, a general-purpose processor, a DSP, an ASIC, and an FPGA or other programmable logic device, transistor logic device, hardware component, or any combination thereof. It is possible to implement or carry out the various illustrative logical blocks, modules and circuits described in connection with the present disclosure. The processor may also be a combination of computing functions, such as one or more microprocessor combinations, a combination of a DSP and a microprocessor, and the like.
The memory 2030 stores data, instructions, commends, or parameters. The memory 2030 may store at least one instruction, at least one program, a code set, or a set of instructions, the at least one instruction, the at least one program, the code set, or the set of instructions being loaded and executed by the processor to implement the corresponding content in the foregoing method embodiments. The memory 2030 may be formed as a volatile or non-volatile memory. The memory 2030 may store the machine learning model. In the memory 2030, nodes or layers of the machine learning model is allocated, and values of parameter sets are stored. The processor 2020 may perform operations of the machine learning model by storing values of nodes or layers in the memory 2030, and using the values of parameter sets stored in the memory 2030.
The memory 2030 may be a ROM or other type of static storage device that may store static information and instructions, RAM or other types of Dynamic storage device that may store information and instruction, may also be EEPROM, CD-ROM or other optical disc storage, optical disc storage (including compression Optical discs, laser discs, optical discs, digital versatile discs, Blu-ray discs, etc.), magnetic disk storage medium or other magnetic storage devices, or any other medium that may be used to carry or store desired program codes in form of instruction or data structure and may be accessed by the computer.
The output interface 2040 outputs a result value of the machine learning model. The output interface 2040 may comprises at least one of a display, a speaker, a touch screen, a printer, or a communication interface. According to an embodiment of the disclosure, at least one of a result of the text recognition 1850, a result of the searching/recommending 1860, or a result of the translating 1870 is output via the output interface 2040. According to another embodiment of the disclosure, at least one of a result of key-points detection 1932, a result of the behavior analysis 1950, a result of the action classification 1960, or a result of the motion sensing game is output via the output interface 2040.
According to an embodiment of the disclosure, the electronic device 2000 may also comprise a transceiver. The processor 2020 is connected to the transceiver, such as via a bus. The electronic device 2000 may comprises one or more transceiver.
The bus may comprise a path for communicating information between the above components. The bus may be a PCI bus or an EISA bus. The bus may be classified into an address bus, a data bus, a control bus, and the like.
According to an embodiment of the disclosure, the processor 2020 comprises a model optimization module 2100. As shown in
The determining module 2110 determines a compression parameter of a set hidden layer in a model to be optimized.
The compression module 2120 compresses the model to be optimized based on the compression parameter of the set hidden layer, to obtain an optimized model.
According to an embodiment of the disclosure, if the compression parameter includes a pruning number, the determining module 2110 may determine a relationship between the pruning number of respective channels of each hidden layer in the set hidden layer and corresponding pruning loss of each hidden layer.
Also, according to an embodiment of the disclosure, the determining module may determine the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and corresponding pruning loss of each hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine a relationship between the pruning number of respective channels and the corresponding pruning loss of a current hidden layer, based on training data of a next hidden layer. A training data of a hidden layer includes a relationship between an output channel of the hidden layer and each input channel of the hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine the pruning number of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss of each hidden layer, and a weight corresponding to the each hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine a relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss of each hidden layer, based on the relationship between the pruning number of respective channels of each hidden layer and the corresponding pruning loss of each hidden layer, and a weight corresponding to the each hidden layer;
The determining module 2110 may determine the pruning number of each hidden layer, according to the relationship between the pruning number of respective channels of each hidden layer and the corresponding weighted pruning loss of each hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may calculate the pruning loss corresponding to each current candidate channel to be pruned. Any current candidate channel to be pruned includes pruned channels determined by a previous channel pruning number and at least one unpruned channel, or any current candidate channel to be pruned includes remaining pruned channels after removing at least one channel from the pruned channels determined by a previous channel pruning number.
The determining module 2110 may determine a current candidate channel to be pruned with the minimum pruning loss as the pruning channel corresponding to the current channel pruning number, to obtain the relationship between the current channel pruning number and the corresponding pruning loss.
According to an embodiment of the disclosure, if the compression parameter includes a quantization rate, the determining module 2110 may determine a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding quantization loss;
The determining module 2110 may determine a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine the relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, based on training data of the current hidden layer. The training data includes a relationship between an output channel of a hidden layer and each input channel of the hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine a quantization rate of each hidden layer based on a relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, and a weight corresponding to each hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine a relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss of each hidden layer, based on the relationship between respective candidate quantization rates of each hidden layer and corresponding quantization loss of each hidden layer, and a weight corresponding to each hidden layer;
The determining module 2110 may determine a quantization rate of each hidden layer, based on the relationship between respective candidate quantization rates of each hidden layer and corresponding weighted quantization loss of each hidden layer.
According to an embodiment of the disclosure, if one current hidden layer corresponds to one next hidden layer, the weight of the current hidden layer is the same as the weight of the next hidden layer.
If a current hidden layer corresponds to least two next hidden layers, the weight of the current hidden layer is the sum of the weights of at least two next hidden layers.
If at least two current hidden layers correspond to one next hidden layer, the weight of each current hidden layer is the weight of the next hidden layer allocated according to a channel proportion of each current hidden layer.
According to an embodiment of the disclosure, the determining module 2110 may determine the compression parameter of the set hidden layer, based on a loss relationship and an overall compression target parameter of the model.
If the compression parameter is a pruning number, then the loss relationship includes a relationship between the pruning number of respective channels of each hidden layer in the set hidden layer and the corresponding weighted pruning loss of each hidden layer.
If the compression parameter is a quantization rate, then the loss relationship includes a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding weighted quantization loss of each hidden layer.
If the compression parameter includes a pruning number and a quantization rate, then the loss relationship includes a relationship between the pruning number of respective channels of each hidden layer in the set hidden layer and the corresponding weighted pruning loss of each hidden layer, and a relationship between respective candidate quantization rates of each hidden layer in the set hidden layer and corresponding weighted quantization loss of each hidden layer.
According to an embodiment of the disclosure, the overall compression target parameter includes an overall compression rate of the model and/or an overall loss of the model.
According to an embodiment of the disclosure, the determining module 2110 may calculate a compression parameter of each set hidden layer that minimizes the overall loss of the model based on the loss relationship and the overall compression rate of the model.
The determining module 2110 may calculate a compression parameter of each set hidden layer that maximizes the overall compression rate of the model based on the loss relationship and the overall loss of the model.
According to an embodiment of the disclosure, the compression module 2120 may select at least one of the following models as a learning model: a model before compression; a model after compressing at least one hidden layer; a model after historical fine-tuning.
The compression module 2120 may fine-tune the compressed model according to the learning model to obtain the optimized model.
According to an embodiment of the disclosure, the compression module 2120 may repeatedly perform, if the fine-tuned model does not satisfy a preset condition, the steps of determining the compression parameter of the set hidden layer in the model to be optimized, compressing the model to be optimized according to the compression parameter of the set hidden layer, and the step of fine-tuning the compressed model according to the learning model, until the fine-tuned model satisfies the preset condition, to obtain the optimized model.
According to an embodiment of the disclosure, the determining module 2110 may determine an overall compression rate of the model and an overall loss of the model by using a reinforcement learning algorithm.
According to an embodiment of the disclosure, the compression module 2120 may split channels of at least one hidden layer of the optimized model into at least two groups of sub-channels, and determine a network parameter of each group of sub-channels. Each group of sub-channels includes corresponding input channels and output channels after grouping.
The compression module 2120 may add a combination layer to obtain a group compressed model. The input of the combination layer is connected to the output channel of each group of sub-channels.
It can be clearly understood by those skilled in the art that the implementation principle and technical effects of the model optimization module 2100 provided by the embodiments of the disclosure are similar to those of the previous method embodiments. The embodiments of the disclosure for the previous method embodiments may be applied to the embodiments of the model optimization module 2100, and the embodiments of the disclosure for the model optimization module 2100 may be applied to the embodiments of the disclosure for the method. For the convenience and brevity of the description, where the device embodiment is not mentioned, reference may be made to the above method embodiments, and details are not described herein again.
According to an embodiment of the disclosure, the processor 2020 comprises a group compression module 2200. As shown in
As shown in
The group compression module 2210 may split channels of each hidden layer in a set hidden layer of the model to be compressed into at least two groups of sub-channels, and determine a network parameter of each group of sub-channels. Each group of sub-channels may comprise corresponding input channels and output channels after grouping.
The adding module 2220 may add a combination layer to obtain a group compressed model. The input of the combination layer is connected to the output channel of each group of sub-channels.
According to an embodiment of the disclosure, the group compression module 2210 may determine a contribution between each pair of input channels and output channels of each hidden layer.
The group compression module 2210 may determine a splitting manner with minimum loss for splitting the channels of each hidden layer into at least two groups of sub-channels, based on the contribution between each pair of input channels and the output channels, and splitting the channels of each hidden layer into at least two groups of sub-channels according to the splitting manner.
According to an embodiment of the disclosure, the group compression module 2210 may determine a contribution between each pair of input channels and output channels of each hidden layer, according to a norm of a calculation parameter respectively associated with each pair of input channels and output channels of each hidden layer.
Also, the group compression module 2210 may determine a relationship between a pruning number and weighted pruning loss associated with each output channel and each input channel of each hidden layer. Also, the group compression module 2210 may determine a contribution between each pair of input channels and output channels of each hidden layer based on the relationship between the pruning number and the weighted pruning loss associated with each output channel and each input channel of each hidden layer.
According to an embodiment of the disclosure, the group compression module 2210 may determine a splitting manner with minimum loss for splitting the channels to be grouped into two groups of sub-channels. The channel to be grouped is a channel of each set hidden layer or at least any one of sub-channels after grouping.
According to an embodiment of the disclosure, the group compression module 2210 may determine a splitting manner with minimum loss for splitting the channels to be grouped into two group of sub-channels through the graph theory algorithm.
According to an embodiment of the disclosure, the group compression module 2200 further comprises a fine-tuning module, configured to select a model to be compressed as a learning model, and perform the fine-tuning process on the group compressed model.
It can be clearly understood by those skilled in the art that the implementation principle and produced technical effects of the group compression module 2200 for a model provided by the embodiments of the disclosure are similar to the previous method embodiments. The embodiments of the disclosure for the previous method embodiments may be applied to the embodiments of the group compression module 2200, and the embodiments of the disclosure for the group compression module 22 may be applied to the embodiments of the disclosure for the method. For the convenience and brevity of the description, where the device embodiment is not mentioned, reference may be made to the above method embodiments, and details are not described herein again.
The embodiments may be implemented in a software program including instructions stored in a computer-readable storage medium. The computer-readable storage medium may store computer instructions, when operated on a computer, enable the computer to execute the corresponding content in the foregoing method embodiments.
The computer is a device capable of calling the stored instructions from the storage medium and operating according to the embodiments in accordance with the called instructions, and may include the electronic according to the embodiments.
The computer-readable storage medium may be provided in the form of a non-transitory storage medium. Here, the term ‘non-transitory’ means that the storage medium is tangible and does not refer to a transitory electrical signal, but does not distinguish that data is stored semi-permanently or temporarily on the storage medium.
Furthermore, the electronic device and the method for compressing a machine learning model according to the embodiments may be provided in a computer program product. The computer program product may be traded between a seller and a purchaser as a commodity.
The computer program product may include a software program and a computer-readable storage medium having stored thereon the software program. For example, the computer program product may include a product (e.g. a downloadable application) in a software program distributed electronically through a manufacturer of the electronic device or an electronic market (e.g., Google Play Store and App Store). For electronic distribution, at least a part of the software program may be stored on the storage medium or may be generated temporarily. In this case, the storage medium may be a storage medium of a server of the manufacturer, a server of the electronic market, or a relay server for temporarily storing the software program.
The computer program product may include a storage medium of a server or a storage medium of a terminal, in a system including the server and the terminal (e.g., the ultrasound diagnosis apparatus). Alternatively, when there is a third device (e.g., a smartphone) that communicates with the server or the terminal, the computer program product may include a storage medium of the third device. Alternatively, the computer program product may include a software program that is transmitted from the server to the terminal or the third device or from the third device to the terminal.
In this case, one of the server, the terminal, and the third device may perform the method according to the embodiments by executing the computer program product. Alternatively, at least two of the server, the terminal, and the third device may divide and perform the method according to the embodiments by executing the computer program product.
For example, the server (e.g., a cloud server, an Al server, or the like) may execute the computer program product stored in the server, thereby controlling the terminal to perform the method according to the embodiments, the terminal communicating with the server.
As another example, the third device may execute the computer program product, thereby controlling the terminal to perform the method according to the embodiments, the terminal communicating with the terminal.
As another example, the third device may directly perform, by executing the computer program product, the method according to the embodiments based on a value input from an auxiliary device (e.g., a probe of a medical apparatus).
When the third device executes the computer program product, the third device may download the computer program product from the server, and may execute the downloaded computer program product. Alternatively, the third device may perform the method according to the embodiments by executing a pre-loaded computer program product.
It should be understood that although the various steps in the flowchart of the drawings are sequentially displayed as indicated by the arrows, these operations are not necessarily performed in the order indicated by the arrows. Except as explicitly stated herein, the execution of these operations is not strictly limited, and may be performed in other sequences. Moreover, at least some of the operations in the flowcharts of the drawings may include multiple sub-operations or stages, which are not necessarily performed at the same time, but may be executed at different times, and the order of execution thereof is not necessarily to be performed sequentially, but may be performed alternately or alternately with at least a portion of the sub-operations or stages of other operations or other operations. The above is only a part of the embodiments of the disclosure, and it should be noted that those skilled in the art may also make several improvements and retouching without departing from the principles of the disclosure. It should be considered as the scope of protection of the disclosure.
Number | Date | Country | Kind |
---|---|---|---|
201910228917.X | Mar 2019 | CN | national |