Handwritten form recognition method, and handwritten form recognition model training method and device
Through the end-to-end handwriting recognition method, the image feature extraction layer, the fully connected layer and the Softmax layer model are used, combined with the multi-neighbor merging and high compression module, to solve the problem of limited handwriting recognition performance in the existing technology and achieve simplification The network structure and recognition accuracy are improved, and while reducing the amount of model parameters and calculations, the recognition accuracy is improved.
Patent Information
- Application Number
- PCT/CN2022/132268
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2022-11-16
- Publication Date
- 2024-09-12
AI Technical Summary
Existing handwriting recognition technology mostly uses a two-stage approach of text detection + recognition, which results in limited detection and recognition performance, and the process of data annotation and model training is cumbersome.
An end-to-end handwriting recognition method is proposed, which uses the model of image feature extraction layer, fully connected layer and softmax layer, and simplifies the network structure through multi-neighborhood merging and high compression module to realize the recognition of single or multi-line text, and in During the training process, single-line sample text images are used for model training, and a dynamic programming word automatic correction algorithm is used to improve the recognition accuracy.
It achieves the simplification of the network structure and the improvement of the recognition accuracy, reduces the amount of model parameters and calculations, and simplifies the data annotation process. It is suitable for the recognition of single or multi-line written text without significantly reducing the recognition accuracy. This significantly reduces the number of parameters and calculations in the model.
Smart Images

