Fine-grained flower image classification method based on CNN and ViT

By constructing the CNN-ViT fine-grained classification model and designing the CSA module, the problem of poor local feature extraction in flower images by Vision transformer was solved, the recognition accuracy and classification accuracy of fine-grained flower images were improved, and global and local features were achieved. effective capture.

CN118691885APending Publication Date: 2024-09-24CHANGZHOU UNIV
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410792389.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-19
Publication Date
2024-09-24

AI Technical Summary

Technical Problem

In the existing technology, the Vision transformer model performs poorly in the process of extracting local features of flower images, resulting in low fine-grained image recognition accuracy and poor task classification accuracy.

Method used

A fine-grained flower image classification method based on CNN and ViT is used to build a CNN-ViT fine-grained classification model and design a convolution parameter shared attention module (CSA) to replace the multi-head self-attention module in the ViT model to enhance local features. The extraction capability, combined with image preprocessing such as data partitioning, random cropping and Gaussian blur processing, improves image classification accuracy.

Benefits of technology

It significantly improves the recognition accuracy of fine-grained images of flowers and the accuracy of task classification, enhances the model's ability to capture global and local features, and improves its ability to capture details.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118691885A
    Figure CN118691885A
Patent Text Reader

Abstract

The invention relates to the technical field of image recognition, and discloses a fine-grained flower image classification method based on CNN and ViT. The method comprises the following steps: firstly, preparing a flower data set, wherein the data set comprises five categories of tulip, sunflower, rose, dandelion and daisy; secondly, performing data set preprocessing on the flower image, wherein the data set preprocessing comprises data set division, image cutting and Gaussian blur processing; constructing a CNN-ViT fine-grained classification model to further enhance the extraction of image feature information, thereby facilitating the improvement of the subsequent image classification accuracy; secondly, training a network model, inputting the preprocessed image into the network model for training, and storing the weight with the highest recognition precision in a model verification process; and loading the trained weight of the flower data set to the model, testing the test set, and calculating the identification accuracy of the test set. The method is reasonable in design, through constructing the CNN-ViT fine-grained classification model, the flower fine-grained image identification precision of the identification method is improved, and the task classification accuracy is good.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technical field of image recognition, and in particular to a fine-grained flower image classification method based on CNN and ViT. Background Art

[0002] With the improvement of social productivity, people's living standards have steadily risen, and the demand for beautiful things such as flowers has gradually increased. Therefore, the research on fine-grained classification of flowers has gradually attracted attention. The focus of fine-grained image classification is on a more detailed division between species, which can effectively distinguish different categories of the same species. Considering that the cost of manual classification is too high, it is necessary to use computers to develop efficient and accurate fine-grained flower image classification methods.

[0003] In recent years, with the increasing maturity of machine vision, fine-grained image classification technology, as one of the important research contents in its field, has also been developed accordingly. At present, a model or network Vision transformer has been proposed for fine-grained image recognition tasks. However, the Vision transformer model (Vit model) performs unsatisfactory in the process of extracting local features of images, resulting in low recognition accuracy of fine-grained images and poor accuracy of task classification. Summary of the invention

[0004] The technical problem to be solved by the present invention is: to overcome the deficiencies in the prior art and provide a fine-grained flower image classification method based on CNN and ViT. The recognition method of the present invention has high recognition accuracy of fine-grained flower images and good accuracy of task classification.

[0005] The technical solution adopted by the present invention to solve its technical problem is:

[0006] A fine-grained flower image classification method based on CNN and ViT specifically includes the following steps:

[0007] Step S1, prepare a flower fine-grained data set and classify five types of flower images: tulip, sunflower, rose, dandelion, and daisy;

[0008] Step S2, image preprocessing, data set division, image cropping, and Gaussian blur processing are performed on the classified five types of flower images;

[0009] Step S3: construct a CNN-ViT fine-grained classification model to further enhance the extraction of image feature information, so as to improve the accuracy of subsequent image classification;

[0010] Step S4: training the network model, inputting the preprocessed image into the network model for training, and saving the weight with the highest recognition accuracy during the model verification process;

[0011] Step S5: load the trained weights of the flower data set, test the test set, and calculate the recognition accuracy of the test set.

[0012] Furthermore, the step S2 specifically includes the following steps:

[0013] Step S21, data division, dividing the prepared flower fine-grained data set into a data set and a test set in a ratio of 6:4;

