Quantification method and device for online handwritten character recognition model and storage medium

By inserting a quantizer into the deep neural network model and performing quantization processing and reconstruction optimization, the problem of large number of model parameters and high number of computing on edge devices is solved, and high performance and acceleration effects on low-bit wide devices are achieved.

CN119106714APending Publication Date: 2024-12-10ANHUI UNIVERSITY OF TECHNOLOGY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411215583.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-02
Publication Date
2024-12-10

AI Technical Summary

Technical Problem

When deploying on edge devices, it is difficult to fully utilize performance due to large parameters and high computing volume. The existing model compression and inference acceleration methods have problems such as loss of information and high computing resources, and cannot effectively adapt to low-level wide devices. .

Method used

Insert a quantizer into the pre-trained convolutional neural network model, quantize the weights and activation values ​​of the convolutional layer and fully connected layer, and optimize the quantization model through layer-by-layer or block-by-block reconstruction method to reduce the quantization error , generate the target model.

Benefits of technology

While reducing the bit accuracy of neural network models, maintain high performance, accelerate the network inference process, and adapt to different hardware conditions, providing effective model compression and inference acceleration methods.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119106714A_ABST
    Figure CN119106714A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a quantization method and device for an online handwritten character recognition model and a storage medium, and the method comprises the steps: inserting a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, performing quantization processing on the weight and the input activation value of each convolutional layer and full connection layer in the convolutional neural network model to generate a quantization model; and reconstructing the quantitative model by adopting a layer-by-layer reconstruction and / or block-by-block reconstruction method, calculating loss through multiple iterations, and gradually optimizing the quantitative model until a target model is generated. According to the embodiment of the invention, high performance can be maintained while the bit precision of the neural network model is reduced, the reasoning process of the network is accelerated, in addition, the bit width can be reduced, and an effective way is provided for the deep neural network to adapt to different hardware conditions.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a quantization method, device and storage medium for an online handwritten character recognition model. Background Art

[0002] In recent years, deep neural networks have been widely used in computer vision, image classification, object detection and recognition, natural language processing and other fields, becoming an important technology for solving these tasks. Due to its success and wide applicability, improving the efficiency and practicality of neural networks has become an important research topic.

[0003] However, while achieving high performance indicators, the parameter scale of neural network models is also becoming larger, which makes them difficult to deploy on heterogeneous devices such as smartphones, IoT applications, and even dedicated low-power hardware. For example, in image classification tasks, the CoCa model achieved a classification accuracy of 91.0% on the ImageNet dataset, but its parameters were as high as 2.1 billion; the parameters of the Transformer, which performed well in natural language processing tasks, also reached hundreds of millions. In addition, even the lightest ResNet-18 in the ResNet series has 11.69 million parameters and 182 million multiplication-add-accumulation operations (MACs). In edge devices that are extremely short of storage and computing resources, existing complex models cannot fully exert their performance. The high number of model parameters and large computing scale have become one of the technical bottlenecks that limit the practical application of deep learning models. Therefore, model compression and inference acceleration have become a research direction that has attracted much attention.

[0004] Currently popular methods mainly include: (1) Pruning: solving the problem of model over-parameterization by removing the parts of the model that have little impact on performance; (2) Knowledge Distillation: using a pre-trained larger-scale network model as the teacher network, and guiding the training of a small-scale network model by designing a specific loss function, so that the performance of the student network can be comparable to that of the teacher network; (3) Neural Architecture Search: automatically designing the neural network structure through an algorithm to find a better lightweight network model structure.

[0005] Although the above technologies have made significant progress in reducing model size and accelerating operations, they also have some shortcomings. Pruning techniques usually require complex hyperparameter adjustments, and may introduce irreversible information loss during the pruning process, limiting the model's recovery ability. Knowledge distillation requires additional design of the student network structure, which requires high model design experience. Neural architecture search consumes a lot of time and computing resources during the search process. Pruning, knowledge distillation, and neural architecture search cannot enable the model to be deployed on low-bitwidth devices.

[0006] Therefore, the prior art has defects and needs to be improved. Summary of the invention

[0007] The embodiments of the present application provide a quantization method, device and storage medium for an online handwritten character recognition model, which can reduce the bit accuracy of the neural network model while maintaining high performance and accelerate the network's reasoning process. In addition, it can also reduce the bit width, providing an effective way for deep neural networks to adapt to different hardware conditions.

[0008] The present application embodiment provides a quantization method for an online handwritten character recognition model, the method comprising:

[0009] Inserting a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, quantizing the weights and input activation values ​​of each convolutional layer and fully connected layer in the convolutional neural network model, and generating a quantized model;

[0010] The quantization model is reconstructed by adopting a layer-by-layer reconstruction and / or a block-by-block reconstruction method, and the loss is calculated through multiple iterations to gradually optimize the quantization model until a target model is generated.

[0011] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, the quantization model is reconstructed by a layer-by-layer reconstruction and / or a block-by-block reconstruction method, and the loss is calculated through multiple iterations to gradually optimize the quantization model until a target model is generated, including:

[0012] Randomly extracting a first preset number of samples from the training set as a calibration set, and using the calibration set to perform predictions through a full-precision model to obtain prediction values ​​for each type of samples in the calibration set, and generating a full-precision prediction result, wherein the full-precision model is the pre-trained convolutional neural network model;

