Cattle face recognition method based on Swin Transform

Through the cow face recognition method based on Swin Transformer, a two-layer routing attention module and a convolution enhancement merging module are constructed to solve the problem of difficult feature extraction in cow face recognition and achieve efficient and accurate recognition in complex backgrounds.

CN120599666APending Publication Date: 2025-09-05INNER MONGOLIA UNIV OF TECH
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202510771104.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-10
Publication Date
2025-09-05

AI Technical Summary

Technical Problem

Existing cow face recognition methods rely on manual recognition, which is prone to errors and inconsistencies. Traditional feature extraction methods have weak feature extraction capabilities in scenarios such as complex lighting, angle changes, or hair occlusion, and are difficult to meet actual application needs.

Method used

A cow face recognition method based on Swin Transformer is adopted to construct an image segmentation module, a backbone network, a two-layer routing attention module and a convolution enhancement merging module. The feature extraction capability is improved by the two-layer routing attention mechanism and the convolution enhancement merging module, and the feature representation is optimized by combining the coordinate attention block.

Benefits of technology

It significantly improves the accuracy and robustness of cow face recognition, reduces computational complexity, can effectively capture fine-grained features in complex backgrounds, and improves recognition accuracy and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120599666A_ABST
    Figure CN120599666A_ABST
Patent Text Reader

Abstract

The invention discloses a cattle face recognition method based on Swin Transform, and the method comprises the steps: obtaining a cattle face image, and carrying out the marking of the cattle face image; constructing a recognition model, wherein the recognition model comprises an image partitioning module, a backbone network, a double-layer routing attention module, a convolution enhancement merging module and a classification head; the backbone network is constructed on the basis of a Swin Transform structure and is divided into four cascade stages, each stage is composed of a Patch merging module and a plurality of Swin Transform Blocks and used for gradually extracting multi-scale feature information in a cattle face image, a double-layer routing attention module is introduced between input and output of one or more stages of the backbone network, and the multi-scale feature information in the cattle face image is extracted. Enhancing global context modeling capability and spatial relationship modeling; a convolution enhancement merging module is introduced into the output of the stage of introducing the double-layer routing attention module, and the output of the stage and the output of the corresponding double-layer routing attention module are fused as the input of the next stage, so that the feature representation capability is enhanced; inputting features output by the backbone network into a classification head for classification; and training the recognition model, and performing cattle face recognition by using the trained recognition model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of image recognition technology, relates to the identity recognition of livestock in animal husbandry, and in particular to a cow face recognition method based on Swin Transformer. Background Art

[0002] Currently, cattle face recognition mainly relies on manual identification by farm staff or veterinarians based on their experience. The differences in facial texture, coat color, angle, etc. between different cattle are subtle, making confusion easy to occur. In addition, due to the lack of unified recognition standards, different operators are highly subjective in judging the identity of cattle, which often leads to inconsistent recognition results, affecting the accuracy of traceability and management. Traditional automated image analysis methods also have many limitations in cattle face recognition tasks. For example, classic feature extraction methods such as SIFT (Scale-Invariant Feature Transform) and LBP (Local Binary Patterns) often exhibit weak feature extraction capabilities, sensitivity to noise, and poor generalization performance when faced with actual scenarios such as complex lighting, angle changes, or hair occlusion, making it difficult to meet the needs of actual applications. Summary of the Invention

[0003] In order to overcome the shortcomings of the above-mentioned prior art, the purpose of the present invention is to provide a cattle face recognition method based on SwinTransformer to identify the individual identity of cattle more accurately and efficiently.

[0004] In order to achieve the above object, the technical solution adopted by the present invention is:

[0005] A cow face recognition method based on Swin Transformer, comprising the following steps:

[0006] Step 1: Obtain cow face images and perform individual identity labeling

[0007] Step 2, constructing a cow face recognition model, the recognition model includes an image segmentation module, a backbone network, a double-layer routing attention module, a convolution enhancement merging module and a classification head; wherein the image segmentation module is used to divide the cow face image into fixed-size and non-overlapping image blocks and perform linear embedding; the backbone network is constructed based on the Swin Transformer structure and is divided into four cascade stages, each stage consisting of a Patch merging module and multiple Swin Transformer Blocks, which are used to gradually extract multi-scale feature information from the cow face image; the double-layer routing attention module is introduced between the input and output of one or several stages of the backbone network to enhance the global context modeling capability and spatial relationship modeling; and the convolution enhancement merging module is introduced at the output of the stage where the double-layer routing attention module is introduced, and the output of the stage is fused with the output of the corresponding double-layer routing attention module as the input of the next stage to enhance the feature representation capability; the features output by the backbone network are input into the classification head for classification;

