Model processing method based on device parameters and electronic device
By analyzing the space capacity and calculating the compression ratio on edge devices, reducing the convolutional layer structure solves the problem of deploying deep neural networks on edge devices, achieves efficient deployment and resource adaptation of deep learning models, and promotes the popularization of edge intelligence. .
Patent Information
- Application Number
- CN202110293414.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-03-18
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2041-03-18
AI Technical Summary
Deep neural networks are difficult to deploy on resource-constrained edge devices, mainly due to excessive computing and storage space requirements, making it difficult for existing technologies to be popularized at the ordinary user level.
By analyzing the space capacity of the target device and the space requirements of the model to be installed, the compression ratio is calculated, and the structure of the convolution layer is reduced based on this ratio, including extracting the convolution filter, determining the similarity parameters, sorting and deleting the ones with low similarity. Filter to achieve model compression and adapt to device resources.
It enables the deployment of deep learning models on resource-constrained devices, reduces storage space requirements, improves computing efficiency on edge devices, and promotes the popularization of artificial intelligence on edge devices.
Smart Images

Figure CN113033779B
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and in particular to a model processing method based on device parameters and an electronic device. Background Art
[0002] "Edge intelligence" refers to the application of artificial intelligence on edge devices so that artificial intelligence can be popularized in the real life of ordinary users. However, edge devices (such as Face ID on mobile phones, drones, decentralized autonomous driving systems, etc.) have limited computing and storage resources. As the demand for machine intelligence in the field of artificial intelligence increases, the structural design of neural networks becomes more complex, and the required computing power and storage space also increase significantly. This makes it difficult to deploy deep neural networks on edge devices. Therefore, current neural networks are still not deeply applied at the level of ordinary users. How to deploy deep learning models on resource-constrained edge devices is a technical problem that needs to be solved urgently. Summary of the invention
[0003] This application provides a model processing method based on device parameters, including:
[0004] Parsing the spatial capacity of the target device and the spatial capacity required by the target model to be installed, the target model comprising at least one convolutional layer;
[0005] In a case where the space capacity of the target device does not meet the space capacity required by the target model, determining a compression ratio of the target model based on the space capacity of the target device;
[0006] Based on the compression ratio, the structure of the convolution layer in the target model that meets the preset conditions is limited to achieve compression of the target model;
[0007] Install the compressed model into the target device.
[0008] In one embodiment, limiting the structure of the convolutional layer in the target model that meets the preset conditions based on the compression ratio includes:
[0009] Extracting convolution filters located in the same convolution layer in the target model;
[0010] Determining similarity parameters of convolution filters;
[0011] Selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter;
[0012] The convolution filter in the convolution layer that meets the preset conditions is deleted to achieve compression of the convolution layer in the target model.
[0013] In one embodiment, determining the similarity parameter of the convolution filter includes:
[0014] Determine the vectors corresponding to all convolution filters of the same convolutional layer;
[0015] Determine the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters in the same layer;
[0016] Take a weighted sum of the similarities between the vector corresponding to each filter and the vectors corresponding to all convolutional filters in the same layer;
[0017] The sum value obtained by performing the weighted summation is determined as the similarity parameter corresponding to each filter.
[0018] In one embodiment, determining the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer includes:
[0019] The similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is calculated according to the angle and vector length between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer;
[0020] When the angle between the vectors is greater than a preset angle, the similarity takes a negative value; when the angle between the vectors is less than a preset angle, the similarity takes a positive value.
[0021] In one embodiment, weighted summing of similarities between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is performed, including:
[0022] Determine a similarity matrix corresponding to the convolution filters located in the same convolution layer, wherein each element in the similarity matrix is used to represent a similarity value between a vector corresponding to the corresponding convolution filter and vectors corresponding to all convolution filters in the same layer;
[0023] Extract all similarity values from the similarity matrix, and assign a weight to each similarity value, wherein a first weight is assigned if the similarity value is positive, and a second weight is assigned if the similarity value is negative, and the first weight is greater than the second weight;
[0024] The weighted similarities are summed up.
[0025] In one embodiment, selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter includes:
[0026] Sorting the convolution filters of the same convolution layer according to the similarity parameters in descending order to form a sorted convolution filter queue;
[0027] A convolution filter whose sorting position meets a preset position is selected from the sorted convolution filter queue as a convolution filter that meets the preset conditions.
[0028] In one embodiment, selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter includes:
[0029] It is determined that the convolution filter whose similarity parameter is within the preset interval is the convolution filter that meets the preset condition.
[0030] In one embodiment, the space capacity of the target device is obtained in the following manner:
[0031] Acquire attribute parameters of the target model;
[0032] Determining the space capacity required by the target model according to the attribute parameters, wherein the space capacity required by the target model at least includes the storage space capacity and the cache space capacity required to install the target model;
[0033] The space capacity of the target device is obtained in the following way:
[0034] Obtaining a configuration parameter table corresponding to the target device;
[0035] The space capacity of the target device is acquired from the configuration parameter table, where the space capacity of the target device at least includes the storage space capacity and the cache space capacity of the target device.
[0036] In one embodiment, determining the compression ratio of the target model based on the space capacity of the target device includes:
[0037] determining a first ratio according to the storage space required by the target model and the storage space capacity of the target device;
[0038] determining a second ratio according to the cache space required by the target model and the cache space capacity of the target device;
[0039] A maximum value of the first ratio and the second ratio is determined as a compression ratio of the target model.
[0040] The present application also provides an electronic device, including:
[0041] A parsing module, used for parsing the spatial capacity of the target device and the spatial capacity required by the target model to be installed, wherein the target model includes at least one convolutional layer;
[0042] a determination module, configured to determine a compression ratio of the target model based on the space capacity of the target device if the space capacity of the target device does not meet the space capacity required by the target model;
[0043] A contraction module, used for contracting the structure of the convolution layer in the target model that meets the preset conditions based on the compression ratio, so as to achieve compression of the target model;
[0044] The installation module is used to install the compressed model into the target device.
[0045] The present application also provides an electronic device, including:
[0046] processor;
[0047] A memory for storing executable instructions in the processor;
[0048] The processor is configured to execute the model processing method based on device parameters provided in any of the above embodiments.
[0049] When the space capacity of the device on which the target model is installed does not meet the space capacity required by the target model, the compression ratio of the target model is automatically determined based on the device parameters, and the target model is compressed based on the compression ratio; the compressed model can be adapted to the space capacity of the target device, thereby achieving the purpose of deploying the deep learning model on resource-constrained devices. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 This is a flow chart of a model processing method based on device parameters in one embodiment of the present application;
[0051] Figure 2 This is a flow chart of a model processing method based on device parameters in another embodiment of the present application;
[0052] Figure 3 A block diagram of an electronic device in an embodiment of the present application;
[0053] Figure 4 This is a block diagram of an electronic device in another embodiment of the present application. DETAILED DESCRIPTION
[0054] Various aspects and features of the present application are described herein with reference to the accompanying drawings.
[0055] It should be understood that various modifications may be made to the embodiments of the present application. Therefore, the above description should not be considered as limiting, but only as an example of an embodiment. Other modifications within the scope and spirit of the present application will occur to those skilled in the art.
[0056] The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate embodiments of the present application and, together with the general description of the present application given above and the detailed description of the embodiments given below, serve to explain the principles of the present application.
[0057] These and other characteristics of the present application will become apparent from the following description of a preferred form of embodiment given as a non-limiting example with reference to the accompanying drawings.
[0058] It should also be understood that although the present application has been described with reference to some specific examples, those skilled in the art will be able to readily implement many other equivalent forms of the present application.
[0059] The above and other aspects, features and advantages of the present application will become more apparent in view of the following detailed description when taken in conjunction with the accompanying drawings.
[0060] Specific embodiments of the present application are described hereinafter with reference to the accompanying drawings; however, it should be understood that the embodiments applied for are merely examples of the present application, which may be implemented in a variety of ways. Well-known and / or repeated functions and structures are not described in detail to avoid unnecessary or redundant details that obscure the present application. Therefore, the specific structural and functional details applied for herein are not intended to be limiting, but merely serve as a basis and representative basis for the claims to teach those skilled in the art to use the present application in a variety of ways with substantially any suitable detailed structure.
[0061] This specification may use the phrases "in one embodiment," "in another embodiment," "in yet another embodiment," or "in other embodiments," all of which may refer to one or more of the same or different embodiments according to the present application.
[0062] Figure 1 This is a flow chart of a model processing method based on device parameters according to an embodiment of the present application, the method comprising the following steps S11-S14:
[0063] In step S11, the spatial capacity of the target device and the spatial capacity required by the target model to be installed are parsed, and the target model includes at least one convolutional layer;
[0064] Before installing a target model to be installed on a target device, the attribute parameters of the target model can be obtained from the device where the target model to be installed is currently located, and then the space capacity required by the target model can be obtained from the attribute parameters. When parsing the space capacity of the target device, the configuration parameter table of the target device can be obtained, and the space capacity of the target device can be obtained according to the configuration parameter table.
[0065] In step S12, when the space capacity of the target device does not meet the space capacity required by the target model, a compression ratio of the target model is determined based on the space capacity of the target device;
[0066] For example, the target device is an embedded development board, and the target model to be installed is a face detection model. After parsing according to the above step S11, it is determined that the file size of the target model is 237M, the required cache space size is 4045k, the central processing unit storage space is 512K, the graphics card storage space is 64K, the cache space is 576K; the external storage space is 9M; assuming that a target model (for example, a face detection model) needs to be run on the development board; the file size of the target model is 237M, and the required cache space size is 4045K.
[0067] When determining the compression ratio of the target model based on the space capacity of the target device, the first ratio is determined according to the storage space required by the target model and the storage space capacity of the target device; the second ratio is determined according to the cache space required by the target model and the cache space capacity of the target device; and the maximum value of the first ratio and the second ratio is determined to be the compression ratio of the target model. Specifically, the first ratio is calculated to be (237-9) / 237=0.962025; the second ratio is (4045-576) / 4045=0.857602. The maximum value of the first ratio and the second ratio is determined to be the compression ratio of the target model. Since the first ratio is greater than the second ratio, the compression ratio of the target model finally obtained is 0.962025.
[0068] In step S13, the structure of the convolutional layer in the target model that meets the preset conditions is limited based on the compression ratio to achieve compression of the target model;
[0069] In this step, the convolution filters in the convolution layer can be deleted, and when deleting, the convolution filters located in the same convolution layer in the target model are extracted; the similarity parameters of the convolution filters are determined; the convolution filters in the convolution layer that meet the preset conditions are selected according to the similarity parameters; the convolution filters in the convolution layer that meet the preset conditions are deleted to achieve compression of the convolution layer in the target model.
[0070] According to the characteristics of neural networks, filters with large similarity to other filters can effectively represent the remaining filters, while filters with small similarity to other filters can ensure the difference in features when convolving the input. Therefore, filters with large or small similarity need to be retained. On this basis, the convolution layer where the filters with similarity within a certain interval are located can be regarded as the convolution layer that meets the preset conditions. And the filters with similarity within a certain interval are regarded as filters that need to be eliminated. Of course, filters with similarity rankings within a certain interval can also be regarded as filters that need to be eliminated. This method of determining the convolution filters that need to be eliminated will be highlighted in the following examples.
[0071] In step S14, the compressed model is installed in the target device.
[0072] Of course, it should be noted that the situation in which the space capacity of the target device involved in the above step S12 does not meet the space capacity required by the target model is not limited to the above example, but it can also be that the size of various applications pre-configured and installed on the target device cannot exceed the set value. When the size of the target model to be installed exceeds the set value, it can also be regarded as a situation in which the space capacity of the target device does not meet the space capacity required by the target model; for another example, under normal circumstances, when the storage space occupied by the installed application exceeds one tenth of the total storage space of the target device, other configurations of the target device cannot meet the requirements of the changed application. Therefore, the situation in which the storage space occupied by the installed application exceeds one tenth of the total storage space of the target device can also be regarded as a situation in which the space capacity of the target device does not meet the space capacity required by the target model; for another example, the target device stores many applications, and the remaining space of the target device does not meet the space capacity required by the target model can be regarded as a situation in which the space capacity of the target device does not meet the space capacity required by the target model.
[0073] When determining the similarity parameters of the convolution filter, the vectors corresponding to all the convolution filters of the same convolution layer are determined; the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all the convolution filters of the same layer is calculated based on the angle and vector length between the vector corresponding to each convolution filter and the vectors corresponding to all the convolution filters of the same layer; wherein, when the angle between the vectors is greater than a preset angle, the similarity takes a negative value; when the angle between the vectors is less than a preset angle, the similarity takes a positive value; and the similarity matrix corresponding to the convolution filters located in the same convolution layer is determined.
[0074] Among them, each element in the similarity matrix is used to represent the similarity value between the vector corresponding to the corresponding convolution filter and the corresponding vectors of all convolution filters in the same layer.
[0075] Extract all similarity values from the similarity matrix and assign a weight to each similarity value, wherein a positive similarity value is assigned a first weight, a negative similarity value is assigned a second weight, and the first weight is greater than the second weight; calculate the sum of the weighted similarities; determine that the sum obtained by weighted summation is the similarity parameter corresponding to each filter. Specifically, the similarity parameter corresponding to each filter can be calculated by the following formula
[0076]
[0077] Among them, β is the first weight and α is the second weight.
[0078] For example, the present application can use the Tonimoto coefficient to calculate the similarity of two convolution filters. The Tonimoto coefficient considers both the angle of the vector and the length of the vector when calculating the vector. The similarity of the two vectors calculated according to the Tonimoto coefficient is the similarity parameter of the convolution filter. The specific calculation formula of the Tonimoto coefficient is as follows:
[0079]
[0080] where w i is the vector corresponding to the i-th convolution filter, w j is the vector corresponding to the jth convolution filter.
[0081] If there are 100 convolution filters in the i-th layer, one convolution filter outputs a feature map, and the goal is to crop 50 convolution filters.
[0082] According to the Tonimoto coefficient, the similarity between each convolution filter and the other 99 convolution filters is calculated, and a 100*100 similarity matrix is obtained. If the T similarity of the two convolution filter vectors is negative, the angle between them is between 90-180°, which is quite different and the similarity is low. Here we only consider the value of their angle, that is, the lengths of the two vectors are normalized to 1. When the absolute value of the Tonimoto coefficient becomes smaller and smaller, the angle between the two becomes smaller and smaller, and the similarity becomes larger and larger. The calculation formula is:
[0083]
[0084] The meaning of the above formula is that if the Tonimoto coefficient of two vectors is less than or equal to 0, the similarity of the two vectors is the Tonimoto coefficient of the normalized vectors; if the Tonimoto coefficient of the two vectors is greater than 0, the similarity of the two vectors is the Tonimoto coefficient of the original vector (before normalization).
[0085] According to the similarity matrix s and formula Calculate the sum of the similarities of each convolution filter and the other 99 convolution filters, where α<<β, s ij Represents the T similarity between the i-th convolution filter and the j-th convolution filter. The similarity matrix s can be expressed as follows:
[0086]
[0087] For example, the similarity of the first convolution filter to the other 99 convolution kernels is: And so on, we get 100 such similarities, and then arrange these 100 values from large to small, recorded as arrangement Q = [q1, q2, ..., q100], and then select 50 convolution kernels to be retained based on this arrangement.
[0088] The selection method is to first select a filter with the greatest similarity to other filters (the filter corresponding to the first value q1 in the arrangement Q). Such a filter can most effectively represent the remaining filters. Then select the filter with the greatest difference from other filters (the filter corresponding to the last value q100 in the arrangement Q). Such a filter is the filter with the greatest difference from other filters and can ensure the difference in the extracted features. Select the filter with the second smallest difference from other filters and the filter with the second largest difference from other filters in turn, until the number of selected filters is 50. The result of this selection is that some filters with low similarity and small difference from other filters are eliminated.
[0089] That is to say, during compression, the convolution filters of the same convolution layer can be sorted based on the similarity parameter to form a sorted convolution filter queue, and then 50 filters are selected from the middle as convolution filters that meet the preset conditions.
[0090] In one embodiment, Figure 2 As shown, the above step S13 can be implemented as the following steps S131-S134:
[0091] In step S131, extract the convolution filter located in the same convolution layer in the target model;
[0092] Typically, the target model contains multiple convolutional layers. After the compression ratio is determined, the convolutional filters of the same convolutional layer need to be extracted because the convolutional filters of the same layer need to be compressed based on similarity.
[0093] In step S132, a similarity parameter of the convolution filter is determined;
[0094] The similarity parameter can be the T similarity of the convolution filter. Assume that there are 100 filters in the convolution layer. After calculating the similarity of each convolution filter with itself and the other 99 convolution filters based on the Tonimoto coefficient, these similarities are then calculated according to the formula The sum obtained after weighted summation is the T similarity.
[0095] In step S133, a convolution filter in the convolution layer that meets the preset conditions is selected according to the similarity parameter;
[0096] For example, the compression ratio is 50%. Assuming that the model consists of only three convolutional layers, the number of convolutional filters of the three convolutional layers is 1000, 600 and 100 respectively. Usually, the convolutional filters of the same convolutional layer have the same size, and the filter sizes of different convolutional layers are different. Therefore, each convolutional layer can be selected to remove the corresponding number of convolutional filters according to the compression ratio, that is, the number of convolutional filters to be removed corresponding to the three convolutional layers is 500, 300 and 50 respectively. Specifically, the ranking of each convolutional filter can be determined based on the T similarity, and then the 500 convolutional filters ranked 251-750 in the first convolutional layer are determined to be convolutional filters that meet the preset conditions, the 300 convolutional filters ranked 151-450 in the second convolutional layer are determined to be convolutional filters that meet the preset conditions, and the 50 convolutional filters ranked 26-75 in the third convolutional layer are determined to be convolutional filters that meet the preset conditions.
[0097] In step S134, the convolution filters in the convolution layer that meet the preset conditions are deleted to achieve compression of the convolution layer in the target model.
[0098] Specifically, the ranking of each convolution filter can be determined based on the T similarity, and then 500 convolution filters ranked 251-750 in the first convolution layer are removed, 300 convolution filters ranked 151-450 in the second convolution layer are removed, and 50 convolution filters ranked 26-75 in the third convolution layer are removed.
[0099] For example, if the i-th layer has 100 convolution filters, one convolution filter outputs a feature map, and the goal is to crop 50 convolution filters.
[0100] According to the Tonimoto coefficient, the similarity between each convolution filter and the other 99 convolution filters is calculated, and a 100*100 similarity matrix s is obtained.
[0101] For example, the similarity of the first convolution filter to the other 99 convolution kernels is: And so on, we get 100 such similarities, and then arrange these 100 values from large to small, recorded as arrangement Q = [q1, q2, ..., q100], and then select 50 convolution kernels to be retained based on this arrangement.
[0102] The selection method is to first select a filter with the greatest similarity to other filters (the filter corresponding to the first value q1 in the arrangement Q). Such a filter can most effectively represent the remaining filters. Then select the filter with the greatest difference from other filters (the filter corresponding to the last value q100 in the arrangement Q). Such a filter is the filter with the greatest difference from other filters and can ensure the difference in the extracted features. Select the filter with the second smallest difference from other filters and the filter with the second largest difference from other filters in turn, until the number of selected filters is 50. The result of this selection is that some filters with low similarity and small difference from other filters are eliminated.
[0103] In one embodiment, the above step S132 may be implemented as the following steps A1-A4:
[0104] In step A1, vectors corresponding to all convolution filters of the same convolution layer are determined;
[0105] In step A2, determining the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer;
[0106] In step A3, a weighted sum is performed on the similarities between the vector corresponding to each filter and the vectors corresponding to all convolutional filters of the same layer;
[0107] In step A4, the sum value obtained by weighted summation is determined as the similarity parameter corresponding to each filter.
[0108] In this embodiment, the above example is still used to determine the vectors corresponding to the 100 convolution filters of the i-th layer, and the similarity between the vector corresponding to each convolution filter and itself and the other 99 convolution filters is determined as follows:
[0109] In one embodiment, the above step A2 can be implemented as the following steps:
[0110] The similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is calculated based on the angle and vector length between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer; when the angle between the vectors is greater than the preset angle, the similarity takes a negative value; when the angle between the vectors is less than the preset angle, the similarity takes a positive value.
[0111] Specifically, the preset angle can be 90 degrees. When the angle between the vectors is greater than 90 degrees, the similarity between the vectors is negative. When the angle between the vectors is less than 90 degrees, the similarity between the vectors is positive. When the angle between the vectors is equal to 90 degrees, the similarity between the vectors is C.
[0112] In one embodiment, the above step A3 can be implemented as the following steps B1-B3:
[0113] In step B1, a similarity matrix corresponding to the convolution filters located in the same convolution layer is determined, wherein each element in the similarity matrix is used to represent the similarity value between the vector corresponding to the corresponding convolution filter and the vectors corresponding to all convolution filters in the same layer;
[0114] In step B2, all similarity values are extracted from the similarity matrix, and a weight is assigned to each similarity value, wherein a first weight is assigned if the similarity value is positive, and a second weight is assigned if the similarity value is negative, and the first weight is greater than the second weight;
[0115] In step B3, the weighted similarities are summed up.
[0116] If the T similarity of the two convolution filter vectors is negative, the angle between them is between 90-180°, which is quite different and has low similarity. Here we only consider the value of their angle, that is, normalize the lengths of both vectors to 1. When the absolute value of the Tonimoto coefficient becomes smaller, the angle between them becomes smaller and smaller, and the similarity becomes larger. The calculation formula is:
[0117]
[0118] The meaning of the above formula is that if the Tonimoto coefficient of two vectors is less than or equal to 0, the similarity of the two vectors is the Tonimoto coefficient of the normalized vectors; if the Tonimoto coefficient of the two vectors is greater than 0, the similarity of the two vectors is the Tonimoto coefficient of the original vector (before normalization).
[0119] According to the similarity matrix s and formula Calculate the sum of the similarities of each convolution filter and the other 99 convolution filters, where α<<β, s ij Represents the T similarity between the i-th convolution filter and the j-th convolution filter. The similarity matrix s can be expressed as follows:
[0120]
[0121] In one embodiment, the above step S133 may be implemented as the following steps C1-C2:
[0122] In step C1, the convolution filters of the same convolution layer are sorted in descending order according to the similarity parameters to form a sorted convolution filter queue;
[0123] Still using the above example, assuming that the i-th layer filter has 100 convolution filters and 50 filters need to be deleted, then calculate the T similarity of each convolution filter, and then get the T similarities corresponding to the 100 convolution filters respectively, and sort these 100 filters according to the size of the T similarity.
[0124] In step C2, a convolution filter whose sorting position meets the preset position is selected from the sorted convolution filter queue as a convolution filter that meets the preset conditions.
[0125] In this step, the convolution filters with sorting positions of 26-75 in the sorting queue are selected as convolution filters that meet the preset conditions.
[0126] In one embodiment, the above step S133 may be implemented as the following steps:
[0127] It is determined that the convolution filter whose similarity parameter is within the preset interval is the convolution filter that meets the preset condition.
[0128] A filter with a large similarity parameter indicates that the filter has a large similarity with other filters. Such a filter can effectively represent the other filters and needs to be retained; while a filter with a small similarity parameter indicates that the filter has a small similarity with other filters. Such a filter is a filter with a large difference from other filters and can ensure the difference of the extracted features. Such a filter also needs to be retained. Therefore, a preset interval can be selected, and the convolution filters whose similarity parameters are within the preset interval are deleted as convolution filters that meet the preset conditions, thereby retaining filters with large similarity parameters and filters with small similarity parameters.
[0129] In one embodiment, the space capacity of the target device is obtained by following the steps D1-D2:
[0130] In step D1, the attribute parameters of the target model are obtained;
[0131] In step D2, the space capacity required by the target model is determined according to the attribute parameters, wherein the space capacity required by the target model at least includes the storage space capacity and the cache space capacity required for installing the target model;
[0132] The target device's space capacity is obtained through the following steps D3-D4:
[0133] In step D3, a configuration parameter table corresponding to the target device is obtained;
[0134] In step D4, the space capacity of the target device is obtained from the configuration parameter table, where the space capacity of the target device at least includes the storage space capacity and the cache space capacity of the target device.
[0135] In one embodiment, the above step S12 may be implemented as the following steps E1-E3:
[0136] In step E1, a first ratio is determined according to the storage space required by the target model and the storage space capacity of the target device;
[0137] For example, assuming that the target device is an embedded development board, the configuration parameter table of the embedded development board is as follows: the central processing unit storage space is 512K, the graphics card storage space is 64K, the cache space is 576K; the external storage space is 9M; assuming that a target model (for example, a face detection model) needs to be run on the development board; the file size of the target model is 237M, and the required cache space size is 4045K.
[0138] According to the storage space required by the target model and the storage space capacity of the target device, the first ratio is determined to be (237-9) / 237=0.962025;
[0139] When calculating the first ratio, it is not limited to simply considering the size of the storage space of the two. It is also necessary to consider whether storage space needs to be reserved and how much storage space needs to be reserved to ensure the storage of the input based on the type of model. For example, if the model is a video recognition system, then after installing the model, it is also necessary to consider that enough space needs to be reserved in the model to store the video to be recognized, such as reserving 500M of space; for another example, if the model is an image recognition system, then the input is an image, and it is necessary to consider reserving enough storage space to store the image to be recognized, such as reserving 100M of space; for another example, if the model is a speech recognition system, then the input is audio, and it is necessary to consider reserving enough storage space to store the sound to be recognized, for example, reserving 50M of space. That is, the types of inputs of different models are different. Therefore, based on the type of model, it is possible to consider how much spare space is needed after compression to store the input information of the model. Therefore, different types of models will also lead to different first ratios.
[0140] Face recognition system, image recognition system, voice recognition system, etc., then,
[0141] In step E2, a second ratio is determined according to the cache space required by the target model and the cache space capacity of the target device;
[0142] The second ratio is determined to be (4045-576) / 4045=0.857602 according to the cache space required by the target model and the cache space capacity of the target device;
[0143] In step E3 , the maximum value of the first ratio and the second ratio is determined as the compression ratio of the target model.
[0144] Since the first ratio is greater than the second ratio, the compression ratio of the target model is finally obtained to be 0.962025.
[0145] It should be noted that if only three significant digits after the decimal point are retained, in order to reserve sufficient space capacity for the target model, the compression ratio needs to be 0.963. That is, max[(237-9) / 237, (4045-576) / 4045] = 0.963. It should also be noted that, under normal circumstances, cache space is used to provide the model with the resources required for the model to run, and for many devices, cache space resources may also be occupied and allocated by multiple processes. Therefore, it is necessary to consider that the cache space allocated to the model will change dynamically. Therefore, sufficient cache space needs to be reserved. Specifically, the minimum value of the cache space that can be reserved for the target model when all applications are running can be calculated based on various applications in the device, and the compression ratio corresponding to the cache space can be determined based on the minimum value.
[0146] Figure 3 This is a block diagram of an electronic device according to an embodiment of the present application, and the electronic device includes the following modules:
[0147] A parsing module 31, used for parsing the spatial capacity of the target device and the spatial capacity required by the target model to be installed, the target model including at least one convolutional layer;
[0148] A determination module 32, configured to determine a compression ratio of the target model based on the space capacity of the target device when the space capacity of the target device does not meet the space capacity required by the target model;
[0149] A contraction module 33, used for contracting the structure of the convolution layer in the target model that meets the preset conditions based on the compression ratio, so as to achieve compression of the target model;
[0150] The installation module 34 is used to install the compressed model into the target device.
[0151] In one embodiment, Figure 4 As shown, the limit reduction module 33 includes:
[0152] An extraction submodule 41, used to extract convolution filters located in the same convolution layer in the target model;
[0153] A determination submodule 42, for determining a similarity parameter of a convolution filter;
[0154] A selection submodule 43 is used to select a convolution filter in the convolution layer that meets a preset condition according to a similarity parameter;
[0155] The deletion submodule 44 is used to delete the convolution filters in the convolution layer that meet the preset conditions, so as to achieve compression of the convolution layer in the target model.
[0156] In one embodiment, determining a submodule includes:
[0157] Determine the vectors corresponding to all convolution filters of the same convolutional layer;
[0158] Determine the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters in the same layer;
[0159] Take a weighted sum of the similarities between the vector corresponding to each filter and the vectors corresponding to all convolutional filters in the same layer;
[0160] The sum value obtained by weighted summation is determined as the similarity parameter corresponding to each filter.
[0161] In one embodiment, determining the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer includes:
[0162] The similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is calculated according to the angle and vector length between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer;
[0163] When the angle between the vectors is greater than a preset angle, the similarity takes a negative value; when the angle between the vectors is less than a preset angle, the similarity takes a positive value.
[0164] In one embodiment, weighted summing of similarities between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is performed, including:
[0165] Determine a similarity matrix corresponding to the convolution filters located in the same convolution layer, wherein each element in the similarity matrix is used to represent a similarity value between a vector corresponding to the corresponding convolution filter and vectors corresponding to all convolution filters in the same layer;
[0166] Extract all similarity values from the similarity matrix, and assign a weight to each similarity value, wherein a positive similarity value is assigned a first weight, a negative similarity value is assigned a second weight, and the first weight is greater than the second weight;
[0167] The weighted similarities are summed up.
[0168] In one embodiment, selecting a convolution filter in a convolution layer that meets a preset condition according to a similarity parameter includes:
[0169] The convolution filters of the same convolution layer are sorted in descending order according to the similarity parameters to form a sorted convolution filter queue;
[0170] A convolution filter whose sorting position meets a preset position is selected from the sorted convolution filter queue as a convolution filter that meets the preset conditions.
[0171] In one embodiment, selecting a convolution filter in a convolution layer that meets a preset condition according to a similarity parameter includes:
[0172] It is determined that the convolution filter whose similarity parameter is within the preset interval is the convolution filter that meets the preset condition.
[0173] In one embodiment, the space capacity of the target device is obtained in the following manner:
[0174] Get the attribute parameters of the target model;
[0175] Determine the space capacity required by the target model according to the attribute parameters, wherein the space capacity required by the target model at least includes the storage space capacity and the cache space capacity required to install the target model;
[0176] The target device's space capacity is obtained in the following ways:
[0177] Get the configuration parameter table corresponding to the target device;
[0178] The space capacity of the target device is obtained from the configuration parameter table, where the space capacity of the target device at least includes the storage space capacity and the cache space capacity of the target device.
[0179] In one embodiment, determining a compression ratio of a target model based on a space capacity of a target device includes:
[0180] determining a first ratio based on the storage space required by the target model and the storage space capacity of the target device;
[0181] determining a second ratio based on the cache space required by the target model and the cache space capacity of the target device;
[0182] A maximum value of the first ratio and the second ratio is determined as the compression ratio of the target model.
[0183] The present application also provides an electronic device, including:
[0184] processor;
[0185] a memory for storing instructions executable by the processor;
[0186] The processor is configured as follows:
[0187] Execute the model processing method based on device parameters provided in any of the above embodiments.
[0188] When the space capacity of the device on which the target model is installed does not meet the space capacity required by the target model, the compression ratio of the target model is automatically determined based on the device parameters, and the target model is compressed based on the compression ratio; the compressed model can be adapted to the space capacity of the target device, thereby achieving the purpose of deploying the deep learning model on resource-constrained devices.
[0189] The above embodiments are only exemplary embodiments of the present application and are not intended to limit the present application. The protection scope of the present application is defined by the claims. Those skilled in the art may make various modifications or equivalent substitutions to the present application within the essence and protection scope of the present application, and such modifications or equivalent substitutions shall also be deemed to fall within the protection scope of the present application.
Claims
1. A model processing method based on device parameters, comprising: Parsing the spatial capacity of the target device and the spatial capacity required by the target model to be installed, the target model comprising at least one convolutional layer; In a case where the space capacity of the target device does not meet the space capacity required by the target model, determining a compression ratio of the target model based on the space capacity of the target device; Based on the compression ratio, the structure of the convolution layer in the target model that meets the preset conditions is limited to achieve compression of the target model; Installing the compressed model into the target device; The space capacity of the target device at least includes the storage space capacity and the cache space capacity of the target device, and determining the compression ratio of the target model based on the space capacity of the target device includes: determining a first ratio according to the storage space required by the target model and the storage space capacity of the target device; determining a second ratio according to the cache space required by the target model and the cache space capacity of the target device; A maximum value of the first ratio and the second ratio is determined as a compression ratio of the target model.
2. The method according to claim 1, wherein the structure of the convolutional layer in the target model that meets the preset conditions is limited based on the compression ratio, comprising: Extracting convolution filters located in the same convolution layer in the target model; Determining similarity parameters of convolution filters; Selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter; The convolution filter in the convolution layer that meets the preset conditions is deleted to achieve compression of the convolution layer in the target model.
3. The method of claim 2, wherein determining the similarity parameter of the convolution filter comprises: Determine the vectors corresponding to all convolution filters of the same convolutional layer; Determine the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters in the same layer; Take a weighted sum of the similarities between the vector corresponding to each filter and the vectors corresponding to all convolutional filters in the same layer; The sum value obtained by performing the weighted summation is determined as the similarity parameter corresponding to each filter.
4. The method of claim 3, wherein determining the similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer comprises: The similarity between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer is calculated according to the angle and vector length between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer; When the angle between the vectors is greater than a preset angle, the similarity takes a negative value; when the angle between the vectors is less than a preset angle, the similarity takes a positive value.
5. The method as claimed in claim 3, wherein weighted summing is performed on the similarities between the vector corresponding to each convolution filter and the vectors corresponding to all convolution filters of the same layer, comprising: Determine a similarity matrix corresponding to the convolution filters located in the same convolution layer, wherein each element in the similarity matrix is used to represent a similarity value between a vector corresponding to the corresponding convolution filter and vectors corresponding to all convolution filters in the same layer; Extracting all similarity values from the similarity matrix and assigning a weight to each similarity value, wherein a first weight is assigned if the similarity value is positive, and a second weight is assigned if the similarity value is negative, and the first weight is greater than the second weight; The weighted similarities are summed up.
6. The method according to claim 2, wherein selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter comprises: Sorting the convolution filters of the same convolution layer according to the similarity parameters in descending order to form a sorted convolution filter queue; A convolution filter whose sorting position meets a preset position is selected from the sorted convolution filter queue as a convolution filter that meets the preset conditions.
7. The method according to claim 2, wherein selecting a convolution filter in the convolution layer that meets a preset condition according to the similarity parameter comprises: It is determined that the convolution filter whose similarity parameter is in the preset interval is the convolution filter that meets the preset condition.
8. The method according to claim 1, wherein the space capacity of the target device is obtained by: Acquire attribute parameters of the target model; Determine the space capacity required by the target model according to the attribute parameters, wherein: The space capacity required by the target model at least includes the storage space capacity and cache space capacity required to install the target model; The space capacity of the target device is obtained in the following way: Obtaining a configuration parameter table corresponding to the target device; The space capacity of the target device is obtained from the configuration parameter table.
9. An electronic device, comprising: A parsing module, used for parsing the spatial capacity of the target device and the spatial capacity required by the target model to be installed, wherein the target model includes at least one convolutional layer; a determination module, configured to determine a compression ratio of the target model based on the space capacity of the target device if the space capacity of the target device does not meet the space capacity required by the target model; A contraction module, used for contracting the structure of the convolution layer in the target model that meets the preset conditions based on the compression ratio, so as to achieve compression of the target model; An installation module, used to install the compressed model into the target device; The space capacity of the target device at least includes the storage space capacity and the cache space capacity of the target device, and determining the compression ratio of the target model based on the space capacity of the target device includes: determining a first ratio according to the storage space required by the target model and the storage space capacity of the target device; determining a second ratio according to the cache space required by the target model and the cache space capacity of the target device; A maximum value of the first ratio and the second ratio is determined as a compression ratio of the target model.