[0013] In each iteration, randomly extracting a second preset number of samples from the calibration set, predicting the second number of samples by using the quantization model to obtain prediction values ​​of each type of samples in the second number of samples, and generating quantization model prediction results;

[0014] The loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result is calculated, and the target model is generated by back-propagating and updating the parameters of the quantization model.

[0015] In the quantization method for an online handwritten character recognition model described in the embodiment of the present application, after generating the full-precision prediction result, the method further includes:

[0016] Calculating the mean vector of each class of samples in the calibration set to generate a class mean vector of each class of samples;

[0017] The calculating the loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result includes:

[0018] The Euclidean distance between the quantized prediction result and the class mean vector of the corresponding class is calculated, and the distance is multiplied by a preset coefficient as the class loss to generate the loss between the quantized model prediction result and the corresponding prediction result in the full-precision prediction result.

[0019] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, before inserting a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, the method further includes:

[0020] The batch normalization layer in the pre-trained convolutional neural network model is merged into the previous convolutional layer.

[0021] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, the weights are quantized channel by channel, and the activation values ​​are quantized layer by layer.

[0022] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, the quantization process is uniform quantization or non-uniform quantization.

[0023] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, the quantization process is symmetric quantization or asymmetric quantization.

[0024] In the quantization method for an online handwritten character recognition model described in an embodiment of the present application, the stepwise optimization of the quantization model includes:

[0025] The quantization parameters of the quantization model are gradually optimized, wherein the optimization method is quantization-aware training or post-training quantization.

[0026] An embodiment of the present application also provides a quantization device for an online handwritten character recognition model, the device comprising a processor and a memory, the memory storing a computer program, and the processor calling the computer program stored in the memory to execute the quantization method for an online handwritten character recognition model described in any of the above embodiments.

[0027] An embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is run on a computer, the computer executes the quantization method for an online handwritten character recognition model described in any of the above embodiments.

[0028] The quantization method for the online handwritten character recognition model provided in the embodiment of the present application first inserts a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, quantizes the weights and input activation values ​​of each convolutional layer and fully connected layer in the convolutional neural network model, and generates a quantized model. However, although the quantized model has low bit precision, it may have a large gap in task performance compared with the original full-precision model. Therefore, the quantized model is then optimized through calibration processing, and the quantized model is reconstructed using a layer-by-layer and / or block-by-block reconstruction method to optimize the model parameters to gradually reduce the quantization error, thereby achieving local optimality. Through this application, it is possible to maintain high performance and accelerate the reasoning process of the network while reducing the bit precision of the neural network model. In addition, the bit width can be reduced, providing an effective way for deep neural networks to adapt to different hardware conditions. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application, and those skilled in the art can obtain other drawings based on these drawings without creative work.

[0030] Figure 1 A flowchart of a quantization method for an online handwritten character recognition model provided in an embodiment of the present application.

[0031] Figure 2 A schematic diagram of uniform quantization provided in an embodiment of the present application.

[0032] Figure 3 A schematic diagram of non-uniform quantization provided in an embodiment of the present application.

[0033] Figure 4 A schematic diagram of symmetric quantization mapping provided in an embodiment of the present application.

[0034] Figure 5 A schematic diagram of asymmetric quantization mapping provided in an embodiment of the present application.

[0035] Figure 6 A schematic diagram of the working principle of the convolutional layer provided in an embodiment of the present application.

[0036] Figure 7 A schematic diagram of quantization-aware training and post-training quantization provided in an embodiment of the present application.

[0037] Figure 8 A schematic diagram of batch normalization layer merging provided in an embodiment of the present application.

[0038] Fig. 9 A schematic diagram of an 8-bit quantization simulation in a convolutional layer provided in an embodiment of the present application.

[0039] Fig.10 A schematic diagram of the actual deployment of 8-bit quantization in the convolutional layer provided in an embodiment of the present application.

[0040] Fig.11 Schematic diagram of the end-to-end convolutional neural network structure provided in an embodiment of the present application.

[0041] Fig.12 A schematic diagram of the residual convolution block structure provided in an embodiment of the present application.

[0042] Fig.13 This is an example diagram of IAHCC-UCAS2016 handwritten Chinese characters provided in an embodiment of the present application.

[0043] Fig.14 A schematic diagram of the loss change trend of the model provided in the embodiment of the present application during the training and testing stages.

[0044] Fig.15 A schematic diagram of the accuracy change trend of the model provided in the embodiment of the present application during the training and testing stages.

[0045] Fig.16 A schematic diagram of the structure of a quantization device for an online handwritten character recognition model provided in an embodiment of the present application. DETAILED DESCRIPTION

[0046] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.

[0047] The embodiment of the present application provides a quantization method for an online handwritten character recognition model, and the quantization method for an online handwritten character recognition model can be applied to an electronic device, which can be a computer or other device.

[0048] See also Figure 1 , Figure 1 A flow chart of a quantization method for an online handwritten character recognition model provided in an embodiment of the present application. The quantization method for an online handwritten character recognition model is applied in an electronic device, and the method may include the following steps:

[0049] Step 101, inserting a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, quantizing the weights and input activation values ​​of each convolutional layer and fully connected layer in the convolutional neural network model, and generating a quantized model.

[0050] The pre-trained convolutional neural network model is a pre-trained online handwritten character recognition model.

[0051] Among them, quantization: compresses and accelerates deep neural networks by reducing the bit width of the network model's calculation values, making them suitable for deployment on resource-constrained, low-bit-width edge devices.