Figure CN2022132268_12092024_PF_FP
Abstract
Description
Handwriting recognition method, handwriting recognition model training method and device Technical Field
[0001] The embodiments of the present disclosure relate to, but are not limited to, the field of artificial intelligence technology, and in particular to a handwriting recognition method, a handwriting recognition model training method, and a device. Background Art
[0002] Currently, full-text handwriting recognition is mostly implemented using a two-stage approach of text detection + recognition. First, the trajectory of the text to be recognized is sent to the detection network to obtain the text position information, and then the position information is sent to the recognition network for text recognition. The overall recognition performance is largely limited by the detection performance of the detector. It requires data labeling and model training for detection and recognition respectively, and the implementation process is cumbersome.
[0003] Related technologies have proposed an end-to-end multi-row recognition network consisting of two stages: encoding and decoding. The encoding stage first uses a residual network to extract the first eigenvector, followed by a bidirectional long short-term memory (LSTM) network and an attention-based encoder to extract the second eigenvector. The decoding stage consists of two branches, performing row and column decoding, respectively, before outputting the recognition result. However, this multi-row recognition network has a relatively complex structure.
[0004] Summary of the Invention
[0005] The following is a summary of the subject matter described in detail herein. This summary is not intended to limit the scope of the claims.
[0006] The present disclosure provides a handwriting recognition method, including:
[0007] Determining an input text image according to a written text trajectory to be recognized;
[0008] Inputting the input text image into a handwriting recognition model to obtain prediction results for different spatial positions in the input text image, the handwriting recognition model comprising an image feature extraction layer, a fully connected layer, and a softmax layer, wherein the image feature extraction layer is used to extract a feature map of the input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, and the softmax layer is used to obtain prediction probability values of written text at different spatial positions, each of the spatial positions comprising a width of at least one pixel and a height of at least one pixel;
[0009] The prediction results of different spatial locations are merged into multiple neighborhoods to obtain the recognition results.
[0010] An embodiment of the present disclosure further provides a handwriting recognition device, comprising a memory; and a processor connected to the memory, wherein the memory is used to store instructions, and the processor is configured to execute the steps of the handwriting recognition method described in any embodiment of the present disclosure based on the instructions stored in the memory.
[0011] An embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the handwriting recognition method described in any embodiment of the present disclosure.
[0012] The present disclosure also provides a method for training a handwriting recognition model, including:
[0013] Constructing a training model for the handwriting recognition model, the handwriting recognition model comprising an image feature extraction layer, a fully connected layer, and a Softmax layer. The training model comprises the handwriting recognition model and a height compression module, wherein the image feature extraction layer is used to extract a feature map of the input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, the Softmax layer is used to obtain predicted probability values of written text at different spatial locations, and the height compression module is disposed between the image feature extraction layer and the fully connected layer and is used to compress the height of the feature map extracted by the image feature extraction layer;
[0014] Acquire a plurality of sample text images, wherein the number of lines of written text in the sample text images is 1, and the height of the sample text images is a pixels, where a is a natural number greater than or equal to 1;
[0015] According to a preset loss function, the training model is trained using a plurality of the sample text images;
[0016] The highly compressed modules in the trained model are removed to obtain the trained handwriting recognition model.
[0017] An embodiment of the present disclosure also provides a training device for a handwriting recognition model, comprising a memory; and a processor connected to the memory, wherein the memory is used to store instructions, and the processor is configured to execute the steps of the training method for the handwriting recognition model described in any embodiment of the present disclosure based on the instructions stored in the memory.
[0018] An embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the training method of the handwriting recognition model described in any embodiment of the present disclosure.
[0019] Other aspects will become apparent upon reading and understanding the drawings and detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] The accompanying drawings are intended to provide a further understanding of the technical solutions of the present disclosure and constitute a part of the specification. Together with the embodiments of the present disclosure, they are used to explain the technical solutions of the present disclosure and do not constitute a limitation of the technical solutions of the present disclosure. The shapes and sizes of the components in the drawings do not reflect the actual scale and are intended only to illustrate the contents of the present disclosure.
[0021] FIG1 is a flow chart of a handwriting recognition method provided by an exemplary embodiment of the present disclosure;
[0022] 2A and 2B are schematic diagrams of two written text trajectories to be recognized provided by an exemplary embodiment of the present disclosure;
[0023] FIG3 is a schematic diagram of the structure of a handwriting recognition model provided by an exemplary embodiment of the present disclosure;
[0024] FIG4 is a schematic diagram of a post-processing flow of a recognition result provided by an exemplary embodiment of the present disclosure;
[0025] FIG5 is a schematic diagram of a state transition matrix created during a word correction process provided by an exemplary embodiment of the present disclosure;
[0026] FIG6 is a flow chart of a method for training a handwriting recognition model according to an exemplary embodiment of the present disclosure;
[0027] FIG7 is a schematic structural diagram of a training model of a handwriting recognition model provided by an exemplary embodiment of the present disclosure;
[0028] FIG8 is a schematic diagram of a knowledge distillation process provided by an exemplary embodiment of the present disclosure;
[0029] FIG9A , FIG9B and FIG9C are schematic diagrams of three recognition results provided by an exemplary embodiment of the present disclosure;
[0030] FIG10 is a schematic structural diagram of a handwriting recognition device provided by an exemplary embodiment of the present disclosure;
[0031] FIG11 is a schematic structural diagram of a handwriting recognition model training device provided by an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0032] To make the objectives, technical solutions and advantages of the present disclosure more clearly understood, the embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings. It should be noted that, unless there is a conflict, the embodiments and features in the embodiments of the present disclosure can be combined with each other in any manner.
[0033] Unless otherwise defined, the technical or scientific terms used in the embodiments of the present disclosure should have the ordinary meaning understood by people with ordinary skills in the field to which the present disclosure belongs. The words "first", "second" and similar words used in the embodiments of the present disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components. The words "include" or "comprising" and similar words mean that the elements or objects preceding the word include the elements or objects listed after the word and their equivalents, without excluding other elements or objects.
[0034] As shown in FIG1 , the present disclosure provides a handwriting recognition method, comprising the following steps:
[0035] Step 101: determining an input text image according to a written text trajectory to be recognized;
[0036] Step 102: Input the input text image into a handwriting recognition model to obtain prediction results for different spatial positions in the input text image. The handwriting recognition model includes an image feature extraction layer, a fully connected layer, and a softmax layer. The image feature extraction layer is used to extract a feature map of the input text image. The fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model. The softmax layer is used to obtain prediction probability values for written text at different spatial positions, where each spatial position includes a width of at least one pixel and a height of at least one pixel.
[0037] Step 103: perform multi-neighborhood merging on the prediction results of different spatial positions to obtain the recognition result.
[0038] In the embodiment of the present disclosure, a spatial position in the input text image may correspond to m*n pixels in the input text image, where m is the number of pixels included along the height direction and n is the number of pixels included along the width direction. For example, assuming that the input text image (size 1×H×W) passes through the image feature extraction layer, the output size is The feature map f is passed through the fully connected layer and the number of channels is adjusted to K, where K is the number of characters supported by the handwriting recognition model, that is, The feature map f' is processed by the Softmax layer to obtain the predicted probability values of written text at different spatial locations. In this case, a spatial location in the input text image corresponds to a size of 16*8 pixels in the input text image. However, the disclosed embodiment does not limit the size of m and n. m and n can be set according to the size of the feature map output by the actual image feature extraction layer.
[0039] As shown in Figures 2A and 2B, the handwriting recognition method of the embodiment of the present disclosure can recognize single-line written text or multiple-line written text. The handwriting recognition method of the embodiment of the present disclosure determines the input text image according to the trajectory of the written text to be recognized, and then inputs the input text image into the handwriting recognition model. The handwriting recognition model includes an image feature extraction layer, a fully connected layer, and a Softmax layer to obtain prediction results of different spatial positions. The prediction results of different spatial positions are multi-neighborhood merged to obtain recognition results. The network structure is simple and the recognition accuracy is high. The written text described in the embodiment of the present disclosure can be English words or any other characters, such as Chinese characters, numbers, pinyin, etc., and the embodiment of the present disclosure does not limit this.
[0040] In some exemplary embodiments, the written text to be recognized includes at least one character, and the character may include words, letters, numbers, operators, punctuation marks, and any other special characters.
[0041] In the embodiment of the present disclosure, special characters are symbols that are less frequently used and difficult to input directly, compared to traditional or commonly used symbols. There are many types of special characters. For example, special characters may include: mathematical symbols (for example, ≈, ≡, ≠, =, ≤, ≥, <, >, etc.), unit symbols (for example, ℃, ﹪, ‰, ㎡, etc.), pinyin characters (for example, ā, á, ǎ, à, ō, ó, ǒ, ò, etc.), etc.
[0042] For example, it is assumed that the written text to be recognized may include any one or more characters among the 56 characters shown in Table 1.
[0043]
[0044] Table 1
[0045] In some exemplary embodiments, each character includes at least one stroke, where a stroke is the writing trajectory from the time the pen is put down to the time it is lifted. For example, the character "L" includes one stroke, and the character "f" includes two strokes.
[0046] In some exemplary embodiments, each stroke includes at least one trajectory point.
[0047] In some exemplary embodiments, the track point information may be divided into multiple arrays, each array including attribute information of multiple track points in a stroke, the attribute information including X-axis coordinates, Y-axis coordinates, pen lift flags, and the like.
[0048] In this embodiment, the attribute information of multiple trajectory points in a stroke forms an array. Exemplarily, the Chinese character "一" includes a stroke "horizontal", and this stroke may include about 100 trajectory points. The attribute information of each trajectory point includes the X-axis coordinate, Y-axis coordinate, pen-lifting flag bit, etc. In some other exemplary embodiments, the attribute information may further include a timestamp, pressure information, speed information, etc.
[0049] In some exemplary embodiments, step 101 of determining the input text image according to the trajectory of the handwritten text to be recognized may include the following steps:
[0050] Step 1011: Obtain the trajectory of the handwritten text to be recognized and determine the equivalent number of lines of the handwritten text;
[0051] Step 1012: Calculate the height of the input text image according to the equivalent number of lines of the handwritten text, and determine the input text image according to the height of the input text image.
[0052] In some other exemplary embodiments, step 101 of determining the input text image according to the trajectory of the handwritten text to be recognized may further include processes such as scaling and denoising the input text image, which are not limited in the embodiments of the present disclosure.
[0053] In some exemplary embodiments, determining the equivalent number of lines of the handwritten text may include:
[0054] Correspondingly set the trajectory of the handwritten text to be recognized in a two-dimensional coordinate system, where the two-dimensional coordinate system includes the X-axis coordinate and the Y-axis coordinate;
[0055] Calculate the height trace_sl_h of a single line of text in the trajectory of the handwritten text to be recognized;
[0056] Calculate the height trace_h of all the handwritten text, where trace_h = (Ymax - Ymin + 1), Ymin is the minimum value of the Y-axis coordinates of all strokes, and Ymax is the maximum value of the Y-axis coordinates of all strokes;
[0057] Determine the equivalent number of lines raw_num of the handwritten text, where raw_num = trace_h / trace_sl_h.
[0058] In the embodiment of the present disclosure, the height trace_sl_h of a single line of text in the trace of the written text to be identified can be calculated using the average single line height of all written texts, or can be calculated using the highest single line height of all written texts. In the embodiment of the present disclosure, when the characters contained in the written text are mainly English characters, the height of a single line of text in the written text can be approximately calculated using the length of the longest stroke in the single line of text. In the embodiment of the present disclosure, the length of each stroke can be calculated using the Euclidean distance formula, or can be approximately calculated using the height of each stroke, or can be approximately calculated using the larger value of the height of each stroke and the width of each stroke, etc., and the embodiment of the present disclosure does not impose any restrictions on this.
[0059] In some exemplary embodiments, the length stroke_len of each stroke in the written text trajectory to be recognized is approximately calculated according to the following formula: stroke_len = max(xmax-xmin+1,ymax-ymin+1), where xmin is the minimum X-axis coordinate value of the current stroke, xmax is the maximum X-axis coordinate value of the current stroke, ymin is the minimum Y-axis coordinate value of the current stroke, ymax is the maximum Y-axis coordinate value of the current stroke, and max(A, B) represents the larger value of A and B.
[0060] In some exemplary embodiments, the height trace_sl_h of a single line of text in the written text trace to be recognized is determined to be the length max(stroke_len) of the longest stroke among all strokes, where max(stroke_len) is the maximum value of all stroke lengths stroke_len.
[0061] For example, the equivalent number of lines of the written text shown in FIG2A is calculated according to the above method to be 1.18 lines, and the equivalent number of lines of the written text shown in FIG2B is 3.2 lines (the spacing between the two lines is approximately 1 times the line height). In the embodiment of the present disclosure, the equivalent number of lines refers to the sum of the number of lines of the actual written text and the equivalent number of lines of the blank area around each line of written text. As shown in FIG2B, when the height of the blank area between the two lines of text is higher, the equivalent number of lines of the written text shown in FIG2B is larger.
[0062] In other exemplary embodiments, the length of each stroke in the track of the written text to be recognized may be calculated according to other methods, which are not limited in the embodiments of the present disclosure. For example, the length of each stroke in the track of the written text to be recognized, stroke_len, may be calculated according to the following formula: xmin is the minimum X-axis coordinate value of the current stroke, xmax is the maximum X-axis coordinate value of the current stroke, ymin is the minimum Y-axis coordinate value of the current stroke, and ymax is the maximum Y-axis coordinate value of the current stroke.
[0063] In some exemplary embodiments, calculating the height of the input text image based on the equivalent number of lines of written text includes:
[0064] The height of the input text image input_h = [raw_num × a], where raw_num is the equivalent number of lines of written text, a is the height of the sample text image used when training the handwriting recognition model or the height of the written text in the sample text image used, [] is a rounding symbol (which can be rounded up, rounded down, or rounded to the nearest integer), a is a natural number greater than or equal to 1, and the number of lines of written text in the sample text image is 1.
[0065] The handwriting recognition method of the embodiment of the present disclosure has a simple network structure. During model training, only a single line of data annotation is required. A single line of sample text image is used for model training. During inference recognition, single or multiple lines of written text trajectories can be inferred and recognized. When using a single line of sample text image for model training, the height of the input single line of written text can be unified to a pixels (when the embodiment of the present disclosure uses a single line of sample text image for model training, the blank area around the written text in the input single line of sample text image is cropped as much as possible, such as the input Television picture shown in Figure 4. Therefore, the height of the written text in the sample text image can be approximately equal to the height of the sample text image). For multiple lines of written text trajectories, the height of the input text image is determined by the method of the embodiment of the present disclosure, so that the height of each line of written text in the input text image is approximately controlled to be a pixels.
[0066] For example, a can be 80. A single-line sample text image is used for model training. The height of the input text image is uniformly 80 pixels. For multi-line written text trajectories, the height of the input text image needs to be determined so that the height of each line of written text is approximately controlled at 80 pixels. Therefore, the embodiment of the present disclosure designs a method for adaptively determining the height of the input text image. The implementation steps are as follows:
[0067] Step 1: Calculate the length of each stroke, stroke_len, and approximate the height of a single line of text in the trace of the written text to be recognized, trace_sl_h, to the length of the longest stroke among all strokes, max(stroke_len). For example, the length of each stroke, stroke_len, is approximately calculated using the following method: stroke_len = max(xmax-xmin+1, ymax-ymin+1), where xmin is the minimum X-axis coordinate value of the current stroke, xmax is the maximum X-axis coordinate value of the current stroke, ymin is the minimum Y-axis coordinate value of the current stroke, and ymax is the maximum Y-axis coordinate value of the current stroke.
[0068] Step 2: Calculate the height trace_h of the written text, where trace_h = (Ymax-Ymin+1), Ymin is the minimum Y-axis coordinate of all strokes, and Ymax is the maximum Y-axis coordinate of all strokes.
[0069] Step 3: Determine the equivalent number of lines raw_num of the written text to be recognized, where raw_num = trace_h / trace_sl_h; based on this, calculate the height input_h of the input text image, where input_h = [raw_num × 80].
[0070] According to the above-mentioned method of adaptively determining the height of the input text image, the height of the input text image corresponding to the single-line written text shown in Figure 2A is calculated to be [1.18×80]=[94.4]=94 pixels; the height of the input text image corresponding to the single-line written text shown in Figure 2B is [3.2×80]=256 pixels.
[0071] In some exemplary embodiments, determining the input text image according to the height of the input text image includes:
[0072] Calculate the scaling factor ratio between the input text image and the written text trace to be recognized, where ratio = input_h / trace_h, where input_h is the height of the input text image and trace_h is the height of the written text trace;
[0073] Determine the coordinates of the trajectory point in the input text image, where point_X = (point_x-xmin) × ratio, point_Y = (point_y-ymin) × ratio, point_x and point_y respectively represent the X-axis coordinate and Y-axis coordinate of the trajectory point in the written text trajectory to be recognized, xmin and ymin respectively represent the minimum X-axis coordinate and the minimum Y-axis coordinate of all trajectory points in the written text trajectory to be recognized, and point_X and point_Y respectively represent the X-axis coordinate and Y-axis coordinate of the trajectory point in the input text image.
[0074] In the embodiment of the present disclosure, since the blank parts around the written text in the input text image (excluding the blank areas between multiple lines of written text) are basically cropped out, the height of the input text image input_h is approximately equal to the difference between the maximum Y-axis coordinates of all trajectory points in the input text image and the minimum Y-axis coordinates of all trajectory points, and the width of the input text image is approximately equal to the difference between the maximum X-axis coordinates of all trajectory points in the input text image and the minimum X-axis coordinates of all trajectory points.
[0075] In some exemplary embodiments, the method further includes: sequentially connecting all trajectory points of each stroke in the input text image with lines of width b in units of strokes to obtain the input text image, where b is greater than or equal to 1 pixel width.
[0076] In the embodiment of the present disclosure, after the height of the input text image is calculated using the above method, the track points are converted into an input text image of the corresponding height by using track point mapping (ensuring that the line width of all characters in the input text image is consistent).
[0077] For example, b can be 2 pixels wide. After calculating the height of the input text image using the above method for calculating the input image height, the track points are converted into input text images of corresponding height using track point mapping (ensuring that the text line width in all images is consistent). The track point mapping implementation process includes:
[0078] Step 1: Calculate the scaling factor, ratio = input_h / trace_h;
[0079] Step 2: Track point mapping, point_X = (point_x - xmin) × ratio, point_Y = (point_y - ymin) × ratio, where point_x and point_y represent the X-axis coordinates and Y-axis coordinates of the original track point before mapping, xmin and ymin represent the minimum X-axis coordinates and the minimum Y-axis coordinates of all original track points, and point_Y and point_Y represent the X-axis coordinates and Y-axis coordinates of the track point after mapping.
[0080] Step 3: Using strokes as units, connect all the track points (trajectory points after mapping) in each stroke with a line width of 2 to obtain the input text image.
[0081] Taking the handwriting recognition model shown in Figure 3 as an example, a scaling ratio is calculated based on the original collected trajectory points (a series of xy coordinates), and the mapped trajectory points are obtained according to this scaling ratio. Initially, a full-white image with a height of H = Ymax - Ymin + 1 and a width of W = Xmax - Xmin + 1, with pixel values of 255 (where Xmin, Ymin, Xmax, and Ymax are the minimum X-axis coordinates, minimum Y-axis coordinates, maximum X-axis coordinates, and maximum Y-axis coordinates of the mapped trajectory points, respectively) is constructed. On this full-white image, all trajectory points in a stroke are connected sequentially with lines of width b to obtain the input text image.
[0082] The input text image obtained after mapping the above trajectory points is input into the handwriting recognition model shown in Figure 3 for multi-line text recognition. The recognition effect is shown in Figure 3. The input text image x (size is 1×H×W, 1 is the number of channels, H is the height, and W is the width) is extracted through the image feature extraction layer (in the embodiment of the present disclosure, the image feature extraction layer can be a convolutional neural network (CNN), for example, the image feature extraction layer can be a feature extraction network ResNet18, however, the embodiment of the present disclosure does not limit this) to extract features, and the output size is The feature map f is obtained by adjusting the number of channels to K (K is the number of character categories supported by the model, and each channel represents the predicted probability value of a different character. For example, K can be 56) through the full connection (FC) layer to obtain a feature map of size The feature map f' is finally passed through the Softmax layer to obtain the predicted probability values of the characters at different spatial positions, and the character ID with the largest predicted probability value is taken as the character ID at that position (or a probability score threshold can be set in advance to retain the prediction results with a predicted probability value greater than or equal to the probability score threshold. For example, the probability score threshold can be set to 0.5), and the prediction result y of each spatial position is output by comparing with the character and ID comparison table shown in Table 1.
[0083] In the embodiments of the present disclosure, multi-neighborhood merging can also be referred to as multi-connected domain merging. In some exemplary embodiments, multi-neighborhood merging is performed on the prediction results of different spatial positions, specifically: eight-neighborhood merging is performed on the prediction results of different spatial positions. That is, for each pixel with a value of 1, if there is a pixel with a value of 1 in its eight neighborhoods, then these two pixels are classified as one character. The eight neighborhoods or eight connected areas refer to the upper, lower, left, right, upper left, upper right, lower left, and lower right of the corresponding position, which are adjacent positions and diagonally adjacent positions, for a total of 8 directions. For example, in the prediction result y of each spatial position in Figure 3, the three w are merged into one w, and the four r are merged into one r...
[0084] In some exemplary embodiments, when performing multi-neighborhood merging on prediction results of different spatial locations, if the number of elements contained in a connected domain is less than 3, the elements contained in the connected domain are filtered out to remove isolated noise points.
[0085] In some exemplary embodiments, the method further comprises: performing in-line alignment on prediction results at different spatial locations.
[0086] In some exemplary embodiments, performing peer alignment on prediction results at different spatial locations includes:
[0087] Calculate the average value avg_x of the X-axis coordinates of all pixels and the average value avg_y of the Y-axis coordinates of all pixels in each connected domain after the multi-neighborhood merging;
[0088] Traverse each connected domain in order from small to large avg_x, and align the pixels whose avg_y difference is less than or equal to c in the same row, where c is less than or equal to 5 pixels wide.
[0089] In this embodiment, after multi-neighborhood merging, the positions of characters originally on the same row may not be at the same height, and there may be a deviation of one, two, or even several pixels above or below. The characters on the same row are aligned by calculating avg_x and avg_y. For example, c can be 2 pixels wide. Pixels with an avg_y difference of less than or equal to 2 are considered to be on the same row and aligned in the same row. Otherwise, they are considered to be line breaks.
[0090] For example, as shown in FIG4 , a handwritten text trajectory to be recognized, including the handwritten character Television, is input into the handwriting recognition network. A probability score threshold is set (assuming it is 0.5). Prediction results with prediction probability values greater than or equal to the probability score threshold are retained. Prediction results y for different spatial locations are obtained. The eight connected domains of the prediction results y are merged. When performing multi-neighborhood merging on the prediction results for each spatial location, if the number of elements in the connected domain is less than 3, the elements contained in the connected domain are filtered out to remove isolated noise points. The final recognition result string predict_string is initialized, and the average values avg_x and avg_y of the X-axis coordinates and Y-axis coordinates of the pixels contained in each connected domain are calculated. Each connected domain is traversed in ascending order of avg_x, and pixels with an avg_y difference less than or equal to c are aligned in the same row. The final recognition result string predict_string is written in ascending order of avg_x in the same row, and the corrected recognition result Television is returned.
[0091] In some exemplary embodiments, the method further comprises:
[0092] Automatically correct English words in the recognition results based on a pre-established corpus.
[0093] The handwriting recognition method of the embodiment of the present disclosure improves the recognition accuracy by adding a word automatic correction algorithm based on dynamic programming in the network post-processing stage.
[0094] In the embodiment of the present disclosure, word correction relies on the establishment of a corpus. For example, the corpus used is composed of the following three parts:
[0095] (1) Gutenberg Corpus data (the Gutenberg Corpus contains approximately 36,000 free electronic books);
[0096] (2) Wiktionary;
[0097] (3) List of the most common words in the British National Corpus.
[0098] However, the embodiments of the present disclosure are not limited thereto.
[0099] In some exemplary embodiments, automatically correcting English words in the recognition results based on a pre-established corpus includes:
[0100] Detect whether the English words in the recognition results are English words in the corpus;
[0101] When the recognition result contains one or more English words that are not in the corpus, mark the one or more English words as words to be corrected, and calculate the minimum edit distance between each word to be corrected and the English words in the corpus (during the calculation, English words in the corpus whose similarity ratio with the word to be corrected is greater than or equal to a preset threshold can be selected for calculation, for example, the preset threshold can be 50%);
[0102] Correct each word to be corrected according to the calculated minimum edit distance.
[0103] In the embodiment of the present disclosure, the minimum edit distance refers to the minimum number of edits required to change a word from the current word to another word. The editing operations are specifically divided into three types: insertion, deletion, and replacement.
[0104] In some exemplary embodiments, correcting each word to be corrected according to the calculated minimum edit distance includes:
[0105] Initialize the current minimum edit distance detection value to 1;
[0106] Detecting whether a first English word exists and the number of the first English words, wherein the minimum edit distance between the first English word and the current word to be corrected is a current minimum edit distance detection value;
[0107] When there is a first English word and the number of the first English word is 1, correcting the word to be corrected to the first English word;
[0108] When there are first English words and the number of the first English words is 2 or more, sorting the 2 or more first English words according to the number of occurrences in the corpus to obtain the first English word with the largest number of occurrences in the corpus, and correcting the word to be corrected to be the first English word with the largest number of occurrences in the corpus;
[0109] When the first English word does not exist, the current minimum edit distance detection value is incremented by 1, and the process returns to the step of detecting whether the first English word exists and the number of first English words for loop detection until the current minimum edit distance detection value is greater than the preset minimum edit distance threshold and the detection is stopped.
[0110] In some exemplary embodiments, the preset minimum edit distance threshold is 2.
[0111] In some exemplary embodiments, correcting each word to be corrected according to the calculated minimum edit distance includes:
[0112] Detect whether there are English words with a minimum edit distance of 1 and the number of English words with a minimum edit distance of 1;
[0113] When there is an English word with a minimum edit distance of 1 and the number of English words with a minimum edit distance of 1 is 1, the word to be corrected is corrected to an English word with a minimum edit distance of 1;
[0114] When there are English words with a minimum edit distance of 1 and the number of English words with a minimum edit distance of 1 is 2 or more, sort the 2 or more English words with a minimum edit distance of 1 according to the number of occurrences in the corpus to obtain the English word with the largest number of occurrences in the corpus and a minimum edit distance of 1, and correct the word to be corrected to the English word with the largest number of occurrences in the corpus and a minimum edit distance of 1;
[0115] When there is no English word with a minimum edit distance of 1, detect whether there is an English word with a minimum edit distance of 2 and the number of English words with a minimum edit distance of 2;
[0116] When there is an English word with a minimum edit distance of 2 and the number of English words with a minimum edit distance of 2 is 1, the word to be corrected is corrected to an English word with a minimum edit distance of 2;
[0117] When there are English words with a minimum edit distance of 2 and the number of English words with a minimum edit distance of 2 is 2 or more, the 2 or more English words with a minimum edit distance of 2 are sorted according to the number of occurrences in the corpus to obtain the English word with the largest number of occurrences in the corpus and a minimum edit distance of 2, and the word to be corrected is corrected to the English word with the largest number of occurrences in the corpus and a minimum edit distance of 2.
[0118] In some exemplary embodiments, calculating the minimum edit distance between each word to be corrected and an English word in the corpus includes:
[0119] Construct the state transition matrix according to the following formula and recursively calculate from D[1, 1] to D[M, N]:
[0120]
[0121] Wherein, D[i, j] represents the minimum edit distance from the i-th element of the word to be corrected to the j-th element of the target English word; the number of letters in the word to be corrected is M, the number of letters in the target English word is N, i is a natural number between 0 and M, j is a natural number between 0 and N, and both M and N are natural numbers greater than or equal to 1, del_cost is the deletion cost, when a character needs to be deleted, del_cost=1; ins_cost is the insertion cost, when a character needs to be inserted, ins_cost=1; rep_cost is the replacement cost, when a character needs to be replaced, rep_cost=1;
[0122] Let D[M, N] be the minimum edit distance between the word to be corrected and the target English word.
[0123] In the embodiment of the present disclosure, recursive calculation from D[1,1] to D[M,N] means first calculating the matrix element D[1,1], then calculating the matrix elements adjacent to the matrix element D[1,1]: D[1,2], D[2,2], D[2,1], and then calculating the elements adjacent to the matrix elements D[1,2], D[2,2], D[2,1]: D[1,3], D[2,3], D[3,1], D[3,2], D[3,3]..., until D[M,N] is calculated.
[0124] In some exemplary embodiments, automatic correction of English words in the recognition results is performed based on a pre-established corpus, and the implementation process is as follows:
[0125] (1) Take out all words in the corpus and count the number of occurrences;
[0126] (II) For the word to be corrected, a dynamic programming algorithm is used to calculate the minimum edit distance between the word to be corrected and the target English word, and words with minimum edit distances of 0, 1, and 2 are found in turn. The minimum edit distance refers to the minimum number of edits required to change a word from the current word to another word. Edit operations are specifically divided into three types: insertion, deletion, and replacement. For example, the minimum edit distance from pay to play is 1 (one insertion operation); the minimum edit distance from play to play is 1 (one deletion operation); and the minimum edit distance from alay to play is 1 (one replacement operation).
[0127] Taking ggay->stay as an example, the implementation steps for calculating the minimum edit distance from ggay to stay are as follows:
[0128] 1) Construct a matrix with the source word ggay as the column and the target word stay as the row. Add a space character # before ggay and stay respectively to obtain the initial matrix of the dynamic programming algorithm as shown in Figure 5;
[0129] 2) Use D[i,j] to represent the minimum edit distance required from the source word [0:i] to the target word [0:j]. Since the number of letters in both the source and target words is 4, D[4,4] is used as the minimum edit distance from ggay to stay. D[i,j] is calculated based on the state transition matrix shown in formula (1).
[0130]
[0131] Taking D[1,1] as an example, we can illustrate the minimum edit distance of g->s. g->s can be achieved in three ways:
[0132] Insertion + deletion: g->gs->s, edit distance 1+1=2;
[0133] Deletion + insertion: g->#->s, edit distance 1+1=2;
[0134] Replace: g->s, edit distance 1;
[0135] Therefore, the minimum edit distance of g->s is D[1,1]=1.
[0136] Then calculate D[1,2], D[2,1], D[2,2], D[1,3], D[2,3], D[3,1], D[3,2], D[3,3], D[1,4], D[2,4], D[3,4], D[4,1], D[4,2], D[4,3], and D[4,4] in turn, and finally conclude that the minimum edit distance from ggay to stay is 2.
[0137] In some exemplary embodiments, the final correction result of a word is determined according to the following three priorities:
[0138] Words with edit distance 0 (the word itself) > words with edit distance 1 > words with edit distance 2;
[0139] whether it appears in the corpus;
[0140] The number of occurrences in the corpus.
[0141] As shown in FIG6 , the embodiment of the present disclosure further provides a method for training a handwriting recognition model, including:
[0142] Step 601: Construct a training model for a handwriting recognition model. The handwriting recognition model includes an image feature extraction layer, a fully connected layer, and a Softmax layer. The training model includes a handwriting recognition model and a height compression module. The image feature extraction layer is used to extract a feature map of an input text image. The fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model. The Softmax layer is used to obtain predicted probability values of written text at different spatial locations. The height compression module is provided between the image feature extraction layer and the fully connected layer and is used to compress the height of the feature map extracted by the image feature extraction layer.
[0143] Step 602: Acquire multiple sample text images, where the number of lines of written text in the sample text images is 1, and the height of the sample text images is a pixels, where a is a natural number greater than or equal to 1;
[0144] Step 603: training the training model using multiple sample text images according to a preset loss function;
[0145] Step 604: Remove the highly compressed modules in the trained model to obtain a trained handwriting recognition model.
[0146] In the disclosed embodiment, the handwriting recognition method for global classification of feature maps requires the model to be able to perform pixel-level prediction. During the training process, sample text images containing single-line written text are used for training, so that the model has pixel-level prediction capabilities through learning.
[0147] In some exemplary embodiments, as shown in FIG7 , the high compression module includes a second convolution (Conv) layer, a batch normalization (BN) layer, an activation function layer, a weight calculation (Softmax-h) layer, and a high compression (HC) layer, wherein:
[0148] The second convolutional layer is used to extract the features of the feature map extracted by the image feature extraction layer;
[0149] The batch normalization layer is used to normalize the features extracted by the second convolutional layer;
[0150] The activation function layer is used to increase the nonlinearity of this highly compressed module;
[0151] The weight calculation layer is used to calculate the weight value of each pixel among all pixels with the same width value;
[0152] The height compression layer is used to multiply each column of the feature map of the input text image in the height direction by the corresponding position of the corresponding column of the weight value in the height direction, and then sum them to obtain a highly compressed feature map. In the embodiment of the present disclosure, the horizontal direction of the image is the width direction, and the vertical direction of the image is the height direction.
[0153] In some exemplary embodiments, the activation function layer may use ReLU as the activation function, but the embodiments of the present disclosure are not limited to this.
[0154] For example, as shown in FIG7 , a text image x (size 1×H×W, 1 is the number of channels, H is the height, and W is the width) is input, and an image feature extraction layer (for example, the image feature extraction layer can be ResNet18) extracts features to obtain a size of To allow the Connectionist Temporal Classification (CTC) training (CTCloss is a loss function designed for sequence learning, so using CTC loss requires converting the two-dimensional feature map output by the last layer of the model into a one-dimensional sequence), a highly compressed module Squeeze Model is introduced to compress the two-dimensional feature map f into one dimension (the compression process is as follows: the two-dimensional feature map f passes through the second convolution layer, batch normalization layer, activation function layer and weight calculation layer, and the size is obtained. The weight feature map α (α includes the weight value of each pixel in all pixels with the same width value), f is multiplied by the corresponding position of the same column of α after the high compression layer, and the size is obtained. The one-dimensional feature map f2. In Figure 7, Softmax-h means that the Softmax of the feature map f is calculated in columns, as shown in formula (4). Finally, the feature size is output after the fully connected (FC) layer and the Softmax layer:
[0155] f=F(x),f∈R 512×h×w (2)
[0156] e=S(f),e∈R 1×h×w (3)
[0157]
[0158]
[0159] c=softmax(FC(f2)),c∈R w×K (6)
[0160] Among them, F in formula (2) represents the feature extractor ResNet18, S in formula (3) represents the second convolutional layer, batch normalization layer, and activation function layer in the highly compressed module Squeeze Model, formula (4) represents the weight calculation layer in the highly compressed module Squeeze Model, formula (5) represents the highly compressed layer in the highly compressed module Squeeze Model (each column of f is multiplied by the corresponding position of the same column of α and the sum is calculated), and formula (6) represents the FC layer and the Softmax layer. K is the number of character categories that the model supports recognition.
[0161] In some exemplary embodiments, the preset loss function includes a connectionist temporal classifier (CTC) loss function.
[0162] In other exemplary embodiments, the preset loss function L total Including CTC loss function L CTC And the auxiliary loss function L sup ,in, K is the number of character categories that the training model can recognize, y k The probability score of the k-th character predicted by the trained model.
[0163]
[0164]
[0165] Among them, k∈in_label means that the predicted character is the same as the true label, and k∈out_label means that the predicted character is different from the true label.
[0166] The training method of the embodiment of the present disclosure adds an auxiliary loss function based on CTC loss to suppress the appearance of characters outside the label (recorded as negative pixels) during the prediction process. According to whether the model predicts that the character is contained in the true label, the predicted characters are divided into two categories: in_label and out_label. An auxiliary loss function is added to suppress the occurrence of negative pixels in the prediction process.
[0167] The training method of the disclosed embodiment can also perform lightweight processing on the handwriting recognition model through channel pruning and knowledge distillation, so as to significantly reduce the number of model parameters and the amount of calculation without significantly reducing the recognition accuracy.
[0168] In some exemplary embodiments, the image feature extraction layer includes a plurality of first convolutional layers, and the training method further includes:
[0169] Determine the channel pruning ratio of each first convolutional layer in the trained training model;
[0170] Get the channels to be deleted in each first convolutional layer;
[0171] Construct a dependency graph, where the dependency graph includes dependency relationships between multiple first convolutional layers;
[0172] A pruning operation is performed on the channels to be pruned, and the channels are aligned according to the dependencies.
[0173] The training method of the embodiment of the present disclosure can perform lightweight processing on the handwriting recognition model through channel pruning (only the first convolutional layer is pruned, and the fully connected layer is not pruned). Exemplarily, channel pruning may include the following steps:
[0174] S1: Use the training model of the handwriting recognition model to train and obtain a trained training model;
[0175] S2: Determine the channel pruning ratio of each first convolutional layer in the trained training model;
[0176] The total compression ratio of the model is determined based on the hardware resources of the model deployment environment. In the embodiment of the present disclosure, the total compression ratio = the number of deleted channels / the number of model channels before compression. When the total compression ratio is 0.75, the pruned model retains 1 / 4 of the channels of the original model.
[0177] The cropping rate level is determined according to the ratio of the total number of channels of the image feature extraction layer to the maximum number of output channels of the first convolutional layer. Taking into account that different network layers have certain differences in importance to the recognition task, different network layers will be graded and different cropping rates will be assigned to each level of network layers. The cropping rate levels of different network layers may be different. For example, when the image feature extraction layer of the embodiment of the present disclosure is ResNet18, the total number of channels of ResNet18 is 3904, of which the number of output channels of the first convolutional layer is at most 512. In order to enable all channels in the same first convolutional layer to be divided into the same level, 3904÷512 is rounded to 7, so a 7-level cropping rate can be selected.
[0178] Assuming that the total compression ratio of the model is set to 0.75 and a seven-level cropping rate is used, the channel compression ratio obtained according to formula (9) is [Ratio-value*3,Ratio-value*2,Ratio-value*1,Ratio,Ratio+value*1,Ratio+value*2,Ratio+value*3]=[0.5625,0.625,0.6875,0.75,0.8125,0.875,0.9375].
[0179]
[0180] The output channels (output_channel: the number of convolution kernels in the corresponding convolution layer) of each first convolution layer in the handwriting recognition model are counted and divided into 7 parts according to the order in the network structure. According to the channel compression ratio described above, the corresponding channel pruning ratio is assigned to the first convolution layer in each part (for example, the channel pruning ratio of the first convolution layer in the first part is 0.5625, the channel pruning ratio of the first convolution layer in the second part is 0.625, and so on). The number of channels that need to be pruned in each first convolution layer is obtained. The total number of pruned channels accounts for 0.75 of the number of channels before pruning.
[0181] S3: Get the ID of the channel to be deleted in each first convolutional layer;
[0182] Calculate the sum of the absolute values of the weights of each convolution kernel in each first convolution layer of the original handwriting recognition model trained in S1, sort them in ascending order, and combine them with the channel deletion ratio of each first convolution layer obtained in S2 to determine the channel ID to be deleted in each first convolution layer (delete the convolution kernel with a small sum of the absolute values of the weights to reduce the number of channels in the output feature map of the first convolution layer).
[0183] S4: Build dependency graph and perform pruning operations;
[0184] To ensure the connection between the previous and next network layers after pruning, before pruning, the dependency relationship between each layer of the entire network is constructed (the number of channels of each convolution kernel in the next convolution layer should be equal to the number of convolution kernels in the previous convolution layer). Based on the ID of the channel to be pruned of each first convolution layer obtained in S3 above, the corresponding network layer is pruned. During the pruning process, the entire dependency graph is traversed, and the channels are automatically aligned according to the dependency relationship, and inconsistent modules (for example, the batch normalization layer BN) are repaired.
[0185] For example, suppose the model before pruning and the model after pruning are as follows:
[0186] (1) Model before pruning: The image with dimension h*w*1 passes through the first convolutional layer (conv1, convolution kernel: 3*3*1*c1) to output the feature map f1 with dimension h*w*c1, and f1 passes through the second convolutional layer (conv2, convolution kernel: 3*3*c1*c2) to output the feature map with dimension h*w*c2;
[0187] (2) Model after pruning: The image with dimension h*w*1 passes through the first convolutional layer (conv1', convolution kernel: 3*3*1*c1') to output the feature map f1' with dimension h*w*c1', and f1' passes through the second convolutional layer (conv2', convolution kernel: 3*3*c1'*c2') to output the feature map f1' with dimension h*w*c2'.
[0188] c1' and c2' are obtained by multiplying c1 and c2 by the pruning coefficient and rounding them up. c1' is obtained by the dependency between conv1 and conv2 (the number of output channels of the current convolutional layer c1' = the number of input channels of the next convolutional layer c1').
[0189] The aligned channels in the disclosed embodiment mainly refer to the process of adaptively adjusting the number of channels in each convolution kernel dimension in the subsequent convolution layer conv2 according to the pruning result of the previous convolution layer conv1. Before pruning, the model image passes through the previous convolution layer conv1 and outputs the dimension h*w*c1, where the c1 channel is obtained by convolving the c1 3*3*1 convolution kernels in conv1 with the input image. When the number of convolution kernels in the previous convolution layer conv1 is compressed to c1' through channel pruning, the dimension of the output feature map of the previous convolution layer conv1 is adjusted to h*w*c1' accordingly, and the number of channels of each convolution kernel in the subsequent convolution layer conv2 also needs to be adjusted from c1 to c1' (which channels are specifically deleted during the adjustment process corresponds to the convolution kernels retained after pruning the conv1 channel).
[0190] The training method of the disclosed embodiment can also improve the recognition accuracy of the model by fine-tuning + knowledge distillation and pruning the small model.
[0191] In some exemplary embodiments, the training method further comprises:
[0192] The trained model is used as the teacher model, and the pruned model is used as the student model;
[0193] Construct a mean squared error loss function between the teacher model and the student model, and a cross entropy loss function between the student model's predicted characters and the true labels;
[0194] Based on the constructed mean square error loss function and cross entropy loss function, the teacher model is used to train the student model.
[0195] Because the recognition accuracy of the handwriting recognition model after channel pruning is lower than that of the original trained handwriting recognition model before pruning, the disclosed embodiment adopts knowledge distillation—using the original large model to perform Logits distillation (a knowledge distillation method) on the pruned small model, thereby improving the recognition accuracy of the pruned small model. The implementation process is shown in Figure 8 (in the figure, ResNet_tiny represents the feature extraction network after channel pruning, Squeeze Model represents the highly compressed module, and the classifier Classifer includes an FC layer and a Softmax layer). The originally trained handwriting recognition model serves as the teacher model, and the small model obtained after channel pruning serves as the student model. The distillation network is divided into two parts. One part still uses the original loss calculation method of the handwriting recognition model—the input image passes through the Softmax layer of the Student model, outputs different character probability values, and the output probability values (hard predictions) are compared with the true labels (hard labels) to calculate the cross entropy loss function (which makes the positive label probability value approach 1, the negative label probability value approach 0, and all negative labels are treated uniformly), i.e., the CTC in Figure 8. Loss; the other part calculates the mean square error loss (MSE Loss) of the probability values output by the Softmax layer of the Teacher model and the Student model, so that the probability value output by the Student model is close to the probability value output by the Teacher model. By directly calculating the loss function on the probability values of the two models, the information contained in the negative label can be fully utilized. For example: in a certain input image: "2" is more similar to "3", and the probability corresponding to "3" in the output value of the model's Softmax layer is significantly higher than the probability values of other negative labels, while in another image, "2" is more similar to "7", and the probability corresponding to "7" in the output value of the model's Softmax layer is higher. The true values (hard targets) corresponding to these two "2s" are the same, but the soft targets (soft targets) are different. The soft target contains more information than the hard target, and when the entropy of the soft target distribution is relatively high, the knowledge contained in the soft target is richer.
[0196] The output values of the Teacher model's Softmax layer are used as soft labels, and the output values of the Student model's Softmax layer are used as soft predictions. The soft labels and soft predictions are used to calculate the MSE loss. The weighted sum of the MSE loss and the CTC loss is used as the final loss in the training process.
[0197] The comparison of the parameters (Params), computational complexity (Flops), and recognition accuracy (character accuracy) of the model before and after lightweighting is shown in Table 2.
[0198]
[0199] Table 2
[0200] The training method of the disclosed embodiment compresses the parameters of the lightweight model from 13.37M to 3.53M, and reduces the computational complexity from 3.53G to 1.09G, without significantly reducing the recognition accuracy compared to the original model (Baseline). Three exemplary recognition results are shown in Figures 9A, 9B, and 9C.
[0201] The handwriting recognition method provided by the embodiment of the present disclosure designs an end-to-end full-text handwriting recognition network, uses an image feature extraction layer to extract input image features, and globally classifies them to achieve full-text recognition. This improves the problem in related methods where the recognition effect is limited by the detection performance of the detector, and the network structure is simple.
[0202] The training method of the handwriting recognition model provided by the embodiment of the present disclosure uses a sample text image with a fixed height of a pixels during the training process. In order to ensure that the height of the text sent to the network is basically controlled at a pixels when recognizing multiple lines of text, a method for adaptively determining the height of the input image is designed; in order to reduce the difference between samples caused by different font sizes and make the training process converge quickly, the trajectory points are converted into images of the target height by using trajectory point mapping in the preprocessing stage to ensure that the font line width sent to the network is consistent. In addition, in order to address the problem of partial letter recognition errors caused by connected strokes, sloppy writing, etc. during the writing process, a word automatic correction algorithm based on dynamic programming is added to the network post-processing stage, a corpus is established, and the recognition results are corrected twice to improve the recognition accuracy. In order to address the problem of a large number of recognition network parameters and a large amount of calculation, the training method of the embodiment of the present disclosure uses a combination of channel pruning and Logits distillation to lightweight the handwriting recognition model, so that the model reduces the number of parameters and the amount of calculation without almost losing accuracy, which is convenient for offline deployment of terminals.
[0203] An embodiment of the present disclosure also provides a handwriting recognition device, comprising a memory; and a processor connected to the memory, wherein the memory is used to store instructions, and the processor is configured to execute the steps of the handwriting recognition method described in any embodiment of the present disclosure based on the instructions stored in the memory.
[0204] As shown in FIG10 , in one example, a handwriting recognition device may include: a first processor 1010, a first memory 1020, a first bus system 1030, and a first transceiver 1040, wherein the first processor 1010, the first memory 1020, and the first transceiver 1040 are connected via the first bus system 1030, the first memory 1020 is used to store instructions, and the first processor 1010 is used to execute the instructions stored in the first memory 1020 to control the first transceiver 1040 to transmit and receive signals. Specifically, the first transceiver 1040 can obtain the writing text trajectory to be recognized from the text input interface under the control of the first processor 1010, and the first processor 1010 determines the input text image based on the writing text trajectory to be recognized; inputs the input text image into the handwriting recognition model to obtain the prediction results of each spatial position, and the handwriting recognition model includes an image feature extraction layer, a fully connected layer and a Softmax layer, wherein the image feature extraction layer is used to extract the feature map of the input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, and the Softmax layer is used to obtain the prediction probability value of the writing text at different spatial positions; the prediction results of each spatial position are multi-neighborhood merged to obtain the recognition result, and the obtained recognition result is output to the text input interface through the first transceiver 1040.
[0205] It should be understood that the first processor 1010 may be a central processing unit (CPU), or may be another general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.
[0206] The first memory 1020 may include a read-only memory and a random access memory, and provides instructions and data to the first processor 1010. A portion of the first memory 1020 may also include a non-volatile random access memory. For example, the first memory 1020 may also store device type information.
[0207] In addition to the data bus, the first bus system 1030 may also include a power bus, a control bus, a status signal bus, etc. However, for the sake of clarity, in FIG10 , various buses are labeled as the first bus system 1030 .
[0208] During implementation, the processing performed by the processing device can be completed by the hardware integrated logic circuit in the first processor 1010 or by instructions in the form of software. That is, the method steps of the embodiment of the present disclosure can be embodied as being executed by a hardware processor, or being executed by a combination of hardware and software modules in the processor. The software module can be located in a storage medium such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the first memory 1020, and the first processor 1010 reads the information in the first memory 1020 and completes the steps of the above method in combination with its hardware. To avoid repetition, it will not be described in detail here.
[0209] The present disclosure also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the handwriting recognition method described in any of the embodiments of the present disclosure. The method for driving prognostic analysis by executing executable instructions is substantially the same as the handwriting recognition method described in the aforementioned embodiments of the present disclosure and is not further described here.
[0210] In some possible implementations, various aspects of the handwriting recognition method provided in the present application may also be implemented in the form of a program product, which includes program code. When the program product is run on a computer device, the program code is used to cause the computer device to execute the steps of the handwriting recognition method according to the various exemplary embodiments of the present application described above in this specification. For example, the computer device may execute the handwriting recognition method described in the embodiments of the present application.
[0211] The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination thereof. More specific examples (a non-exhaustive list) of readable storage media include: an electrical connection having one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0212] An embodiment of the present disclosure also provides a training device for a handwriting recognition model, comprising a memory; and a processor connected to the memory, wherein the memory is used to store instructions, and the processor is configured to execute the steps of the training method for the handwriting recognition model as described in any embodiment of the present disclosure based on the instructions stored in the memory.
[0213] As shown in FIG11 , in one example, a training device for a handwriting recognition model may include: a second processor 1110, a second memory 1120, a second bus system 1130, and a second transceiver 1140, wherein the second processor 1110, the second memory 1120, and the second transceiver 1140 are connected via the second bus system 1130, the second memory 1120 is used to store instructions, and the second processor 1110 is used to execute the instructions stored in the second memory 1120 to control the second transceiver 1140 to transmit and receive signals. Specifically, the second transceiver 1140 can obtain multiple sample text images under the control of the second processor 1110, where the number of lines of written text in the sample text image is 1 line, and the height of the sample text image is a pixels, where a is a natural number greater than or equal to 1. The second processor 1110 constructs a training model for the handwriting recognition model, where the handwriting recognition model includes an image feature extraction layer, a fully connected layer, and a Softmax layer. The training model includes the handwriting recognition model and a height compression module, wherein the image feature extraction layer is used to extract a feature map of the input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, the Softmax layer is used to obtain predicted probability values of written text at different spatial positions, and the height compression module is arranged between the image feature extraction layer and the fully connected layer to compress the height of the feature map extracted by the image feature extraction layer; according to a preset loss function, the training model is trained using the multiple sample text images; and the height compression module in the trained training model is removed to obtain the trained handwriting recognition model.
[0214] It should be understood that the second processor 1110 may be a central processing unit (CPU), or may be another general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.
[0215] The second memory 1120 may include a read-only memory and a random access memory, and provides instructions and data to the second processor 1110. A portion of the second memory 1120 may also include a non-volatile random access memory. For example, the second memory 1120 may also store device type information.
[0216] In addition to the data bus, the second bus system 1130 may also include a power bus, a control bus, a status signal bus, etc. However, for the sake of clarity, various buses are labeled as the second bus system 1130 in FIG.
[0217] During the implementation process, the processing performed by the processing device can be completed by the hardware integrated logic circuit in the second processor 1110 or by instructions in the form of software. That is, the method steps of the embodiment of the present disclosure can be embodied as being executed by a hardware processor, or being executed by a combination of hardware and software modules in the processor. The software module can be located in a storage medium such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the second memory 1120, and the second processor 1110 reads the information in the second memory 1120 and completes the steps of the above method in combination with its hardware. To avoid repetition, it will not be described in detail here.
[0218] An embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for training a handwriting recognition model as described in any embodiment of the present disclosure.
[0219] In some possible implementations, various aspects of the handwriting recognition model training method provided in the present application may also be implemented in the form of a program product, which includes program code. When the program product is run on a computer device, the program code is used to cause the computer device to execute the steps of the handwriting recognition model training method according to the various exemplary embodiments of the present application described above in this specification. For example, the computer device may execute the handwriting recognition model training method described in the embodiments of the present application.
[0220] The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination thereof. More specific examples (a non-exhaustive list) of readable storage media include: an electrical connection having one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0221] It will be appreciated by those skilled in the art that all or some of the steps, systems, and functional modules / units in the methods disclosed above may be implemented as software, firmware, hardware, and appropriate combinations thereof. In hardware implementations, the division between the functional modules / units mentioned in the above description does not necessarily correspond to the division of physical components; for example, a physical component may have multiple functions, or a function or step may be performed by several physical components in cooperation. Some or all components may be implemented as software executed by a processor, such as a digital signal processor or a microprocessor, or implemented as hardware, or implemented as an integrated circuit, such as an application-specific integrated circuit. Such software may be distributed on a computer-readable medium, which may include a computer storage medium (or non-transitory medium) and a communication medium (or temporary medium). As is well known to those skilled in the art, the term computer storage medium includes volatile and non-volatile, removable, and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media include, but are not limited to, RAM, ROM, EEPROM, flash memory or other memory technology, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other medium that can be used to store the desired information and can be accessed by a computer. In addition, it is well known to those skilled in the art that communication media generally embodies computer-readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information delivery media.
[0222] Although the embodiments disclosed in this disclosure are as described above, the contents described are merely embodiments adopted to facilitate understanding of the disclosure and are not intended to limit the present invention. Any person skilled in the art may make any modifications and changes in the form and details of the implementation without departing from the spirit and scope of the disclosure. However, the scope of patent protection of the present invention shall still be based on the scope defined by the appended claims.
Claims
1. A handwriting recognition method, comprising: Determine an input text image according to the written text trajectory to be recognized; Inputting the input text image into a handwriting recognition model to obtain prediction results of different spatial positions in the input text image, wherein the handwriting recognition model includes an image feature extraction layer, a fully connected layer and a Softmax layer, wherein the image feature extraction layer is used to extract a feature map of the input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, and the Softmax layer is used to obtain prediction probability values of written texts at different spatial positions, each of the spatial positions includes a width of at least one pixel*a height of at least one pixel; The prediction results of different spatial locations are merged in multiple neighborhoods to obtain the recognition results.
2. The handwriting recognition method according to claim 1, wherein: The method further comprises: training the handwriting recognition model through the following process: Constructing a training model of the handwriting recognition model, the training model comprising the handwriting recognition model and a height compression module, wherein the height compression module is arranged between the image feature extraction layer and the fully connected layer, and is used to compress the height of the feature map extracted by the image feature extraction layer; Acquire a plurality of sample text images, wherein the number of lines of written text in the sample text images is 1, and the height of the sample text images is a pixels, where a is a natural number greater than or equal to 1; According to a preset loss function, the training model is trained using a plurality of the sample text images; The highly compressed modules in the trained model are removed to obtain the trained handwriting recognition model.
3. The handwriting recognition method according to claim 2, wherein: The highly compressed module includes a second convolutional layer, a batch normalization layer, an activation function layer, a weight calculation layer and a highly compressed layer, wherein: The second convolutional layer is used to extract features of the feature map extracted by the image feature extraction layer; The batch normalization layer is used to normalize the features extracted by the second convolutional layer; The activation function layer is used to increase the nonlinearity of the height compression module; The weight calculation layer is used to obtain the weight value of each pixel among all pixels with the same width value; The highly compressed layer is used to multiply each column of the feature map of the input text image in the height direction by the corresponding position of the corresponding column of the weight value in the height direction and then sum them up to obtain a highly compressed feature map.
4. The handwriting recognition method according to claim 2, wherein: The preset loss function includes a connectionist temporal classifier CTC loss function.
5. The handwriting recognition method according to claim 4, wherein: The preset loss function also includes an auxiliary loss function L sup ,in, K is the total number of characters predicted by the training model, y k is the probability score of the kth character predicted by the training model, k∈in_label means that the predicted character is the same as the true label, and k∈out_label means that the predicted character is different from the true label.
6. The handwriting recognition method according to claim 1, wherein: The step of determining the input text image according to the written text trajectory to be recognized includes: Obtaining the written text track to be recognized, and determining the equivalent number of lines of the written text; The height of the input text image is calculated according to the equivalent number of lines of the written text, and the input text image is determined according to the height of the input text image.
7. The handwriting recognition method according to claim 6, wherein: The step of calculating the height of the input text image according to the equivalent number of lines of the written text comprises: The height of the input text image input_h = [raw_num×a], wherein raw_num is the equivalent number of lines of the written text, a is the height of the sample text image used when training the handwriting recognition model, [] is a rounding symbol, a is a natural number greater than or equal to 1, and the number of lines of the written text in the sample text image is 1.
8. The handwriting recognition method according to claim 6, wherein: The written text includes at least one character, each of the characters includes at least one stroke, and determining the equivalent number of lines of the written text includes: Determine the height trace_sl_h of a single line of text in the written text trace to be recognized; Calculate the height trace_h of the written text, where trace_h=(Ymax-Ymin+1), Ymin is the minimum Y-axis coordinate of all strokes, and Ymin is the maximum Y-axis coordinate of all strokes; Determine the equivalent line number raw_num of the written text, where raw_num=trace_h / trace_sl_h.
9. The handwriting recognition method according to claim 8, wherein: Each of the strokes includes at least one trajectory point, and determining the input text image according to the height of the input text image includes: Calculate the scaling factor ratio between the input text image and the trace of the written text to be recognized, wherein ratio=input_h / trace_h, input_h is the height of the input text image, and trace_h is the height of the written text; Determine the coordinates of the trajectory points in the input text image, where point_X = (point_x-xmin)×ratio, point_Y = (point_y-ymin)×ratio, point_x and point_y respectively represent the X-axis coordinate and Y-axis coordinate of the trajectory point in the written text trajectory to be identified, xmin and ymin respectively represent the minimum X-axis coordinate and the minimum Y-axis coordinate of all trajectory points in the written text trajectory to be identified, and point_X and point_Y respectively represent the X-axis coordinate and Y-axis coordinate of the trajectory point in the input text image.
10. The handwriting recognition method according to claim 1, wherein: After the prediction results of different spatial positions are merged in multiple neighborhoods, the method further includes: The prediction results at different spatial locations are aligned in the same row.
11. The handwriting recognition method according to claim 10, wherein: The step of aligning the prediction results at different spatial positions in the same row includes: Calculate the average value avg_x of the X-axis coordinates of all pixels and the average value avg_y of the Y-axis coordinates of all pixels in each connected domain after the multi-neighborhood merging; Traverse each connected domain in order from small to large avg_x, and align the pixels whose avg_y difference is less than or equal to c in the same row, where c is less than or equal to 5 pixels in width.
12. The handwriting recognition method according to claim 1, wherein: The method further comprises: Automatically correct English words in the recognition results based on the pre-established corpus.
13. The handwriting recognition method according to claim 12, wherein: The automatic correction of English words in the recognition result according to the pre-established corpus includes: Detect whether the English words in the recognition result are English words in the corpus; When the recognition result includes one or more English words that are not in the corpus, marking the one or more English words as words to be corrected, and calculating the minimum edit distance between each of the words to be corrected and the English words in the corpus; Each of the words to be corrected is corrected according to the calculated minimum edit distance.
14. The handwriting recognition method according to claim 13, wherein: Correcting each of the words to be corrected according to the calculated minimum edit distance includes: Initialize the current minimum edit distance detection value to 1; Detecting whether a first English word exists and the number of the first English words, wherein the minimum edit distance between the first English word and the word to be corrected is the current minimum edit distance detection value; When the first English word exists and the number of the first English word is 1, correcting the word to be corrected to be the first English word; When the first English words exist and the number of the first English words is 2 or more, sorting the 2 or more first English words according to the number of occurrences in the corpus to obtain the first English word with the largest number of occurrences in the corpus, and correcting the word to be corrected to be the first English word with the largest number of occurrences in the corpus; When the first English word does not exist, the current minimum edit distance detection value is incremented by 1, and the step of detecting whether the first English word exists and the number of the first English words is returned to perform a loop detection until the current minimum edit distance detection value is greater than a preset minimum edit distance threshold and the detection is stopped.
15. The handwriting recognition method according to claim 13, wherein: The step of calculating the minimum edit distance between each of the words to be corrected and the English words in the corpus comprises: The state transfer matrix is constructed according to the following formula, and is recursively calculated from D[1, 1] to D[M, N]: Wherein, D[i, j] represents the minimum edit distance from the i-th element of the word to be corrected to the j-th element of the target English word, and the target English word is an English word in the corpus; the number of letters of the word to be corrected is M, the number of letters of the target English word is N, i is a natural number between 0 and M, j is a natural number between 0 and N, M and N are both natural numbers greater than or equal to 1, del_cost is the deletion cost, when a character needs to be deleted, del_cost=1; ins_cost is the insertion cost, when a character needs to be inserted, ins_cost=1; rep_cost is the replacement cost, when a character needs to be replaced, rep_cost=1; D[M, N] is used as the minimum edit distance between the word to be corrected and the target English word.
16. A handwriting recognition device, comprising a memory; and a processor connected to the memory, the memory being used to store instructions, the processor being configured to execute the steps of the handwriting recognition method according to any one of claims 1 to 15 based on the instructions stored in the memory.
17. A computer-readable storage medium having a computer program stored thereon, wherein when the program is executed by a processor, the handwriting recognition method according to any one of claims 1 to 15 is implemented.
18. A method for training a handwriting recognition model, comprising: Constructing a training model of the handwriting recognition model, the handwriting recognition model includes an image feature extraction layer, a fully connected layer and a Softmax layer, and the training model includes the handwriting recognition model and a height compression module, wherein the image feature extraction layer is used to extract a feature map of an input text image, the fully connected layer is used to adjust the number of channels of the feature map to the number of characters supported by the handwriting recognition model, the Softmax layer is used to obtain predicted probability values of written texts at different spatial positions, and the height compression module is arranged between the image feature extraction layer and the fully connected layer, and is used to compress the height of the feature map extracted by the image feature extraction layer; Acquire a plurality of sample text images, wherein the number of lines of written text in the sample text images is 1, and the height of the sample text images is a pixels, where a is a natural number greater than or equal to 1; According to a preset loss function, the training model is trained using a plurality of the sample text images; The highly compressed modules in the trained model are removed to obtain the trained handwriting recognition model.
19. The training method according to claim 18, wherein: The highly compressed module includes a second convolutional layer, a batch normalization layer, an activation function layer, a weight calculation layer and a highly compressed layer, wherein: The second convolutional layer is used to extract features of the feature map extracted by the image feature extraction layer; The batch normalization layer is used to normalize the features extracted by the second convolutional layer; The activation function layer is used to increase the nonlinearity of the height compression module; The weight calculation layer is used to obtain the weight value of each pixel among all pixels with the same width value; The highly compressed layer is used to multiply each column of the feature map of the input text image in the height direction by the corresponding position of the corresponding column of the weight value in the height direction and then sum them up to obtain a highly compressed feature map.
20. The training method according to claim 18, wherein: The preset loss function includes a CTC loss function.
21. The training method according to claim 20, wherein: The preset loss function also includes an auxiliary loss function L sup ,in, K is the total number of characters predicted by the training model, y k is the probability score of the kth character predicted by the training model, k∈out_label means that the predicted character is the same as the true label, and k∈out_label means that the predicted character is different from the true label.
22. The training method according to claim 18, wherein: The image feature extraction layer includes a plurality of first convolutional layers, and the method further includes: Determine a channel pruning ratio of each of the first convolutional layers in the trained training model; Obtaining a channel to be deleted from each of the first convolutional layers; Constructing a dependency graph, wherein the dependency graph includes dependency relationships between a plurality of the first convolutional layers; A pruning operation is performed on the to-be-deleted channels, and the channels are aligned according to the dependency relationships.
23. The training method according to claim 22, wherein: The method further comprises: The trained training model is used as a teacher model, and the pruned training model is used as a student model; Constructing a mean square error loss function between the teacher model and the student model, and a cross entropy loss function between the predicted characters and the true labels of the student model; Based on the constructed mean square error loss function and cross entropy loss function, the student model is trained using the teacher model.
24. A training device for a handwriting recognition model, comprising a memory; and a processor connected to the memory, the memory being used to store instructions, the processor being configured to execute the steps of the training method for a handwriting recognition model as described in any one of claims 18 to 23 based on the instructions stored in the memory.
25. A computer-readable storage medium having a computer program stored thereon, wherein when the program is executed by a processor, the method for training a handwriting recognition model as claimed in any one of claims 18 to 23 is implemented.