[0014] Step S22, image cropping, randomly cropping the input flower image to a size of 224×224 pixels; random cropping specifically includes: during the training process, randomly cropping the input image to generate multiple different training samples; the purpose of this is to introduce data diversity and increase the model's ability to recognize objects of different scales and positions;

[0015] Step S23, Gaussian blur processing, for the fine-grained flower test set image, the Gaussian blur method is used to reduce the overall image noise, and the Gaussian function is used as a template to perform convolution operation with the input image to remove the high-frequency components of the image and make the image blurred. In order to more accurately calculate the image gradient and edge amplitude, Gaussian blur uses normal distribution to calculate the transformation of each pixel in the image. The two-dimensional space normal distribution G(u,v) equation is expressed as follows:

[0016]

[0017] r 2 =u 2 +v 2 ;

[0018] Where u and v are the horizontal and vertical distances from the starting pixel, r is the blur radius, and σ is the standard deviation of the normal distribution.

[0019] In two-dimensional space, the contour lines of the generated surface are concentric circles with normal distribution starting from the center. The convolution matrix composed of pixels with non-zero distribution is transformed with the original image. The pixel value of each pixel is the weighted average of the values ​​of the surrounding adjacent pixels. The original pixel value has the largest Gaussian distribution value, so the weight is the largest. The weight of adjacent pixels gradually decreases as the distance from the original pixel increases.

[0020] Furthermore, in step S3, since the effect of extracting local information in the ViT model in the prior art is not good, the present invention designs a convolution parameter shared attention module CSA to replace the multi-head self-attention module MSA in the ViT model; the core structure of the CSA module is a shared convolution block, which first convolves the 2D features of the input encoder through a depth-separable convolution, and then converts the 2D feature vector output after the convolution into a 1D sequence of feature vectors L:

[0021] L = Flatten(DWConv2d(x i ,s,s));

[0022] Among them, x i is the projection feature before convolution, Flatten represents the conversion function from 2D to 1D features, DWCon2d represents the depthwise separable convolution operation, and s represents the drop rate.

[0023] Furthermore, in order to make the feature dimension of the query feature vector Q in the model consistent, the feature vector Q in the CSA module does not undergo a convolution operation, and the attention score is generated by the similarity between QL, which is expressed by the following formula:

[0024]

[0025] Among them, Attention(Q,L) represents the feature vector after attention weighting, d head represents the dimension of each attention head, Q represents the query feature vector in the CSA module, and L is the feature vector converted from a 2D feature vector to a 1D feature vector.

[0026] The CSA module adds convolution operations in the attention calculation process, which ensures the model's original global feature extraction capability while further enhancing the model's attention to local features, and can effectively improve the ability to capture details of fine-grained flower images.

[0027] Furthermore, the step S4 specifically includes the following steps:

[0028] Step S41: Set the network model parameters, the image slice size is 16 inches, the sliding window step size is 12 steps, the training parameter batch size is set to 2 per graphics card, the total number of training steps is set to 30000, the optimizer uses SGD, the momentum is set to 0.9, and the weight decay is set to 1×10 -4 ,The learning rate of the fine-grained flower test set is set to 0.03, and the cosine annealing learning rate decay strategy is used;

[0029] Step S42: Set the network loss function for the network model, train the network, and save the weight with the highest recognition accuracy during the model verification process.

[0030] Furthermore, the step S5 is specifically as follows: loading the model with the weight with the highest recognition accuracy during the verification process, testing its test set, and calculating the recognition accuracy of the test set.

[0031] The beneficial effects of the present invention are as follows: the present invention is reasonably designed, and by constructing a CNN-ViT fine-grained classification model, the multi-head self-attention module in the ViT model is replaced by a designed CSA module, so that the network can capture global and local features at the same time, further enhancing the feature extraction capability of the network, greatly improving the recognition accuracy of the flower fine-grained image recognition method, and at the same time, the accuracy of task classification is also good. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0033] Figure 1 It is a schematic diagram of the process of the present invention;

[0034] Figure 2 is a schematic diagram of a fine-grained flower dataset in the present invention;

[0035] Figure 3 It is a structural diagram of the CSA module in the present invention;

[0036] Figure 4 It is a comparison diagram of the image before and after Gaussian blur processing in the present invention. DETAILED DESCRIPTION

[0037] It should be noted that the following detailed descriptions are illustrative and are intended to provide further explanation of the present application. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present application belongs.

[0038] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present application. As used herein, unless the context clearly indicates otherwise, the singular form also includes the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this description, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.