[0008] Step 3: train the recognition model and use the trained model to perform individual identity recognition on the cow face image.

[0009] In one embodiment, the image segmentation module is composed of an image segmentation module and a linear embedding unit, and performs the following processes on the input cow face image in sequence:

[0010] First, the cow face image is divided into fixed-size and non-overlapping image blocks, and then each image block is mapped into a fixed-dimensional feature vector through a linear embedding layer;

[0011] The embedded image block features are input into the subsequent backbone network as input features of the backbone network.

[0012] In one embodiment, a first double-layer routing attention module is introduced between the input and output of the third stage of the backbone network, and a second double-layer routing attention module is introduced between the input and output of the fourth stage;

[0013] The first convolution enhancement merging module is introduced into the output of the third stage, and the second convolution enhancement merging module is introduced into the output of the fourth stage. The first convolution enhancement merging module fuses the output of the third stage and the output of the first double-layer routing attention module as the input of the fourth stage. The second convolution enhancement merging module fuses the output of the fourth stage and the output of the second double-layer routing attention module as the output of the backbone network.

[0014] In one embodiment, the dual-layer routing attention module includes a normalization layer, a DW convolution layer, a multi-layer perceptron, and a dual-layer routing attention mechanism. The following steps are performed in sequence on the feature map input to the dual-layer routing attention module:

[0015] Normalization processing;

[0016] Convolution is performed on the normalized feature map to implicitly encode relative position information;

[0017] Apply a two-layer routing attention mechanism to dynamically filter key areas and only calculate attention on a few relevant tokens to avoid global calculations;

[0018] The token at each position is transformed nonlinearly through a multi-layer perceptron.

[0019] In one embodiment, the dual-layer routing attention module performs feature modeling based on a dual-layer routing attention mechanism, including a coarse-grained routing selection stage and a fine-grained attention aggregation stage;

[0020] In the coarse-grained routing selection phase, a coarse-grained correlation evaluation is performed on the query and key globally, and regions or paths with high correlation are screened out and defined as key regions;

[0021] The fine-grained attention aggregation stage performs detailed multi-head self-attention calculations only in selected key areas, thereby achieving a balance between computational efficiency and modeling capabilities.

[0022] In one embodiment, the coarse-grained routing stage uses low-resolution feature maps for similarity scoring and constructs a sparse attention map, thereby efficiently screening key areas globally and improving the computational efficiency and regional focusing ability of the attention mechanism.

[0023] The fine-grained attention aggregation stage performs local self-attention calculation on the selected key areas in the high-resolution space to achieve more accurate recognition of the local features of the cow face.

[0024] In one embodiment, the dual-layer routing attention module introduces a gating unit to dynamically adjust the coarse-grained screening threshold during the execution of the dual-layer routing attention mechanism, thereby achieving adaptive attention path selection under different image scenarios; at the same time, residual connections and normalization operations are used to improve model stability and training convergence speed.

[0025] In one embodiment, the convolution enhancement merging module includes a coordinate attention block, a double-layer convolution block and a SaE block in sequence, which is used to fuse channel attention and spatial coordinate information;

[0026] Among them, the coordinate attention block is used to model the spatial position information in the feature map; the double-layer convolution block consists of two consecutive convolution operations, each of which includes a convolution layer, a batch normalization layer and a ReLU activation function in sequence, for further extracting local context features; the SaE block is used to enhance the feature interaction capability between channels; the convolution enhancement merging module is used as a whole to fuse spatial and channel attention information to improve feature expression capabilities.

[0027] In one embodiment, the coordinate attention block in the convolution enhancement merging module generates description vectors in two directions by performing row pooling and column pooling on the input feature map, and concatenates them along the height dimension to form a joint representation. After processing with a shared 1×1 convolution and activation function, attention weights are generated and applied to the original feature map respectively.

[0028] The SaE block enhances the expressiveness through channel compression and attention activation. The outputs of the two branches are concatenated and fused with 1×1 convolution to generate an enhanced feature map.

[0029] In one embodiment, the coordinate attention block, row pooling and column pooling refer to performing global average pooling on the input feature map in the vertical and horizontal directions respectively, extracting the height direction feature map and the width direction feature map, that is, the description vectors in the two directions; then the description vectors in the two directions are spliced ​​along the height dimension to form a joint representation, and the fusion features are extracted through 1×1 convolution and activation function; the fusion features are then split into two corresponding directional components along the height dimension, and the height attention map and the width attention map are generated by 1×1 convolution mapping respectively, and then normalized by the Sigmoid function respectively, and acted on the corresponding dimensions of the original feature map to obtain the output features, thereby completing the joint modeling process of space and channel, and finally outputting the results after fusing the attention weights of the two directions by element-by-element multiplication to obtain the output features of the coordinate attention block;

