Fine-grained image classification method based on feature fusion and semantic enhancement

Through the multi-level attention mechanism and semantic enhancement technology combined with the cross-attention mechanism, the problem of insufficient feature extraction in fine-grained image classification is solved, and the classification accuracy is improved, especially in complex backgrounds and high-similarity sub-category scenarios. .

CN118799646AActive Publication Date: 2024-10-18SICHUAN DIGITAL ECONOMY RESEARCH INSTITUTE (YIBIN) +1

Patent Information

Application Number
CN202411084301.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-08
Publication Date
2024-10-18
Estimated Expiration
2044-08-08

AI Technical Summary

Technical Problem

Existing fine-grained image classification technology has difficulty in accurately locating discriminative feature areas and extracting discriminative features in scenarios with high sub-category similarity and complex image background information, and it is difficult for a single-level attention mechanism and a single loss function to distinguish subtle differences.

Method used

A multi-level attention mechanism combined with semantic enhancement technology is used to extract global features and local features through the ViT model, and the cross-attention mechanism is used to fuse features, and cross-entropy and contrast loss functions are designed to optimize the model.

Benefits of technology

It improves the positioning ability and feature extraction accuracy of discriminative feature areas, and improves classification accuracy, especially the performance in complex backgrounds and high-similarity subcategory scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118799646A_ABST
    Figure CN118799646A_ABST
Patent Text Reader

Abstract

The invention discloses a fine-grained image classification method based on feature fusion and semantic enhancement. The method comprises the following steps: firstly, performing feature extraction by using a ViT (Visual Transform) model, segmenting an input image into non-overlapping patches, converting the patches into embedded vectors through linear projection, and inputting the embedded vectors into a Transform encoder to generate global features; then, through combination of multi-level attention fusion and semantic information, the attention weight in each layer of Transformer is extracted, and in combination with semantic embedding generated by a pre-training language model, the importance score of each token is calculated, and a key token is selected; then, carrying out secondary blocking and projection on the key token, and reselecting a secondary key token; and fusing the global features and the local features through a cross attention mechanism to generate fused features. And finally, combining the fused features with the global classification features, inputting the combined features into a classifier for classification, and generating classification output. Through multi-level attention fusion, semantic enhancement and key token selection, accurate positioning of a fine-grained image discriminative feature region is realized, the feature discrimination is enhanced, and the classification accuracy is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field The present invention belongs to the technical field of fine-grained image classification, and specifically, relates to a fine-grained image classification method based on feature fusion and semantic enhancement. The invention aims to improve the deficiencies of the existing fine-grained image classification method in accurately locating the discriminative feature area and extracting the discriminative features through strategies such as multi-level attention mechanism and semantic enhancement, thereby improving the classification accuracy of the model in scenarios with high sub-category similarity and complex image background information. Background Art Fine-grained image classification is an important research direction in computer vision tasks, which aims to classify and identify images of similar subcategories under a large category. Fine-grained image recognition has a wide range of applications in biodiversity detection, smart retail systems, smart transportation, and military target recognition. Existing fine-grained image recognition technology mainly relies on deep learning methods, which have shortcomings in feature extraction and feature fusion. Chinese patent application CN 118135290A proposes a Transformer-based fine-grained image classification method. The Transformer backbone network is used to divide the image into small tiles, and then each token is scored through the information discarding module, and low-scoring tokens are filtered out according to the score. The feature selection module is then used to select strong discriminant areas and splice to obtain aggregated features. This method can filter out noise and irrelevant tokens to a certain extent, and then select discriminative feature areas. However, it is difficult to fully extract discriminative fine-grained features in the image using a single-stage visual transformer (ViT) model. The information discarding module may discard some key tokens containing discriminative information; and using only a single cross-entropy loss function to optimize the model results in insufficient discriminability of the extracted features, making it difficult to distinguish subtle differences between subcategories. Chinese patent application CN 118262180A proposes a fine-grained image classification method based on multi-level division of foreground and background and feature fusion. This method extracts features by image segmentation based on the distribution value of attention intensity, obtains foreground images and multi-level background images, and performs weighted fusion twice to improve classification accuracy. However, when a single-level attention mechanism assigns weights to all tokens, it is difficult to utilize highly discriminative features and fully extract fine-grained information of the image; the single-level feature weighting method is difficult to fully integrate the fine-grained features of the image with the global features, which reduces the accuracy of classification.

