A cow re-identification method based on overlapping word segmentation attention mechanism
By constructing HSE feature sequences and a Transformer encoder, and combining overlapping word segmentation attention mechanism and cross-entropy loss function, the problems of insufficient feature utilization and insufficient robustness in dairy cow re-identification technology are solved, achieving high-precision and low-cost individual dairy cow identification, and adapting to the identification needs of complex breeding environments.
Patent Information
- Application Number
- CN202611191159.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-07
- Publication Date
- 2026-11-03
AI Technical Summary
Existing dairy cow re-identification technologies suffer from problems such as insufficient feature utilization, lack of global context, poor adaptability of attention mechanisms, high deployment costs, and insufficient robustness in complex farming environments, resulting in low identification accuracy and difficulty in meeting the actual application needs of large-scale smart farms.
We construct HSE feature sequences and achieve high-precision re-identification of individual cows by using an overlapping word segmentation attention mechanism and a Transformer encoder, combined with a cross-entropy loss function and a label smoothing strategy.
It improves the accuracy and stability of cross-scene and cross-frame recognition of individual dairy cows, and achieves high-precision and low-cost dairy cow re-identification, adapting to the recognition needs of complex breeding environments.
Smart Images

Figure FT_1 
Figure FT_2 
Figure FT_3
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer vision for individual animal weight recognition, specifically involving a dairy cow re-identification method (HSE-ReID) based on an overlapping word segmentation attention mechanism. This invention constructs an HSE (HOG-SE overlapping word segmentation attention) feature sequence and embeds it into a Transformer deep learning network model to achieve high-precision re-identification of individual dairy cows, effectively improving the accuracy and stability of cross-scene and cross-frame recognition of individual dairy cows. Background Technology
[0002] Individual cow weight recognition is a key foundational technology for intelligent livestock farming, disease monitoring, breeding, and intelligent claims processing. Its recognition accuracy, operational efficiency, and environmental adaptability directly impact the level of intelligent and refined development in the livestock industry. Current dairy cow weight recognition methods mainly fall into two categories: one relies on handcrafted features such as Scale-Invariant Feature Transform (SIFT), Speeded-Up Robust Features (SURF), and Histogram of Oriented Gradients (HOG) combined with traditional classifiers; the other is based on deep learning methods using convolutional neural networks and Transformer architectures. Existing technologies have several shortcomings in practical livestock applications, failing to meet the high-precision recognition requirements of complex farming environments. Specific problems include: First, insufficient feature utilization. Traditional HOG recognition methods often input extracted features as a global vector into the model, ignoring the spatial structure information contained in the HOG feature map, failing to achieve interactive fusion of local features, and struggling to effectively mine the individual differences among dairy cows. Second, insufficient global context modeling capabilities. Traditional shallow classifiers and some lightweight deep models cannot effectively model long-range dependencies in images, and they are poorly adaptable to common animal husbandry scenarios such as pose changes, lighting fluctuations, and complex background interference, resulting in weak algorithm robustness. Third, the attention mechanism has poor adaptability. Existing attention enhancement methods are mostly designed for convolutional feature maps and lack dedicated attention mechanisms adapted to HOG feature label sequences. They cannot specifically enhance core discriminative features such as cow body markings and local textures, easily leading to feature confusion and misidentification. Fourth, there is an imbalance between deployment costs and data requirements. When the native Transformer network is applied to cow re-identification tasks, it requires a large amount of labeled data to support training, and has high computational complexity and low inference efficiency, demanding high equipment computing power. Deployment on large-scale farms is costly and its practicality is limited. Fifth, there is insufficient robustness in complex scenarios. Traditional methods are highly sensitive to non-ideal acquisition conditions such as lighting changes, viewpoint shifts, and body occlusion, easily leading to feature failure and significantly reduced recognition accuracy under complex conditions. In summary, there is currently no dedicated attention enhancement mechanism in the field that can effectively integrate the advantages of HOG feature local shape description with the global modeling capabilities of Transformer, adapt to HOG feature sequences, and take into account high accuracy, strong robustness and low-cost deployment of dairy cow re-identification scheme. It is difficult to adapt to the actual application needs of large-scale smart ranches. The development of the technical solution of this invention has important engineering application value and industrial promotion significance. Summary of the Invention
[0003] This invention proposes a cow re-identification method (HSE-ReID) based on an overlapping word segmentation attention mechanism, aiming to solve the problems of low recognition accuracy caused by insufficient feature utilization, lack of global context, and poor adaptability of the attention mechanism in existing technologies. The specific technical solution includes the following three steps: Step 1: Construct HSE (HOG-SE Overlapping Segmentation Attention) Feature Sequence: First, extract the Histogram of Oriented Gradients (HOG) features from the input cow image to generate a HOG feature map with spatial structure. Then, use this feature map to construct overlapping segmentation—using an overlapping sliding window method to convert the HOG feature map into a feature label sequence with spatial adjacency. Map the feature label sequence to overlapping segmentation of a unified dimension that adapts to the input of the Transformer network. Finally, introduce a squeeze-and-excitation (SE) attention enhancement mechanism, perform global average pooling along the dimension of the number of feature labels in the overlapping segmentation, and recalibrate the embedded feature sequence channel by channel through channel attention weights to output the HSE feature sequence. Step 2, Transformer Encoding Recognition: Using the HSE feature sequence output from Step 1 as input, learnable category labels and positional codes are introduced. A multi-layer Transformer encoder is used to model the global dependency relationship of the HSE feature sequence, and the re-identification accuracy of each cow is output through the classification head. Step 3: The training phase employs the cross-entropy loss function combined with a label smoothing strategy to optimize the training process and calculate the difference between the predicted results and the actual cow identity labels. A method for re-identifying cows based on an overlapping word segmentation attention mechanism is proposed. Step 1 is implemented as follows: First, the input image undergoes preprocessing including size unification, grayscale conversion, and necessary illumination equalization. Then, the gradient magnitude and direction of each pixel are calculated to reflect local edge intensity. The image is divided into units, and the gradient direction distribution is statistically analyzed within each unit to form a gradient direction histogram. Adjacent units are then combined into blocks, and the histograms within each block are normalized to enhance illumination robustness. Finally, all blocks are arranged in spatial order to obtain a HOG feature map that retains both local texture information and global spatial structure. Next, overlapping word segmentation is constructed using this HOG feature map—a fixed-size sliding window is used to scan the HOG feature map sequentially, extracting all channel features within the window's coverage area each time and expanding them into a feature label. By controlling the sliding step size to be smaller than the window size, adjacent windows form 20%~50% overlap in both horizontal and vertical directions, ensuring the continuity of local gradient information. After traversing all window positions, the continuously distributed HOG feature map is transformed into an ordered sequence of feature labels. The feature label sequence is then fed into a fully connected mapping network. Two nonlinear transformations project the feature label sequence into a unified embedding space to obtain an embedded feature sequence, making its dimensions suitable for subsequent Transformer processing. Layer normalization is then performed on the embedded feature sequence; a multilayer perceptron with residual connections is then used to perform information interaction between adjacent labels, outputting overlapping word segments with consistent dimensions. Finally, the overlapping word segmentation is combined with the SE attention mechanism to complete the construction of the HSE feature sequence. Global average pooling is then performed on the overlapping word segmentation along the dimension of the number of feature labels to obtain the global response of each channel. Then, a two-layer fully connected network transforms the channels to generate activation weights for each channel. Adaptive channel weights in the 0-1 interval are obtained through Sigmoid activation mapping. These weights are then multiplied by the original overlapping word segmentation channel by channel to achieve adaptive recalibration of the feature channels, resulting in the HSE feature sequence. The HSE feature sequence can enhance sensitive discriminative features such as cow body patterns and texture boundaries, while suppressing noise caused by background and lighting changes. A cow re-identification method based on overlapping word segmentation attention mechanism is proposed. Step 2 is implemented as follows: First, learnable category labels are constructed and initialized using a truncated normal distribution, then concatenated at the beginning of the HSE feature sequence. Next, a learnable positional encoding matrix is constructed and added element-wise to the concatenated category label sequence, injecting spatial positional information into each HSE feature sequence. Then, a random deactivation operation is applied to the HSE feature sequence with added positional encoding before inputting it into a Transformer encoder. The Transformer encoder consists of 6 cascaded Transformer blocks. After 6 layers of Transformer encoding, the category label feature at the first position of the output HSE feature sequence is extracted as a global representation and input into the classification head. During the inference phase, the cow identification accuracy is output using the Softmax function. The training data is divided into training, validation, and test sets in a 7:2:1 ratio, with stratified sampling ensuring a balanced distribution of samples across categories. During model training, a cross-entropy loss function combined with a label smoothing strategy is used, with a smoothing coefficient set to 0.1. The optimizer uses the AdamW algorithm, with an initial learning rate of 3e-4, a weight decay coefficient of 0.005, and an exponential decay rate estimated by the first moment. Second-order moment estimation of exponential decay rate Training data is divided in a 7:2:1 ratio to ensure that images of the same cow do not span multiple datasets. The training batch size is set to 64, and the total number of training epochs is set to 300. After each epoch, the recognition accuracy is calculated on the validation set, and the model parameters with the highest accuracy on the validation set are saved. After training, the optimal model parameters are loaded onto the test set to evaluate the final recognition accuracy. During the model inference phase, the images to be recognized are sequentially input into the constructed HSE feature sequence and the Transformer encoding recognition module, and the final output is the accuracy of the cow re-identification result. Attached Figure Description
[0004] Figure 1 This is a diagram illustrating the overall architecture of a cow re-identification method based on an overlapping word segmentation attention mechanism.
[0005] Figure 2 Flowchart for constructing HSE feature sequences.
[0006] Figure 3 Flowchart for the Transformer encoding and recognition module. Implementation
[0007] The implementation steps of the present invention are described in detail below with reference to the accompanying drawings and embodiments.
[0008] A method for re-identifying cows based on overlapping word segmentation attention mechanism. Figure 1This is an overall architecture diagram of a cow re-identification method based on overlapping word segmentation and attention mechanism, which includes two steps: constructing HSE feature sequences and Transformer encoding recognition. Its specific implementation is as follows:
[0009] Step 1: Construct HSE feature sequences. Figure 2 The flowchart for constructing HSE feature sequences includes the following sub-steps: (1) HOG feature extraction: Receive cow images (JPG format), scale the images to a fixed size of 128×128 pixels, and convert them to grayscale. The Sobel operator is used to calculate the horizontal gradient for the changes in the magnitude and direction of the image pixel gradients. gradient in the vertical direction The operator kernel size is fixed at 3, and the gradient magnitude is... gradient direction Furthermore, θ is mapped from [-180°, 180°) to the unsigned interval [0°, 180°), so that opposite gradient directions correspond to the same direction category, reducing directional redundancy. Then, the image is divided into several 8×8 pixel units, resulting in a total of Each unit is divided into 9 intervals (0°, 180°). Each interval is 20° wide. The gradient magnitude of each pixel is accumulated between adjacent intervals using bilinear interpolation, forming a 9-dimensional gradient direction histogram for each unit. Then, adjacent 2×2 units are grouped into blocks, and the entire image is traversed with a stride of 1 unit, resulting in (16-2+1)×(16-2+1)=15×15=225 blocks. Each block contains 2×2×9=36-dimensional gradient histogram features. Intra-block normalization uses the L2-Hys strategy: first, the L2 norm is calculated. ,in To prevent division by zero constants, the block feature vector v is normalized to... Then, the normalized vector is truncated to V=min(V,0.2), and L2 norm normalization is performed again. The normalized HOG features of all blocks are concatenated in spatial order to obtain a HOG feature vector with a total dimension of 225×36=8100. In order to preserve the spatial adjacency relationship between blocks, the vector is rearranged into a HOG feature map of size 15×15×36, where the first dimension is the vertical spatial position (15 in total), the second dimension is the horizontal spatial position (15 in total), and the third dimension is the 36-dimensional normalized HOG descriptor corresponding to each block. All feature extraction processes are implemented using the OpenCV library. Image reading and scaling are performed using the cv2.imread and cv2.resize functions, and the interpolation method is cv2.INTER_AREA. Therefore, by statistically analyzing the pixel gradient direction distribution in local regions, HOG features effectively extract the texture, stripe boundaries, and local morphological structure information of the cow's body surface, which has stronger illumination robustness and structural expression ability compared to the original RGB features. At the same time, HOG features adopt a local region description method, which can preserve fine-grained differences between individuals. (2) Overlapping word segmentation construction module. First, the HOG feature map with a size of 15×15×36 is globally normalized, and the three-dimensional HOG feature is unfolded into a one-dimensional feature vector with a length of 8100. Calculate the mean of the entire HOG feature vector. with standard deviation Perform on each element To eliminate numerical scale differences between different images, where, It refers to the k-th element in the numerical value of the 8100-dimensional HOG eigenvector, not the entire eigenvector; This represents the standardized value of the corresponding element. This is a very small constant to prevent division by zero, avoiding a denominator of zero. After standardization, the HOG features are restored to a 15×15×36 three-dimensional feature representation to reduce the differences in numerical scale between different images caused by factors such as lighting and shooting environment, thus improving the stability of subsequent feature extraction. A fixed-size sliding window is used on the standardized HOG feature map to perform local region segmentation and extraction. The sliding window size is set to 3×3, and the sliding step size is set to 2. Since the sliding step size is smaller than the sliding window size, overlapping areas will form between adjacent sliding windows. Specifically, in the vertical direction, the starting position i takes values {0,2,4,6,8,10,12}, for a total of 7 starting positions, satisfying i+3≤15 always holds true; the same starting position values are used in the horizontal direction, also extracting 7 sliding window positions, for a total of 7×7=49 local feature blocks. For each sliding window, its coverage area is a 3×3×36=324-dimensional local HOG feature. All features within this area are expanded in a fixed order: vertical first, then horizontal, and finally channel direction, forming a 3×3×36=324-dimensional feature label. After traversing all 49 window positions, a feature label sequence of size 49×324 is obtained, where 49 is the number of feature labels and 324 is the original feature dimension of each label. Adjacent feature labels share a portion of the HOG region, enhancing the continuous expression of local texture information and facilitating the capture of fine-grained coat texture, pattern structure, and local morphological differences between individual cows. A two-layer fully connected mapping network is used to process the 49×324-dimensional feature label sequence. The first fully connected network layer has a weight matrix of 324×192 dimensions and a bias term of 192 dimensions. It performs independent linear mapping on each feature label, compressing the feature dimension from 324 to 192. Then, a non-linear transformation is introduced using the GELU activation function: GELU(x) = x∙Φ(x), where x represents a single value in a single feature label sequence, indicating an element-wise non-linear transformation; Φ(x) is the cumulative distribution function of a standard normal distribution. The second fully connected network layer has a weight matrix of 192×192 dimensions and a bias term of 192 dimensions, outputting 192-dimensional embedding features. Each label undergoes a mapping from 324 to 192, transforming the original sequence from 49×324 to a 49×192 embedding feature sequence. Layer normalization is performed on this embedding sequence, and the mean μ and variance are calculated for each feature label along its 192-dimensional embedding feature direction. Normalize each tag ,in Then, an affine transformation is performed using a learnable scaling parameter γ and a translation parameter β. , where ⊙ represents element-wise product. After layer normalization, a residual labeling mixing module is introduced. This module is implemented using a multilayer perceptron consisting of two fully connected layers, with the GELU activation function used in between; the calculation process... The input is added element-wise to the output of the multilayer perceptron (MLP) through residual connections to obtain the final 49×192-dimensional overlapping word segmentation. (3) Construction of HSE feature sequence. First, global average pooling is performed on the overlapping word segmentation along the dimension of the number of feature labels (N dimensions) to obtain the channel-level global description vector. The global description of its c-th channel is Where b ranges from [1, B], n ranges from [1, N], and c ranges from [1, C], the channel description vector is obtained. The global description vector is then input into the channel attention weight generation network, which consists of two fully connected layers: the first fully connected layer uses dimensionality reduction mapping to compress the 192-dimensional channel features to 24 dimensions, with a compression ratio of r=8. To ensure the network's expressive power, the hidden layer dimensions are... The first layer has a weight matrix of dimension C×H and uses the ReLU activation function to enhance its non-linear expressive power. The second fully connected layer uses a dimension-up mapping to restore the dimension from H to C, and the weight matrix has a dimension of H×C. The output is then mapped to the (0,1) interval by the Sigmoid activation function to generate channel attention weights. Expand the channel attention weights S along the label dimension to Feature recalibration is achieved through element-wise multiplication. X represents the initial overlapping word segmentation. This is the HSE feature tag sequence after SE attention enhancement. By multiplying the weights element-wise with the overlapping word segments, the discriminative feature channels such as important texture and pattern structures are enhanced, while suppressing background and noise information.
[0010] Step 2: Transformer encoding and recognition module, Figure 3 This is a flowchart of the Transformer encoder recognition module. It includes the following: global relational modeling of the HSE feature sequence using the Transformer encoder. The input HSE feature sequence is represented as... First, construct learnable category labels. Expanded by batch dimension And concatenate it at the beginning of the HSE feature sequence to obtain the extended sequence. A learnable positional encoding matrix is then introduced, initialized with a truncated normal distribution with a standard deviation of 0.02, and element-wise added to the extended sequence X' = X + P to inject spatial positional information. A random deactivation ratio of 0.1 is applied to the HSE feature sequence after adding the positional encoding, randomly discarding some feature responses to improve generalization ability. The processed HSE feature sequence is then input into a Transformer encoder, which consists of six cascaded Transformer Blocks. Each Transformer Block includes a multi-head self-attention mechanism, a feedforward neural network, layer normalization, residual connections, and random depth regularization. The computation process for each Transformer Block is as follows: , Here, LN represents layer normalization; MSA represents the output after multi-head self-attention; FFN represents a feedforward neural network, and residual connections are used to fuse input features with module output. The number of multi-head self-attention heads is set to 8, and the key dimension of each attention head is... The self-attention calculation process is as follows: Where Q, K, and V represent the query matrix, key matrix, and value matrix, respectively. The feedforward neural network expansion factor is set to 4, and the GELU activation function is used in the middle. The random depth probability of the 6 TransformerBlock layers increases linearly from shallow to deep, and is set to [0, 0.02, 0.04, 0.06, 0.08, 0.1]. After encoding by 6 TransformerBlock layers, the output sequence is subjected to final layer normalization, and the CLSToken corresponding features are extracted. This global feature is input into the classification head, and the recognition accuracy of individual cows is obtained by linearly mapping from 192 dimensions to 89 dimensions. During the inference stage, the Softmax function is used to obtain the recognition accuracy of individual cows. The training data is divided into training set, validation set, and test set in a 7:2:1 ratio, and stratified sampling is used to ensure a balanced distribution of samples in each category. Cross-entropy loss combined with label smoothing (coefficient 0.1), AdamW optimizer (learning rate 3e-4, weight decay 0.005), batch size 64, and training epochs 300 are used. After each epoch, the accuracy is evaluated on the validation set and the optimal model is saved. The early stopping method monitors the accuracy on the validation set; training stops if there is no improvement after 20 consecutive rounds. During the testing phase, the optimal model parameters are loaded, and the ratio of correctly predicted samples on the test set to the total number of samples is used as the final recognition accuracy. All matrix operations are accelerated on the GPU (CUDA).
[0011] To verify the effectiveness of this invention, tests were conducted on a Holstein-Friesian dairy cow image dataset, which covers tens of thousands of individual cow torso image samples. Experimental results show that the re-identification accuracy of the method of this invention reaches 97.75% on the test set, effectively verifying the effectiveness and superiority of this invention in the task of re-identifying individual dairy cows.
[0012] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A cow re-identification method (HSE-ReID) based on overlapping word segmentation attention mechanism, characterized in that, Includes the following steps: Step 1: Constructing the HSE Feature Sequence: First, extract the Histogram of Oriented Gradients (HOG) features from the input cow image to generate a HOG feature map with spatial structure. Then, construct overlapping word segmentation using the HOG feature map—convert the HOG feature map into a feature label sequence with spatial adjacency using an overlapping sliding window method. Map the feature label sequence to overlapping word segmentation of a unified dimension that adapts to the input of the Transformer network. Finally, introduce a squeeze-and-excitation (SE) attention enhancement mechanism, perform global average pooling along the dimension of the number of overlapping word segmentation, and recalibrate the overlapping word segmentation channel by channel through channel attention weights to output an enhanced HSE (HOG-SE overlapping word segmentation attention) feature sequence. Step 2, Transformer Encoding Recognition: Using the HSE feature sequence output from Step 1 as input; First, learnable class labels are constructed and initialized using a truncated normal distribution, then concatenated at the beginning of the HSE feature sequence. Next, a learnable positional encoding matrix is constructed and added element-wise to the concatenated class label sequence, injecting spatial positional information into each HSE feature sequence. Subsequently, a Transformer encoder is used to model the global dependencies of the HSE enhanced feature sequences. The Transformer encoder consists of six cascaded Transformer blocks. After encoding by the Transformer blocks, the output sequence is layer-normalized, and the re-identification accuracy of the cow is output through the classification head. Step 3: The training phase employs the cross-entropy loss function combined with a label smoothing strategy to optimize the training process and calculate the difference between the predicted results and the actual cow identity labels.
2. The method according to claim 1, characterized in that, In step 1, a fixed-size sliding window is used to scan the HOG feature map sequentially, extracting all channel features within the window's coverage area each time and expanding them into a single feature marker. By controlling the sliding step size to be smaller than the window size, adjacent windows form a 20% to 50% overlap in both the horizontal and vertical directions, thus ensuring the continuity of local gradient information.
3. The method according to claim 1, characterized in that, In step 1, the feature label sequence is mapped to a unified embedding space through a nonlinear transformation, making the feature label sequence dimension suitable for subsequent Transformer processing. Layer normalization is then performed on the feature label sequence projected to the unified embedding space; subsequently, a multilayer perceptron with residual connections is used to exchange information between adjacent feature label sequences. Finally, the output is an overlapping word segmentation with consistent dimensions.
4. The method according to claim 1, characterized in that, In step 1, global average pooling is performed on the overlapping word segments along the dimension of the number of feature tags to obtain the global response of each channel. Then, the channels are transformed through a two-layer fully connected network to generate activation weights for each channel. The Sigmoid activation function is used to implement adaptive channel weights. These weights are then multiplied by the original overlapping word segments channel by channel to achieve adaptive recalibration of the feature channels, resulting in the HSE feature sequence.
5. The method according to claim 1, characterized in that, In step 1, the SE attention enhancement mechanism should be placed after overlapping word segmentation and before Transformer encoding recognition.
6. A method for re-identifying cows based on overlapping word segmentation attention mechanism, wherein the dataset needs to be divided according to the individual cow identity identifier, and the images of different cows are assigned to the training set, validation set and test set respectively, to ensure that all images of the same cow belong to only a single dataset.
7. A method for re-identifying cows based on overlapping word segmentation attention mechanism, characterized in that... The processor is used to execute the method as described in any one of claims 1 to 6, wherein the modules are connected in sequence and the dataset is used to achieve re-identification of dairy cows.