[0030] The two-layer convolution block takes the output of the coordinate attention block as input, and sequentially passes through two layers of convolution with BatchNorm and ReLU activation to extract local context information and perform residual addition with the input features;

[0031] The SaE block takes the output of the double-layer convolution block as input, first performs global average pooling to extract the global context features of each channel; then, the features are sent to four different linear mapping branches in parallel to simulate four attention perception paths, and the input features are respectively subjected to channel compression and nonlinear activation, thereby forming four sets of low-dimensional feature representations and completing the feature encoding of four different receptive fields; then, the four sets of low-dimensional features are spliced ​​in the channel dimension to form a multi-scale joint expression, and then fused and channel score calculated through a linear layer, and the final channel attention weight is obtained using the Sigmoid function; then, the weight is readjusted to the same dimension as the input feature and multiplied element-by-element with the original input feature on each channel to achieve channel recalibration of the input feature;

[0032] The features fused by the convolution enhancement merging module are mapped to the target output channel number through a 1×1 convolution to obtain the output features of the convolution enhancement merging module.

[0033] Compared with the prior art, the present invention has the following beneficial effects:

[0034] In order to solve the problem of difficulty in feature extraction in the cow face recognition task due to complex background and large individual differences, the present invention designs a double-layer routing attention module and a convolution enhancement merging module based on the Swin Transformer, and constructs an improved Transformer structure that integrates spatial and channel attention mechanisms. The double-layer routing attention module effectively improves the model's ability to focus on key area features through the double-layer routing attention mechanism; while the convolution enhancement merging module sequentially integrates the coordinate attention block, the double-layer convolution block and the SaE block, enhancing the channel feature expression capability while retaining spatial position information. While improving the global modeling capability of the model, this structure effectively enhances the ability to characterize the fine-grained features of the cow face, reduces redundant calculations while ensuring recognition accuracy, and significantly improves the robustness and efficiency of the model in complex backgrounds. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1 This is the structural diagram of the cow face recognition model based on Swin Transformer.

[0036] Figure 2 This is the model structure diagram of Swin Transformer.

[0037] Figure 3 This is the structural diagram of the double-layer routing attention module of the present invention.

[0038] Figure 4 Schematic diagram of the dense matrix of the double-layer routing attention module of the present invention.

[0039] Figure 5 This is the structural diagram of the convolution enhancement and merging module of the present invention.

[0040] Figure 6 Schematic diagram of the coordinate attention block of the present invention.

[0041] Figure 7 Schematic diagram of the SaE block of the present invention. DETAILED DESCRIPTION

[0042] The embodiments of the present invention are described in detail below with reference to the accompanying drawings and examples.

[0043] With the advancement of agricultural modernization, the demand for cattle face recognition in smart farming continues to grow. In order to meet the dual requirements of accuracy and real-time performance of the recognition system, the cattle face recognition algorithm needs to achieve a good balance between feature extraction capability and computational efficiency. The present invention proposes a cattle face image recognition method based on Swin Transformer, which combines the dual-layer routing attention mechanism of the dual-layer routing attention module and the spatial-channel joint attention of the convolution enhancement merging module to enhance the model's ability to capture the detailed features of the cattle face while reducing the computational complexity. In order to further enhance the attention effect of the model, a feature enhancement module based on the coordinate attention block is introduced to optimize the feature representation. In addition, through the multi-layer feature fusion mechanism, the effective interaction of features at different levels in the recognition network is realized, thereby significantly improving the accuracy and robustness of cattle face recognition.

[0044] like Figure 1 As shown, the cow face recognition method based on Swin Transformer of the present invention specifically includes the following steps:

[0045] Step 1: Obtain a cow face image and perform individual identity labeling on the cow face.

[0046] This step captures cow face images using high-resolution video equipment under standard lighting conditions. The resulting video data is then manually labeled and cropped to construct a cow face recognition dataset. Specifically, this example used high-resolution video equipment to capture videos of 131 Holstein dairy cows and Simmental beef cattle. During the recording process, the camera operator captured each cow from different angles and distances.

[0047] Using video frame extraction technology, the captured video is decomposed into images. Because some of the extracted images have high similarity, to ensure data diversity, images with excessive similarity need to be removed. The structural similarity index (SSIM) algorithm is used to further eliminate redundant images. The specific calculation formula for SSIM is as follows:

[0048]

[0049] In the formula, x and y represent two images, μ x and μ y represents the mean of each image, σ x and σ y is their standard deviation, σ xy represents their covariance, c1 and c2 are arbitrary constants.