[0006] Chinese patent CN 114676776 A proposes a Transformer-based fine-grained image classification method. Each head of the multi-head attention mechanism selects the token feature with the highest correlation with the classification token as the global feature, then cuts out the local area containing the semantic component from the input image to obtain the local feature, and fuses the two features. However, there are problems of feature duplication and feature loss when selecting global features through multiple heads; selecting image block tokens with a high correlation with the classification token based on empirical thresholds cannot flexibly adapt to different images; using a single center loss function lacks direct optimization of the classification boundary, and it is difficult to find an accurate classification boundary in scenarios where there are only slight differences between categories or feature space overlap. Summary of the invention In order to solve the above problems, the present invention provides a fine-grained image classification method based on feature fusion and semantic enhancement. The fine-grained features of the image are extracted layer by layer through a multi-level attention mechanism to accurately locate the discriminative feature area; semantic enhancement technology and key token selection mechanism are introduced to extract global features and fine-grained features with strong discriminativeness based on the ViT model, and the cross-attention mechanism is used to fuse global features and fine-grained features to more comprehensively capture the details and global information of the image; the model is optimized based on cross entropy loss and contrast loss, which is conducive to extracting discriminative features and improving classification accuracy. A fine-grained image classification method based on feature fusion and semantic enhancement includes the following steps: S1.ViT model feature extraction: The input image I is divided into non-overlapping patches and converted into an embedding vector E through linear projection i , input to the Transformer encoder to generate the global feature E global . S2. Multi-level attention fusion and key token selection combining semantic information: extracting the attention weights a in each layer of Transformer l , and combined with the semantic embedding emb generated by the pre-trained language model, calculate the importance score s of each token i , select the key token and record it as z key . S3. Semantic enhancement and refinement of key token selection: for z in S2 key Perform secondary block division and projection to generate a new embedding vector E′ i,j , re-enter the Transformer encoder, and select the secondary key token again, recorded as z′ key . S4. Cross-attention fusion: Through the cross-attention mechanism, the global feature Z global and local feature z′ keyFusion is performed to generate fusion feature Z fused . S5. Feature fusion and classification: The fusion feature Z fused With the global classification feature E global Combine, input into the classifier for classification, and generate the classification output y′. S6. Loss function design and training: Design cross entropy loss L cross and contrast loss L con , and optimize the model parameters through back-propagation. Furthermore, the feature extraction of the ViT model in S1 includes: S101. Image segmentation and linear projection: The input image I is segmented into several non-overlapping patches (i.e., tokens) of size 16×16, and then each patch P is linearly projected. i Converted to an embedding vector E i As shown in formula (1): E i = LinearProjection(P i ) (1) Among them, E i Represents the embedding vector of the i-th patch. S102. Embedding sequence combination: combine all embedding vectors into a sequence E = [E1, E2, ..., E i ] and add a classification token embedding vector E0: E′=[E0,E1,E2,...,E i ] S103. Transformer encoder: Input the embedding vector sequence E′ into the multi-layer Transformer encoder, and generate the query matrix Q, key matrix K and value matrix V through linear transformation. Each layer of Transformer encoder consists of a multi-head self-attention mechanism and a feedforward neural network. The attention mechanism calculation process is shown in formula (2): Among them, softmax represents the softmax activation function d k represents the dimension of the key matrix; A l represents the attention weight matrix of the lth layer. S104. Global features: A1. In the Transformer encoder of all layers, the classification token E0 will absorb information from other tokens layer by layer and update the vector E containing global features. global . A2. Take the weighted sum of the attention weight matrices of all layers to obtain the global feature representation Z fused by the attention mechanism globa , as shown in formula (3): Among them, V l A represents the value matrix of the lth layer; l represents the attention weight matrix of image I at layer l; L represents the total number of layers of Transformer; Z global Represents the global feature representation. Furthermore, the key token selection for multi-level attention fusion and semantic information in S2 includes: S201. Extract and fuse attention weights: Extract the attention weight A of each token from each layer of Transformer in S1 l The attention weights of all layers are fused by matrix multiplication to generate the fused global attention, as shown in formula (4): Among them, a fimal represents the fused global attention, Represents the attention weight of each token in the first layer. S202. Normalization: Normalize the attention weight of each token in the global attention, as shown in formula (5): Among them, a final,i represents the attention weight of the i-th token, ∑ j a final,j Represents the sum of the attention weights of all tokens in the global attention; Represents the normalized attention weight of the ith token. S203. Generate semantic embedding and fuse semantic information to calculate importance score: Use the pre-trained language model to generate semantic embedding emb for each token, then multiply the normalized attention weight and the generated semantic embedding element by element (Hadamard product), and perform normalization to generate enhanced attention weight, as shown in formula (6): Among them, ⊙ represents element-by-element multiplication; emb i Represents the semantic embedding vector of the i-th token; a enhanced,i Represents the enhanced attention weight of the i-th token. Use the enhanced attention weight a enhanced,iCalculate the importance score of each token as shown in formula (7): a i =a enhanced,i ·emb i (7) Among them, s i Indicates the importance score of the i-th token. Select several tokens with the highest scores as key tokens and record them as z key . 4. Further, the semantic enhancement and refinement of key token selection in S3 include: S301. Perform secondary block division and linear projection on the key token: z selected in S2 key Further block division is performed to divide each key token into patches of size 16×16 again. Linear projection is performed on these newly divided patches to generate new embedding vectors, as shown in formula (8): E′ i,j =LinearProjection(P i,j ) (8) Among them, P i,j represents the jth new block of the i-th key token; E′ i,j Represents the embedding vector of the jth new chunk of the i-th key token. S302. Embedding sequence combination: These embedding vectors are combined into a new sequence, and the classification token E0 is added, which is input into the Transformer encoder for processing, as shown in formula (9): E″ i =[E0,E′ i,1 ,E′ i,2 ,…,E′ i,j ] (9) S303. Secondary selection of key token: The calculation method is the same as S2. Calculate the secondary attention weight and select the secondary key token denoted as z′ key . Furthermore, the cross-attention fusion in S4 includes: S401. Introducing the cross attention mechanism: After the secondary key token selection, the cross attention mechanism is introduced to fuse the global features and local features. The cross attention is calculated as shown in formula (10): Among them, Q represents the global feature Z global The query matrix of ; K represents the secondary key token feature z′ key The bond matrix of kj represents the dimension of the key matrix; Across represents the criss-cross attention weight matrix. S402. Apply the cross attention weight to generate the fusion feature as shown in formula (11): Z fused =A cross V (11) Where V represents the secondary key token feature z′ key The value matrix, Z fused Represents the fused feature matrix. Furthermore, feature fusion and classification in S5 include: S501. Final feature fusion: Fuse the cross-attention feature Z fused and the global classification feature vector E obtained in S1 global The fusion is performed as shown in formula (12): Z final =[E global ,Z fused ] (12) Among them, Z final Represents the final fused feature vector. S502. Classification output: The final fused feature vector Z containing the classification token and the cross-attention fusion feature final Input into the classifier for classification, as shown in formula (13): y′=Classifier(Z final ) (13) Among them, Classifier represents the classifier; y′ represents the classification output of the model. Furthermore, the loss function design and training in S6 include: A1. Calculate the cross entropy loss: After each forward propagation, calculate the cross entropy loss of the classification task, as shown in formula (14): Among them, y n represents the true label of the nth sample; p n represents the predicted probability of the nth sample; L cross represents the cross entropy loss. 0066]A2. Calculation of contrast loss: In order to further extract discriminative features, maximize the similarity between samples of the same category, and minimize the similarity between samples of different categories, after each forward propagation, calculate the contrast loss, as shown in formula (15): Where B represents the batch size; α represents the hyperparameter that controls the similarity threshold; y n and mIndicates the labels of the nth sample and the mth sample; Sim(z n ,z m ) represents the similarity between the nth and mth samples; is the Kroneckerdelta function, when y n =y m When it is 1, otherwise it is 0; L con represents contrast loss. A3. Combined loss function: the final loss function L F is the cross entropy loss L cross and contrast loss L con The weighted sum of is shown in formula (16): L F =L cross +βL con (16) Among them, β represents the weight of contrast loss. A4. Back propagation and parameter update: After each forward propagation, the total loss function L is calculated F And back propagate to minimize the total loss, as shown in formula (17): Among them, θ represents the model parameters; η represents the learning rate; Represents the gradient of the loss function with respect to the model parameters. Compared with the prior art, the present invention has the following advantages: 1. The present invention introduces semantic embedding generated by a pre-trained language model to enhance the discriminability of features. Through multi-level attention fusion and key token selection combined with semantic information, the present invention improves the ability to locate discriminative feature areas. In addition, through secondary block division and selection of key tokens, the present invention further refines the feature extraction process, captures more discriminative fine-grained features, and improves the classification accuracy of the model in scenarios such as complex backgrounds and high-similarity subcategories. 2. The present invention fuses global features and local features through a cross-attention mechanism, thereby enhancing the discriminability of features and improving classification accuracy. 3. The present invention combines cross entropy loss and contrast loss. Compared with the method of using a single cross entropy loss function, the introduction of contrastive learning is conducive to enhancing the discriminability of features, alleviating the problem of difficulty in distinguishing subtle differences between categories, and improving classification accuracy. BRIEF DESCRIPTION OF THE DRAWINGS Figure 1 is a flow chart of the method of the present invention; Figure 2 This is a network structure diagram of the present invention. DETAILED DESCRIPTION In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution in the implementation mode of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described implementation mode is only a specific implementation mode of the present invention, not all implementation modes. Based on the implementation mode of the present invention, all other implementation modes obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention. The present invention captures highly discriminative image features through key tokens and introduces semantic information. Using a two-stage ViT model, the first stage captures key tokens with global features, and the second stage further refines the blocks based on the key tokens to capture key tokens with more discriminative local features, alleviating the shortcomings of inaccurate regional positioning and incomplete feature extraction of discriminative features of fine-grained image subcategories. This paper applies computer vision technology to fine-grained image classification tasks and proposes a fine-grained image classification method based on feature fusion and semantic enhancement, which provides new ideas for fine-grained image classification problems. Specifically: like Figure 1 , Figure 2 As shown, a fine-grained image classification method based on feature fusion and semantic enhancement includes the following steps: S1.ViT model feature extraction: The input image I is divided into non-overlapping patches and converted into an embedding vector E through linear projection i , input to the Transformer encoder to generate the global feature E global ; The step S1 comprises: S101. Image segmentation and linear projection: The input image I is divided into several non-overlapping patches (i.e., tokens) of size 16×16, and each patch P is transformed into i Converted to an embedding vector E i , as shown in formula (1): E i =LinearProjection(P i ) (1) Among them, E i Represents the embedding vector of the i-th patch. S102.Embedded sequence combination: Combine all embedding vectors into a sequence E = [E1, E2, ..., E i ] and add a classification token embedding vector E0: E′=[E0,E1,E2,...,E i ] S103.Transformer encoder: The embedding vector sequence E′ is input into the multi-layer Transformer encoder, and the query matrix Q, key matrix K and value matrix V are generated through linear transformation. Each layer of Transformer encoder consists of a multi-head self-attention mechanism and a feedforward neural network. The attention mechanism calculation process is shown in formula (2): Among them, softmax represents the softmax activation function d k represents the dimension of the key matrix; A l represents the attention weight matrix of the lth layer. S104. Global feature acquisition: A1. In the Transformer encoder of all layers, the classification token E0 will absorb information from other tokens layer by layer and update the vector E containing global features. global . A2. Take the weighted sum of the attention weight matrices of all layers to obtain the global feature representation Z fused by the attention mechanism globa , Z globa The calculation formula is shown in formula (3): Among them, V l A represents the value matrix of the lth layer; l represents the attention weight matrix of the lth layer; L represents the total number of layers of Transformer; Z global Represents the global feature representation. S2. Multi-level attention fusion and key token selection combining semantic information: Extracting the attention weights A in each layer of Transformer l , and combined with the semantic embedding emb generated by the pre-trained language model, calculate the importance score s of each token i , select the key token and record it as z key ; The step S2 comprises: S201. Extract and fuse attention weights: Extract the attention weight A of each token from each layer of Transformer in S1 l The attention weights of all layers are fused by matrix multiplication to generate the fused global attention, a final The calculation process is shown in formula (4): Among them, a final represents the fused global attention, Represents the attention weight of each token in the first layer. S202. Normalization processing:

[0110] Normalize the attention weight of each token in the global attention. The calculation formula is shown in formula (5): Among them, a final,i represents the attention weight of the i-th token, ∑ j a final,j Represents the sum of the attention weights of all tokens in the global attention; Represents the normalized attention weight of the ith token. S203. Generate semantic embedding and fuse semantic information to calculate importance score: The pre-trained language model is used to generate a semantic embedding emb for each token. The normalized attention weight is then element-wise multiplied with the generated semantic embedding (Hadamard product) and normalized to generate an enhanced attention weight, as shown in formula (6): Among them, ⊙ represents element-by-element multiplication; emb i Represents the semantic embedding vector of the i-th token; a enhanced,i Represents the enhanced attention weight of the i-th token. Use the enhanced attention weight a enhanced,i Calculate the importance score of each token, s i The calculation process of is shown in formula (7): s i =a enhanced,i ·emb i (7) Among them, s i Indicates the importance score of the i-th token. Select several tokens with the highest scores as key tokens and record them as z key . S3. Semantic enhancement and refinement of key token selection: For z in step 2 key Perform secondary block division and projection to generate a new embedding vector E′ i,j , re-enter the Transformer encoder, and select the secondary key token again, recorded as z′ key ; The step S3 comprises: S301. Perform secondary block division and linear projection on the key token: The z selected in S2 key Further block division is performed to divide each key token into 4×4 patches. Linear projection is performed on these newly divided patches to generate new embedding vectors, as shown in formula (8): E′ i,j =LinearProjection(P i,j ) (8) Among them, P i,j represents the jth new block of the i-th key token; E′ i,j Represents the embedding vector of the jth new chunk of the i-th key token. S302. Embedded sequence combination: These embedding vectors are combined into a new sequence, and the classification token E0 is added and input into the Transformer encoder for processing, as shown in formula (9): E″ i =[E0,E′ i,1 ,E′ i,2 ,…,E′ i,j ] (9) S303. Secondary selection of key token: The calculation method is the same as S2. Calculate the secondary attention weight and select the secondary key token as z′ key . S4. Cross-attention fusion: Through the cross-attention mechanism, the global feature Z global and local feature z′ key Fusion is performed to generate fusion feature Z fused ; The step S4 comprises: S401. Introducing the cross attention mechanism: After the secondary key token selection, the cross attention mechanism is introduced to fuse global features and local features. The cross attention is calculated as shown in formula (10): Among them, Q represents the global feature Z global The query matrix of ; K represents the secondary key token feature z′ key The bond matrix of k represents the dimension of the key matrix; A cross represents the criss-cross attention weight matrix. S402. Apply the cross attention weight to generate the fusion feature as shown in formula (11): Z fused =A cross V (11) Where V represents the secondary key token feature z′ key The value matrix, Z fused Represents the fused feature matrix. S5. Feature fusion and classification: The fusion feature Z fused With the global classification feature E global Combine, input into the classifier for classification, and generate classification output y′; The step S5 comprises: S501. Final feature fusion: The cross-attention fusion feature Z fused and the global classification feature vector E obtained in S1 global The fusion is performed as shown in formula (12): Z final =[E global ,Z fused ] (12) Among them, Z final Represents the final fused feature vector. S502. Classification output: The final fused feature vector Z containing the classification token and the cross-attention fusion features final Input into the classifier for classification, as shown in formula (13): y′=Classifier(Z final ) (13) Among them, Classifier represents the classifier; y′ represents the classification output of the model. S6. Loss function design and training: Design cross entropy loss L cross and contrast loss L con , and optimize the model parameters through back-propagation. The step S6 comprises: A1. Calculate the cross entropy loss: After each forward propagation, the cross entropy loss of the classification task is calculated as shown in formula (14): Among them, y n represents the true label of the nth sample; p n represents the predicted probability of the nth sample; L cross represents the cross entropy loss. A2. Calculate contrast loss: In order to further extract discriminative features, maximize the similarity between samples of the same category, and minimize the similarity between samples of different categories, after each forward propagation, the contrast loss is calculated as shown in formula (15): Where B represents the batch size; α represents the hyperparameter that controls the similarity threshold; y n and m Indicates the labels of the nth sample and the mth sample; Sim(z n ,z m ) represents the similarity between the nth and mth samples; is the Kroneckerdelta function, when y n =y m When it is 1, otherwise it is 0; L con represents contrast loss. A3. Combined loss function: The final loss function L F is the cross entropy loss L cross and contrast loss L con The weighted sum of is shown in formula (16): L F =L cross +βL con (16) Among them, β represents the weight of contrast loss. A4. Back propagation and parameter update: After each forward propagation, the total loss function L is calculated F And back propagate to minimize the total loss, as shown in formula (17): Among them, θ represents the model parameters; η represents the learning rate; Represents the gradient of the loss function with respect to the model parameters. Although the above describes the illustrative specific embodiments of the present invention to facilitate those skilled in the art to understand the present invention, it should be clear that the present invention is not limited to the scope of the specific embodiments. For those of ordinary skill in the art, as long as various changes are within the spirit and scope of the present invention as defined and determined by the attached claims, these changes are obvious, and all inventions and creations using the concept of the present invention are protected.

