Seal recognition method and system integrating data enhancement and deep learning
Expanding the seal data set and building the Vision Transformer model through data enhancement technology solves the problems of data scarcity and labeling difficulties in seal recognition tasks, improves the accuracy and robustness of seal recognition, and promotes the spread of seal culture.
Patent Information
- Application Number
- CN202410875204.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2044-07-02
AI Technical Summary
In the current seal recognition task, due to the scarcity of data sets and the high requirements for seal knowledge of annotators, it is difficult for the model to accurately identify seal images in complex situations, and there is a lack of large-scale annotation data sets, which affects the dissemination and promotion of seal culture.
Use data enhancement technology to transform the seal image, expand the data set, and build a deep learning model based on the Vision Transformer model, through image reduction, cropping, brightness adjustment, contrast adjustment, masking, rotation, flipping, handwriting coverage, texture coverage, randomization Noise and edge expansion are used to generate new data sets for training and validating deep learning models.
It effectively improves the generalization ability of the neural network model in seal recognition tasks, improves the accuracy and robustness of seal recognition, provides a method to quickly and accurately identify seal images in complex situations, and promotes the spread of seal culture.
Smart Images

Figure CN118430002B
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of image recognition in complex situations, and specifically relates to a seal recognition method and system integrating data enhancement and deep learning. Background Art
[0002] my country's seal culture has a long history. Seal culture not only shows the unique aesthetic characteristics and feelings of the Chinese nation, but also inherits and promotes the spiritual connotation of Chinese culture. Seals can usually be divided into official seals and private seals. As a kind of identity certificate, they are widely used in scenes such as rights and identity authentication, property sealing and document delivery. As time goes by, official seals continue to be used as official seals, and they still play an important role in government agencies, groups and enterprises and institutions. However, with the advancement of technology, personal biometric information such as fingerprints and faces has gradually replaced the role of private seals. At present, seal culture is more of a cultural heritage, used to entrust the owner's interests. Since calligraphy and painting works usually have extremely high appreciation value and collection value, collectors of all generations will carefully preserve the works, and the seals on the works can therefore be preserved and inherited. Therefore, learning and understanding seal knowledge can help people improve their understanding of the background of the works and more easily perceive the long heritage and historical accumulation of the works. In addition, learning and understanding seal knowledge can also improve people's aesthetic cultivation and artistic appreciation ability.
[0003] Seal script is a font with the characteristics of regular structure, precise stroke length, simple and beautiful character shape. It is applied to seals through seal carving techniques, which can ensure the clarity, accuracy and standardization of the seal text. However, compared with simplified Chinese characters, the use of seal script is relatively limited and is no longer well known. In addition, as a spiritual sustenance of the owner, the seal usually reflects the owner's ingenuity and uniqueness. Therefore, in the design of the content and layout of the seal, people pay more attention to innovation and personalization. Therefore, the styles of seals vary greatly, and each seal has its own unique charm. In addition, pictographic seals have also evolved, but these situations have also increased the difficulty of people understanding seals. Many people are interested in seal culture, but due to problems such as not knowing where to start or difficulty in understanding, they often cannot deeply learn and appreciate the beauty of seal culture, which hinders the development of seal culture. Therefore, the use of neural networks and other technologies to identify seals can reduce the difficulty of people's query and learning of seals and improve the dissemination ability of seal culture.
[0004] Since there are many categories in the seal recognition task and the number of samples in each category is small, directly using a deep learning model for training will result in poor model recognition results. Even if it is barely fitted through careful parameter adjustment, it is impossible to extract the deep semantic information of the seal and it is difficult to recognize seal images in complex situations. Therefore, in the face of the above dilemma, a larger data set is usually required, but the seal image data itself is relatively scarce, and the seal knowledge required of the annotator itself is high, resulting in a lack of large-scale annotated data sets in the field of seal recognition. In order to reduce the difficulty of user query and seal recognition and improve the level of promotion of seal culture, there is an urgent need for a method that can quickly and accurately identify the content of seal images. Summary of the invention
[0005] One object of the present invention is to address the shortcomings of the prior art and provide a seal recognition method that integrates data enhancement and deep learning. The method transforms limited data by using data enhancement technology to generate new data, thereby expanding the data set while ensuring that the label remains unchanged, so as to achieve accurate recognition of seal images.
[0006] In order to solve the above technical problems, the present invention adopts the following technical solutions:
[0007] A seal recognition method integrating data enhancement and deep learning comprises the following steps:
[0008] Step 1: Obtain a seal image, and identify and annotate the seal image;
[0009] Step 2: Perform data enhancement on the annotated seal image to obtain enhanced image data and divide it into a training set, a validation set, and a test set;
[0010] Step 3: Build a ViT deep learning model based on the Vision Transformer model. The ViT deep learning model is used to recognize seal images. The training set is used to train the ViT deep learning model and the validation set is used to verify the trained ViT deep learning model. Finally, the test set is used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model that meets the performance requirements.
[0011] Step 4: Use the ViT deep learning model obtained in step 3 to identify the seal image to be identified.
[0012] Furthermore, the method for marking the seal image in step 1 is:
[0013] The seal image is annotated in the format of person-content-style-number, where the person is the owner of the seal, the content is the content contained in the seal, and the style is distinguished by letters;
[0014] In the case where the same seal appears multiple times during the collection process, a number is assigned to each seal.
[0015] Furthermore, the data enhancement in step 2 includes image reduction, image cropping, image brightness adjustment, image contrast adjustment, image masking, image rotation, image flipping, image text overlay, image texture overlay, image random noise and image edge expansion of the seal image.
[0016] Furthermore, the method of image cropping is:
[0017] Set two random decimals r 1∈[0, 1] and r 2∈[0.1, 0.6], when r When 1<0.5, horizontal cropping is performed; when r When 1 ≥ 0.5, vertical cutting is performed;
[0018] During the cutting process, r When the second digit of 1 is an even number, choose to crop from top to bottom or from left to right; when r When the second digit of 1 is an odd number, choose to crop from right to left or from bottom to top. r 2 is the cropping ratio;
[0019] The method to adjust the image brightness is:
[0020] Set a random decimal r 3∈[0.2, 4], and set the Gamma value of the image to r 3. When r When 3<1, increase the image brightness. r When 3>1, reduce the image brightness;
[0021] The method for adjusting image contrast is:
[0022] Set a random decimal r 4∈[0.2, 1.2], and set the contrast parameter value of the image to r 4. When r When 4<1, the image contrast is increased, and when r When 4>1, the contrast of the image is reduced.
[0023] Furthermore, the image masking method is:
[0024] From random integers r 5∈[1, 10], and then select the number of coverage areas from a random integer r 6∈[5, 50], and then select the side length of the coverage area from a random integer w∈[1, a ]、 h ∈[1, b ] respectively select the horizontal and vertical coordinates of the upper left corner of the area, where a and b Respectively represent the width and height of the stamp image. Finally, fill the area with black;
[0025] The method of overlaying image text is:
[0026] First, use Python Image Library to import the handwriting font library, then randomly select a word from the content of the work where the seal is located, and then select a word from the random number r 8∈[5, 30] to select the font size of the text; again, select the font size of the text from the random number w ∈[1, a ], h∈[1, b ] respectively select an integer as the coordinate of the upper left corner of the area, where a and b Respectively represent the width and height of the seal image; finally, the covered image is cropped according to the size of the original seal image;
[0027] Image edge dilation method:
[0028] First, we need to convert the seal image to the HSV color space, which uses three components: hue, saturation, and brightness to represent color. By limiting the hue value range to [0, 36]∪[216, 300], we can extract the red area in the seal image. Then, we use the morphology module in skimage to dilate the red area and expand it horizontally and vertically. r 9 pixels, r 9∈[5,15], and finally fill the enlarged area with the average color in the red area.
[0029] Furthermore, the ViT deep learning model includes an input layer, an encoding layer, and an output layer;
[0030] Among them, the input layer receives the data-enhanced image and preprocesses the image to generate data for the encoding layer to learn;
[0031] The encoding layer includes multiple layers of superimposed Transformer encoders, which are used to identify the global feature information of the seal image;
[0032] The output layer includes a classification head, which includes a fully connected layer. The output dimension of the fully connected layer is equal to the number of categories in the data set. The classification head is used to map the feature representation generated by the Transformer encoder to the category probability distribution. After linear and nonlinear transformations through the fully connected layer, the output of the Transformer encoder is converted into a probability distribution through a softmax activation function, and finally the probability of each category is output, thereby realizing the recognition of the seal image.
[0033] Furthermore, the method for the input layer to process the image includes:
[0034] After the enhanced image is input to the input layer, the input layer adjusts the image size and then divides it into multiple square image blocks. The image block embedding module reduces it from three dimensions to one dimension to obtain the image block after dimension conversion. Then the position information is added, and the position information is a trainable matrix with the same dimension as the image block embedding. Finally, the image block embedding and the position embedding are added to obtain a matrix suitable for Transformer encoder learning.
[0035] Furthermore, the Transformer encoder includes a multi-head self-attention mechanism, attention weights and residual connections, a feedforward neural network, and regularization;
[0036] Among them, the multi-head self-attention mechanism obtains three representations of query, key and value by linearly transforming the input sequence, and then calculates the attention weight to obtain the self-attention encoding. The attention weight represents the correlation between each position in the input sequence and other positions;
[0037] In the weighted attention and residual connection, the result of the self-attention encoding is weightedly added to the input sequence, and a residual connection is added, allowing the attention mechanism to selectively fuse information at different positions;
[0038] In the feedforward neural network, the result after self-attention encoding is nonlinearly transformed through a feedforward neural network with two fully connected layers; the regularization operation is to perform layer normalization between the input and output of each sub-layer.
[0039] Furthermore, in step 3, the evaluation index is used as the accuracy P , recall rate R and F 1 value to evaluate the performance of the ViT deep learning model; specifically, first calculate each category separately P , R , F 1. Then find the average value of all categories. When calculating a certain category of samples, the samples of this category are positive samples, and the remaining samples are negative samples, which are defined as:
[0040]
[0041]
[0042]
[0043] in, n represents the total number of categories, TP i Indicates identification as i The number of samples that are correctly identified in the samples of the class; FP i Indicates the number of negative samples identified as positive samples, FN i is the number of positive samples identified as negative samples. P Indicates that it is correctly identified as i The number of samples of the class and all samples identified as i The ratio of the number of samples of the first class to the i The proportion of classes; R Indicates that it is correctly identified as i The number of samples of the class and the actual i The ratio of the number of samples of the class; F The value of 1 is P and R The equally weighted harmonic mean of P and R Evaluation of model performance.
[0044] Another object of the present invention is to provide a system for implementing the above-mentioned seal recognition method integrating data enhancement and deep learning, comprising:
[0045] An image acquisition module is used to obtain, identify and annotate the seal image;
[0046] A data enhancement module is used to perform data enhancement on the seal image in the image acquisition module to obtain enhanced image data;
[0047] The data set partitioning module divides the enhanced image data into training set, validation set and test set;
[0048] The model building and optimization module is used to build a ViT deep learning model based on the Vision Transformer model, and use the training set to train the ViT deep learning model and use the validation set to verify it to obtain the trained ViT deep learning model. The test set is then used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model whose performance meets the requirements. Finally, the obtained ViT deep learning model is used to recognize the seal image to be recognized.
[0049] Compared with the prior art, the beneficial effects of the present invention are as follows: In view of the problem that it is difficult to fully train a neural network model in the case of a lack of training set data in the current prior art, by analyzing the characteristics of seal images, data augmentation is carried out specifically for the scenarios that occur, effectively improving the generalization ability of the neural network model in the seal recognition task. At the same time, the ViT model with excellent feature extraction ability and strong scalability is used as the feature extractor for the seal recognition task, and better seal recognition results can be obtained. Therefore, the present invention has a certain application value for the spread of seal culture, provides a new research idea for quickly and accurately recognizing seal images in complex scenarios, and the data augmentation mode for seal data can provide a research basis for subsequent seal recognition research. Description of the Drawings
[0050] Figure 1 It is the overall flowchart of the seal recognition method that integrates data augmentation and deep learning in the embodiment of the present invention;
[0051] Figure 2 They are seal images of the same content in different styles in the embodiment of the present invention. Among them, (a) is one style and (b) is another style;
[0052] Figure 3 It is a data augmentation example of the seal image of "An Qi - Lin Cun" in the embodiment of the present invention. Detailed Embodiments
[0053] Next, the technical solutions in the embodiments of the present invention will be described clearly and completely in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0054] It should be noted that, without conflict, the embodiments in the present invention and the features in the embodiments can be combined with each other.
[0055] Next, the present invention will be further described in conjunction with specific embodiments, but it is not limited to the present invention.
[0056] In order to reduce the difficulty of user query and seal identification and improve the level of promotion of seal culture, a method that can quickly and accurately identify the content of seal images is urgently needed. Since the number of categories of seal recognition tasks is large and the number of samples in each category is small, directly using deep learning models for training will lead to poor model recognition results. Even if it is barely fitted through careful parameter adjustment, it is impossible to extract the deep semantic information of the seal, and it is difficult to identify seal images in complex situations. Therefore, in the face of the above dilemma, a larger data set is usually required, but the seal image data itself is relatively scarce, and the seal knowledge requirements of the annotator itself are high, resulting in a lack of large-scale annotated data sets in the field of seal recognition. In order to solve the above problems, such as Figure 1 As shown, the embodiment of the present invention discloses a seal recognition method integrating data enhancement and deep learning, comprising the following steps:
[0057] Step 1: Obtain a seal image, and identify and annotate the seal image, wherein the annotation content is intended to identify and record the owner of the seal and the specific content of the seal image;
[0058] In this step, a clear seal image is captured from the work and annotated. Figure 1 The left side shows the acquisition and annotation of seal data. To ensure that the basic seal image has high clarity, it is necessary to obtain the image of the calligraphy and painting work in TIFF format. TIFF format is a non-distortion compression format that can retain the color and level of the original image. Next, the seal images are captured one by one from the work, and the seal images that are severely damaged or unrecognizable are eliminated during the capture process. At the same time, the non-seal part of the captured image is minimized as much as possible, that is, the captured seal image should be close to the edge of the seal. Finally, domain experts identify and annotate the owner and content of the seal.
[0059] like Figure 2 As shown in (a) and (b), the owner may have multiple seals with the same content but different styles. Therefore, the "person-content-style-number" format is used in the annotation process, where the person is the owner of the seal, the content is the content contained in the seal, and the style is distinguished by English letters. The same seal may be stamped in different places, so it may appear multiple times during the collection process. To avoid repeated naming, each seal needs to be assigned a number. According to this annotation format, Figure 2 The seal of (a) is marked as "Qianlong-Wufu Wudaitang Guxi Tianzibao-A-1". Figure 2 The seal in (b) is labeled "Qianlong-Wufu Wudaitang Guxi Tianzibao-B-1". When training the deep learning model later, the number is removed and "character-content-style" is retained as the label of the seal image.
[0060] Step 2: Perform data enhancement on the annotated seal image to obtain enhanced image data and divide it into a training set, a validation set, and a test set;
[0061] Data augmentation is a method of expanding a dataset by rotation, cropping, brightness and contrast transformation, latent space transformation, etc. However, the information contained in the original dataset itself is limited. Some data augmentation methods can increase some information by introducing human prior knowledge, but this information cannot be increased indefinitely. Once a data augmentation method that does not match the task is used, noise will be introduced into the dataset, resulting in a decrease in recognition ability. Therefore, it is crucial to choose an appropriate data augmentation method based on the characteristics of the task. In the seal recognition task, no matter where the seal image is stamped, it cannot be separated from the original physical seal. By simulating the scene where the seal image appears, the seal data can be effectively expanded, thereby improving the generalization ability of the model. After the shape, texture, color, size, type and distribution of the seal image are analyzed by domain experts, a matching data augmentation method is formulated based on the understanding of the overall characteristics of the seal data and the different complex scenes in which the seal image is located. See Figure 3 , specifically including:
[0062] (1) Image reduction
[0063] Affected by factors such as image acquisition equipment, shaking during shooting, and image compression caused by network transmission, the seal image will be a degraded image with blur issues. It is difficult for the model to recognize blurred seal images that are not included in the training set. For this reason, this module simulates blurred seal images through the image reduction module. Specifically, by setting the random decimal r ∈[1, 3], and reduce the image r This can simulate the blurry image to a certain extent, making the algorithm more robust.
[0064] (2) Image Cropping
[0065] Calligraphy and paintings usually exist on paper and other media. In order to better protect the works, they sometimes need to be remounted. However, due to the size of the works or the influence of factors such as operating skills, the edge of the seal may be encapsulated inside during the mounting process. In addition, an inappropriate shooting angle may also cause the seal image to be blocked. The above reasons will cause the seal to be missing horizontally or vertically. In order to improve the recognition ability of missing seals, the seal is cropped horizontally or vertically to simulate the above scenario. Specifically, two random cropping decimals are set. r 1∈[0, 1] and crop ratio r 2∈[0.1,0.6]. r When 1<0.5, horizontal cropping is performed; when rWhen 1≥0.5, vertical cutting is performed. r When the second digit of 1 is an even number, choose to crop from top to bottom or from left to right; when r When the second digit of 1 is an odd number, choose to crop from right to left or from bottom to top. r 2 is the cropping ratio.
[0066] (3) Image brightness adjustment
[0067] Due to the influence of factors such as exposure time, aperture size, and changes in ambient light, the seal image may appear too bright or too dark during the acquisition process. In order to improve the robustness of the seal recognition model under brightness changes, the gamma value of the image can be adjusted to simulate image samples that are too bright or too dark. Specifically, a random decimal can be set r 3∈[0.2, 4], and set the Gamma value of the image to r 3. When r When 3<1, the image brightness will increase. r When 3>1, the brightness of the image will be reduced. This can simulate images with different brightness, thereby increasing the robustness of the model and improving the accuracy of seal recognition.
[0068] (4) Image contrast adjustment
[0069] In the process of collecting seal images, the hardware differences of the camera will have a significant impact on the contrast of the image. Especially with the popularity of smartphones today, users prefer to use more convenient mobile phones to take pictures. At the same time, in order to form a brand style, smartphone manufacturers may use different white balance algorithms, color restoration algorithms, HDR modes or exposure compensation strategies when taking pictures, resulting in large differences in contrast. In addition, the contrast of the seal image will also be affected by the quality of the ink itself and the amount of ink dipped. In order to simulate the above situation, you can set a random decimal r 4∈[0.2, 1.2], and set the contrast parameter value of the image to r 4. When r When 4<1, the contrast of the image increases, and when r When 4>1, the contrast of the image decreases.
[0070] (5) Image masking
[0071] Calligraphy and paintings that have been preserved for a long time are easily affected by environmental factors, resulting in stains and spots on the surface. At the same time, during the appreciation, borrowing or exhibition process, they will also be affected by external factors, further exacerbating the damage of the surface. In order to improve the accuracy of the seal recognition model in the above situation, a method can be adopted to randomly generate black squares to cover the seal. Specifically, first, a random integer r 5∈[1, 10], and then select the number of coverage areas from a random integer r 6∈[5, 50], and then select the side length of the coverage area from a random integer w ∈[1, a ]、 h ∈[1, b ] respectively select the horizontal and vertical coordinates of the upper left corner of the area, where a and b Respectively represent the width and height of the stamp image. Finally, fill the area with black.
[0072] (6) Image rotation
[0073] Due to the shaking that may occur during the stamping process, the incorrect placement of the seal and the angle of the camera, the seal image may be deflected. To address this problem, the image rotation module rotates the image at a random angle. Specifically, it generates a random integer r 7∈[-30, 30], and rotate the stamp image r 7 degrees. This can improve the model's recognition ability in this situation.
[0074] (7) Image flip
[0075] There is a horizontal flip relationship between the stamp mold and the stamp pattern. In order to meet people's recognition needs for the stamp pattern content, the stamp image is horizontally flipped to simulate the stamp pattern, so that the model has the ability to recognize the stamp content. At the same time, it is difficult to completely straighten the stamp pattern when shooting, so it is necessary to rotate the horizontally flipped pattern at a random angle to improve the recognition ability of the model. The fliplr method in the numpy library can be used to horizontally flip the image matrix, and the image rotation module method can be used to rotate the flipped stamp image at a random angle.
[0076] (8) Image and text coverage
[0077] If the writing process is not well planned, the blank area of the calligraphy and painting works will be limited, which will cause the ink to cover the seal. Or some authors will stamp their own seals on the writing to identify their own writing. In order to simulate the above situation, you need to generate some text and then overlay the text on the seal image. First, use PIL (Python Image Library) to introduce the handwriting font library, then randomly select a word from the content of "Lanting Preface", and then select a random number from the random number. r 8∈[5, 30] to select the font size of the text. Again, select the font size of the text from the random number w ∈[1, a ], h∈[1, b ] respectively select an integer as the coordinate of the upper left corner of the area, where a and b Respectively represent the width and height of the seal image. Finally, in order to prevent the font from being too large and exceeding the range of the seal image, the overlaid image needs to be cropped according to the size of the original seal image.
[0078] (9) Image texture coverage
[0079] Because calligraphy and painting works are affected by many factors during long-term storage and appreciation, such as folding, handling, and lighting, the seal may fade with fine lines. In addition, carriers of different materials, such as paper or silk cloth, will cause large deviations in the texture of the seal image, increasing the difficulty of seal recognition. At the same time, when users visit calligraphy and painting works in museums or libraries, the works are usually placed in glass cabinets, and circular spots or conical light beams may be produced when spotlights illuminate the works. In order to simulate these situations, the word image can be replaced with a pre-prepared texture image to add different texture details to the seal image. The specific method is similar to the image handwriting overlay module. You only need to replace the text with a texture image and overlay it on the seal image.
[0080] (10) Image random noise
[0081] In order to simulate the noise of image acquisition in the real world, Gaussian noise and salt and pepper noise can be added to the image. Gaussian noise can simulate various factors that affect image quality, such as thermal noise in image sensors and quantization noise in signal processors. Salt and pepper noise can simulate problems such as bad pixels in image sensors and loss during signal transmission. Generating noisy seal images can increase the complexity and randomness of the data set, thereby making the algorithm more robust. The present invention uses the random_noise method in the skimage library to add different noises.
[0082] (11) Image edge expansion
[0083] During the stamping process, the seal may take up too much ink, resulting in adhesion of the seal pattern, which will make it difficult to identify the seal. Therefore, the present invention simulates this situation by extracting the seal text area of the seal and expanding several pixels. First, the seal image needs to be converted to the HSV color space, which uses three components of hue, saturation, and value to represent color. By limiting the value range of hue to [0, 36]∪[216, 300], the red area in the seal image can be extracted. Next, the morphology module in skimage is used to expand the red area. In the present invention, the red area is expanded horizontally and vertically. r 9 pixels, r 9∈[5,15]. Finally, the enlarged area is filled with the average color of the red area.
[0084] In this example, 16 famous calligraphy and paintings are used, including "Lanting Preface", "Sacrifice to Nephew", "Cold Food Postscript", "Boyuan Postscript", "Jiuhua Postscript", "Kuaixue Shiqing Postscript", "Boyuan Postscript", "Mid-Autumn Postscript", "Zhongni Mengdian Postscript", "Shangyangtai Postscript", "Luo Shenfu", "Songfengge Postscript", "Shusu Postscript", "Self-narration Postscript", "Lianfang Poetry Postscript" and "Along the River During the Qingming Festival", which are saved in TIFF format. Domain experts intercepted and annotated 1,259 seal images, totaling 529 categories. Each category represents an image printed by a physical seal, such as Figure 2 The category of the seal on the left is "Qianlong-Wufu Wudaitang Guxi Tianzibao-A". This category is also the prediction target of the model. Through model prediction, the owner of the seal image and the seal content can be obtained. The owners of the seals include ancient royal families, nobles, calligraphers and painters, collectors, officials, institutions and other types, and the content covers names, collections, mottos, interests, reigns, positions and other aspects. The seal images in the dataset have a large time span and rich types, which can be used to reasonably evaluate the recognition ability of the model.
[0085] The above 1259 seal images are enhanced to generate new seal images. This includes using the above ten data enhancement processes (except the image flip module) to enhance the original seal, and each process generates ten new seal images based on one original seal image. Since the image flip module flips the seal image horizontally, the result is unique, so the original seal image is flipped horizontally once. In general, 101 new images are generated for each seal image through enhancement. The images obtained by the above data enhancement are divided into training set and test set. In order to more comprehensively evaluate the seal image recognition ability of the proposed method in complex situations and avoid data leakage, the test set should be independent of the training set and the validation set and cover all categories as much as possible. Therefore, independent seal data is obtained from the Internet as a test set, which contains seal images in various situations such as blur, incomplete, large brightness and contrast differences, angle deviation, handwriting coverage, different textures, and edge adhesion. The obtained seals are also subjected to the above data enhancement as the final test set. Finally, the above data sets were randomly sorted, and the final training set, validation set, and test set contained 1,259, 127,159, and 522 seal images, respectively.
[0086] Step 3: Build a ViT deep learning model based on the Vision Transformer model. The ViT deep learning model is used to recognize seal images. The training set is used to train the ViT deep learning model and the validation set is used to verify the trained model parameters. Finally, the test set is used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model that meets the requirements.
[0087] like Figure 1As shown on the right, the ViT deep learning model is used to complete the seal recognition task. The ViT deep learning model includes an input layer, an encoding layer, and an output layer; the input layer receives the data-enhanced image and preprocesses the image to generate data for the encoding layer to learn; the encoding layer includes a multi-layer stacked Transformer encoder, which is used to identify the global feature information of the seal image; the output layer is mainly composed of fully connected layers, which map the high-dimensional space of the hidden layer to the low-dimensional space, thereby achieving feature compression. The last output layer uses the softmax function as the activation function, which can output the probability of each category, thereby realizing the recognition of the seal image. The emergence of the ViT deep learning model broke the dominance of CNN in the field of computer vision. Unlike the CNN model, the ViT deep learning model uses Transformer to replace the convolution layer to extract image features. Since CNN is limited by the fixed size of the convolution kernel, it is difficult to obtain the global features of the input sequence. However, the self-attention mechanism in the Transformer can capture the relationship between all elements of the input sequence, thereby obtaining the global features of the output sequence. Therefore, the ViT deep learning model has stronger robustness than CNN in the face of image occlusion, data distribution shift, adversarial patch and image segmentation permutation. These situations are similar to the situation where the seal is covered by handwriting, the texture is different due to different materials of the seal position, there are stains on the seal image, and the seal image is cropped or missing. The use of the ViT deep learning model has a strong adaptability to the complex situation of seal recognition. However, the original seal data is difficult to meet the training requirements of the ViT deep learning model. The seal data after data enhancement just solves this problem. In addition, due to the strong scalability of Transformer, there is no performance saturation phenomenon with the increase of model parameters and data volume. Therefore, the use of the ViT deep learning model can also better adapt to the subsequent increase of data sets. In summary, the ViT deep learning model based on data enhancement can be well applied to the seal recognition task. Specifically, the method of ViT deep learning model to recognize seal images is as follows:
[0088] The image embedding block in the ViT deep learning model structure is used as the input layer, which is responsible for generating data forms suitable for Transformer encoder learning. Specifically, the image is input into the input layer, which first adjusts the image size to [224, 224, 3], where the parameters represent the height, width and number of channels of the image. Then, it is divided into 196 square image blocks with a side length of 16, and the image block embedding module is used to reduce it from three dimensions to one dimension. A vector of length 768 is used to represent each small image block of size [16, 16, 3]. At this time, the dimension of the input image is transformed into [196, 768]. However, for different seal images, the key image blocks are not fixed, and a single image block cannot replace the global features. Therefore, the [class] tag is added at the head position of the ViT deep learning model. Since the tag itself does not contain the seal image information, it is input into the Transformer together with the remaining 196 image block vectors to learn, and the global features of the seal image can be obtained. At the same time, position information is also a key feature of the seal image, and it is necessary to add position information before model training. The position information is a trainable matrix with a dimension of [197,768]. Then, the image block embedding and position embedding are added to obtain a matrix suitable for Transformer learning.
[0089] The encoding layer uses a 16-layer stacked Transformer encoder to learn the input matrix. The learned [class] label contains the global feature information of the input seal image. The multi-head self-attention mechanism, attention weighting and residual connection, feedforward neural network and regularization are the key parts of the Transformer encoder. The multi-head self-attention mechanism obtains three representations of query, key and value by linearly transforming the input sequence, and then calculates the attention weight to obtain the self-attention encoding. The attention weight represents the correlation between each position in the input sequence and other positions. In order to capture different relationships, the multi-head self-attention mechanism uses multiple sets of independent attention heads to calculate the attention weights in parallel. In the attention weighting and residual connection, the result of the self-attention encoding is weightedly added to the input sequence, and a residual connection is added. This can preserve the original information of the input sequence and allow the attention mechanism to selectively fuse information at different positions. In the feedforward neural network, the result of the self-attention encoding is nonlinearly transformed by a feedforward neural network with two fully connected layers. This can enhance the expression ability and nonlinear modeling ability of the model. In order to speed up the training process and improve the robustness of the model, layer normalization is also performed between the input and output of each sub-layer. This regularization operation helps to speed up the training process and improve the performance of the model.
[0090] The output layer contains a classification head, which is specially designed for seal recognition. The classification head consists of a fully connected layer whose output dimension is equal to the number of categories in the dataset. The main function of the classification head is to map the feature representation generated by the Transformer encoder to the category probability distribution. After linear and nonlinear transformations through the fully connected layer, it converts the output of the encoder into a probability distribution through the softmax activation function, and finally outputs the probability of each category. The classification head plays a decision-making role in the classification task, enabling the model to classify the input and predict the category it belongs to. Through backpropagation during the training process, the parameters of the classification head will be optimized according to the training data, so that the model can learn better feature representation and classification capabilities.
[0091] This paper uses the GPU server cluster of Wuhan University Supercomputer Center as the experimental platform. The server uses Intel(R) Xeon(R) E5-2640 CPU and Nvidia Tesla V100 GPU, and is equipped with 128GB memory. The operating system is CentOS 7.7. The experimental code is written based on Python 3.8 and Tensorflow 2.5 framework. In order to reduce the impact of random errors, the method of repeated experiments is adopted. Specifically, each experiment is repeated 10 times, and the average of the results is taken as the final experimental result. In addition, the experiment adopts the early stop strategy to avoid model overfitting and reduce the experimental time overhead. During the training process, when the loss value of the validation set has not decreased for three consecutive epochs (training rounds), the model training is stopped and the model with the smallest loss value is saved. This method can ensure the reliability of the experimental results and improve the experimental efficiency. In order to determine the optimal model parameter combination, the experiment adopts a grid search strategy, sets a candidate value list for each parameter, and obtains the experimental results of each parameter combination by traversing the loop. Finally, the combination with the best effect is selected as the final parameter of the model. The specific parameters and values are shown in Table 1.
[0092] Table 1 Model parameter settings
[0093]
[0094] In this embodiment, the test set is used to evaluate the performance of the ViT deep learning model. The evaluation index of the ViT deep learning model is the accuracy P (Precision)、Recall R (Recall) and F 1 value. In multi-classification tasks, the actual calculation is the macro average. First, calculate each category separately. P , R ,F 1, and then find the average value of all categories. When calculating a certain category of samples, the samples of this category are positive samples, and the rest of the samples are negative samples, which are defined as:
[0095]
[0096]
[0097]
[0098] in, n represents the total number of categories, TP i Indicates identification as i The number of samples that are correctly identified in the samples of the class; FP i Indicates the number of negative samples identified as positive samples, FN i is the number of positive samples identified as negative samples. P Indicates that it is correctly identified as i The number of samples of the class and all samples identified as i The ratio of the number of samples of the first class to the i The proportion of classes; R Indicates that it is correctly identified as i The number of samples of the class and the actual i The ratio of the number of samples of the class; F The value of 1 is P and R The equally weighted harmonic mean of P and R Evaluation of model performance.
[0099] Step 4: Use the ViT deep learning model obtained in step 3 to identify the seal image to be identified.
[0100] In order to verify the effectiveness of data enhancement and ViT deep learning models in seal recognition tasks, this embodiment selects CNN, VGG and ResNet as comparison models, wherein the hidden layer of the CNN model is composed of three convolutional layers and three pooling layers alternately superimposed, the number of neurons in the convolutional layer is 64, 128, 256, and the output layer is a two-layer fully connected layer. The VGG model has the advantages of simple structure and strong mobility, and is one of the most commonly used methods in the field of computer vision. ResNet can build a deeper network structure without gradient disappearance by introducing the concept of residual blocks. In order to explore the impact of model depth on seal recognition results, this experiment uses ResNet models of different scales, including ResNet50, ResNet101 and ResNet152. The VGG, ResNet and ViT models are pre-trained on the ImageNet data set by loading TensorFlow Hub, introducing certain prior knowledge. The comparison results shown in Table 2 are obtained.
[0101] Table 2 Comparison results
[0102] Model (%) (%) 1(%) CNN \ \ \ VGG 16 \ \ \ ResNet_ 50 \ \ \ ResNet_ 101 \ \ \ ResNet_ 152 \ \ \ ViT \ \ \ ∆CNN 28.90 20.16 23.75 ∆VGG 16 70.24 60.39 64.94 ∆ResNet_ 50 74.31 51.14 60.59 ∆ResNet_ 101 63.85 54.09 58.57 ∆ResNet_ 152 48.77 40.91 44.50 ∆ViT 77.43 68.63 72.76
[0103] Note: ∆ means data enhancement; \ means the model is not fitted (the relevant evaluation index is less than 10%).
[0104] As can be seen from Table 2, all models without data augmentation cannot achieve effective fitting. This is mainly due to the insufficient number of seal images in the original data set, with an average of only 2.4 seal images per category. In this case of data sparseness, it is difficult for the model to learn the differences between different seals, which leads to serious overfitting and the inability to accurately identify the seal images in the test set. Although the VGG16, ResNet, and ViT models were pre-trained on the ImageNet dataset and obtained certain prior knowledge, this still cannot help the model achieve effective fitting. However, after applying the data augmentation method, all models can achieve effective fitting, indicating that in the seal image recognition task, the data augmentation method can effectively solve the overfitting problem caused by data sparsity and improve the robustness of the model.
[0105] Specifically, the F1 values of CNN, VGG16, ResNet50, ResNet101, ResNet152 and ViT models increased to 23.75%, 64.94%, 60.59%, 58.57%, 44.50% and 72.76% respectively. After data enhancement, the model needs to process richer and more complex image features. Due to the relatively simple structure of the CNN model, it is difficult to capture sufficient features for seal recognition, resulting in F1 is the lowest. In contrast, the VGG16 model has a deeper network structure, including 13 convolutional layers and 3 fully connected layers, so its feature extraction capability is stronger and its seal recognition capability is enhanced. F 1 value increased to 41.19%. Generally, shallow convolution kernels are used to learn simple edge, texture and color features, while deep convolution kernels are used to combine shallow features to learn discriminative features for specific tasks. Therefore, the ResNet-50 model has a deeper network structure, but its F The value of 1 is 4.35% lower than that of VGG16. The reason may be that the deeper network structure of ResNet-50 can capture more complex combination features, but the content and color features of the seal image are relatively simple, and the overly strong feature extraction ability may lead to overfitting. Similarly, the value of ResNet-101 and ResNet-152 is F The value of 1 is reduced by 6.37% and 20.44% respectively compared with VGG16. The seal recognition task needs to consider not only local details, but also global features and their arrangement. Due to the self-attention mechanism in its Transformer, the ViT model can better capture the relationship between each image block, rather than focusing on local information like the convolution kernel. Therefore, after the data enhancement method, the ViT model can obtain richer global features and achieve the best seal recognition results.
[0106] Another object of the present invention is to provide a system for implementing the above-mentioned seal recognition method integrating data enhancement and deep learning, comprising:
[0107] An image acquisition module is used to obtain, identify and annotate the seal image;
[0108] A data enhancement module is used to perform data enhancement on the seal image in the image acquisition module to obtain enhanced image data;
[0109] The data set partitioning module divides the enhanced image data into training set, validation set and test set;
[0110] The model building and optimization module is used to build a ViT deep learning model based on the Vision Transformer model, and use the training set to train the ViT deep learning model and use the validation set to verify it to obtain the trained ViT deep learning model. The test set is then used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model whose performance meets the requirements. Finally, the obtained ViT deep learning model is used to recognize the seal image to be recognized.
[0111] The above are only preferred embodiments of the present invention, and are not intended to limit the implementation methods and protection scope of the present invention. Those skilled in the art should be aware that all solutions obtained by equivalent substitutions and obvious changes made using the contents of the specification of the present invention should be included in the protection scope of the present invention.
Claims
1. A seal recognition method integrating data enhancement and deep learning, characterized in that: The steps include: Step 1: Obtain a seal image, and identify and annotate the seal image; Step 2: Perform data enhancement on the annotated seal image to obtain enhanced image data and divide it into a training set, a validation set, and a test set; Step 3: Build a ViT deep learning model based on the Vision Transformer model. The ViT deep learning model is used to recognize seal images. The training set is used to train the ViT deep learning model and the validation set is used to verify the trained ViT deep learning model. Finally, the test set is used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model that meets the performance requirements. Step 4: Use the ViT deep learning model obtained in step 3 to identify the seal image to be identified; The data enhancement in step 2 includes image reduction, image cropping, image brightness adjustment, image contrast adjustment, image masking, image rotation, image flipping, image handwriting overlay, image texture overlay, image random noise and image edge expansion of the seal image; The method of image cropping is: Set two random decimals r 1∈[0, 1] and r 2∈[0.1, 0.6], when r When 1<0.5, horizontal cropping is performed; when r When 1≥0.5, vertical cutting is performed; During the cutting process, r 1 When the second digit is an even number, select from top to bottom or from left to right; r When the second digit of 1 is an odd number, choose to crop from right to left or from bottom to top. r 2 is the cropping ratio; The method to adjust the image brightness is: Set a random decimal r 3∈[0.2, 4], and set the Gamma value of the image to r 3. When r When 3<1, increase the image brightness. r When 3>1, reduce the image brightness; The method for adjusting image contrast is: Set a random decimal r 4∈[0.2, 1.2], and set the contrast parameter value of the image to r 4. When r When 4<1, the image contrast is increased, and when r When 4>1, the contrast of the image is reduced; The image masking method is: From random integers r 5∈[1, 10], and then select the number of coverage areas from a random integer r 6∈[5, 50], and then select the side length of the coverage area from a random integer w ∈[1, a ]、 h ∈[1, b ] respectively select the horizontal and vertical coordinates of the upper left corner of the coverage area, where: a and b Respectively represent the width and height of the stamp image. Finally, fill the area with black; The method of overlaying image text is: First, use Python Image Library to import the handwriting font library, then randomly select a word from the content of the work where the seal is located, and then select a word from the random number r 8∈[5, 30], select the font size of the word; again, select the font size of the word from the random number w ∈[1, a ], h∈[1, b ] respectively select an integer as the upper left corner coordinate of the area where the word is located, where a and b Respectively represent the width and height of the seal image, select an area containing the characters with the coordinates of the upper left corner as the starting point and cover the area on the seal image; finally, crop the covered image according to the size of the original seal image; Image edge dilation method: First, the seal image needs to be converted to the HSV color space, which uses three components, hue, saturation, and brightness, to represent color. The red area in the seal image is extracted by limiting the value range of hue to [0, 36]∪[216, 300]. Then, the morphology module in skimage is used to dilate the red area and expand it horizontally and vertically. r 9 pixels, r 9∈[5,15], and finally fill the enlarged area with the average color in the red area; The ViT deep learning model includes input layer, encoding layer and output layer; Among them, the input layer receives the data-enhanced image and preprocesses the image to generate data for the encoding layer to learn; The encoding layer includes multiple layers of superimposed Transformer encoders, which are used to identify the global feature information of the seal image; The output layer includes a classification head, which includes a fully connected layer. The output dimension of the fully connected layer is equal to the number of categories in the data set. The classification head is used to map the feature representation generated by the Transformer encoder to the category probability distribution. After linear and nonlinear transformations through the fully connected layer, the output of the Transformer encoder is converted into a probability distribution through a softmax activation function, and finally the probability of each category is output, thereby realizing the recognition of the seal image.
2. The seal recognition method integrating data enhancement and deep learning according to claim 1, characterized in that: The method for annotating the seal image in step 1 is: The seal image is annotated in the format of person-content-style-number, where the person is the owner of the seal, the content is the content contained in the seal, and the style is distinguished by letters; In the case where the same seal appears multiple times during the collection process, a number is assigned to each seal.
3. The seal recognition method integrating data enhancement and deep learning according to claim 1, characterized in that: The input layer processes images in the following ways: After the enhanced image is input to the input layer, the input layer adjusts the image size and then divides it into multiple square image blocks. The image block embedding module reduces it from three dimensions to one dimension to obtain the image blocks after dimension conversion. Then the position information is added, and the position information is a trainable matrix with the same dimension as the image block embedding. Finally, the image block embedding and the position embedding are added to obtain a matrix suitable for Transformer encoder learning.
4. The seal recognition method integrating data enhancement and deep learning according to claim 1, characterized in that: The Transformer encoder includes a multi-head self-attention mechanism, attention weights and residual connections, a feedforward neural network, and regularization; Among them, the multi-head self-attention mechanism obtains three representations of query, key and value by linearly transforming the input sequence, and then calculates the attention weight to obtain the self-attention encoding. The attention weight represents the correlation between each position in the input sequence and other positions; In the weighted attention and residual connection, the result of the self-attention encoding is weightedly added to the input sequence, a residual connection is added, and the attention mechanism is allowed to selectively fuse information at different positions; In the feedforward neural network, the result after self-attention encoding is nonlinearly transformed through a feedforward neural network with two fully connected layers; the regularization operation is to perform layer normalization between the input and output of each sub-layer.
5. The seal recognition method integrating data enhancement and deep learning according to claim 1, characterized in that: In step 3, the evaluation index used is accuracy P , recall rate R and F 1 value to evaluate the performance of the ViT deep learning model; first calculate each category separately P , R , F 1. Then find the average value of all categories. When calculating a certain category of samples, the samples of this category are positive samples, and the remaining samples are negative samples, which are defined as: in, n represents the total number of categories, TP i Indicates identification as i The number of samples that are correctly identified in the samples of the class; FP i Indicates the number of negative samples identified as positive samples, FN i is the number of positive samples identified as negative samples; P Indicates that it is correctly identified as i The number of samples of the class and all samples identified as i The ratio of the number of samples of the first class to the i The proportion of classes; R Indicates that it is correctly identified as i The number of samples of the class and the actual i The ratio of the number of samples of the class; F The value of 1 is P and R The equally weighted harmonic mean of P and R Evaluation of model performance.
6. A system for implementing the seal recognition method integrating data enhancement and deep learning as described in any one of claims 1 to 5, characterized in that: include: An image acquisition module is used to obtain, identify and annotate the seal image; A data enhancement module is used to perform data enhancement on the seal image in the image acquisition module to obtain enhanced image data; The data set partitioning module divides the enhanced image data into training set, validation set and test set; The model building and optimization module is used to build a ViT deep learning model based on the Vision Transformer model, and use the training set to train the ViT deep learning model and use the validation set to verify it to obtain the trained ViT deep learning model. The test set is then used to evaluate the performance of the trained ViT deep learning model and select the ViT deep learning model whose performance meets the requirements. Finally, the obtained ViT deep learning model is used to recognize the seal image to be recognized.