[0050] After video frame extraction, the image contains background noise other than the cow's face, which requires further processing to construct a cow face recognition dataset. The present invention uses YOLOv8 for cow face detection to assist in determining the location of the cow's face in the image. After manual correction of the detection results, the cow face image is accurately cropped.

[0051] Step 2: Build a cow face recognition model.

[0052] The model of the present invention mainly includes an image segmentation module, a backbone network, a two-layer routing attention module, a convolution enhancement merging module and a classification head. Its architecture is as follows: Figure 1 As shown in Figure 2, the image segmentation module is used to divide the cow face image into fixed-size, non-overlapping image patches and perform linear embedding. The backbone network is built based on the Swin Transformer architecture and consists of four cascaded stages. Each stage consists of a patch merging module and multiple Swin Transformer blocks to gradually extract multi-scale feature information from the cow face image. The classification head uses the features output by the backbone network for classification.

[0053] A two-layer routing attention module is introduced and placed between the input and output of one or several stages of the backbone network to enhance global context modeling and spatial relationship modeling. A convolution-enhanced merging module is introduced and placed at the output of a backbone network stage with a two-layer routing attention module. Its function is to fuse the output of that stage with the output of the two-layer routing attention module between its input and output, and use it as the input of the next stage. Through the convolution-enhanced merging operation, the fusion operation enhances feature representation capabilities.

[0054] The image segmentation module of the present invention consists of an image segmentation module and a linear embedding unit, and performs the following processes on the input cow face image in sequence:

[0055] First, the image segmentation module divides the cow face image into fixed-sized, non-overlapping image blocks. Then, a linear embedding layer consisting of linear embedding units maps each image block into a fixed-dimensional feature vector. Finally, the embedded image block features are fed into the backbone network as input features.

[0056] The present invention selects Swin Transformer as the basic network for cow face recognition. Swin Transformer is a visual backbone network based on Transformer and is a variant of the Transformer model. The model architecture of Swin Transformer is as follows: Figure 2 Compared to the common attention formula, the Swin Transformer adopts a windowed multi-head self-attention mechanism, which divides the image into fixed-size, non-overlapping local windows and performs independent calculations between each window. Subsequently, these windows are converted into fixed-dimensional vector representations through patch embedding. Next, a window shift strategy is introduced to enhance cross-window information interaction, and a hierarchical feature downsampling mechanism is introduced to construct multi-scale feature representations. The window attention formula is as follows:

[0057]

[0058] Among them, "Q", "K" and "V" are Query, Key and Value matrices respectively, "d" represents the attention dimension and "B" represents the relative position bias.

[0059] The model proposed in this invention adopts an architectural form. A first dual-layer routing attention module is introduced between the input and output of the third stage of the backbone network. The output of the second stage is simultaneously input into the third stage and the first dual-layer routing attention module. Through the dual-layer routing attention mechanism, dynamic, query-aware sparse attention is achieved, enhancing the local spatial relationship modeling capability. A first convolution enhancement merging module is introduced at the output of the third stage. The first convolution enhancement merging module fuses the output of the third stage with the output of the first dual-layer routing attention module as the input of the fourth stage. That is, the output of the third stage and the first dual-layer routing attention module are input into the first convolution enhancement merging module for feature fusion. By collaboratively optimizing spatial perception and channel relationships, the representation capability of key features is improved. A second dual-layer routing attention module is introduced between the input and output of the fourth stage. The output of the first convolution enhancement merging module is simultaneously input into the fourth stage and the second dual-layer routing attention module. A second convolution enhancement merging module is introduced at the output of the fourth stage. The second convolution enhancement merging module fuses the output of the fourth stage with the output of the second dual-layer routing attention module. That is, the output of the fourth stage is further fused with the output of the second dual-layer routing attention module. The final cow face feature is obtained through the second convolution enhancement merging module. The present invention fuses multi-channel attention and spatial coordinate information through a convolution enhancement merging module to obtain the final cow face features as the output of the backbone network.

[0060] In further embodiments of the present invention, the number and location of the dual-layer routing attention modules and the convolution enhancement merging modules can be adjusted, but they must be matched. That is, the convolution enhancement merging module must take the output of one dual-layer routing attention module and the output of one stage of the backbone network as input. Furthermore, the dual-layer routing attention module can be placed across stages if necessary, for example, between the input of the first stage and the output of the second stage.

[0061] This step is the core part of the present invention, and each module involved is further introduced in detail as follows.

[0062] 1. Two-layer routing attention module