Claims

1. A fine-grained image classification method based on feature fusion and semantic enhancement, characterized in that: The following steps are involved: S1.ViT model feature extraction: The input image I is split into non-overlapping patches and converted into an embedding vector E through linear projection i , input to the Transformer encoder to generate the global feature E global ; S2. Multi-level attention fusion and key token selection combining semantic information: Extract the attention weights A in each layer of Transformer, and combine them with the semantic embedding emb generated by the pre-trained language model to calculate the importance score s of each token i , select the key token and record it as z key ; S3. Semantic enhancement and refinement of key token selection: For z in S2 key Perform secondary block division and projection to generate a new embedding vector E′ i,j , re-enter the Transformer encoder, and select the secondary key token again, recorded as z′ key ; S4. Cross-Attention Fusion: Through the cross attention mechanism, the global feature Z global and local feature z′ key Fusion is performed to generate fusion feature Z fused ; S5. Feature fusion and classification: The fusion feature Z fused With the global classification feature E global Combine, input into the classifier for classification, and generate classification output y′; S6. Loss function design and training: Design the cross entropy loss L cross and contrast loss L con , and optimize the model parameters through back-propagation.

2. The fine-grained image classification method based on feature fusion and semantic enhancement according to claim 1 is characterized in that: The ViT model feature extraction in S1 includes: S101. Image segmentation and linear projection: The input image I is divided into several non-overlapping patches (i.e., tokens) of size 16×16, and each patch P is transformed into i Converted to an embedding vector E i As shown in formula (1): E i =LinearProjection(P i ) (1) Among them, E i Represents the embedding vector of the i-th patch; S102.Embedded sequence combination: Combine all embedding vectors into a sequence E = [E1, E2, ..., E i ] and add a classification token embedding vector E0: E′=[E0,E1,E2,...,E i ]; S103.Transformer encoder: The embedding vector sequence E′ is input into the multi-layer Transformer encoder, and the query matrix Q, key matrix K and value matrix V are generated through linear transformation. Each layer of Transformer encoder consists of a multi-head self-attention mechanism and a feedforward neural network. The calculation process of the attention mechanism is shown in formula (2): Among them, softmax represents the softmax activation function d k represents the dimension of the key matrix; A l represents the attention weight matrix of the lth layer; S104. Global features: A1. In the Transformer encoder of all layers, the classification token E0 will absorb information from other tokens layer by layer and update the vector E containing global features. global ; A2. Take the weighted sum of the attention weight matrices of all layers to obtain the global feature representation Z fused by the attention mechanism globa As shown in formula (3): Among them, V l A represents the value matrix of the lth layer; l represents the attention weight matrix of image I at layer l; L represents the total number of layers of Transformer; Z global Represents the global feature representation.