[0052] Among them, quantization technology can reduce model parameters, improve computing speed, and reduce bit width, providing an effective way for deep neural networks to adapt to different hardware conditions. The following is an introduction to quantization:

[0053] 1. Basic concepts of quantification

[0054] (1) Problem definition

[0055] Assume a deep neural network with L layers and learnable parameters G = {W1, W2, ..., W L}, where W i Represents the weight tensor of the i-th layer of the network. In the supervised learning task, n training samples are used to perform a prediction mapping on the training dataset Γ. The mapping is from the feature space η to the label space δ, where each sample point is represented by (x i ,y i )∈η×δ. The goal is to learn a classifier G that has the minimum empirical loss on the training data of this mapping:

[0056]

[0057] Among them, l(.;.,.) is the loss function of each sample data (for example, mean square error or cross entropy loss), and N represents the total number of samples. When the network model is fully trained to convergence, its parameters G are usually stored in single-precision floating point numbers (FP32) and are highly sparse. Based on these facts, it is particularly necessary to quantize these parameters to lower bit precision (for example, 4-bit, 2-bit).

[0058] Generally speaking, the quantization problem of a pre-trained neural network model can be defined as the following optimization problem:

[0059] For the neural network model G, assume that it contains l parameter layers that need to be quantized, and the weights in all parameter layers form a set W = {w1,…,w l}; At the same time, it contains m activation values ​​that need to be quantized, and all activation values ​​form a set A = {a1,…,a m Given a set of quantization mappings corresponding to all weights and activations F = {q1(w1;b1),…,q l+m (a m ; b l+m )}, where b represents the bit width of the low-precision value after quantization mapping. This set of quantization mappings is inserted into a specific position in the model G to form a quantization model Q. This set of quantization mappings F is optimized to minimize the original task loss function L corresponding to the quantization model Q, that is:

[0060]

[0061] (2) Uniform quantization and non-uniform quantization

[0062] First, we define a quantizer Δ, which quantizes the weights and activation values ​​of the neural network model into a finite set of discrete values. Assume that the weight parameters of the convolutional layer are represented by a 4D tensor Indicates that C out and C in Denote the number of output channels and input channels respectively, and K is the size of the convolution kernel. The 4D tensor W is expanded into a weight tensor w, and the quantized weights It can be expressed as:

[0063]

[0064] Among them, Clamp(.,.,.) is a range clamping function used to truncate the rounded value to a restricted integer set [α,β], which is determined by the target bit width b of the quantization mapping. For linear quantization, the range of b-bit signed integers is [-2 b-1 ,2 b-1 -1], while the range of unsigned integers is [0,2b -1]. After truncation, each element of the weight w is projected onto the quantization level Δ(α, β, b).

[0065] According to the quantization type adjusted by the step size, the quantization method can be divided into uniform quantization and non-uniform quantization. The function images are as follows: Figure 2 and Figure 3 As shown. Uniform quantization forms a uniform step function within the representation range, that is, the step size and range of each quantization level are equal. Non-uniform quantization, on the other hand, has a non-uniform distribution in the range and height of the step function, that is, the step size and range between different quantization levels are not equal.

[0066] For uniform quantization (assuming here that -α=β), the quantization weights should come from the following set:

[0067]

[0068] For each floating-point number, uniform quantization maps it to a b-bit fixed-point number in Δ(α, β, b). The process mainly involves three quantization parameters: scaling factor s, zero point (offset z), and bit width b. The scaling factor is usually expressed as a floating-point number and is used to determine the step size of the quantizer, that is, the interval between each quantization level. The zero point is an integer that is used to ensure that real zero can be quantized without error, which helps to reduce quantization errors caused by operations such as zero padding or ReLU. The bit width determines the accuracy and range of the quantized representation, that is, the number of discrete levels.

[0069] A complete quantization process usually includes two steps: quantization and dequantization, which are expressed as follows:

[0070] Quantize:

[0071]

[0072] Among them, "·" is the round-to-nearest integer operator, q min and q max are the minimum and maximum values ​​of the quantized integer range respectively. This step maps the weights or activation values ​​to the target integer range [q min ,q max ]Inside.

[0073] Dequantize:

[0074]

[0075] In this step, the quantized integer value X is converted to int Dequantize back to floating point Combining the two steps of quantization above, a general definition of the quantization function can be provided:

[0076]

[0077] Uniform quantization divides the entire value range equally, which may lead to insufficient quantization accuracy in the concentrated part, thus affecting model performance. In contrast, non-uniform quantization uses smaller quantization intervals in the concentrated part to improve accuracy; it uses larger intervals in the range of larger values, thereby better maintaining model performance.

[0078] By dividing the true value with several non-uniform steps, we can get the corresponding segmentation point set v = {δ1,…,δ 2b+1}, and its quantization function is defined as follows:

[0079]

[0080] Among them, q i Represents the corresponding fixed-point number in discrete quantization. i The value of is in the fixed point number δ i and δ i+1 When the quantizer Δ projects it to the corresponding quantized value q i .

[0081] PoT (Power-of-Two) is a non-uniform quantization method with exponential quantization levels, which converts floating-point weights into discrete powers of two or zero. Specifically, the b-bit quantization weights should come from the following set:

[0082]

[0083] From 32-bit floating point weights w to PoT quantized weights The quantization function is:

[0084]

[0085] In order to simplify the description, h("w, α") is abbreviated as h, and its purpose is to map the values ​​in [-1, +1] to the range of [0, 1]. For example, h(x) = x / 2 + 0.5, and "w, α" is defined as:

[0086] 「w,α」=min(max(w,-α),α)#(11)

[0087] Since PoT (Power-of-Two) quantization can implement multiplication through shift operations, it has significant advantages in hardware implementation. Although non-uniform quantization has significantly improved model performance, its implementation complexity is relatively high, and it is difficult to obtain the expected benefits in actual hardware. Therefore, uniform quantization is still the most common and effective quantization implementation method.

[0088] In some embodiments, the quantization process is uniform quantization or non-uniform quantization.

[0089] Among them, the embodiments of the present application can select uniform quantization or non-uniform quantization according to actual conditions.

[0090] (3) Symmetric and asymmetric quantization

[0091] Quantization is a lossy mapping process, because there is a certain error ∈ between the dequantized value and the original true value, which is called the quantization error. During the forward propagation of the neural network, these shallow quantization errors will accumulate and amplify, which may eventually lead to a significant decrease in the accuracy of the model task.

[0092] Therefore, the strategy for determining the quantization scaling factor (or quantization step size) s is the key to quantizing the neural network model. A common choice of quantization scaling factor is as follows:

[0093]

[0094] Where p and n represent the lower and upper bounds of the true value range, respectively, also known as the quantization cutoff threshold. This value is determined based on the target bit width of the quantization mapping (i.e., bit width b) and the quantization error ∈, and is not necessarily the minimum and maximum values ​​of the true value.

[0095] In quantization, the process of selecting the truncation threshold is usually called calibration. According to the selection method of the truncation threshold, quantization can be divided into symmetric quantization and asymmetric quantization.

[0096] In symmetric quantization, the cutoff thresholds p and n are usually symmetric, that is, their absolute values ​​are equal, which means that the quantization range is symmetric about zero. For example, this approach is suitable for weight matrices with Gaussian or Laplace distributions, or activation functions with negative outputs. In symmetric quantization schemes, the quantization offset z should be set to 0.

[0097] In asymmetric quantization, the cutoff thresholds p and n are not necessarily symmetrical, that is, they can have different absolute values, so as to more flexibly adapt to the actual distribution of data. Asymmetric quantization may waste some values ​​in the value range, resulting in low information utilization. To solve this problem, asymmetric quantization introduces a zero point (offset) to adjust the scaled value to make the representation range more accurate.

[0098] The mapping methods of symmetric quantization and asymmetric quantization are as follows: Figure 4 and Figure 5 As shown, the floating point number f is quantized into an integer number I in each case by a corresponding method.

[0099] In summary, symmetric quantization has the advantages of being easy to implement and reducing zero-point storage overhead. However, in cases where the value distribution is skewed and asymmetric, symmetric quantization may lead to insufficient information utilization. In this case, asymmetric quantization is a better choice because it can more flexibly handle asymmetric weight or activation value distributions, thereby improving quantization accuracy.

[0100] In some embodiments, the quantization process is symmetric quantization or asymmetric quantization.

[0101] Among them, the embodiments of the present application can select symmetric quantization or asymmetric quantization according to actual conditions.

[0102] (4) Quantification granularity

[0103] Figure 6 This shows how the convolutional layer works. When the input data has 3 channels, the number of channels of the convolution kernel or filter should also be 3. Symbol Represents a dot product operation, the result of which is a feature map.

[0104] When quantizing the convolution filter weights, we may face the problem that the value range of each filter is different. To solve this problem, we can choose two strategies to deal with the scaling factor s and zero points z: scaling each filter independently or sharing scaling factors and zero points.

[0105] In convolutional neural networks, convolution filters of different channels may have significantly different ranges, which makes it difficult for a single quantization strategy to adapt to all scenarios. Therefore, it is necessary to select a suitable quantization method based on the specific task, hardware limitations, and performance requirements. According to the definition granularity of the truncation range [α, β], the quantization method can be divided into layer-by-layer quantization and channel-by-channel quantization.

[0106] Layer-wise Quantization is a simple quantization method that uses the same cutoff range for the entire network layer. Its implementation steps include analyzing the statistical information (such as minimum and maximum values) of all convolutional filters in the current layer, and then setting a unified cutoff threshold for the layer. The advantages are simple implementation and high computational efficiency, which is suitable for scenarios with limited computing resources. However, when the filter ranges in the same layer vary greatly, this method may reduce the quantization accuracy, thereby affecting the model performance.

[0107] Channel-wise Quantization defines the cutoff range for the convolution filter of each output channel separately. It needs to analyze the filter weights of each channel separately and set an independent cutoff threshold for each channel. This method can capture the feature distribution of each channel more accurately and usually provides better accuracy, especially when the weight distribution between channels is significantly different. However, the implementation of channel-wise quantization is more complicated and may increase the computational and storage overhead, so a trade-off needs to be made between the accuracy improvement and the additional overhead.

[0108] In some embodiments, the weights are quantized channel by channel, and the activation values ​​are quantized layer by layer.

[0109] Step 102, reconstructing the quantization model by layer-by-layer reconstruction and / or block-by-block reconstruction, calculating the loss through multiple iterations, and gradually optimizing the quantization model until a target model is generated.

[0110] Among them, the target model is a model that meets the requirements and is obtained by continuously optimizing the quantitative model.

[0111] Among them, although the quantized model has low bit precision, there may be a large gap between its task performance and the original full-precision model. Therefore, this application optimizes the quantized model through calibration processing, and reconstructs the quantized model using a layer-by-layer and / or block-by-block reconstruction method to optimize the model parameters to gradually reduce the quantization error, thereby achieving local optimality. Through this application, it is possible to reduce the bit precision of the neural network model while maintaining high performance and accelerating the network's reasoning process.

[0112] In some embodiments, the layer-by-layer and / or block-by-block reconstruction method is used to reconstruct the quantization model, and the loss is calculated through multiple iterations to gradually optimize the quantization model until a target model is generated, including:

[0113] Randomly extracting a first preset number of samples from the training set as a calibration set, and using the calibration set to perform predictions through a full-precision model to obtain prediction values ​​for each type of samples in the calibration set, and generating a full-precision prediction result, wherein the full-precision model is the pre-trained convolutional neural network model;

[0114] In each iteration, randomly extracting a second preset number of samples from the calibration set, predicting the second number of samples by using the quantization model to obtain prediction values ​​of each type of samples in the second number of samples, and generating quantization model prediction results;

[0115] The loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result is calculated, and the target model is generated by back-propagating and updating the parameters of the quantization model.

[0116] In some embodiments, after generating the full-precision prediction result, the method further includes:

[0117] Calculating the mean vector of each class of samples in the calibration set to generate a class mean vector of each class of samples;

[0118] The calculating the loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result includes:

[0119] The Euclidean distance between the quantized prediction result and the class mean vector of the corresponding class is calculated, and the distance is multiplied by a preset coefficient as the class loss to generate the loss between the quantized model prediction result and the corresponding prediction result in the full-precision prediction result.

[0120] In some embodiments, the stepwise optimizing the quantization model comprises:

[0121] The quantization parameters of the quantization model are gradually optimized, wherein the optimization method is quantization-aware training or post-training quantization.

[0122] Among them, those skilled in the art can choose quantization-aware training or post-training quantization according to actual conditions.

[0123] 2. Introduction to model quantization algorithm

[0124] When quantizing a deep neural network model, it is inevitable that some information learned during training will be lost, resulting in quantization errors and affecting the performance of the model. Therefore, after selecting a quantization method and building a quantized model, it is necessary to optimize the quantization parameters or the parameters in the original network model to restore the performance of the model as much as possible. Currently, the optimization methods can be divided into two types: quantization-aware training (QAT) and post-training quantization (PTQ). The schematic diagrams of the two methods are shown in Figure 2. Figure 7 As shown:

[0125] (1) Quantization-aware training

[0126] The quantization error introduced by the quantization operation is similar to the noise interference of the original value, which may cause the model to deviate from the local optimal point where the training converges, resulting in performance degradation. As the quantization error propagates and accumulates in the network layer, the task loss usually increases.

[0127] To solve this problem, quantization operations can be introduced during model training so that the model can adapt to the quantized numerical representation. Quantization-aware training can significantly reduce quantization errors and improve the performance of quantized models by simulating quantization effects in forward propagation and optimizing quantization parameters in backpropagation.

[0128] (2) Post-training quantization

[0129] Although quantization-aware training can make the model close to the full-precision model in task performance, its high computational and time costs and high dependence on training datasets make it difficult to quickly deploy the model to actual scenarios.

[0130] In contrast, post-training quantization does not require retraining the model and is less dependent on the dataset, so it is more suitable for rapid deployment of quantized models. It is also usually simple to implement and fast, making it suitable for scenarios with limited computing resources.

[0131] It should be noted that quantization-aware training (QAT) performs quantization by inserting a pseudo quantizer during training and fine-tuning, while post-training quantization (PTQ) performs quantization by inserting an actual quantizer during the calibration phase of the quantization model. QAT maintains good performance in INT8 quantization scenarios for most network models, but may cause significant performance degradation when quantizing at low bit widths (such as 4-bit or 2-bit). In this case, the PTQ method may be more suitable.

[0132] In some embodiments, before inserting a quantizer for calculating quantization parameters into the pre-trained convolutional neural network model, the method further includes:

[0133] The batch normalization layer in the pre-trained convolutional neural network model is merged into the previous convolutional layer.

[0134] 3. Folding of batch normalization layers

[0135] The Batch Normalization (BN) layer has become a standard component of modern convolutional neural networks. It effectively solves problems such as overfitting and gradient explosion by normalizing the output features of linear layers (such as convolutional layers), and speeds up the convergence speed during training. Its calculation formula is:

[0136]

[0137] Among them, μ and σ 2 denote the mean and variance computed during training, ∈ is a small positive number used to prevent the denominator from being zero, while γ and β are the learnable scaling factors and bias parameters for each channel.

[0138] Suppose there is a convolution layer with a weight matrix w and a bias b, and an input x. Combining the convolution calculation with batch normalization (BN), i.e. Conv+BN, the whole process can be described as follows:

[0139]

[0140] make:

[0141]

[0142] We can get:

[0143] y=w'*x+b'#(17)

[0144] like Figure 8 As shown in Figure 1, after combining the convolution layer with the batch normalization (BN) layer, new weights w' and biases b' can be obtained.

[0145] During model inference, the computational overhead of the BN layer may lead to a decrease in inference speed. Therefore, the BN layer is usually folded into the previous convolutional layer to remove the BN operation. This approach not only reduces the additional scaling and offset computational overhead, but also avoids the problem of data movement and quantization of layer output.

[0146] 4. Quantitative simulation and actual quantification

[0147] In order to test the operation of the neural network model after quantization, the quantization behavior is usually simulated on the same hardware used for training, which is called quantization simulation. Its purpose is to approximate the fixed-point operation of the hardware through floating-point operations. Compared with quantization directly on the actual hardware, this method is more convenient.

[0148] For the quantization of each weight and activation, you first need to choose a quantization scheme. For example, asymmetric quantization may have better expressiveness due to the introduction of additional offsets, but it also increases the computational overhead. The following takes the convolution layer as an example to describe its quantized weight matrix And the input feature matrix The product process:

[0149]

[0150] Among them, Y represents the output of the convolutional layer or the activation value of the next layer. In order to perform quantization tests on dedicated hardware (such as GPU), full-precision floating-point numbers (FP32) are usually used to simulate the quantization process. Although and represent quantized values, the FP32 format is still used in the actual storage and calculation process. However, in the actual hardware inference acceleration process, the quantized model only uses integers for arithmetic reasoning (Integer-Arithmetic-Only Inference) and performs accumulation operations at INT32 precision. That is:

[0151]

[0152] Among them, Q sIt means that before the convolution calculation, the weight W and the input feature value X are quantized and dequantized. In this process, the calculation results and biases are simulated in FP32 format, which is called quantization simulation. r In the actual deployment process, the weights and input feature values ​​are stored and calculated in fixed-point format, which is called actual quantization

[0153] Fig. 9 and Fig.10 These two figures show the simulation and actual deployment process of 8-bit quantization of the convolution layer respectively. The former shows the process of quantization after convolution operation in floating-point format. The latter shows the steps of directly using 8-bit integers for convolution operation and quantization in actual deployment.

[0154] 5. Analysis of experimental results

[0155] Based on the detailed introduction of quantization theory above, this section will focus on the experimental part, including the configuration of the experimental environment, the selection of models and datasets, the details of model training, and the specific quantization implementation methods. These steps are aimed at verifying the theory and evaluating the impact of different quantization strategies on model performance through experiments.

[0156] (1) Experimental environment

[0157] In subsequent experiments, training, testing, and quantization were all performed under the same configuration: Intel(R) Xeon(R) Silver4210R CPU@2.40GHz, 251G memory, NVIDIARTXA5000 graphics card, Ubuntu20.04.5LTS operating system, and PyTorch deep learning open source framework.

[0158] (2) Model selection

[0159] The network model used in this experiment is the end-to-end convolutional neural network (End-to-End CNN, referred to as ETECNN), whose structure is as follows Fig.11 As shown. x represents the input handwritten Chinese character data, This means that Pad expansion is performed before the convolution operation, and batch normalization (BN) and PReLU activation are performed sequentially after the convolution; similarly, MaxPool ◇ Indicates that Pad expansion is performed before the maximum pooling. Block:64 indicates that the number of output channels of each convolution block is 64. Fig.12 Next, global average pooling is used to obtain a fixed-size feature vector, which is input into the fully connected layer (FC), and finally Softmax is used for classification.

[0160] (3) Dataset selection

[0161] IAHCC-UCAS2016 is an aerial handwritten Chinese character dataset, each sample is written by a single stroke and does not contain any upper and lower stroke information. The dataset contains samples from 500 different individuals, a total of 350,612 samples, covering 3811 Chinese character classes, with 115 samples in each class. The samples in each class are randomly assigned, with 80% as training sets and 20% as test sets. Fig.13 An example of handwriting Chinese characters in the air is shown.

[0162] (4) Model training

[0163] The above end-to-end convolutional neural network (ETECNN) was used to train the handwritten Chinese characters in the air dataset (IAHCC-UCAS2016). The Adam (Adaptive Moment Estimation) optimizer was used, and the initial learning rate was set to 0.001. In order to accelerate convergence and improve model performance, the learning rate was decayed to 10% of the original value every 25 epochs. The specific hyperparameter settings are shown in Table 1:

[0164] Table 1. Training hyperparameters

[0165]

[0166]

[0167] exist Fig.14 and Fig.15 The figure shows the loss and accuracy trends of the model in the training and testing phases based on the above training configuration. It can be clearly observed that as the training progresses, the loss curve gradually stabilizes, indicating that the model is gradually converging. In addition, during the training process, the model and its parameters with the highest test accuracy at the 32nd Epoch are saved, which lays a solid foundation for the subsequent model quantization steps.

[0168] (5) Specific quantitative implementation

[0169] In this application, the theoretical basis of the quantization method has been explained in detail in the previous article. For the experiment, the model that showed the highest test accuracy during the training process was first selected as the object of quantization. Specifically, the asymmetric quantization method in uniform quantization was used, and the quantized data was mapped to unsigned integers.

[0170] During the quantization process, different quantization granularities are used for weights and activation values: weights are quantized channel by channel to ensure that each channel can independently adapt to different dynamic ranges, thereby improving quantization accuracy; activation values ​​are quantized layer by layer, which not only simplifies the quantization operation but also reduces computational overhead. This design significantly reduces the demand for computing and storage resources while ensuring model performance.

[0171] Before implementing quantization, you need to have a deep understanding of reconstruction. The core of quantization is not just mapping floating point numbers (FP32) to integers (INT8) and then dequantizing to form a quantized model. The quantized model usually has a gap in task performance compared to the original full-precision model, that is, quantization error occurs. Therefore, the key to quantization is how to adjust the model through a small amount of calibration data (Calibration) so as to maintain performance as close to the original precision model as possible at a low bit width (such as 4-bit or 2-bit).

[0172] It is particularly important to note that the quantization of activation values ​​has the greatest impact on accuracy, so during the optimization process, special attention should be paid to the adjustment of the data area. Generally speaking, 1024 samples can be selected from the training set as the calibration set, and these samples can be input into the quantization model to update the maximum and minimum values ​​in the network to match the optimal quantization scaling factor and offset.

[0173] Despite the above steps, the calibrated quantization model may still have a certain performance gap with the original model. In this case, you can consider using block reconstruction technology to further improve the accuracy of the quantization model. The introduction of reconstruction technology aims to optimize the parameter balance in the model quantization process, which is mainly divided into layer-wise reconstruction and block-wise reconstruction.

[0174] Layer-by-layer reconstruction reduces generalization error by matching the output distribution layer by layer, similar to a regularization mechanism. However, it also has limitations because it ignores the complex interactions between layers, which may lead to the loss of the global optimal solution. To solve this problem, block-by-block reconstruction optimizes at an intermediate granularity, combining the advantages of layer-by-layer reconstruction in reducing generalization error while taking into account the interactions between multiple layers. This method not only retains the advantages of layer-by-layer reconstruction, but also avoids the overfitting problem that may be caused by global reconstruction, allowing the model to better adapt to different data distributions while maintaining accuracy.

[0175] The specific quantitative implementation steps are:

[0176] <1> First, train the model to convergence and monitor the test accuracy during training. Select the model with the highest test accuracy and its parameters. For the model in this application, its full precision (FP32) Top-1 accuracy is 96.3914;

[0177] <2> Load the model parameters into the network, named fp_model (full precision model) and qnn (quantized model) respectively, merge the batch normalization (BN) layer into the previous convolution layer, and insert the quantizer for calculating quantization related parameters for subsequent quantization processing;

[0178] <3> Randomly obtain 1024 samples and their corresponding labels from the training set as the calibration set. These samples will be used to calibrate the quantized model to ensure that the weights and activation values ​​can accurately reflect the feature distribution of the original model;

[0179] <4> Perform layer-by-layer / block reconstruction: For each convolutional layer or fully connected layer, a layer-by-layer reconstruction method is used for optimization. For more complex structures including residual blocks, a block-by-block reconstruction method is used for optimization. During the reconstruction process, the calibration set (1024 samples) is first used to predict through the full-precision model fp_model to obtain the predicted value of each class, and a full-precision prediction result of size is obtained;

[0180] <5> The reconstruction process is divided into 20,000 iterations. In each iteration, 32 samples are randomly selected from the calibration set. For the selected samples, the prediction results are calculated by the quantization model, and the corresponding full-precision prediction values ​​are obtained (according to the index of the sample). The loss between the prediction of the quantization model qnn and the prediction of the full-precision model is calculated, and the parameters of the quantization model are back-propagated and updated so that the quantization error gradually decreases. Through continuous iterations, the quantization model gradually approaches the prediction performance of the full-precision model during the reconstruction process, and finally converges.

[0181] <6> The method proposed in this patent is: after obtaining the full-precision prediction results of the calibration set, the mean vector of each class is calculated. i , check its occurrence count in 1024 samples. If class C i If there is only one sample in the calibration set, the mean vector of the class is the full-precision prediction value of the sample. i If there are only multiple samples (number Num) in the calibration set, the class mean is the sum of the full-precision prediction values ​​of all samples in the class divided by the number of samples, that is, the class mean is (∑C i ) / Num;

[0182] <7> In each iteration, the quantized prediction results of 32 randomly selected samples are obtained. The class mean vector of the class to which each sample belongs can be determined using the index of these samples. The Euclidean distance between the quantized prediction result and the mean vector of the corresponding class is calculated and multiplied by a coefficient as the class loss. The specific formula is as follows:

[0183]

[0184] Among them, q i is the quantitative prediction value of the i-th sample, μ i is the mean vector of the class to which the sample belongs, and N is the number of samples extracted. The experimental results are shown in Table 2 below:

[0185] Table 2 Quantitative experimental results

[0186]

[0187] All the above optional technical solutions can be arbitrarily combined to form optional embodiments of the present application, which will not be described one by one here.

[0188] In specific implementation, the present application is not limited by the execution order of the various steps described. If no conflict occurs, some steps can be performed in other orders or simultaneously.

[0189] As can be seen from the above, the quantization method for the online handwritten character recognition model provided by the embodiment of the present application first inserts a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, and quantizes the weights and input activation values ​​of each convolutional layer and fully connected layer in the convolutional neural network model to generate a quantized model. However, although the quantized model has low bit precision, it may have a large gap in task performance compared with the original full-precision model. Therefore, the quantized model is then optimized through calibration processing, and the quantized model is reconstructed using a layer-by-layer and / or block-by-block reconstruction method to optimize the model parameters to gradually reduce the quantization error, thereby achieving local optimality. Through this application, it is possible to reduce the bit precision of the neural network model while maintaining high performance and accelerating the reasoning process of the network. In addition, it is possible to reduce the bit width, providing an effective way for deep neural networks to adapt to different hardware conditions.

[0190] The embodiment of the present application also provides a quantization device for an online handwritten character recognition model, and the quantization device for an online handwritten character recognition model can be integrated in an electronic device, which can be a computer or other device.

[0191] See also Fig.16 , Fig.16A schematic diagram of the structure of a quantization device for an online handwritten character recognition model provided in an embodiment of the present application. The quantization device 30 for an online handwritten character recognition model includes a memory 120, one or more processors 180, and one or more applications, wherein the one or more applications are stored in the memory 120 and configured to be executed by the processor 180; the memory 120 can be used to store applications and data. The application stored in the memory 120 contains executable code. The application can be composed of various functional modules. The processor 180 executes various functional applications and data processing by running the application stored in the memory 120. In addition, the memory 120 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage device. Accordingly, the memory 120 may also include a memory controller to provide the processor 180 with access to the memory 120.

[0192] The processor 180 is the control center of the device, and uses various interfaces and lines to connect various parts of the entire terminal, and executes various functions of the device and processes data by running or executing applications stored in the memory 120, and calling data stored in the memory 120, so as to monitor the device as a whole. Optionally, the processor 180 may include one or more processing cores; preferably, the processor 180 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs.

[0193] Specifically in this embodiment, a computer program is stored in the memory 120 , and the processor 180 executes the quantization method for the online handwritten character recognition model described in any of the above embodiments by calling the computer program stored in the memory 120 .

[0194] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. When the computer program runs on a computer, the computer executes the quantization method for an online handwritten character recognition model described in any of the above embodiments.

[0195] It should be noted that, for the quantization method for online handwritten character recognition model described in the present application, ordinary testers in the field can understand that all or part of the process of implementing the quantization method for online handwritten character recognition model described in the embodiment of the present application can be completed by controlling the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, such as stored in the memory of the device, and executed by at least one processor in the device, and the execution process may include the process of the embodiment of the quantization method for online handwritten character recognition model. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM, Read Only Memory), a random access memory (RAM, Random Access Memory), etc.