[0063] When extracting features from an image, in order to effectively improve the model's ability to model details of key areas (such as a cow's face) while controlling the overall computational overhead, it is usually necessary to finely model local features while maintaining the receptive field. As a backbone network with a hierarchical structure and a window attention mechanism, SwinTransformer has demonstrated high performance in image classification and target recognition tasks. However, the local attention mechanism based on a fixed window adopted by it is easily restricted by window division when faced with images with complex structures and obvious local differences (such as a cow's face), making it difficult to fully capture the global and fine-grained features of the target. Therefore, in order to more effectively focus on the target area in the feature extraction stage and improve the perception of the details of the cow's face, the present invention introduces a two-layer routing attention module based on Swin Transformer to enhance the model's discrimination ability while maintaining efficient computation. The structure of the two-layer routing attention module is as follows: Figure 3 As shown in the figure, it mainly includes normalization layer, DW convolution layer, multi-layer perceptron and double-layer routing attention mechanism.

[0064] For the feature map input to the two-layer routing attention module, perform the following steps in sequence:

[0065] First, the input image is normalized, and then the normalized feature map is convolved to implicitly encode relative position information. In this embodiment, the convolution parameter is 3×3. Next, a two-layer routing attention mechanism (BRA) is applied to dynamically filter key areas, focusing only on a few relevant tokens to avoid global computation. Finally, a multi-layered layered prediction (MLP) is used to perform a nonlinear transformation on the token at each position. In this embodiment, the MLP has a two-layer structure with an expansion ratio of e.

[0066] The dual-layer routing attention module of the present invention performs feature modeling based on the dual-layer routing attention mechanism, which specifically includes a coarse-grained routing selection stage and a fine-grained attention aggregation stage.

[0067] In the coarse-grained routing stage, a coarse-grained correlation assessment is performed globally on queries and keys, screening out highly correlated regions or paths and defining them as key regions. Furthermore, in this stage, low-resolution feature maps are used for similarity scoring and a sparse attention graph is constructed, effectively filtering out key regions globally and improving the computational efficiency and regional focusing capabilities of the attention mechanism.

[0068] The fine-grained attention aggregation stage performs refined multi-head self-attention calculations only within selected key regions, achieving a balance between computational efficiency and modeling capabilities. Furthermore, in this stage, local self-attention calculations are performed on selected key regions within a high-resolution space, enabling more accurate recognition of local features of the cow's face (such as the eyes, nose, and ears).

[0069] Specifically, refer to Figure 4 As shown in the figure, the BRA mechanism realizes dynamic sparse attention through three steps: region division, routing screening, and fine-grained attention. First, given a 2D feature map Divide X into S×S non-overlapping regions, each region includes Characteristic vectors. Then, a shared linear layer is used to generate the query, key, and value, as shown below:

[0070] Q=X r W q ,K=X r W k ,V=X r W v

[0071] Where, is the projection weight.

[0072] Region-to-region routing filters the top-K related regions that each region needs to interact with. First, average pooling is performed on Q and K of each region to obtain the region-level representation, as shown in the following formula:

[0073]

[0074] Then calculate the inter-region correlation matrix

[0075] A r =Q r (K r ) T

[0076] A r(i, j) represents the semantic association strength between region i and region j. Finally, for each region, the most relevant k adjacent regions are retained to generate the routing index matrix

[0077] Among them, token-to-token attention only performs fine-grained attention calculation on the area after routing screening. r Collect the tokens of the relevant area from K and V, the formula is as follows:

[0078] K g =gather(K,I r ),V g =gather(V,I r )

[0079] Where, is the collected key-value tensor, and then, attention is applied to the collected key-value pairs:

[0080] O=Attention(Q,K g ,V g )+LCE(V)

[0081] According to the above process, the dual-layer routing attention module of the present invention has two advantages: first, the dual-level routing attention mechanism can dynamically select key areas for attention calculation based on the image content, significantly reducing the interference of irrelevant areas, effectively reducing the computational overhead while ensuring recognition accuracy; second, the introduction of positional deep convolution enhances the model's perception of local spatial structure, helps capture the fine-grained texture features of the cow face area, and improves the robustness to posture changes and complex background conditions.

[0082] In a further embodiment of the present invention, during the execution of the dual-layer routing attention mechanism by the dual-layer routing attention module, a gating unit can be introduced to dynamically adjust the coarse-grained screening threshold, thereby achieving adaptive attention path selection under different image scenes; at the same time, residual connections and normalization operations can be used to improve model stability and training convergence speed, such as Figure 3 shown.

[0083] 2. Convolution Enhanced Merging Module

[0084] The present invention sets a convolution enhancement merging module between the third and fourth stages of the backbone network, and after the output of the fourth stage, to enhance the key feature capture capability in cow face image recognition, improve the detail distinction capability and control the model calculation complexity. In the Patch Partition stage of the traditional Swin Transformer, the feature map will be roughly downsampled by 4 times, resulting in the loss of significant information of the cow face local features (such as nose lines, eye frames, and hair flow). Especially when the shallow features have not been fully extracted, directly entering the window attention processing may lead to subsequent confusion. To address this problem, the present invention proposes to add a convolution enhancement merging module to the backbone network to achieve efficient information retention and attention enhancement.

[0085] like Figure 5 As shown in the figure, the convolution enhancement merging module includes a coordinate attention block, a two-layer convolution block, and a SaE block to fuse channel attention with spatial coordinate information. It fuses the features perceived by attention through a residual convolution block and completes channel mapping through a 1×1 convolution, enhancing the responsiveness of key areas and reducing redundant calculations in invalid areas.

[0086] Among them, the coordinate attention block is used to model the spatial position information in the feature map. It has two core steps: coordinate information embedding and coordinate attention generation. It also models channel relationships and long-range dependencies and retains accurate position information. Its structure is as follows Figure 6 As shown in the figure. The coordinate attention block performs row pooling and column pooling on the input feature map, that is, it performs global average pooling on the input feature map in the vertical and horizontal directions respectively, extracts the height feature map and the width feature map, and obtains description vectors in two directions. These two description vectors are then concatenated along the height dimension to form a joint representation. The fused features are extracted through a shared 1×1 convolution and activation function. The fused features are then split into two corresponding directional components along the height dimension. The height attention map and the width attention map are generated through 1×1 convolution mapping respectively, thereby obtaining the attention weights. The obtained height attention map and width attention map are normalized by the Sigmoid function and applied to the corresponding dimensions of the original feature map to obtain the output features, completing the joint modeling process of space and channel. Finally, the attention weights of the two directions are fused through element-by-element multiplication and the output result is obtained as the output feature of the coordinate attention block.

[0087] Traditional channel attention (such as SE module) compresses spatial information through global average pooling, but loses position information. To solve this problem, coordinate attention decomposes global pooling into one-dimensional feature encoding in two directions. For each channel c and height h of the input feature map X, one-dimensional average pooling is performed along the width direction (W) with a kernel size of (H, 1) to generate feature map z h and z w :

[0088]

[0089] The above two transformations aggregate features along two spatial directions respectively to produce a pair of direction-aware feature maps, which can avoid the loss of position information and locate the target area more accurately.

[0090] Coordinate attention generation is based on the embedded coordinate information and generates attention weights through lightweight transformation. Given the generated aggregate feature map z h and z w , splicing z h and z w , the intermediate feature f is generated by shared 1×1 convolution F1 and nonlinear activation, the formula is as follows:

[0091]

[0092] Where [·,·] represents the concatenation operation along the spatial dimension, δ is the nonlinear activation function, and r is the reduction ratio of the control block size. Then, f is split along the spatial dimension into and Use 1×1 convolution F respectively h and F w Transformation, and the attention weight g is obtained through Sigmoid activation h and g w .

[0093] Will g h and g w Broadcast to the input size and multiply by the original feature element by element. The formula is as follows:

[0094]

[0095] The double-layer convolution block consists of two consecutive convolution operations, each of which includes a convolution layer (3×3 in this example), a batch normalization layer, and a ReLU activation function to further extract local context features. The double-layer convolution block takes the output of the coordinate attention block as input, and sequentially passes it through two layers of convolution with BatchNorm and ReLU activation to extract local context information and perform residual addition with the input features.

[0096] The SaE block is a multi-path channel attention mechanism that enhances expressiveness through channel compression and attention activation. The outputs of the two branches are concatenated and fused with 1×1 convolution to generate an enhanced feature map, thereby enhancing the feature interaction capability between channels.

[0097] Specifically, if Figure 7As shown, in the SaE block, the input feature map is first globally average pooled to compress spatial information, compress each channel into a numerical value, generate a channel description vector, and extract the channel features at the full image level, that is, the global context features of each channel. Then, four parallel different linear mapping branches are used to simulate four attention perception paths, and the input features are channel compressed and nonlinearly activated respectively, thereby forming four groups of low-dimensional feature representations, and completing the feature encoding of four different receptive fields. In this embodiment, the linear mapping branch is a fully connected layer, and the four fully connected layers extract features from different subspaces, and each path processes the same input feature separately. Next, the four groups of low-dimensional features output by the four paths are spliced ​​in the channel dimension, and the same group of features are processed using multiple different dimensionality reduction paths, so that attention learning is more diverse and expressive, avoiding the limitations brought by a single path. The present invention concatenates the four output vectors into a large vector along the channel dimension, i.e., a multi-scale joint expression, and then performs fusion and channel score calculation through a linear layer. Finally, the channel attention weight is generated through the Sigmoid activation function, and the attention weight is broadcast to the original feature map size. It is multiplied channel by channel with the original feature map to achieve adaptive feature recalibration. This method improves feature representation capabilities by increasing path diversity while maintaining computational efficiency through shared reduction. The formula is as follows:

[0098]

[0099] s=σ(W c ·y concate )

[0100] x out =x⊙s expand

[0101] Where x represents the input feature of the SaE module. Represents the pooling result of the bth sample and the cth channel. k W represents the output of the kth path. k represents the learnable weight matrix. r represents the compression ratio. W c represents the fusion weight matrix. s represents the channel attention weight.

[0102] The calculation of the entire SaE block can be summarized as:

[0103] x out =x⊙σ(W c ·[ReLU(W1·y),ReLU(W2·y),ReLU(W3·y),ReLU(W4·y)])

[0104] After the above steps, the features fused by the convolution enhancement merging module are mapped to the target output channel number through a 1×1 convolution to obtain the output features of the convolution enhancement merging module.

[0105] Based on the introduction to each module above, the process of building the cow face recognition model in this step can be described as follows: First, the base model is constructed using the Swin Transformer. Second, a two-layer routing attention mechanism is introduced into the base model to construct a two-layer routing attention module. Third, a convolutional enhancement merging module is constructed within the base model that integrates directional and channel attention. This module combines spatial directional attention with multi-branch channel attention to enhance the expressive power of the feature map.

[0106] Step 3: Recognition model training

[0107] Set parameters such as learning rate and perform model training.

[0108] Step 4: Input the cow face image into the trained recognition model to achieve cow face recognition.

[0109] Using the dataset constructed in step 1, we examine the efficiency and accuracy of various models, and the results are shown in Table 1.

[0110] Table 1 Various indicators of the model

[0111]

[0112] The results show that Mixer performs best in this group, achieving 97.3% precision, 97.8% accuracy, 96.9% recall, and 97.1% F1-Score. However, the Swin-BiCAS model surpasses the mainstream baseline in all metrics, achieving 99.1% accuracy. Compared to well-known models such as Vision Transformer and ResNet50, our model has advantages in precision, recall, accuracy, and F1 score.

Claims

1. A cow face recognition method based on Swin Transformer, characterized in that: The steps include: Step 1: Obtain cow face images and perform individual identity labeling Step 2, constructing a cow face recognition model, the recognition model includes an image segmentation module, a backbone network, a double-layer routing attention module, a convolution enhancement merging module and a classification head; wherein the image segmentation module is used to divide the cow face image into fixed-size and non-overlapping image blocks and perform linear embedding; the backbone network is constructed based on the Swin Transformer structure and is divided into four cascade stages, each stage consisting of a Patch merging module and multiple Swin Transformer Blocks, which are used to gradually extract multi-scale feature information from the cow face image; the double-layer routing attention module is introduced between the input and output of one or several stages of the backbone network to enhance the global context modeling capability and spatial relationship modeling; and the convolution enhancement merging module is introduced at the output of the stage where the double-layer routing attention module is introduced, and the output of the stage is fused with the output of the corresponding double-layer routing attention module as the input of the next stage to enhance the feature representation capability; the features output by the backbone network are input into the classification head for classification; Step 3: train the recognition model and use the trained model to perform individual identity recognition on the cow face image.

2. The cow face recognition method based on Swin Transformer according to claim 1, characterized in that: The image segmentation module consists of an image segmentation module and a linear embedding unit, and performs the following steps on the input cow face image: First, the cow face image is divided into fixed-size and non-overlapping image blocks, and then each image block is mapped into a fixed-dimensional feature vector through a linear embedding layer; The embedded image block features are input into the subsequent backbone network as input features of the backbone network.

3. The cow face recognition method based on Swin Transformer according to claim 1, characterized in that: The first double-layer routing attention module is introduced between the input and output of the third stage of the backbone network, and the second double-layer routing attention module is introduced between the input and output of the fourth stage; The first convolution enhancement merging module is introduced into the output of the third stage, and the second convolution enhancement merging module is introduced into the output of the fourth stage. The first convolution enhancement merging module fuses the output of the third stage and the output of the first double-layer routing attention module as the input of the fourth stage. The second convolution enhancement merging module fuses the output of the fourth stage and the output of the second double-layer routing attention module as the output of the backbone network.

4. The cow face recognition method based on Swin Transformer according to claim 1, 2 or 3, characterized in that: The dual-layer routing attention module includes a normalization layer, a DW convolution layer, a multi-layer perceptron, and a dual-layer routing attention mechanism. The following steps are performed in sequence on the feature map input to the dual-layer routing attention module: Normalization processing; Convolution is performed on the normalized feature map to implicitly encode relative position information; Apply a two-layer routing attention mechanism to dynamically filter key areas and only calculate attention on a few relevant tokens to avoid global calculations; The token at each position is transformed nonlinearly through a multi-layer perceptron.

5. The cow face recognition method based on Swin Transformer according to claim 4, characterized in that: The dual-layer routing attention module performs feature modeling based on the dual-layer routing attention mechanism, including a coarse-grained routing selection stage and a fine-grained attention aggregation stage; In the coarse-grained routing selection phase, a coarse-grained correlation evaluation is performed on the query and key globally, and regions or paths with high correlation are screened out and defined as key regions; The fine-grained attention aggregation stage performs detailed multi-head self-attention calculations only in selected key areas, thereby achieving a balance between computational efficiency and modeling capabilities.

6. The cow face recognition method based on Swin Transformer according to claim 5, characterized in that: The coarse-grained routing stage uses low-resolution feature maps for similarity scoring and constructs a sparse attention map, thereby efficiently screening key areas globally and improving the computational efficiency and regional focusing ability of the attention mechanism. The fine-grained attention aggregation stage performs local self-attention calculation on the selected key areas in the high-resolution space to achieve more accurate recognition of the local features of the cow face.

7. The cow face recognition method based on Swin Transformer according to claim 6, characterized in that: The dual-layer routing attention module introduces a gating unit to dynamically adjust the coarse-grained screening threshold during the execution of the dual-layer routing attention mechanism, thereby achieving adaptive attention path selection in different image scenarios; at the same time, residual connections and normalization operations are used to improve model stability and training convergence speed.

8. The cow face recognition method based on Swin Transformer according to claim 1, characterized in that: The convolution enhancement merging module includes a coordinate attention block, a double-layer convolution block and a SaE block in sequence, which is used to fuse channel attention and spatial coordinate information; Among them, the coordinate attention block is used to model the spatial position information in the feature map; the double-layer convolution block consists of two consecutive convolution operations, each of which includes a convolution layer, a batch normalization layer and a ReLU activation function in sequence, for further extracting local context features; the SaE block is used to enhance the feature interaction capability between channels; the convolution enhancement merging module is used as a whole to fuse spatial and channel attention information to improve feature expression capabilities.

9. The cow face recognition method based on Swin Transformer according to claim 8, characterized in that: The coordinate attention block in the convolution enhancement merging module generates description vectors in two directions by performing row pooling and column pooling on the input feature map, and concatenates them along the height dimension to form a joint representation. After processing with a shared 1×1 convolution and activation function, attention weights are generated and applied to the original feature map respectively. The SaE block enhances the expressiveness through channel compression and attention activation. The outputs of the two branches are concatenated and fused with 1×1 convolution to generate an enhanced feature map.

10. The cow face recognition method based on Swin Transformer according to claim 9, characterized in that: The coordinate attention block, row pooling and column pooling refer to performing global average pooling on the input feature map in the vertical and horizontal directions respectively, extracting the height direction feature map and the width direction feature map, that is, the description vectors in the two directions; then the description vectors in the two directions are spliced ​​along the height dimension to form a joint representation, and the fusion features are extracted through 1×1 convolution and activation function; the fusion features are then split into two corresponding directional components along the height dimension, and the height attention map and the width attention map are generated by 1×1 convolution mapping respectively, and then normalized by the Sigmoid function respectively, and acted on the corresponding dimensions of the original feature map to obtain the output features, thereby completing the joint modeling process of space and channel, and finally outputting the results after fusing the attention weights of the two directions by element-by-element multiplication to obtain the output features of the coordinate attention block; The two-layer convolution block takes the output of the coordinate attention block as input, and sequentially passes through two layers of convolution with BatchNorm and ReLU activation to extract local context information and perform residual addition with the input features; The SaE block,takes the output of the double-layer convolution block as input,first performs global average pooling to extract the global context features of each channel; Subsequently, the feature is fed into four different linear mapping branches in parallel to simulate four attention perception paths, performing channel compression and nonlinear activation on the input features respectively, thereby forming four sets of low-dimensional feature representations and completing feature encoding of four different receptive fields; then, the four sets of low-dimensional features are spliced ​​in the channel dimension to form a multi-scale joint expression, which is then fused and channel-scored through a linear layer, and the final channel attention weight is obtained using the Sigmoid function; then, the weight is rescaled to the same dimension as the input feature and multiplied element-wise with the original input feature on each channel to achieve channel recalibration of the input feature; The features fused by the convolution enhancement merging module are mapped to the target output channel number through a 1×1 convolution to obtain the output features of the convolution enhancement merging module.

Citation Information

Cited By

  • Remote sensing image defogging method based on improved DehazeFormer

    CN120833279A

  • A remote sensing image defogging method based on improved DehazeFormer

    CN120833279B