3. The fine-grained image classification method based on feature fusion and semantic enhancement according to claim 2 is characterized in that: The key token selection for multi-level attention fusion and semantic information in S2 includes: S201. Extract and fuse attention weights: Extract the attention weight A of each token from each layer of Transformer in S1 l ; The attention weights of all layers are fused by matrix multiplication to generate the fused global attention as shown in formula (4): Among them, a final represents the fused global attention, Represents the attention weight of each token at layer l; S202. Normalization processing: The attention weight of each token in the global attention is normalized as shown in formula (5): Among them, a final,i represents the attention weight of the i-th token, ∑ j a final,j Represents the sum of the attention weights of all tokens in the global attention; represents the normalized attention weight of the i-th token; S203. Generate semantic embedding and fuse semantic information to calculate importance score: The pre-trained language model is used to generate a semantic embedding emb for each token. The normalized attention weight is then element-wise multiplied with the generated semantic embedding (Hadamard product) and normalized to generate an enhanced attention weight, as shown in formula (6): Among them, ⊙ represents element-by-element multiplication; emb i Represents the semantic embedding vector of the i-th token; a enhanced,i Represents the enhanced attention weight of the i-th token; Use the enhanced attention weight a enhanced,i Calculate the importance score of each token as shown in formula (7): s i =a enhanced,i ·emb i (7) Among them, s i Indicates the importance score of the i-th token. Select several tokens with the highest scores as key tokens and record them as z key .