[0196] The above is a detailed introduction to the quantization method, device and storage medium for the online handwritten character recognition model provided by the embodiment of the present application. This article uses specific examples to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core ideas of the present application; at the same time, for those skilled in the art, according to the ideas of the present application, there will be changes in the specific implementation methods and application scopes. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A quantization method for an online handwritten character recognition model, characterized in that: The method comprises: Inserting a quantizer for calculating quantization parameters into a pre-trained convolutional neural network model, quantizing the weights and input activation values ​​of each convolutional layer and fully connected layer in the convolutional neural network model, and generating a quantized model; The quantization model is reconstructed by adopting a layer-by-layer reconstruction and / or a block-by-block reconstruction method, and the loss is calculated through multiple iterations to gradually optimize the quantization model until a target model is generated.

2. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: The method of reconstructing the quantization model by layer-by-layer reconstruction and / or block-by-block reconstruction, and gradually optimizing the quantization model by calculating the loss through multiple iterations until a target model is generated, includes: Randomly extracting a first preset number of samples from the training set as a calibration set, and using the calibration set to perform predictions through a full-precision model to obtain prediction values ​​for each type of samples in the calibration set, and generating a full-precision prediction result, wherein the full-precision model is the pre-trained convolutional neural network model; In each iteration, randomly extracting a second preset number of samples from the calibration set, predicting the second number of samples by using the quantization model to obtain prediction values ​​of each type of samples in the second number of samples, and generating quantization model prediction results; The loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result is calculated, and the target model is generated by back-propagating and updating the parameters of the quantization model.