[0039] The technical solution of the present invention will be clearly and completely described below in conjunction with the embodiments. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0040] Example 1

[0041] like Figure 1 A fine-grained flower image classification method based on CNN and ViT is shown, which specifically includes the following steps:

[0042] Step S1: prepare a fine-grained flower dataset. In the present invention, a fine-grained flower dataset is prepared, which contains 5425 images of flowers of different types, including tulips, sunflowers, roses, dandelions, and daisies. Figure 2 As shown;

[0043] Step S2, image preprocessing, data set division, image cropping, and Gaussian blur processing are performed on the classified five types of flower images;

[0044] Step S3: construct a CNN-ViT fine-grained classification model to further enhance the extraction of image feature information, so as to improve the accuracy of subsequent image classification;

[0045] Step S4: training the network model, inputting the preprocessed image into the network model for training, and saving the weight with the highest recognition accuracy during the model verification process;

[0046] Step S5: load the trained weights of the flower data set, test the test set, and calculate the recognition accuracy of the test set.

[0047] Furthermore, the step S2 specifically includes the following steps:

[0048] Step S21, data division, dividing the prepared flower fine-grained data set into a data set and a test set in a ratio of 6:4, with 3255 images in the data set and 2170 images in the test set;

[0049] Step S22, image cropping, randomly cropping the input flower image to a size of 224×224 pixels;

[0050] Step S23, Gaussian blur processing, for the fine-grained flower test set image, the Gaussian blur method is used to reduce the overall image noise, and the Gaussian function is used as a template to perform convolution operation with the input image to remove the high-frequency components of the image and make the image blurred. In order to more accurately calculate the image gradient and edge amplitude, Gaussian blur uses normal distribution to calculate the transformation of each pixel in the image. The two-dimensional space normal distribution G(u,v) equation is expressed as follows:

[0051]

[0052] r 2 =u 2 +v 2 ;

[0053] Where u and v are the horizontal and vertical distances from the starting pixel, r is the blur radius, and σ is the standard deviation of the normal distribution.

[0054] In two-dimensional space, the contour lines of the generated surface are concentric circles with normal distribution starting from the center. The convolution matrix composed of pixels with non-zero distribution is transformed with the original image. The pixel value of each pixel is the weighted average of the values ​​of the surrounding adjacent pixels. The original pixel value has the largest Gaussian distribution value, so the weight is the largest. The weight of adjacent pixels gradually decreases as the distance from the original pixel increases.

[0055] Figure 4 This is a comparison diagram before and after the Gaussian blur processing of the image in step S23.