4. The fine-grained image classification method based on feature fusion and semantic enhancement according to claim 3 is characterized in that: Semantic enhancement and refinement of key token selection in S3 include: S301. Perform secondary block division and linear projection on the key token: The z selected in S2 key Further block division is performed to divide each key token into 4×4 patches again; these newly divided patches are linearly projected to generate new embedding vectors, as shown in formula (8): E′ i,j =LinearProjection(P i,j ) (8) Among them, P i,j represents the jth new block of the i-th key token; E′ i,j Represents the embedding vector of the jth new block of the i-th key token; S302. Embedded sequence combination: These embedding vectors are combined into a new sequence, and the classification token E0 is added and input into the Transformer encoder for processing, as shown in formula (9): E″ i =[E0,E′ i,1 ,E′ i,2 ,...,E′ i,j ] (9) S303. Secondary selection of key token: The calculation method is the same as S2. Calculate the secondary attention weight and select the secondary key token as z′ key。 5. According to claim 4, a fine-grained image classification method based on feature fusion and semantic enhancement is characterized in that: The cross-attention fusion in S4 includes: S401. Introducing the cross attention mechanism: After the secondary key token selection, the cross attention mechanism is introduced to fuse global features and local features. The cross attention is calculated as shown in formula (10): Among them, Q represents the global feature Z global The query matrix of ; K represents the secondary key token feature z′ key The bond matrix of k represents the dimension of the key matrix; A cross represents the cross attention weight matrix; S402. Apply the cross attention weight to generate the fusion feature as shown in formula (11): From fused =A cross In (11) Where V represents the secondary key token feature z′ key The value matrix, Z fused表 The fused feature matrix is ​​shown.