3. The quantization method for online handwritten character recognition model according to claim 2, characterized in that: After generating the full-precision prediction result, the method further includes: Calculating the mean vector of each class of samples in the calibration set to generate a class mean vector of each class of samples; The calculating the loss between the prediction result of the quantization model and the corresponding prediction result in the full-precision prediction result includes: The Euclidean distance between the quantized prediction result and the class mean vector of the corresponding class is calculated, and the distance is multiplied by a preset coefficient as the class loss to generate the loss between the quantized model prediction result and the corresponding prediction result in the full-precision prediction result.

4. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: Before inserting a quantizer for calculating quantization parameters into the pre-trained convolutional neural network model, the method further includes: The batch normalization layer in the pre-trained convolutional neural network model is merged into the previous convolutional layer.

5. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: The weights are quantized channel by channel, and the activation values ​​are quantized layer by layer.

6. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: The quantization process is uniform quantization or non-uniform quantization.

7. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: The quantization process is symmetric quantization or asymmetric quantization.

8. The quantization method for online handwritten character recognition model according to claim 1, characterized in that: The stepwise optimization of the quantization model comprises: The quantization parameters of the quantization model are gradually optimized, wherein the optimization method is quantization-aware training or post-training quantization.

9. A quantization device for an online handwritten character recognition model, characterized in that: The device includes a processor and a memory, wherein a computer program is stored in the memory, and the processor is used to execute the quantization method for an online handwritten character recognition model as described in any one of claims 1 to 7 by calling the computer program stored in the memory.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed on a computer, the computer is enabled to execute the quantization method for an online handwritten character recognition model according to any one of claims 1 to 7.