Efficient fine-grained image classification model based on improved ViT
By optimizing the visual Transformer model through local patch occlusion and depth feature selection modules, the problems of insufficient utilization of local information and the influence of background noise in fine-grained image classification are solved, thereby improving classification accuracy and generalization ability.
Patent Information
- Application Number
- CN202410903197.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-08
- Publication Date
- 2024-11-01
AI Technical Summary
Existing visual Transformer models struggle to effectively utilize local image information in fine-grained image classification tasks and are sensitive to background noise, resulting in low classification accuracy. They are particularly inadequate in handling subtle differences between categories, complex data annotations, and class imbalance.
A local patch masker module is used to randomly mask images. Combined with a depth feature selection module and a classification prediction module, key features are selected by cosine similarity. A focus loss and label smoothing loss function are constructed for training to optimize the model's feature fusion and classification accuracy.
It significantly improves the model's ability to capture local fine-grained information, reduces background interference, enhances classification accuracy and generalization ability, solves the class imbalance problem, and achieves more efficient fine-grained image classification.
Smart Images

Figure CN118887445A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of fine-grained image classification, and in particular to an efficient fine-grained image classification model based on improved ViT. Background Art
[0002] Fine-grained visual classification (FGVC) is an important task in the field of computer vision, which aims to classify images in more detail, such as bird species and dog breeds. In this task, the distinction between each category is often very subtle, which puts higher demands on traditional image classification methods. It has a wide range of applications in the real world, including biodiversity monitoring, agriculture, medical image analysis and other fields. Fine-grained image classification tasks require the model to be able to accurately distinguish tiny details and features between different categories, which usually requires the model to have strong perception and generalization capabilities.
[0003] In the task of fine-grained image classification, there are a series of significant challenges. Among them, the slight differences between categories are one of the key issues. These differences are often extremely subtle and may only be reflected in subtle features such as texture, shape or color of the image, which requires the model to have a high degree of discrimination ability to accurately identify and distinguish these subtle differences. Therefore, in fine-grained image classification, the design and training of the model need to pay special attention to how to improve the sensitivity and recognition accuracy to slight differences. The second is the complexity of data annotation: Given the high similarity between categories, the data annotation process is usually quite challenging. To ensure the accuracy of classification, it is necessary to rely on professional domain knowledge and a large amount of labeled data as support. The third is the imbalance of sample categories: in practical applications, the number of samples in some categories may be relatively large, while the number of samples in other categories may be relatively scarce.
[0004] Vision Transformer (ViT) is an emerging model that has attracted widespread attention in recent years. It was introduced from natural language processing to the field of computer vision and has attracted widespread attention. Recently, many studies have been devoted to applying ViT to the field of FGVC and have made significant breakthroughs. These research attempts with ViT as the core have far surpassed the existing convolution-based methods in performance, fully demonstrating the excellent performance of ViT in the field of FGVC. However, when applying ViT to this task, although it performs well in general image classification tasks, it should be noted that its main focus is on global coarse-grained information rather than local fine-grained information. This feature may make the performance of the ViT model in fine-grained image classification tasks unsatisfactory.
[0005] Therefore, it is necessary to propose an efficient fine-grained image classification model based on improved ViT, which can better utilize the local information of the image to perform more accurate fine-grained classification of the image, while also reducing the impact of noise data in the background, thereby improving the practicality and accuracy of the fine-grained classification method. Summary of the invention
[0006] The purpose of the present invention is to provide an efficient fine-grained image classification model based on improved ViT to solve the technical problems existing in the background technology.
[0007] In order to achieve the above object, the present invention provides the following technical solutions:
[0008] An efficient fine-grained image classification model based on improved ViT, the method comprises the following steps:
[0009] Step 1: Input the image dataset into the backbone network of the model and cut the image into small blocks of 16×16;
[0010] Step 2: Based on the local patch masker (LPC) module, the information area in the small image is randomly selected in proportion, and the selected part is masked;
[0011] Step 3: Extract the feature vector tokens of each small tile, and introduce position encoding and classification tokens;
[0012] Step 4: Use the deep feature selection module (DFS) to compare each token and select the token with higher similarity with the classifier head;
[0013] Step 5: The output of each encoder layer is fused with the classification token through the transformer module;
[0014] Step 6: Use the classification prediction module to perform classification prediction on the fusion features, construct a new FS Loss loss function for training, and finally obtain the classification results.
[0015] 2. According to claim 1, an efficient fine-grained image classification model based on improved ViT, characterized in that the local patch masker (LPC) module in step 2, first, the visual Transformer backbone consists of a feature extractor and a classifier head, and the image x∈R is split into non-overlapping H×W×3 Split into N patches (where H and W represent their height and width respectively, and 3 is the number of channels of the original image), and converted into a two-dimensional sequence, represented as Then use the local patch masker to randomly select image data, create a mask according to the set value, and then mask the randomly selected image data according to the mask size. The number after processing is represented by M:
[0016]
[0017] Where P is the artificially set patch size used to segment the image, usually 16, and m is an initially defined random value ranging from 1% to 0.5%.
[0018] 3. An efficient fine-grained image classification model based on improved ViT according to claim 1, characterized in that in step 3, a feature vector token of each small patch is extracted, and each patch is mapped to a token vector through a linear embedding layer. In addition, a learnable embedding marker x is introduced cls to classify, placing it at the beginning of the input sequence embedding and adding the position embedding x p ∈R (N+1)×D To preserve spatial information. Therefore, the first layer Transformer input z0 is as follows:
[0019] z0=[x cls ||x patch ]+x p
[0020] 4. According to the invention, the efficient fine-grained image classification model based on improved ViT is characterized in that the deep feature selection module (DFS) in step 4 selects tokens with higher similarity to the classifier head in the Encoder part of the Transformer model according to the similarity between tokens through the deep feature selection module; the selection method compares the similarity between the global feature and each local feature by calculating the cosine similarity, and sorts them according to the similarity, which is expressed as Sim(x i , c), where x i is the representation of the i-th token, and c is the representation of the global feature:
[0021]
[0022] 5. According to claim 1, an efficient fine-grained image classification model based on improved ViT is characterized in that in step 5, the classification token fusion is to select the image block with the highest similarity to the global feature as the optimal choice, that is, to select the image block with the largest cosine similarity. Specifically, this paper defines the optimal selection function:
[0023] OL(X, C) = arg max i Sim(x i , c)
[0024] Where X represents the set of all image patch tokens and C represents the global feature.
[0025] 6. According to the improved ViT-based efficient fine-grained image classification model of claim 1, the specific method of step 6 is as follows:
[0026] Step 6.1, use the classification prediction module to classify and predict the fusion features, and construct a new FS Loss loss function for training, which combines the focal loss (Focal Loss) and label smoothing (Label Smoothing) loss, expressed as:
[0027]
[0028] in, It is a loss function used to solve the problem of class imbalance. It adjusts the weight of the loss function so that the loss value of difficult samples (i.e., samples that are misclassified) increases, while the loss value of easy samples decreases. This can help the model pay more attention to samples that are difficult to classify and improve the performance of the model:
[0029]
[0030] p t is the probability predicted by the model, and α and γ are adjustable parameters that control the decay rate of focus loss and the degree of focusing, respectively.
[0031] Label smoothing is a loss function that reduces overfitting by introducing noise by replacing the true label with a small probability value between 0 and 1. This makes the model more robust to label predictions, thereby improving generalization capabilities:
[0032]
[0033] The value of the smoothing parameter indicates the degree of smoothing.
[0034] Step 6.2, use the optimizer to optimize the loss function. If the preset number of iterations is not reached or the accuracy is improved, then update the network model weights and biases through the gradient back propagation method. If the preset number of iterations is reached or the accuracy is no longer improved, the final prediction value is obtained.
[0035] The beneficial effects of the present invention are:
[0036] First, the FGVC Transformer model is proposed, which can accurately capture more local fine-grained information, effectively select class tokens containing key classification target information, and optimize the overfitting problem of the model. In addition, the model shows higher accuracy in image classification tasks, providing strong support for research and application in related fields.
[0037] Second, a local patch masker is proposed, which effectively reduces the interference of complex background information in the patch on model discrimination, and significantly alleviates the problem of insufficient edge information capture when cutting images, thereby improving the overall performance.
[0038] Third, a deep feature selection module is designed to solve the common problem in the Transformer structure that key class tokens cannot be accurately identified when selecting all class tokens for fusion. Through rigorous logic and efficient algorithms, this selection layer can accurately screen out class tokens containing key information, while effectively excluding class tokens with insufficient information, thereby achieving a more accurate and efficient class token fusion process. This innovative design not only improves the performance of the model, but also provides more reliable and effective support for subsequent data processing and analysis.
[0039] Fourth, an optimal feature selection layer is designed to solve the common problem in the Transformer structure that the key class tokens cannot be accurately identified when selecting all class tokens for fusion. Through rigorous logic and efficient algorithms, this selection layer can accurately screen out class tokens containing key information and effectively exclude class tokens with insufficient information, thereby achieving a more accurate and efficient class token fusion process. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to make the purpose, technical solutions and advantages of the present invention more clear, the present invention will be described in detail below in conjunction with the accompanying drawings, wherein:
[0041] Figure 1 A flowchart of an efficient fine-grained image classification model based on improved ViT of the present invention;
[0042] Figure 2 A network structure diagram of the model of the present invention; DETAILED DESCRIPTION
[0043] The present invention will be further described below in conjunction with the accompanying drawings.
[0044] like Figure 1 As shown, the present invention provides an efficient fine-grained image classification model based on improved ViT, comprising the following steps:
[0045] Step 1: Input the image dataset into the backbone network of the model and cut the image into small blocks of 16×16;
[0046] Step 2: Based on the local patch masker (LPC) module, the information area in the small image is randomly selected in proportion, and the selected part is masked;
[0047] Step 3: Extract the feature vector tokens of each small tile, and introduce position encoding and classification tokens;
[0048] Step 4: Use the deep feature selection module (DFS) to compare each token and select the token with higher similarity with the classifier head;
[0049] Step 5: The output of each encoder layer is fused with the classification token through the transformer module;
[0050] Step 6: Use the classification prediction module to perform classification prediction on the fusion features, construct a new FS Loss loss function for training, and finally obtain the classification results.
[0051] Details Figure 2 The implementation process of each step is as follows:
[0052] (1) Local Patch Concealer (LPC module)
[0053] Before being fed into the feature extractor, the image x∈R is split into H×W×3 Split into N patches es (where H and W represent their height and width respectively, and 3 is the number of channels of the original image), converted into a two-dimensional sequence, represented as P is the manually set patch size used to segment the image, usually 16, and a local patch masker (LPC) is proposed. The number of local masks mask-hide represents M:
[0054]
[0055] Where m is the initially defined random value, ranging from 1% to 0.5%. Next, we need to create an occlusion mask (K) to mark the patch positions that need to be occluded. We can use a tensor of shape (batch_size, num_patches, 1) to represent K. The initial value of K is a tensor of all 1s, that is, all positions are not occluded. Then, we set the occlusion mask position corresponding to the selected position to 0. In this way, the selected patch position will be occluded, and the other positions will remain 1, which can be expressed as follows:
[0056] K[i, j, k] = 1 (2)
[0057] K[i, j, k] = 0 (3)
[0058] The above represents an element in the occlusion mask tensor, where i represents the index of the batch, j represents the index of the patch, and k represents the channel of the occlusion mask. In (2), j is not in the occlusion mask K, and in (3), j is in the occlusion mask K. In this way, the problem of preserving local neighborhood information is alleviated to a certain extent. Each patch is then mapped to a token vector through a linear embedding layer. In addition, a learnable embedding tag x is introduced cls to classify, placing it at the beginning of the input sequence embedding and adding the position embedding x p ∈R (N+1)×D To preserve spatial information. Therefore, the first layer Transformer input z0 is as follows:
[0059] z0=[x cls ||x patch ]+x p (4)
[0060] The Transformer feature extractor consists of multiple layers of Transformer layers, each of which consists of a multi-head self-attention (MSA) module and a two-layer fully connected feedforward neural network. The output z of the Transformer layer y The calculation is shown in (7):
[0061]
[0062] LN represents layer normalization. The Transformer encoder consists of alternating layers of multi-head self-attention (MSA) and multi-layer perceptron (MLP) as shown in (5)(6).
[0063] (2) Transformer module (DFS module)
[0064] The tokens selected by the linear layer are input into the Encoder module in the Transformer and then into the Deep Feature Selection (DFS) module. The cosine similarity is calculated to compare the similarity between the global feature and each local feature, and the features are sorted according to the similarity. Finally, the local information with the highest similarity to the global feature is selected as the optimal local feature to ensure that the selected feature can fully reflect the core characteristics of the overall data.
[0065] Feature map adjustment: Adaptive parameters are used to control the weight of each image region in the feature map to flexibly adjust the feature map. First, for a given feature map X, the adaptive parameter A is calculated through matrix multiplication and softmax operation. Then, the original feature map is weighted using the calculated adaptive parameter to obtain the optimized feature map X′. Detailed description:
[0066] 1. Perform local feature selection on the output of each encoder block and select the local information with the highest similarity to the global feature as the optimal local feature.
[0067] 2. Use adaptive parameters to control the weight of feature mapping, flexibly adjust feature mapping, and enhance the perception of fine-grained features.
[0068] So after local feature selection, it can be expressed as follows:
[0069] diff=Co sin eSimilarity(cls_res,cls_one_layer) (8)
[0070] optimal_token_idx=argsort(diff, descending=True) (9)
[0071] X′=X[:,optimal_token_idx] (10)
[0072] Among them, cls_res is the global feature, cls_one_layer is the local classifier output, and W is the adaptive parameter matrix.
[0073] At the output of each Transformer layer, we calculate the cosine similarity between each image patch token and the global feature, denoted as Sim(x i , c), where x i is the representation of the i-th token, and c is the representation of the global feature:
[0074]
[0075] Then, this paper selects the image block with the highest similarity to the global feature as the optimal choice, that is, the image block with the largest cosine similarity. Specifically, this paper defines the optimal selection function:
[0076] OL(X, C) = arg max i Sim(x i ,c) (12)
[0077] Where X represents the set of all image block tokens and C represents the global feature.
[0078] Finally, this paper fuses these optimally selected image patches with the global features to obtain the optimized feature map Z, as shown below:
[0079] z=Concat(OL(X,C),X) (13)
[0080] (3) Heterogeneous module optimization (HMO module)
[0081] After the Transformer module, it first passes through a fully connected layer and then enters a custom loss function that combines focal loss and label smoothing loss. Specifically, the loss function is expressed as:
[0082]
[0083] in, It is a loss function used to solve the problem of class imbalance. It adjusts the weight of the loss function so that the loss value of difficult samples (i.e., samples that are misclassified) increases, while the loss value of easy samples decreases. This can help the model pay more attention to samples that are difficult to classify and improve the performance of the model. Its mathematical expression is as follows:
[0084]
[0085] p t is the probability predicted by the model, and α and γ are adjustable parameters that control the decay rate of focus loss and the degree of focusing, respectively.
[0086] Label smoothing is a loss function that reduces overfitting by replacing the true label with a small probability value between 0 and 1 to introduce noise. This makes the model more robust in predicting labels, thereby improving generalization capabilities. Its mathematical expression is as follows:
[0087]
[0088] The value of the smoothing parameter indicates the degree of smoothing. When the smoothing parameter is close to 0, it means that less smoothing is used; when the smoothing parameter is close to 1, it means that more smoothing is used. i is the true label after label smoothing, is the prediction probability of the model, and N is the number of categories. Combining the focal loss function and label smoothing technology, we proposed a new loss function - FS Loss. The FS Loss loss function first uses the cross entropy loss function, and then passes through the loss function that combines the focal loss and label smoothing loss to obtain the final loss value. In this way, the FSLoss loss function can achieve better performance on the problem of category imbalance and improve the generalization ability of the model. Finally, the model performs classification prediction, and the experimental results also prove our model.
Claims
1. An efficient fine-grained image classification model based on improved ViT, characterized by: The following steps are involved: Step 1: Input the image dataset into the backbone network of the model and cut the image into small blocks of 16×16; Step 2: Based on the local patch masker (LPC) module, the information area in the small image is randomly selected in proportion and the selected part is masked; Step 3: Extract the feature vector tokens of each small patch and introduce position encoding and classification tokens; Step 4: Use the Deep Feature Selection module (DFS) to compare each token and select the token with higher similarity with the classifier head; Step 5: The output of each encoder layer is fused with the classification token through the transformer module; Step 6: Use the classification prediction module to perform classification prediction on the fusion features, construct a new FS Loss loss function for training, and finally obtain the classification results.
2. According to claim 1, an efficient fine-grained image classification model based on improved ViT, characterized in that The local patch masker (LPC) module mentioned in step 2, first, the visual Transformer backbone consists of a feature extractor and a classifier head. Before being input into the feature extractor, the image is split into N patches (where H and W represent their height and width respectively, and 3 is the number of channels of the original image) through non-overlapping splitting and converted into a two-dimensional sequence, represented as Then use the local patch masker to randomly select image data, create a mask according to the set value, and then mask the randomly selected image data according to the mask size. The number after processing is represented by M: Where P is the manually set patch size used to segment the image, usually 16, and m is an initially defined random value ranging from 1% to 0.5%.
3. The efficient fine-grained image classification model based on improved ViT according to claim 1, characterized in that: Step 3 extracts the feature vector token of each small patch and maps each patch to a token vector through a linear embedding layer. In addition, a learnable embedding tag x is introduced. cls to classify, placing it at the beginning of the input sequence embedding and adding the position embedding x p ∈R (N+1)×D To retain spatial information, therefore, the first layer Transformer input z0 is as follows: z0=[x cls ||x patch ]+x p 4. The efficient fine-grained image classification model based on improved ViT according to claim 1, characterized in that: The Deep Feature Selection (DFS) module mentioned in step 4 selects tokens with high similarity to the classifier head based on the similarity between tokens in the encoder part of the Transformer model. The selection method calculates the cosine similarity, compares the similarity between the global feature and each local feature, and sorts them according to the similarity, which is expressed as Sim(x i , c), where x i is the representation of the i-th token, and c is the representation of the global feature:
5. The efficient fine-grained image classification model based on improved ViT according to claim 1, characterized in that: In step 5, the classification tokens are fused, which selects the image block with the highest similarity to the global feature as the optimal choice, that is, the image block with the largest cosine similarity is selected. Specifically, this paper defines the optimal selection function: OL(X,C)=arg max i Sim(x i ,c) where represents the set of all image patch tokens and represents the global feature.
6. The efficient fine-grained image classification model based on improved ViT according to claim 1, characterized in that: The specific method of step 6 is as follows: In step 6.1, the classification prediction module is used to perform classification prediction on the fusion features, and a new FSLoss loss function is constructed for training, which combines the focal loss (FocalLoss) and label smoothing (LabelSmoothing) loss, expressed as: in, It is a loss function used to solve the problem of class imbalance. It adjusts the weight of the loss function so that the loss value increases for difficult samples (i.e., samples that are misclassified), while the loss value decreases for easy samples. This helps the model pay more attention to samples that are difficult to classify and improves the performance of the model. is the probability predicted by the model, and are adjustable parameters that control the decay rate of focus loss and the degree of focus, respectively; Label smoothing is a loss function that reduces overfitting by introducing noise by replacing the true label with a small probability value between 0 and 1; this makes the model more robust to label predictions, thereby improving generalization capabilities: The value of the smoothing parameter indicates the degree of smoothing; In step 6.2, the loss function is optimized using the optimizer. If the pre-set number of iterations is not reached or the accuracy is improved, the network model weights and biases are updated using the gradient backpropagation method. If the pre-set number of iterations is reached or the accuracy is no longer improved, the final prediction value is obtained.
Citation Information
Cited By
Open set fine granularity identification method and device
CN119851013A
Compression method of visual Transform model
CN120124699A
Image classification method and system based on GNF3Linear mapping function fine tuning
CN121033509A