6. The fine-grained image classification method based on feature fusion and semantic enhancement according to claim 5 is characterized in that: Feature fusion and classification in S5 include: S501. Final feature fusion: The cross-attention fusion feature Z fused and the global classification feature vector E obtained in S1 global The fusion is performed as shown in formula (12): With final =[E global ,With fused ] (12) Among them, Z final The feature vector representing the final fusion; S502. Classification output: The final fused feature vector Z containing the classification token and the cross-attention fusion features final Input into the classifier for classification, as shown in formula (13): y′=Classifier(Z final ) (13) Among them, Classifier represents the classifier; y′ represents the classification output of the model.

7. The fine-grained image classification method based on feature fusion and semantic enhancement according to claim 6 is characterized in that: The loss function design and training in S6 include: A1. Calculate the cross entropy loss: After each forward propagation, the cross entropy loss of the classification task is calculated as shown in formula (14): Among them, y n represents the true label of the nth sample; p n represents the predicted probability of the nth sample; L cross represents the cross entropy loss; A2. Calculate contrast loss: In order to further extract discriminative features, maximize the similarity between samples of the same category, and minimize the similarity between samples of different categories, after each forward propagation, calculate the contrast loss, as shown in formula (15): Where B represents the batch size; α represents the hyperparameter that controls the similarity threshold; y n and m Indicates the labels of the nth sample and the mth sample; Sim(z n , z m ) represents the similarity between the nth and mth samples; is the Kroneckerdelta function, when y n =y m When it is 1, it is 0 otherwise; L con represents contrast loss; A3. Combined loss function: The final loss function L F is the cross entropy loss L cross and contrast loss L con The weighted sum of is shown in formula (16): L F =L cross +βL con (16) Among them, β represents the weight of contrast loss; A4. Back propagation and parameter update: After each forward propagation, the total loss function L is calculated F And back propagate to minimize the total loss, as shown in formula (17): Among them, θ represents the model parameters; η represents the learning rate; Represents the gradient of the loss function with respect to the model parameters.

Citation Information

Patent Citations

  • Transform-based fine-grained image classification method

    CN114676776A

  • Transform-based fine-grained image classification method

    CN118135290A

  • Fine-grained image classification method based on foreground and background multistage division and feature fusion

    CN118262180A

  • Fine-grained identification method based on alternate knowledge distillation strategy

    CN111144490A

  • Self-adaptive cross-modal feature mapping image recognition method based on zero-order learning

    CN116342956A

Cited By

  • Transform-based fine-grained image classification method and system

    CN119152300A

  • A Transformer-based fine-grained image classification method and system

    CN119152300B

  • ViT model improvement method, system and device and storage medium

    CN119206444A

  • Model training method and device based on reattention mechanism

    CN120087412A

  • Semantic segmentation method and related product

    CN120125828A