[0056] Step S3 constructs a CNN-ViT fine-grained classification model. Since the ViT model in the prior art has a poor effect on extracting local information, the present invention designs a convolution parameter shared attention module CSA (module CSA structure diagram as shown in Figure 3 The core structure of the CSA module is the shared convolution block. The shared convolution module first convolves the 2D features of the input encoder through a depth-separable convolution, and then converts the 2D feature vector output after the convolution into a 1D sequence feature vector L:

[0057] L = Flatten(DWConv2d(x i ,s,s));

[0058] Among them, x i is the projection feature before convolution, Flatten represents the conversion function from 2D to 1D features, DWCon2d represents the depthwise separable convolution operation, and s represents the drop rate.

[0059] In order to make the feature dimension of Q vector consistent in the model, the feature vector Q in the CSA module does not undergo convolution operation, and the attention score is generated by the similarity between QL, which is expressed as follows:

[0060]

[0061] Among them, Attention(Q,L) represents the feature vector after attention weighting, d head represents the dimension of each attention head, Q represents the query feature vector in the CSA module, and L is the feature vector converted from a 2D feature vector to a 1D feature vector.

[0062] The CSA module adds convolution operations in the attention calculation process, which ensures the model's original global feature extraction capability while further enhancing the model's attention to local features, and can effectively improve the ability to capture details of fine-grained flower images.

[0063] Step S4 specifically includes the following steps:

[0064] Step S41: Set the network model parameters, the image slice size is 16 inches, the sliding window step size is 12 steps, the training parameter batch size is set to 2 per graphics card, the total number of training steps is set to 30000, the optimizer uses SGD, the momentum is set to 0.9, and the weight decay is set to 1×10 -4 ,The learning rate of the fine-grained flower test set is set to 0.03, and the cosine annealing learning rate decay strategy is used;

[0065] Step S42: setting the network loss function for the network model, training the network, and saving the weight with the highest recognition accuracy during the model verification process;

[0066] The network loss function uses the cross entropy loss function, and the calculation formula of the cross entropy loss function is as follows:

[0067]

[0068] Where C represents the error, x represents the sample, y represents the actual value, a represents the output value, and n represents the total number of samples.

[0069] Step S5 is specifically as follows: loading the model with the weight with the highest recognition accuracy during the verification process, testing its test set, and calculating the recognition accuracy of the test set.

[0070] In summary, the present invention is reasonably designed. By constructing a CNN-ViT fine-grained classification model and replacing the multi-head self-attention module in the ViT model with the designed CSA module, the network can capture global and local features at the same time, further enhancing the feature extraction capability of the network, greatly improving the recognition accuracy of the fine-grained image of flowers in the recognition method, and at the same time, the accuracy of task classification is also good.

[0071] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A fine-grained flower image classification method based on CNN and ViT, Features: The specific steps include: Step S1, prepare a flower fine-grained data set and classify five types of flower images: tulip, sunflower, rose, dandelion, and daisy; Step S2, image preprocessing, data set division, image cropping, and Gaussian blur processing are performed on the classified five types of flower images; Step S3: construct a CNN-ViT fine-grained classification model to further enhance the extraction of image feature information, so as to improve the accuracy of subsequent image classification; Step S4: training the network model, inputting the preprocessed image into the network model for training, and saving the weight with the highest recognition accuracy during the model verification process; Step S5: load the trained weights of the flower data set, test the test set, and calculate the recognition accuracy of the test set.

2. A fine-grained flower image classification method based on CNN and ViT according to claim 1, Features: The step S2 specifically includes the following steps: Step S21, data division, dividing the prepared flower fine-grained data set into a data set and a test set in a ratio of 6:4; Step S22, image cropping, randomly cropping the input flower image to a size of 224×224 pixels; Step S23, Gaussian blur processing, for the fine-grained flower test set image, the Gaussian blur method is used to reduce the overall image noise, and the Gaussian function is used as a template to perform convolution operation with the input image to remove the high-frequency components of the image and make the image blurred. In order to more accurately calculate the image gradient and edge amplitude, Gaussian blur uses normal distribution to calculate the transformation of each pixel in the image. The two-dimensional space normal distribution G(u,v) equation is expressed as follows: r 2 =u 2 +v 2 ; Where u and v are the horizontal and vertical distances from the starting pixel, r is the blur radius, and σ is the standard deviation of the normal distribution.

3. The fine-grained flower image classification method based on CNN and ViT according to claim 1, Features: The CNN-ViT fine-grained classification model constructed in step S3 adopts a convolution parameter shared attention module CSA. The core structure of the CSA module is a shared convolution block. The shared convolution module in the CSA module first performs convolution processing on the 2D features of the input encoder through a depth-separable convolution, and then converts the 2D feature vector output after the convolution into a 1D sequence feature vector L: L=Flatten(DWConv2d(x i ,s,s)); Among them, x i is the projection feature before convolution, Flatten represents the conversion function from 2D to 1D features, DWCon2d represents the depthwise separable convolution operation, and s represents the drop rate.

4. The fine-grained flower image classification method based on CNN and ViT according to claim 3, Features: The CSA module includes a query feature vector Q. The feature vector Q does not undergo a convolution operation. The attention score is generated by the similarity between QL, which is expressed as follows: Among them, Attention(Q,L) represents the feature vector after attention weighting, d head represents the dimension of each attention head, Q represents the query feature vector in the CSA module, and L is the feature vector converted from a 2D feature vector to a 1D feature vector.

5. The fine-grained flower image classification method based on CNN and ViT according to claim 1, Features: The step S4 specifically includes the following steps: Step S41: Set the network model parameters, the image slice size is 16 inches, the sliding window step length is 12 steps, the training parameter batch size is set to 2 per graphics card, the total number of training steps is set to 30000, the optimizer uses SGD, the momentum is set to 0.9, and the weight decay is set to 1×10 -4 ,The learning rate of the fine-grained flower test set is set to 0.03, and the cosine annealing learning rate decay strategy is used; Step S42: Set the network loss function for the network model, train the network, and save the weight with the highest recognition accuracy during the model verification process.

6. The fine-grained flower image classification method based on CNN and ViT according to claim 1, Features: The step S5 specifically includes: loading the model with the weight with the highest recognition accuracy during the verification process, testing its test set, and calculating the recognition accuracy of the test set.

Citation Information

Cited By

  • Flower quality grading method, equipment and medium

    CN120071063A