A method for identifying the identity of a group of sheep based on facial features

CN122695652APending Publication Date: 2026-09-04HENAN UNIV OF SCI & TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610471832.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-04-10
Publication Date
2026-09-04

AI Technical Summary

Technical Problem

[0003]现有技术中,传统羊只身份识别方式以耳标、烙印和RFID等方法为主,虽解决羊只身份识别问题,但存在显著缺点

Benefits of technology

基于现有技术存在的不足,本发明提供一种基于面部特征的群羊身份识别方法,通过优化结构设计,本发明具有如下技术效果:

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122695652A_ABST
    Figure CN122695652A_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of algorithm identification, in particular to a sheep group identity recognition method based on facial features, comprising constructing a sheep face original data set after de-redundancy and data enhancement, realizing sheep face area and three facial key point detection by using an improved RetinaFace network, extracting a 128-dimensional normalized sheep face feature vector through a HTL-Net network fused with TripletAttention, constructing a feature database, developing an intelligent platform integrating detection and recognition, and completing identity matching based on Euclidean distance. The method solves the problems of traditional ear tags, RFID and other recognition methods, such as easy to fall off, high cost, harm to sheep, low precision of existing recognition models, and difficult deployment, and has a sheep face detection accuracy of 96.15%, a recognition accuracy of 91.90%, a lightweight model, supports multi-form real-time recognition, and is suitable for large-scale sheep group precision management requirements.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of algorithm recognition technology, specifically to a method for identifying sheep in a flock based on facial features. Background Technology

[0002] Sheep farming is an important component of my country's animal husbandry industry. Traditional sheep farming relies on manual management, resulting in high labor costs, low efficiency, and susceptibility to human factors, making it difficult to adapt to the demands of large-scale sheep farming. Exploring intelligent farming technologies to promote the transformation of sheep farming from a traditional labor-intensive to a technology-intensive model, improving farming efficiency and resource utilization, and optimizing management and feeding methods are key to solving these problems. Currently, traditional small-scale sheep farming is gradually being replaced by large-scale, intelligent farms. In large-scale farming, sheep are raised at high densities, and improper management can lead to problems such as environmental degradation and outbreaks of diseases. Accurately identifying sheep's identity information is crucial for sheep health monitoring, improving the farming environment, and reducing disease risks, and is a key link in achieving precise monitoring and refined management of sheep.

[0003] Current technologies primarily rely on ear tags, branding, and RFID for sheep identification. While these methods solve the problem, they have significant drawbacks. Ear tags are prone to detachment, damage, or contamination, leading to information loss or unrecognizable traits. Prolonged wear can cause discomfort, infection, or trauma to sheep. Branding involves high temperatures, causing pain or injury, stress, or health problems. RFID tags are expensive to install, require specialized reading equipment, and are susceptible to environmental interference, resulting in decreased accuracy and failing to meet the needs of large-scale farming.

[0004] The market urgently needs a sheep identification method that avoids causing pain or injury to sheep, resulting in stress or health problems; a method that is low-cost, unaffected by environmental factors, and can meet the needs of large-scale farming. Summary of the Invention

[0005] To address the aforementioned technical issues, this invention proposes a flock sheep identification method based on facial features. Utilizing deep learning-based image recognition technology, and taking sheep facial images as the research object, a lightweight sheep face detection and recognition model is constructed. This model integrates two key modules: sheep face detection and sheep face recognition. A sheep face feature database is established, and a flock sheep identification system is designed to achieve end-to-end processing from image input to sheep identification, effectively meeting the needs of large-scale sheep farms for identification.

[0006] The technical solution adopted in this invention is: a method for identifying sheep in a flock based on facial features, comprising the following steps: S1. Construction of the original dataset: Obtain the sheep video data to be identified, and extract the original image sequence from the sheep video data by frame extraction; S2, Sheep face detection: Input the dataset constructed in step S1 into the RetinaFace sheep face detection network, and output the sheep face region localization information and facial key point coordinates; S3, Sheep face recognition: Input the sheep face region detected in step S2 into the HTL-Net sheep face recognition network for training. After training, extract features from the sheep face region to be recognized and output the sheep face feature vector. S4. Construction of sheep face feature database: Associate the sheep face feature vectors output by the recognition network in step S3 with the sheep identity information and store them in the sheep face feature database. S5. Intelligent Display Platform and Sheep Identity Recognition System: Based on PyQt5, a graphical user interface is developed, integrating the sheep face detection network completed in step S2 and the sheep face recognition network trained in step S3. It detects and extracts features from the sheep images to be identified, calculates the Euclidean distance between the extracted feature vector and the registered feature vectors in the database, and completes identity matching according to the minimum distance principle. It supports real-time detection and recognition of images, videos, and cameras, and has identity registration, database query, and management functions.

[0007] As a preferred option, the detailed steps of step S1 are as follows: S101. Keyframe extraction is performed on the acquired sheep herd video to obtain the original image. The original image sequence is then filtered for similarity using a differential hashing algorithm. The hash values ​​of adjacent frame images are calculated and the Hamming distance is compared. Redundant images with excessive similarity are removed. The processing steps of the differential hashing algorithm include: first, scaling the image to a fixed size, calculating the difference value of adjacent pixels to generate a hash value; then, calculating the Hamming distance between the hash values ​​of adjacent frame images, and determining that the image is similar and removing it when the Hamming distance is less than a preset threshold. S102. Data annotation is performed on the images filtered in step S101, annotating the sheep face region and three facial key points: eyes and nose. Data augmentation processing is then performed to form the original dataset. The data augmentation includes random blurring, random noise, changing brightness, random scaling, random occlusion, and random rotation.

[0008] As a preferred option, the detailed steps of step S2 are as follows: S201. The sheep face detection network uses GhostNet as a lightweight backbone network to extract features from the original dataset obtained in step S1. Basic features are generated through fewer convolution operations, and more features are generated through linear transformation and the basic feature map is output. S202. The basic feature map output in step S201 is enhanced by the SRM feature extraction module. The SRM module integrates the RepNCSPELAN4 module and introduces the multi-scale hole attention mechanism MSDA in the residual connection to perform fine-grained feature extraction on the basic feature map and output the enhanced feature map. S203. Construct the A_EFPN feature fusion network and input the enhanced feature map output in step S202 into it to perform multi-scale feature fusion and output multi-scale fused features. The A_EFPN network combines EfficientRepGFPN and variable kernel convolution AKConv to achieve the fusion of features at different scales.

[0009] Furthermore, in step S201, the GhostNet backbone network is composed of a series of GhostBottleneck (G-bneck) stacks. The image size of the input raw data is set to 640×640×3, and feature maps of sizes 80×80×64, 40×40×128 and 20×20×256 are taken as the output part of the backbone network. The GhostBottleneck is mainly composed of Ghost modules, which include two types: stride=1 and stride=2. In step S202, the Multi-Scale Dilated Attention (MSDA) mechanism captures multi-scale contextual information through dilated convolutions with different dilation rates, and enhances feature representation capabilities by combining ChannelShuffle operations, reducing the impact of ambient light changes on sheep face feature extraction. Specifically, MSDA obtains the corresponding Q (queries), K (keys), and V (values) of the input feature map through linear projection and divides the feature map channels into four different heads. Each head is set with a different dilation rate r, and each head processes different feature subsets in parallel to avoid ignoring key sheep face features. The dilation rates r are 1, 2, 3, and 4, respectively. In step S203, the variable kernel convolution AKConv breaks through the fixed local window limitation, allowing the convolution kernel to have any number of parameters and sampling shape. Its convolution operation is implemented in the following way: first, the sampling position of the convolution kernel is dynamically adjusted according to the size of the input feature map and the target receptive field; finally, the detection capability of sheep faces at different scales is improved through an adaptive sampling strategy. In step S204, the calculation formula for the multi-task joint loss function is as follows: In the formula, in the formula For face classification loss function, For predicting labels, For real labels, This is the facial bounding box loss function, used to predict the bounding box. With the true bounding box Interval error, Lpts( ) is the facial landmark regression loss function used for sheep face feature localization. SmoothL1Loss is used to measure the predicted landmarks. With real key points The difference is that λ1 and λ2 are the weights of the bounding box loss and keypoint loss functions, respectively, and the values ​​of λ1 and λ2 are both 0.5.

[0010] As a preferred option, the detailed steps of step S3 are as follows: S301. The HTL-Net network uses HG-Block as the basic unit for feature extraction, extracts features from the sheep face region output in step S2, and gradually aggregates local and global information in a hierarchical manner to output hierarchical features. S302. Input the hierarchical features output in step S301 into the TripletAttention mechanism, and use a three-branch structure to capture cross-dimensional interaction features between height, width and channels, and output the attention-enhanced features. S303. The attention-enhanced features obtained in step S302 are sequentially aggregated through global average pooling, Dropout regularization, fully connected layer and batch normalization to obtain a 128-dimensional feature vector. The feature vector is then normalized by L2 normalization to output a normalized sheep face feature vector. S304. Using the Triplet Loss function based on Euclidean distance combined with the cross-entropy loss function, the normalized sheep face feature vector output in step S303 is used as the training target to optimize the intra-class and inter-class distances in the feature space, thus completing the training of the sheep face recognition network. The Triplet Loss is a metric function based on Euclidean distance, with the optimal threshold range being 0 to 4 and a step size of 0.01. Values ​​less than the optimal threshold are considered as the same individual.

[0011] Furthermore, in step S301, the specific steps of the HG-Block hierarchical feature aggregation unit in processing the feature map are as follows: First, the input feature map is divided into multiple sub-feature groups according to the channel dimension; second, convolution operation is performed on each sub-feature group and they are aggregated step by step; finally, local and global information at different levels are fused through hierarchical connection. In step S302, the TripletAttention mechanism captures cross-dimensional interactive features of the input feature map in the three dimensions of height-channel, width-channel, and height-width through a three-branch structure, and averages and aggregates the outputs of the three branches. The specific steps of the average aggregation are as follows: first, a specific dimension is compressed through Z-pooling operation; second, attention weights are generated through convolution and activation functions; and finally, the attention weights of the three branches are aggregated and applied to the original feature map. In step S304, the formula for calculating the TripletLoss triplet loss function is as follows: Where, in the formula For anchor point samples, As a positive sample, For negative samples, α is a preset interval hyperparameter; the triplet loss function minimizes the feature vectors of the same sheep to make them closer together in the feature space, and the feature vectors of different sheep to make them farther apart, thus forming a more discriminative feature embedding.

[0012] As a preferred embodiment, in step S5, the identity matching process includes: during the identification process, using the Euclidean distance metric to determine whether the sheep face belongs to an identity already existing in the database, calculating the similarity between the feature vector of the sheep face to be identified and the feature vector stored in the database, and according to the set similarity threshold, if it is less than the threshold, it is determined to be the same sheep; otherwise, it is a different individual.

[0013] The beneficial effects of this invention are: To address the shortcomings of existing technologies, this invention provides a method for identifying sheep in flocks based on facial features. Through optimized structural design, this invention achieves the following technical advantages: Firstly, this invention addresses the problems encountered in actual detection processes, such as the similarity of facial and torso texture features, susceptibility to background interference, and the existence of sheep faces at different scales. It uses RetinaFace as the framework and GhostNet as the backbone network for feature extraction to reduce model complexity and computational load. It integrates the MSDA attention mechanism with RepNCSPELAN4 to design an SRM module as the feature extraction module, reducing environmental influences and enhancing the extraction of fine-grained features from sheep faces. Furthermore, it combines EfficientRepGFPN and AKConv to design the A_EFPN feature fusion network, strengthening feature interaction and improving the model's multi-scale detection capabilities. Finally, it optimizes the multi-task joint loss function, retaining three facial key points—eyes and nose—to reduce the number of regression parameters in the model. Experimental results show that the method proposed in this invention has an accuracy of 96.15%, a recall of 97.06%, an F1 score of 96.60%, an average precision of 98.31%, a normalized average error of 2.40% for key points, a computational cost of 2.36G, a model parameter count of 3.63M, and a weight size of 14.75MB. It is applicable to the rapid and accurate detection of sheep faces in actual breeding environments.

[0014] Secondly, this invention proposes an efficient HTL-Net sheep face recognition network model, which uses HG-Block as the basic unit for feature extraction. It processes feature maps in a hierarchical manner, gradually aggregating local and global information. While maintaining the efficiency of the model, it fully captures features of different depths and scales, thereby improving the recognition accuracy.

[0015] Third, this invention addresses the problem of high similarity among sheep species, making it difficult to extract effective features. It integrates the Triplet Attention mechanism into the feature extraction module to enhance the feature weights of key regions of sheep faces, reduce redundant information interference, and improve the model's ability to extract features with strong discriminative power for sheep faces.

[0016] Fourth, to optimize model computational efficiency, this invention uses LightConv lightweight convolutions instead of ordinary convolutions, further reducing network parameters and computational complexity. It combines global average pooling, Dropout regularization, fully connected layers, batch normalization, and L2 normalization to generate feature vectors corresponding to each sheep face image. Comparative experiments were conducted on feature vectors of lengths 128, 256, and 512, and the 128-length feature vector was selected as the model output. Five metrics based on Euclidean distance, cosine similarity, and angular margin were compared, and Triplet Loss based on Euclidean distance combined with cross-entropy loss was chosen as the loss function for the sheep face recognition network in this embodiment. Experimental results show that the proposed method in this embodiment has a false recognition rate of 8.90%, a rejection rate of 7.40%, an accuracy of 91.90%, a model computational cost of 0.06G, a model parameter count of 0.79M, and a weight size of 3.37MB, achieving accurate identification of sheep.

[0017] Fifth, this invention also includes an intelligent display platform and a flock sheep identification system, integrating two key modules: sheep face detection and sheep face recognition, and establishing a sheep face feature database. By detecting sheep face regions and key facial points, the system performs sheep face image cropping, alignment, and feature extraction, and finally compares the results with information in the database, achieving a complete process from input image to sheep identification. Attached Figure Description

[0018] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0019] Figure 1 This is a flowchart of the present invention; Figure 2 This is a flowchart of the differential hashing algorithm in step S1 of the present invention; Figure 3 This is an image enhancement effect obtained after data enhancement processing in step S1 of the present invention; Figure 4 These are images showing the sheep face detection results in different scenarios according to the present invention; Figure 5 This is the overall process of identity recognition in this invention; Figure 6 This is a diagram of the RetinaFace network structure in step S2 of this invention; Figure 7 This is a schematic diagram of the multi-scale hole attention mechanism (MSDA) in step S2 of this invention; Figure 8This is a diagram of the HTL-Net network structure in step S3 of this invention; Figure 9 This is a rendering of the intelligent display platform and flock sheep identification system of the present invention. Detailed Implementation

[0020] The present invention will now be described in detail through exemplary embodiments. However, it should be understood that, without further description, elements, structures, and features in one embodiment may be advantageously incorporated into other embodiments.

[0021] It should be noted that, unless otherwise defined, the technical or scientific terms used herein should have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "a," "an," or "the," and similar words used in the specification and claims of this patent application do not express a limitation of quantity, but rather indicate the presence of at least one; terms such as "comprising" or "including" indicate that the elements or objects preceding "comprising" encompass the elements or objects listed following "comprising" or "including" and their equivalents, but do not exclude other elements or objects having the same function.

[0022] To more clearly describe the specific structural components of this facial feature-based flock sheep identification method, in conjunction with the appendix... Figure 1 -Appendix Figure 9 This embodiment is described as follows: The following is an example of a method for identifying sheep in a flock based on facial features: S1. Construction of the original dataset: Obtain the sheep video data to be identified, and extract the original image sequence from the sheep video data by frame extraction; Sheep herd video collection: The sheep images used in this embodiment are from Xiangshun Agricultural and Animal Husbandry Technology Co., Ltd., Luoning County, Luoyang City, Henan Province. In the actual breeding environment, a Canon EOS 750D SLR camera with a frame rate of 30 frames / s and a mobile phone were used for assisted shooting to obtain videos of sheep in their natural state through long-distance, non-contact shooting. To simulate the complex environment of a real sheep farm, the collected data includes images from different angles, distances, light intensities, occlusions, lambs, single sheep, and flocks of sheep to ensure data diversity. Keyframes were extracted from the original video data every 25 frames using FFMpeg and stored in "*.png" format, while images containing complete and clear sheep faces were selected and retained. This embodiment involves data collection in a standardized sheep farm environment. The sheep farm uses a pen-based rearing model, with approximately ten sheep per pen. In this environment, individuals are prone to stacking and obscuring each other. If group video recording is used directly, the sheep's identities are easily confused and it is difficult to obtain complete facial images. Therefore, it was decided to select 1-3 sheep from each pen and track them with a mobile phone to ensure that only 1-2 sheep appear in each video segment. During the filming process, the sheep's movement trajectory is tracked in real time to ensure the integrity of the sheep's facial area. At the same time, the shooting angle and distance are continuously changed to increase data diversity.

[0023] After video acquisition, the OpenCV library in Python was used to extract video frames from each video segment. To improve processing efficiency and reduce data redundancy, two frames per second were extracted. Images with severely missing or blurred faces were removed, resulting in 6484 images of 155 individuals. The images of each individual were categorized and saved into different folders, named from "ID001" to "ID155". Finally, the categorized images were sequentially input into the sheep face detection model constructed in this embodiment, with a confidence threshold of 0.5, and images of sheep facial regions exceeding the threshold were selected.

[0024] To address the issue of excessively high similarity between images, a differential hashing (d-Hash) algorithm is used to calculate the hash values ​​of two images. The similarity is then determined by comparing the Hamming distance between the two images, and images with high similarity are discarded. The specific steps are as follows: First, the two input images are scaled down to 9×8 pixels to reduce the amount of data. Second, the scaled images are converted to grayscale to reduce the influence of color information and focus on brightness changes. Then, each row of pixels is traversed, and the brightness difference between adjacent pixels is calculated. If the brightness of the preceding pixel is greater than that of the second pixel, the difference value is set to "1"; otherwise, it is set to "0", generating two 8×8 binary hash matrices. Finally, the two hash matrices are converted into hexadecimal strings, representing the hash value of each image. The Hamming distance between the two images is then calculated; the smaller the distance, the more similar they are.

[0025] In this embodiment, a Hamming distance threshold of 10 is set. If the Hamming distance is less than 10, one image is deleted; if it is greater than or equal to 10, it is retained. Simultaneously, images with blurred sheep faces or severely missing sheep face information are removed from the remaining images, resulting in 2906 original images.

[0026] The selected images were annotated. In this embodiment, Labelme software was used to annotate sheep face images. The coordinates of the upper left corner were connected to the lower right corner to select the sheep's facial region, ensuring that the sheep's face appeared completely within the target box, while minimizing the target box to avoid the influence of cluttered backgrounds. By observing the sheep's facial images, it was found that the sheep's facial features were mainly concentrated in the area enclosed by the eyes and nose. In the case of the sheep's frontal view, the left and right corners of the mouth were easily obscured and the features were not obvious. Therefore, three key facial points, namely the sheep's eyes and nose, were selected for annotation to locate the sheep's facial region, preparing for subsequent sheep face alignment.

[0027] After labeling using Labelme software, a JSON file was obtained for each image, containing the coordinates of the top-left and bottom-right corners of the sheep's face bounding box, as well as the coordinates of three facial keypoints: eyes and nose. Since the label file used for training this model is in the Wider Face facial detection dataset format, a Python program was written to read the JSON file information, convert it to the Wider Face dataset format, and store it in a "*.txt" file. The file begins with a "#" symbol to indicate the relative path where each image is stored. The following lines contain the sheep's face location information, with each line representing one sheep face. This includes the coordinates of the top-left corner of the sheep's face bounding box, the width and height of the bounding box, and the coordinates of the three facial keypoints, separated by "0.0". If any facial keypoints are missing, all three facial keypoint coordinates are set to "-1.0".

[0028] In deep learning, training neural network models requires a large amount of data. Insufficient data can lead to the neglect of important features and overfitting. To improve the generalization ability and robustness of deep learning models, data augmentation techniques are used to expand the original samples, increasing data diversity and richness.

[0029] Due to the diverse variations in lighting, background, and pose conditions encountered in actual sheep face image acquisition environments, and considering the characteristics of sheep face detection data, data augmentation strategies were designed, including Gaussian noise, random scaling, brightness adjustment, pixel modification, and translation. Adjusting brightness simulates various lighting conditions, enhancing the model's robustness under different lighting environments. Adding Gaussian noise and modifying pixels improves the model's adaptability to complex backgrounds and external interference. Geometric transformation techniques such as random scaling and translation simulate changes in facial features of sheep under different poses, thereby enhancing the model's adaptability to pose diversity. Through data augmentation, each original image was augmented with six enhanced images, resulting in a sheep face recognition dataset of 45,388 sheep face images from 155 individuals. These 155 sheep were randomly divided into training, validation, and test sets in a 7:1:2 ratio. Specific randomization conditions are as follows: (1) Random blur. By applying Gaussian blur to the image and randomly selecting Gaussian kernel size of 1×1, 2×2 or 3×3, the blurring effect caused by camera inaccuracy or sheep movement in the actual acquisition environment is simulated, thereby improving the robustness of the model when processing low-quality images.

[0030] (2) Random noise. Gaussian noise is randomly added to the image to simulate the effects of natural light fluctuations, environmental interference or sensor noise on the camera equipment, thereby improving the stability of the model in noisy environments.

[0031] (3) Change the brightness. By randomly adjusting the image brightness value within a range of ±50%, the scene changes under different lighting conditions are simulated to enhance the model's adaptability to changes in light intensity.

[0032] (4) Random scaling. By adjusting the image scale by 0.5-1.0 and filling it, sheep face samples of different scales are generated to enhance the model's multi-scale detection capability.

[0033] (5) Random Occlusion. Randomly generate rectangular occlusions that cover 10%-20% of the image to simulate the situation where part of the sheep's face is occluded in the actual scene, thereby improving the model's ability to recognize sheep under occlusion conditions.

[0034] (6) Random rotation. The image is randomly rotated between ±45° to simulate the changes in facial features of sheep in different postures, thereby enhancing the model's recognition ability from different perspectives.

[0035] S2, Sheep Face Detection: Input the dataset constructed in step S1 into the trained sheep face detection network, and output the sheep face region localization information and facial key point coordinates.

[0036] To achieve efficient and accurate sheep face detection in the complex environment of a sheep farm, the RetinaFace network model is adopted as the basic framework for sheep face detection in this embodiment. RetinaFace is a single-stage face detection algorithm. Unlike traditional two-stage detectors, RetinaFace has more efficient and faster inference capabilities. It consists of three parts: a backbone network, a feature fusion module, and a predictor. The RetinaFace backbone network uses ResNet50 as the feature extractor. Its core advantage lies in effectively solving problems such as vanishing or exploding gradients during deep network training by introducing residual structures. The use of skip connections facilitates efficient information flow within the network, allowing it to be transmitted to deeper layers, avoiding the training difficulties caused by increasing the number of layers, and improving convergence speed. The feature fusion module consists of a Feature Pyramid Network (FPN) and a Single Stage Headless (SSH) module. The FPN introduces top-down and lateral connection strategies to fuse deep and shallow features to obtain feature maps that combine high-level semantic information with low-level spatial details, improving the model's multi-scale detection capability. SSH employs a multi-scale receptive field technique, introducing convolutional kernels of different sizes (3×3, 5×5, 7×7) on each feature layer to promote the fusion of local and global information and enhance the model's feature representation capabilities. The predictor adopts a multi-task learning (MTL) strategy, including three tasks: face classification, face bounding box regression, and face keypoint regression. Each task focuses on a different detection target, thereby improving the model's overall performance and generalization ability.

[0037] To reduce model complexity and computational load, this embodiment uses GhostNet as the backbone network for feature extraction to achieve rapid sheep face detection. Addressing the issue of uneven lighting distribution in actual sheep farms, unclear boundaries between the sheep's face and torso, and similar texture features leading to inaccurate sheep face localization, an SRM module is designed by integrating the MSDA attention mechanism and RepNCSPELAN4 as the main component of the feature fusion module. This reduces environmental influences and enhances the extraction of fine-grained sheep face features. To address the issue of missed and false detections due to the large number of sheep and varying face sizes in large-scale farming environments, an A_EFPN feature fusion network is designed using Efficient RepGFPN and AKConv to replace FPN, improving the model's multi-scale detection capability. To adapt to the characteristics of the sheep's facial region, the multi-task joint loss function is optimized, retaining three facial key points: the eyes and nose, reducing the number of regression parameters in the model.

[0038] In this embodiment, GhostNet is selected as the backbone network. GhostNet is a lightweight network architecture that addresses the feature redundancy phenomenon in neural networks by designing a Ghost module to generate "ghost features." Its core idea is to generate basic features with fewer convolution operations, and then generate more features through a series of simple linear transformations, thereby reducing the computational complexity of the network.

[0039] If the Ghost module is used to generate the same number of feature maps as the regular convolution, given an input feature map size of h×w×n, an output feature map size of h′×w′×c′, a convolution kernel size of k×k, an average kernel size of d×d for linear operations, and a feature map channel compression factor of s, then the computational cost (FLOPs) of the regular convolution and the Ghost module are respectively: (3-1) Where k×k, d×d, and s are all much smaller than c, and their ratio is: Therefore, it can be seen that the computational cost of using the Ghost module is reduced to about 1 / s of that of ordinary convolution.

[0040] GhostNet is composed of a series of Ghost Bottleneck (G-bneck) stacks, and the network parameters are shown in Table 1 below: Table 1 To improve the model's multi-scale detection capability, the input image size was set to 640×640×3, and feature maps of sizes 80×80×64, 40×40×128, and 20×20×256 were used as the backbone network output. The Ghost Bottleneck mainly consists of Ghost modules, including stride=1 and stride=2 types. The input feature map undergoes feature extraction and channel number modification via the Ghost module. Simultaneously, a shortcut branch directly connects the input to the Ghost module output, reducing model computation and minimizing feature information loss during inter-layer propagation.

[0041] To address the issues of inaccurate sheep face bounding boxes and keypoint localization caused by background interference and the similarity in color between the sheep's face and torso during actual detection, this paper integrates the RepNCSPELAN4 module from YOLOv9. Simultaneously, it introduces the MSDA attention mechanism into the residual connections and designs an SRM feature extraction unit based on the Channel Shuffle concept to reduce environmental influences and enhance the extraction of fine-grained features from the sheep's face. The input feature map is linearly combined across different channels using a 1×1 depthwise separable convolution to achieve cross-channel information interaction and reduce computational cost. The M_RCE module further enhances feature extraction, improving spatial and contextual feature representation and obtaining fine-grained features. A Concat operation fuses shallow spatial information with deep semantic information to obtain global features. Finally, Channel Shuffle randomly mixes features along the channel dimension, breaking down the isolation between channels, avoiding redundancy and interdependence between feature channels, and improving the model's generalization ability.

[0042] RepNCSPELAN4 is mainly composed of RepNCSP and RepNBottleneck. It incorporates the ELAN concept with a multi-branch structure, enabling the capture of multi-dimensional feature information across different receptive fields. The branching structure allows for effective feature transfer within the network, enhancing the model's feature representation capabilities. By stacking multiple sub-modules, the network depth is increased, capturing deeper features and improving the ability to recognize targets in complex scenes. Furthermore, a reparameterization technique is introduced, using multiple different convolutional branches during training and merging these branches into a single equivalent convolutional kernel during inference. This achieves the same feature extraction effect with less computation, improving inference efficiency.

[0043] To reduce the influence of background factors and enhance the extraction of key features and multi-scale features of sheep faces, an MSDA attention mechanism is introduced in the RepNBottleneck residual connection. Given an input feature map, Q (queries), K (keys), and V (values) are obtained through linear projection. The feature map channels are divided into four different heads, each with a different dilation rate r (r=1, 2, 3, and 4). Multi-scale sliding window dilated attention is performed on different heads with different dilation rates. The outputs of each head are merged through a concat operation and finally input into a linear layer for feature aggregation, integrating the information learned from each head to obtain rich feature representations. MSDA utilizes the sparsity of the self-attention mechanism at different scales, focusing on features at different scales by setting different dilation rates on different heads, obtaining rich contextual information, and improving the model's multi-scale detection capability. At the same time, separating the feature map channels into multiple heads, with each head processing different feature subsets in parallel, avoids ignoring key features of sheep faces and improves the accuracy of sheep face bounding boxes and keypoint localization.

[0044] To improve the model's ability to detect sheep faces of different sizes, an A_EFPN feature fusion network was designed to replace the FPN, based on the Efficient RepGFPN connection path fusion with AKConv. Since features at different scales have significant depth differences, direct fusion is not conducive to multi-scale feature representation. For example, when using large-scale features to detect small objects, the feature depth is shallow, failing to capture detailed target information and affecting the detection capability of small objects. Unlike the FPN, which directly fuses the three feature layers C3, C4, and C5, A_EFPN consists of five nodes composed of SRM modules, processing high-level semantic information and low-level spatial information with equal priority. It simultaneously receives serial feature inputs from different scales above or below and from the same feature depth, greatly increasing the amount of information fused and promoting the fusion of multi-scale information at the same depth. This enhances the interactive fusion of features at different scales and depths. AKConv defines the initial sampling shape for different sized convolutional kernels and adjusts the initial sampling shape using learned offsets. The feature map is then resampled based on the adjusted sampling shape, effectively adapting to various target size variations and improving the model's multi-scale detection capability. Meanwhile, AKConv uses asymmetric convolution kernels, which supports linear adjustment of the number of convolution parameters, thus contributing to model lightweighting.

[0045] The RetinaFace loss function includes face classification loss, face bounding box loss, and loss for five facial landmarks. Based on the actual situation of sheep faces, it was found that the distinguishable features of sheep faces are mainly concentrated in the area enclosed by the eyes and nose, and the left and right corners of the mouth are easily obscured. Therefore, the facial landmark loss function was optimized to retain only three facial landmarks: eyes and nose.

[0046] The joint loss function for multiple tasks is shown below: In the formula For face classification, the cross-entropy loss function is used. For predicting labels, The labels are for real numbers. The goal of the face classification task is to determine whether an image contains a sheep's face. By learning the facial features of sheep in candidate regions, the model can accurately distinguish sheep faces from background regions, helping it to quickly identify images containing sheep faces and providing preliminary facial region predictions for subsequent face bounding box regression and keypoint regression. The loss function for the face bounding box is a smooth L1 loss function, which is used to regress the location of the sheep face bounding box and predict the bounding box. With the true bounding box Inter-range error. The goal of the facial bounding box regression task is to accurately regress the sheep's face location information, predict the coordinates of the sheep's face in the image's upper left and lower right corners, generate the sheep's face bounding box, and further adjust the size and shape of the bounding box to accurately enclose the sheep's face region, providing an accurate reference frame for facial keypoint regression and further improving the accuracy of facial keypoints. Lpts( () is the facial landmark regression loss function used for sheep face feature localization. Smooth L1 Loss is used to measure the predicted landmarks. With real key points The difference between the two faces is investigated. Through this task, the predictor can accurately locate the coordinates of the key points of the eyes and nose on the sheep's face, thereby obtaining more refined facial feature information, which provides a foundation for subsequent sheep face alignment operations. λ1 and λ2 are the weights of the bounding box loss and key point loss functions, respectively. To ensure the balance of loss for each task, λ1 and λ2 are both set to 0.5.

[0047] During training, face classification, bounding box regression, and keypoint regression are trained jointly to fully leverage the correlation between different tasks. This effectively avoids performance bottlenecks that may occur in single-task training, promotes convergence across different tasks, reduces the risk of overfitting, and enables the model to have higher adaptability and accuracy in various scenarios, thereby improving the model's generalization ability. Simultaneously, all sub-tasks can share the feature representations extracted from the network's intermediate layers, thus reducing computational overhead and improving training efficiency and inference speed.

[0048] This embodiment builds a sheep face detection network based on the PyTorch framework and uses an Intel(R) Xeon(R) Silver4210R CPU @ 2.40GHz and an NVIDIA Quadro RTX 5000 GPU for model training. To ensure efficient training, the input images are uniformly adjusted to 640×640 pixels to ensure the consistency of input data in the model and avoid training instability caused by inconsistent input sizes. This preserves sufficient facial feature information while reducing memory consumption and excessive training time caused by excessively large images. The iteration cycle (epoch) is set to 150 rounds. In each training cycle, the model updates its parameters iteratively through forward and backward propagation, gradually approaching the optimal solution. To balance memory limitations and training performance, the training batch size is set to 16. In each training process, the model processes 16 image data simultaneously and updates network parameters by calculating gradients. This ensures computational efficiency while avoiding memory overflow or computational bottlenecks caused by excessively large batches. To optimize the model learning process, this embodiment selects Stochastic Gradient Descent (SGD) as the optimizer to optimize model parameters. By gradually adjusting the parameters, the objective function loss is minimized, allowing the model to continuously approach the global optimum during training. Furthermore, the learning rate is set to a maximum of 0.01 and a minimum of 0.0001. To avoid the learning rate being set too high or too low during training and affecting model convergence, this embodiment employs Cosine Annealing Learning Rate Decay (CALR) to adaptively adjust the current learning rate during training. Dynamically changing the learning rate helps the network converge quickly in the early stages, effectively avoiding the oscillation problem caused by an excessively high learning rate in the later stages of training, and improving the stability of model training.

[0049] Evaluation indicators To verify the overall performance of the model, this embodiment selects precision, recall, F1 score, average precision (AP), normalized mean error (NME), computational cost (FLOPs), number of parameters, and weight as evaluation metrics, with the corresponding formulas as follows: In the formula, TP represents the number of samples correctly predicted as positive, FP represents the number of samples incorrectly predicted as positive, and FN represents the number of positive samples incorrectly predicted as negative. Precision is the proportion of samples predicted as positive that are actually positive. Recall is the proportion of samples actually positive that were correctly predicted as positive by the model. The positive samples mentioned above represent sheep-face targets, and the negative samples represent non-sheep-face targets. F1 Score is the harmonic mean of Precision and Recall, used to comprehensively evaluate Precision and Recall. AP is the area enclosed by the PR curve plotted with Recall on the horizontal axis and Precision on the vertical axis. NME is a metric for evaluating keypoint detection performance, calculating the error between predicted and ground truth keypoints.

[0050] Backbone Network Comparison Experiment To achieve model lightweighting, ResNet50 was replaced with MobileNetv1, MobileNetv3, and GhostNet as the RetinaFace backbone networks for comparative experiments. To ensure the fairness of the experimental results, all models were retrained on the sheep face dataset created in this embodiment, and the results are shown in Table 2. Replacing the original ResNet50 backbone network of RetinaFace with a lightweight network will result in some loss of detection accuracy due to the reduced feature extraction capability caused by the shallower model depth, but FLOPs, Params, and Weight are significantly reduced, making it more suitable for mobile deployment in actual farming environments. Comparing MobileNetv1, MobileNetv3, and GhostNet, MobileNetv1 has the lowest FLOPs, Params, and Weight, but its detection accuracy is insufficient and keypoint error is large. GhostNet has the highest Precision, Recall, F1 Score, and AP, and the lowest NME keypoint error, while also meeting the lightweighting requirements. Therefore, GhostNet was selected as the backbone network in this embodiment, and the performance comparison results of different backbone networks are shown in Table 2 below. Table 2 To verify the effectiveness of the method proposed in this embodiment, a performance comparison analysis of the model before and after the improvement was conducted, and the results are shown in Table 3: Table 3 In Table 3, ① indicates the use of the GhostNet backbone network; ② indicates the use of the A_EFPN feature fusion network; and ③ indicates the introduction of the MSDA attention mechanism in the SRM module of the A_EFPN feature fusion network. As shown in the table, compared to the original RetinaFace, replacing the backbone network with GhostNet resulted in a decrease in model detection accuracy, but a significant reduction in complexity. Replacing FPN with the A_EFPN feature fusion network designed in this embodiment increases the amount of feature fusion information in the model while enhancing the model's feature interaction capabilities, significantly improving detection accuracy. Introducing the MSDA attention mechanism enhances the model's focus on key target information, further improving detection accuracy. Compared to RetinaFace, Precision, Recall, F1 Score, and AP increased by 1.78%, 0.22%, 1.01%, and 0.83%, respectively, while NME, FLOPs, Params, and Weight decreased by 0.02%, 94.71%, 86.70%, and 86.21%, respectively. In summary, the method proposed in this embodiment ensures detection accuracy while offering faster detection speed and optimal overall performance, thus meeting the actual detection needs and deployment requirements of sheep farms.

[0051] To further verify the model's detection performance, such as Figure 4 As shown, 12 different scenarios were selected to simulate the living conditions of sheep in a real farm environment, and sheep face selection and key point detection were performed. Figure 4 (a) Figure 4 (b) Figure 4 (d) and Figure 4 (e) shows the sheep's face from four different angles: front view, side view, upward view, and downward view; Figure 4 (g) and Figure 4 (h) indicates large-scale and small-scale sheep faces; Figure 4 (j) and Figure 4 (k) indicates low-light and high-light environments; Figure 4 (c) and Figure 4 (f) represents the cubs and the environment affected by background interference; Figure 4 (i) and Figure 4 (l) represents a scene of flocks of sheep and flocks of sheep stacked together. As can be seen from the figure, the model proposed in this embodiment can still accurately select the bounding box and detect key points of sheep faces in different scenarios, meet the actual sheep face detection needs of sheep farms, and provide a foundation for subsequent sheep face recognition.

[0052] S3. Sheep face recognition: Input the sheep face region detected in step S2 into the trained sheep face recognition network HTL-Net for training. After training, extract features from the sheep face region to be recognized and output the sheep face feature vector.

[0053] The core idea of ​​sheep face recognition in this embodiment is based on the comparison between feature vectors. A deep learning model maps the input sheep face image into a feature space with rich, high-dimensional semantic information, representing it as feature vectors to capture deep-seated features of the sheep face, making the distribution of different individuals in the feature space highly distinguishable. In the feature matching stage, the recognition system uses Euclidean distance as a similarity metric, utilizing the fact that feature vectors of the same individual are close in Euclidean distance, while feature vectors of different individuals are far apart in Euclidean distance to distinguish and identify the target.

[0054] Unlike traditional classification methods, this embodiment employs an efficient strategy that eliminates the need for frequent model retraining, offering high flexibility and scalability. When identifying new individual sheep, only the sheep's facial image needs to be input into the feature extraction network to generate a corresponding high-dimensional feature vector, which is then stored in the database. This allows for rapid matching with existing individuals during the identification process, without requiring retraining the entire model, enabling large-scale, unconstrained sheep face recognition. This method significantly simplifies model maintenance costs, improves system adaptability and practicality, and allows the sheep face recognition system to achieve real-time detection and dynamic updates, adapting to the constantly changing sheep populations in the actual environment of a farm.

[0055] This embodiment proposes an efficient HTL-Net sheep face recognition network model. It uses HG-Block as the basic feature extraction unit and processes feature maps hierarchically, gradually aggregating local and global information. While maintaining high model efficiency, it fully captures features at different depths and scales, improving recognition accuracy. Addressing the issue of high similarity among sheep species, making effective feature extraction difficult, a Triplet Attention mechanism is integrated into the feature extraction module to enhance the feature weights of key sheep face regions, reduce redundant information interference, and improve the model's ability to extract highly discriminative features for sheep faces. To optimize computational efficiency, LightConv lightweight convolutions replace ordinary convolutions, further reducing network parameters and computational complexity. Global Average Pooling (GAP) is used to calculate the mean of the feature maps, mapping high-dimensional feature maps to fixed-length global feature vectors, preserving global information and reducing redundant parameters. Dropout regularization is applied, randomly discarding some neurons to prevent the model from over-relying on certain features during training, preventing overfitting and improving generalization ability. Fully connected layers are utilized. The network uses Layer 1 (FC) and Batch Normalization (BN) to aggregate features and obtain a 128-dimensional feature vector, accelerating model training and improving model stability. Finally, L2 Normalization normalizes the 128-dimensional feature vector to a unit length, ensuring efficient matching of target features in a unified feature space. This allows the similarity between feature vectors to be directly measured using Euclidean distance, cosine similarity, or angular margin. The network parameters are shown in Table 4 below. Here, Stem represents the initial preprocessing module consisting of multiple stacked convolutional layers. Table 4 In this sheep face recognition task, the high facial similarity between individual sheep, coupled with the sheep's non-cooperation and environmental factors such as lighting during the recognition process, makes sheep face feature extraction quite challenging. A TripletAttention mechanism is introduced to highlight key facial features, improving the model's ability to distinguish highly similar individuals; it also suppresses background noise and irrelevant features, reducing misidentification and feature redundancy, thus enhancing model adaptability. Traditional attention mechanisms use channel attention and spatial attention to weight features from the feature channels and spatial dimensions, respectively. However, channel attention and spatial attention are separate and computationally independent. TripletAttention, on the other hand, employs a three-branch structure, capturing the height, width, and cross-dimensional interactive features of the input data, fully mining the salient features of sheep faces, and achieving more refined feature representation. Furthermore, TripletAttention establishes inter-dimensional dependencies through rotation operations and residual transformations, encoding channel and spatial information with negligible computational overhead. The upper branch is responsible for calculating the attention weights for C and H. First, the input tensor is rotated 90° counterclockwise along the H-axis, then processed through Z-pooling and convolutional layers. Finally, the attention weights are generated using the sigmoid function and rotated 90° clockwise along the H-axis to maintain the same shape as the input tensor. The middle branch is responsible for establishing the interaction between the C and W dimensions. It first rotates 90° counterclockwise along the W-axis, then rotates 90° clockwise along the W-axis, with the remaining operations being the same as the upper branch. The lower branch does not perform any rotation but establishes the interaction between the H and W dimensions. After generating the attention weights, each branch undergoes a permutation transformation. The outputs of the three branches are then averaged and aggregated (Avg) to obtain the final triple attention output.

[0056] In the HTL-Net network architecture proposed in this embodiment, the HTLBlock module, which integrates Triplet Attention and LightConv, serves as the basic unit for feature extraction. A hierarchical approach is adopted to process input features, extracting features at different levels of abstraction, achieving multi-scale feature extraction and fusion stepwise, thereby capturing global and local information in image processing tasks. By progressively reducing the feature map resolution through multi-layer convolution operations, deeper semantic information is obtained. Batch normalization (BN) and activation functions are combined for feature transformation, enhancing the network's non-linear expressive ability. Skip connections fuse shallow features with deep semantic information along the path, ensuring the network simultaneously focuses on local details and global features, effectively mitigating the gradient vanishing problem that may occur during deep network training and improving feature transfer efficiency. Comparing RepConv, GhostConv, DWConv, and LightConv, LightConv was chosen to replace ordinary convolutions, reducing computational complexity while maintaining efficient feature extraction capabilities. Triplet Attention is integrated to simultaneously optimize feature representation across feature channels, spatial dimensions, and cross-interaction dimensions, improving the model's focus on key sheep face features and preventing feature loss due to environmental factors such as lighting and occlusion, thus enhancing the model's ability to distinguish between different individual sheep faces.

[0057] In sheep face recognition, the loss function measures the difference between the model's predictions and the true values ​​during training, guiding the model's optimization. Its goal is to minimize the error between the model's predicted output and the true label, thereby improving classification accuracy. Sheep face recognition faces numerous challenges. Different sheep faces belong to different categories, but these categories all belong to the same order of sheep. Individual features are highly similar, resulting in small inter-class differences, which may lead to intra-class distances being greater than inter-class distances. Furthermore, sheep non-cooperative behavior, changes in ambient lighting, pose variations due to different shooting angles, and partial occlusion all contribute to the difficulty of classification. Therefore, the loss function not only needs strong discriminative power, allowing the facial features of the same sheep to cluster in a smaller feature space region, clearly distinguishing different individuals and reducing classification errors, but also needs good generalization ability, enabling the model to maintain high recognition accuracy when facing new sheep individuals and different environmental conditions.

[0058] Sheep face recognition is typically a fine-grained recognition problem. Traditional classification loss functions may be insufficient in handling subtle differences between individuals, resulting in unclear distinctions between different individuals. By introducing a metric learning loss function, the feature space is optimized, enhancing the discriminative power of sheep face features. Therefore, this embodiment comprehensively considers the characteristics of sheep face recognition tasks, comparing AdaCos Loss (based on cosine distance), CosFace Loss (based on angular distance), SphereFace Loss, ArcFace Loss (based on angular distance), and Triplet Loss (based on Euclidean distance). Ultimately, Triplet Loss is selected and combined with the cross-entropy loss function to fully utilize distance information in the feature space and improve recognition accuracy.

[0059] The core idea of ​​Triplet Loss is to construct triplet samples, namely anchor samples, positive samples of the same class as the anchor, and negative samples of different classes, to optimize the distance relationship between samples in the feature space. This ensures that features of the same sheep are more closely packed in the embedding space, while features of different sheep are more distant, thereby improving the model's recognition ability. The formula is as follows: In the formula For anchor point samples, As a positive sample, For negative samples, α is the preset margin hyperparameter.

[0060] However, relying solely on Triplet Loss can lead to unstable feature clustering during training, making it difficult for the model to form clear classification boundaries when the number of categories is large. Therefore, this embodiment introduces a cross-entropy loss function during model training to assist in feature category differentiation and further enhance the model's discriminative ability. Triplet Loss optimizes the distribution of sheep face features at the feature level, making feature vectors of the same sheep as close as possible in high-dimensional space, while keeping feature vectors of different sheep as far apart as possible, thus forming more discriminative feature embeddings. The cross-entropy loss function further strengthens the mapping relationship between features and categories, ensuring that the model has higher accuracy in the final classification decision. By jointly optimizing the two loss functions, the model is effectively prevented from getting trapped in local optima, improving its robustness to small samples and highly similar samples, and providing more stable and accurate recognition performance in complex environments.

[0061] Analysis of experimental results: (1) Model training and evaluation metrics: (1.1) Model Training The experimental environment and platform required for training the sheep face recognition network are the same as those described above. Input images are uniformly set to 160×160 pixels, with an iteration cycle of 100 rounds, a training batch size of 48, and an initial learning rate of 0.01. A stochastic gradient descent optimizer is selected to optimize model parameters, and a cosine annealing learning rate reduction method is used to adaptively adjust the learning rate during training.

[0062] (1.2) Evaluation Indicators To evaluate model performance, false acceptance rate (FAR), false rejection rate (FRR), and accuracy were selected as metrics for recognizing performance, while FLOPs, Params, and Weight were selected to evaluate model computational complexity.

[0063] FAR (Fault-Adjusted Ranking) is the proportion of different individuals that the model misidentifies as the same individual during the recognition process. A misidentification occurs when an image of one sheep is mistakenly identified as belonging to another sheep. The formula is as follows: FRR is the proportion of times the model incorrectly identifies the same individual as a different individual during the identification process. When the system cannot correctly identify a known sheep, a rejection error will occur. The formula is as follows: Accuracy represents the proportion of correctly identified samples out of the total number of samples during the recognition process. In sheep face recognition tasks, accuracy is the most direct indicator of the model's overall performance, reflecting the system's applicability in real-world scenarios. The formula is shown below: (2) Algorithm comparison test To verify the performance of the HTL-Net network, InceptionResNet, MobileNet, EfficientNet, and ShuffleNetV2 were selected for comparative experiments based on FAR, FRR, Accuracy, FLOPs, Params, and Weight. The experimental results are shown in Table 5 below.

[0064] Table 5 The loss curves for the training set (left) and validation set (right) during training are shown below: The loss curves in the figure show that HTL-Net exhibits faster loss reduction in the initial training stage compared to MobileNet, EfficientNet, and ShuffleNetV2 models, and its loss tends to stabilize in the later stages, demonstrating excellent convergence speed and stability. Although InceptionResNet shows a rapid decrease in loss value in the early stages of training, the model experiences fluctuations or slowdowns during training, indicating instability in the training process.

[0065] Comparing the results in Table 5, regarding model recognition accuracy, InceptionResNet and HTL-Net perform similarly in terms of FAR (Failure Rate), at 8.30% and 8.90% respectively. MobileNet and ShuffleNetV2 have higher false recognition rates, at 16.80% and 14.60% respectively, indicating that InceptionResNet and HTL-Net are better at avoiding incorrectly accepting sheep face images that do not belong to the target when distinguishing different individuals. HTL-Net has the lowest FRR (7.40%) among the five models, indicating that HTL-Net can accurately identify target individuals in complex backgrounds or under interference, reducing the rejection problem caused by environmental factors. HTL-Net has the best accuracy, reaching 91.90%, which is 1.25, 4.85, 3.45, and 4.20 percentage points higher than InceptionResNet, MobileNet, EfficientNet, and ShuffleNetV2, respectively. The computational complexity of the HTL-Net model is significantly lower than other models, with only 0.06G FLOPs, compared to InceptionResNet's 1.43G. HTL-Net significantly reduces computational overhead through its lightweight network structure. With 0.79M params, HTL-Net can rapidly improve processing efficiency in applications requiring fast deployment and real-time inference. Weight size directly impacts model storage requirements; HTL-Net's weight size is 3.37MB, a 96.23% reduction compared to InceptionResNet's 89.50MB, providing greater flexibility for model deployment and updates.

[0066] (3) Convolution comparison experiment In this embodiment, the performance of five convolution methods—Conv, GhostConv, RepConv, DWConv, and LightConv—is compared, and the results are shown in Table 6 below.

[0067] Table 6 In terms of recognition performance, LightConv performs best in FRR and Accuracy, at 7.40% and 91.90% respectively, with an FAR of 8.90%, higher than RepConv's 8.50%. Considering computational complexity, DWConv has the lowest FLOPs, Params, and Weight, but its accuracy is lower, indicating that its computational cost is small and its feature representation ability is insufficient when handling complex sheep face recognition tasks. In contrast, LightConv maintains low FLOPs, Params, and Weight while having high recognition accuracy; therefore, LightConv is selected as the convolution in this embodiment.

[0068] (4) Comparison of measurement functions To evaluate the performance of different metric functions in sheep face recognition, comparative experiments were conducted on AdaCos Loss (based on cosine margin), CosFace Loss, ArcFace Loss (based on angle margin), SphereFace Loss, and Triplet Loss (based on Euclidean distance). The experimental data are shown in Table 7. Table 7 The ROC curves for different metrics are shown in the figure below: First, 1242 sheep face images from 30 sheep were paired into two groups. Each time, 100 groups of images within each class and 100 groups of images between classes were selected, and this process was repeated 10 times without repetition, resulting in a total of 2000 samples. The optimal threshold and the FAR, FRR, and Accuracy at the optimal threshold were tested under different metric functions. Both AdaCos Loss and CosFace Loss use cosine similarity metrics. The optimal threshold range is -1 to 1, with a step size of 0.01; values ​​greater than the optimal threshold are considered the same individual. AdaCos employs an adaptive cosine margin strategy, while CosFace introduces angle loss and margin optimization strategies. Both performed poorly in terms of FAR and FRR. AdaCos achieved an FAR of 22.70%, an FRR of 14.60%, and an accuracy of only 81.35%, indicating weak recognition ability in the sheep face recognition task of this embodiment. Although CosFace showed a decrease in FAR and FRR, at 17.50% and 12.30% respectively, its accuracy was 85.10%, lower than other similar metrics.

[0069] ArcFace Loss and SphereFace Loss are both angular margin-based metrics. The optimal threshold range is 0 to π radians, with a step size of 0.01. Values ​​less than the optimal threshold are considered the same individual. By introducing an angle optimization strategy to enhance feature discriminability, the angle-based metric performs better than the cosine similarity metric in this embodiment's sheep face recognition task. ArcFace Loss achieves an accuracy of 89.75%, and SphereFace Loss achieves 87.90%, both outperforming the cosine similarity metric. ArcFace's FAR is 9.70% and FRR is 10.8%, while SphereFace's FAR is 13.10% and FRR is 11.10%. Although these two metrics improve accuracy, some false positives and false negatives still exist when sheep facial features have high similarity.

[0070] Triplet Loss is a metric function based on Euclidean distance. The optimal threshold range is 0 to 4, with a step size of 0.01. Values ​​less than the optimal threshold are considered the same individual. Metric learning optimizes intra-class and inter-class distances in the feature space, making features closer to those of the same type and more distant to those of different individuals. Triplet Loss performs best among all metric functions, with an FAR of 8.90%, FRR of 7.40%, and an accuracy of 91.90%, demonstrating excellent performance in improving recognition accuracy and reducing false positives and false negatives. Compared to other metric functions, Triplet Loss handles subtle differences in facial features among sheep better and exhibits strong robustness in large-scale sheep flock recognition tasks. Therefore, Triplet Loss was ultimately chosen as the metric function for the sheep face recognition model in this embodiment.

[0071] (5) Eigenvector comparison experiment Comparative experiments were conducted using feature vectors of lengths 128, 256, and 512, and the results are shown in Table 8. Table 8 The 128-length feature vector in the table shows the best performance, with an FAR of 8.90%, an FRR of 7.40%, and an accuracy of 91.90%. This indicates that the model can effectively distinguish different sheep identities in the feature space while maintaining a low false positive and false negative rate. Compared to feature vectors of other lengths, the 128-length feature vector, while ensuring high recognition accuracy, has stronger generalization ability and higher robustness.

[0072] For a feature vector of length 256, the FAR and FRR were 15.60% and 6.80%, respectively. Compared to the feature vector of length 128, the FRR decreased, indicating a reduction in the probability of missed detections. However, the FAR increased significantly, indicating that the model's ability to distinguish between different individuals weakened, leading to more false acceptances. The accuracy also decreased to 88.80%, indicating that while longer feature vectors provide more feature representation capabilities, they may also introduce more noise or overfitting, thus affecting the model's generalization ability.

[0073] For a feature vector of length 512, the model's FAR further increased to 16.00%, FRR reached 8.70%, and Accuracy dropped to 87.65%, resulting in the worst overall performance. This indicates that although the 512-length feature vector provides more feature dimensions, the model's ability to distinguish features in a high-dimensional feature space is not improved. Instead, it may lead to overfitting and other problems, resulting in a decrease in feature generalization ability. While excessively long feature vectors can express more refined data information, they may also cause unnecessary noise when processing diverse sheep facial features, thus reducing overall recognition accuracy. Therefore, this embodiment selects a feature vector of length 128 as the final feature representation length to ensure that the model achieves the best balance in terms of accuracy, efficiency, and robustness.

[0074] (6) Ablation experiment of sheep face recognition model To investigate the effectiveness of the improved measures in this embodiment, an ablation experiment was conducted, and the results are shown in Table 9. Table 9 Baseline represents a sheep face recognition model composed of HG-Block modules; ① represents an HG-Block module fused with TripletAttention; ② represents replacing ordinary convolution with LightConv. The improvement effect of each module on the model is comprehensively evaluated by analyzing the model's FAR, FRR, Accuracy, FLOPs, Params, and Weight. The results show that the Baseline model has an accuracy of 85.55%, an FAR of 19.20%, and an FRR of 9.70%, which can initially achieve the sheep face recognition task. However, the high false recognition rate indicates a weak ability to distinguish between different sheep, and there is still room for optimization in model complexity. After introducing TripletAttention to enhance feature representation, the FAR significantly decreased to 11.80%, and the accuracy increased to 88.90%, indicating that TripletAttention plays a positive role in capturing the discriminative features of sheep faces. The FRR increased to 10.40%, possibly because the model is more rigorous in distinguishing certain inter-class samples after the introduction of the attention mechanism. The model's FLOPs increased by only 0.01G, while the number of parameters remained essentially unchanged, indicating that Triplet Attention improved performance without significantly increasing computational overhead. Building on this, replacing ordinary convolutions with the lightweight LightConv reduced the model's FAR and FRR to 8.90% and 7.40%, respectively, while the accuracy improved to 91.90%, achieving optimal accuracy and robustness for the sheep face recognition model. Furthermore, FLOPs decreased to 0.06G, the number of parameters decreased to 0.79M, and the weight size decreased to 3.37MB, significantly reducing storage and computational costs and providing a foundation for the practical deployment of the sheep face recognition model.

[0075] S4. Construction of sheep face feature database: Associate the sheep face feature vectors output by the recognition network in step S3 with the sheep identity information and store them in the sheep face feature database. To achieve accurate and efficient sheep identification, a flock sheep identification system was designed, integrating two key modules: sheep face detection and sheep face recognition. A sheep face feature database was also established. The overall process is as follows: Figure 5 As shown, by detecting the sheep's face region and facial key points, the sheep face image is cropped, aligned, and its features are extracted. Finally, it is compared with information in the database to achieve a complete process from input image to sheep identification.

[0076] In the sheep face detection stage, the sheep face region in the input image is first located, accurately segmented from the complex background environment, and facial key points are extracted to provide geometric information for subsequent sheep face alignment. After obtaining the sheep face detection results, the sheep face image is cropped according to the sheep face bounding box coordinates, and based on the key point coordinate information, affine transformation is used to standardize the sheep face, eliminating pose deviations caused by factors such as shooting angle and lighting conditions. This ensures that sheep face images of the same sheep acquired under different conditions have similar feature representations, enhancing the model's generalization ability. The processed sheep face image is then input into the sheep face recognition network for feature extraction, obtaining a high-dimensional feature representation of the sheep face. A feature normalization method is used to map this representation to a 128-dimensional feature vector space, reducing recognition errors caused by differences in feature scale. Finally, the extracted 128-dimensional feature vector is stored in a database and associated with the corresponding sheep identity ID for subsequent identity matching. During the identification process, the Euclidean distance metric is used to determine whether a sheep's face belongs to an existing identity in the database. The similarity between the feature vector of the sheep's face to be identified and the feature vector stored in the database is calculated. Based on a set similarity threshold, if the similarity is greater than the threshold, it is determined to be the same sheep; otherwise, it is a different individual. This system can update the sheep face feature vectors in the database to adapt to changes in facial features during sheep growth. Furthermore, when adding new sheep, only their feature vectors need to be extracted and saved to the database for identification; no model retraining is required, enabling large-scale sheep identification.

[0077] S5. Intelligent Display Platform and Sheep Identity Recognition System: Based on PyQt5, a graphical user interface is developed, integrating the sheep face detection network completed in step S2 and the sheep face recognition network trained in step S3. It detects and extracts features from the sheep images to be identified, calculates the Euclidean distance between the extracted feature vector and the registered feature vectors in the database, and completes identity matching according to the minimum distance principle. It supports real-time detection and recognition of images, videos, and cameras, and has identity registration, database query, and management functions.

[0078] To achieve end-to-end sheep identification, a sheep face detection and recognition network is integrated to establish a sheep face feature database, realizing integrated functions for sheep identification registration and recognition. To meet the needs of automation and intelligence, this embodiment designs an intelligent display operating system based on PyQT5 to display sheep identification results in real time and integrates data management functions, supporting functions such as sheep identification information input, query, database update, and deletion, thereby improving user operation efficiency and management convenience.

[0079] The sheep identification system designed in this embodiment needs to have an intuitive and simple graphical user interface (GUI) to provide users with a way to interact with the system, simplifying complex operations and meeting users' actual needs. The GUI design follows the principle of user-friendliness, ensuring that users without professional knowledge can operate the system easily and intuitively. At the same time, it maintains a simple and clear interface layout, highlighting the main operation flow, allowing users to operate flexibly and easily. The interface mainly includes function buttons and an information display area, allowing users to perform operations such as data uploading, identification, querying, and management through the buttons.

[0080] (1) Data upload and display: Users can select local images or videos or call the camera to read the individuals to be identified through the buttons on the main interface. If the reading is successful, the images or videos will be displayed in the main interface window. If the opening or calling fails, a pop-up warning will be displayed.

[0081] (2) Sheep face detection and recognition: The system should integrate the pre-created sheep identification network into the GUI software. After reading the individuals to be identified, it can complete a series of operations such as sheep face detection, alignment, feature extraction and identity matching, and display the recognition results in real time, including sheep face detection boxes and their confidence scores, three facial key points, sheep IDs, the number of individuals detected and the Euclidean distance between feature vectors.

[0082] (3) Sheep face identity registration and update: When adding a new sheep, the user can choose to open its sheep face image and enter the identity ID information, extract the feature vector and save it to the database. For sheep that already exist in the database, the user can re-identify them and add new feature vectors.

[0083] (4) Database query and management: Users can use the database query function to display the identity information of existing sheep on the system interface, including sheep image name, identity ID, registration time and existing number of sheep, and allow users to add or delete sheep.

[0084] (5) System Deployment: To meet the deployment requirements of the system in different environments, the entire Python program is packaged into an executable file "*.exe" for the Windows platform using tools such as PyInstaller. This allows users to run the program directly without configuring a Python environment, supports one-click startup, simplifies the user operation process, and is compatible with different versions of the Windows operating system, providing convenience for large-scale deployment in practical applications. Simultaneously, this facilitates the development and deployment of the Jetson Orin Nano Super device to meet the needs of different users.

[0085] This system's development environment is based on the Windows 10 operating system. It utilizes the Anaconda virtual environment to manage the various libraries and frameworks the project depends on, avoiding version conflicts between different projects. PyTorch 2.1.0 is used as the deep learning framework for building and training sheep face detection and recognition networks. CUDA 12.1 is combined to achieve GPU-accelerated computation, improving model training and inference speed. Python 3.8 is chosen as the development language, and PyCharm is used as the development tool for code writing and debugging.

[0086] This system's graphical user interface (GUI) is developed using PyQt5, and designed using Qt Designer, Pyuic, and Pyrcc to ensure flexible layout management, efficient function integration, and a good interactive experience. PyQt5 is a Qt application development framework for Python, providing rich GUI components, including window controls, buttons, text boxes, tables, menu bars, etc., and supports event-driven mechanisms for efficient handling of user interactions. To improve development efficiency, Qt Designer is used as the interface design tool. Qt Designer is an official visual interface design tool provided by Qt, supporting quick layout of window controls via drag-and-drop, eliminating the need to manually write tedious interface code and allowing direct visual design within Qt Designer. Pyuic is used to convert the "*.ui" files generated by Qt Designer into Python code, facilitating integration into projects and seamlessly integrating the front-end interface with back-end logic. Pyrcc is used to convert Qt's "*.qrc" resource files, such as images and icons, into Python files, allowing these resources to be used directly in the program, enhancing the interface's aesthetics and customizability.

[0087] To verify the intelligent sheep identification system, 50 sheep were selected, with 6 images of each sheep, totaling 300 images for testing. One image from each sheep was chosen (50 images in total), and feature vectors were extracted and entered into the database, with the identification names sequentially named "ID001, ID002, ID003...ID050". The remaining 250 images were input into the system for experimental verification. The overall recognition accuracy was 90.80%, with 4.37 iterations per second and an average detection time of 228ms per image, which meets the requirements for sheep identification.

[0088] After the identity recognition system was developed, to facilitate user operation, this embodiment uses PyInstaller to package the Python code, converting it into a "*.exe" executable file for the Windows platform, enabling efficient system deployment and development. Simultaneously, by combining virtual environment management, resource file packaging, and environment variable configuration, software compatibility is improved, ensuring stable system operation in different environments. Users can run the system directly by double-clicking the program on different Windows computers without installing Python and related dependencies. During the packaging process into "*.exe" files, the command-line window was retained to monitor the software's running status in real time, facilitating the identification and resolution of potential problems, and providing strong data support for subsequent software maintenance and updates. Adding a sheep icon to the software conforms to standard software development design specifications, increasing software recognizability and enhancing user experience and interface aesthetics.

[0089] To meet the needs of different users, this embodiment deploys the sheep identification system in an embedded development environment, using the Jetson Orin Nano Super as the computing platform. The Jetson Orin Nano Super is a high-performance, low-power embedded AI computing device from NVIDIA. It features an Ampere architecture GPU with 1024 CUDA cores and 32 Tensor cores, and a 6-core Arm Cortex-A78AE CPU, providing 67 TOPS of AI computing power. It also supports deep learning acceleration libraries such as CUDA and cuDNN, ensuring high computing performance while maintaining low power consumption. This allows for efficient execution of neural network inference tasks on edge devices, making it suitable for resource-constrained applications.

[0090] In the model deployment process, this embodiment selects Ubuntu 20.04.6 LTS, a Linux-based operating system, as the development operating system for the JetsonOrin Nano Super. The corresponding JetPack SDK package is installed according to the L4T version, providing a complete software development kit for environment setup. This includes AI computing libraries such as CUDA, cuDNN, TensorRT, DeepStream, and OpenCV, supporting GPU-accelerated computing, multimedia processing, graphics rendering, and computer vision, and providing optimized support for hardware resources. The corresponding versions of CUDA 11.4 and cuDNN 8.6 are selected to configure the environment, ensuring the device can normally utilize GPU inference computation. Finally, Anaconda is installed to set up the deep learning environment. To ensure the model runs correctly, Python 3.8 and PyTorch 2.1.0 are selected as the deep learning framework, ensuring consistency with the Windows platform environment.

[0091] Since the Jetson Orin Nano Super is based on the ARM architecture, while PyQt5 relies on the Qt framework and SIP tools, the pre-compiled binaries are usually designed for the x86 architecture. This architecture mismatch prevents dependencies from being installed directly. Therefore, this embodiment manually compiles PyQt5 and its dependencies to ensure that the visual sheep identification GUI interface developed above can function correctly on the Jetson device.

[0092] After the model was deployed, 250 sheep images were detected and identified to verify its performance on the Jetson Orin Nano Super device. Experimental results showed that the system iterated at 3.62 it / s, with a detection time of 69s and an average detection time of 276ms per image.

[0093] It should be noted that although the present invention has been described through the above embodiments, the present invention may have many other embodiments. Without departing from the spirit and scope of the present invention, those skilled in the art can obviously make various corresponding changes and modifications to the present invention, but all such changes and modifications should fall within the scope of protection of the appended claims and their equivalents.

Claims

1. A method for identifying sheep in a flock based on facial features, characterized in that, Includes the following steps: S1. Construction of the original dataset: Obtain the sheep video data to be identified, and extract the original image sequence from the sheep video data by frame extraction; S2, Sheep face detection: Input the dataset constructed in step S1 into the RetinaFace sheep face detection network, and output the sheep face region localization information and facial key point coordinates; S3, Sheep face recognition: Input the sheep face region detected in step S2 into the HTL-Net sheep face recognition network for training. After training, extract features from the sheep face region to be recognized and output the sheep face feature vector. S4. Construction of sheep face feature database: Associate the sheep face feature vectors output by the recognition network in step S3 with the sheep identity information and store them in the sheep face feature database. S5. Sheep Identity Recognition and System Implementation: A graphical user interface is developed based on PyQt5, integrating the sheep face detection network completed in step S2 and the sheep face recognition network trained in step S3. The system detects and extracts features from the sheep images to be identified, calculates the Euclidean distance between the extracted feature vectors and the registered feature vectors in the database, and completes identity matching based on the minimum distance principle. It supports real-time detection and recognition of images, videos, and cameras, and has identity registration, database query, and management functions.

2. The method for identifying sheep based on facial features according to claim 1, characterized in that, The detailed steps of step S1 are as follows: S101. Extract keyframes from the acquired sheep herd video to obtain the original image. Use the differential hashing algorithm to perform similarity screening on the original image sequence, calculate the hash value of adjacent frame images and compare the Hamming distance, and remove redundant images with high similarity. The processing steps of the differential hashing algorithm include: first, scaling the image to a fixed size, calculating the difference value of adjacent pixels to generate a hash value; then calculating the Hamming distance between the hash values ​​of adjacent frame images, and when the Hamming distance is less than a preset threshold, determining that it is a similar image and removing it; S102. Data annotation is performed on the images filtered in step S101, annotating the sheep face region and three facial key points: eyes and nose. Data augmentation processing is then performed to form the original dataset. The data augmentation includes random blurring, random noise, changing brightness, random scaling, random occlusion, and random rotation.

3. The method for identifying sheep based on facial features according to claim 1, characterized in that, The detailed steps of step S2 are as follows: S201. The RetinaFace sheep face detection network uses GhostNet as a lightweight backbone network to extract features from the original dataset obtained in step S1. It generates basic features through fewer convolution operations, and then generates more features through linear transformation and outputs the basic feature map. S202. The basic feature map output in step S201 is enhanced by the SRM feature extraction module. The SRM module integrates the RepNCSPELAN4 module and introduces the multi-scale hole attention mechanism MSDA in the residual connection to perform fine-grained feature extraction on the basic feature map and output the enhanced feature map. S203. Construct the A_EFPN feature fusion network and input the enhanced feature map output in step S202 into it to perform multi-scale feature fusion and output multi-scale fused features. The A_EFPN network combines EfficientRepGFPN and variable kernel convolution AKConv to achieve the fusion of features at different scales.

4. The method for identifying sheep based on facial features according to claim 3, characterized in that, In step S201, the GhostNet backbone network is composed of a series of GhostBottleneck (G-bneck) stacks. The image size of the input raw data is set to 640×640×3. Feature maps of sizes 80×80×64, 40×40×128 and 20×20×256 are taken as the output part of the backbone network. The GhostBottleneck is mainly composed of Ghost modules, which include two types: stride=1 and stride=2. In step S202, the Multi-Scale Dilated Attention (MSDA) mechanism captures multi-scale contextual information through dilated convolutions with different dilation rates, and enhances feature representation capabilities by combining ChannelShuffle operations, reducing the impact of ambient light changes on sheep face feature extraction. Specifically, MSDA obtains the corresponding Q (queries), K (keys), and V (values) of the input feature map through linear projection and divides the feature map channels into four different heads. Each head is set with a different dilation rate r, and each head processes different feature subsets in parallel to avoid ignoring key sheep face features. The dilation rates r are 1, 2, 3, and 4, respectively. In step S203, the variable kernel convolution AKConv breaks through the fixed local window limitation, allowing the convolution kernel to have any number of parameters and sampling shape. Its convolution operation is implemented in the following way: "First, the initial sampling shape of the convolution kernel is dynamically adjusted according to the size of the input feature map and the target receptive field; then, the sampling position is corrected by the learned offset; finally, the feature map is resampled through an adaptive sampling strategy to improve the detection capability of sheep faces at different scales.

5. The method for identifying sheep based on facial features according to claim 3, characterized in that, In step S204, the calculation formula for the multi-task joint loss function is as follows: In the formula, in the formula For face classification loss function, For predicting labels, For real labels, This is the facial bounding box loss function, used to predict the bounding box. With the true bounding box Interval error, Lpts( ) is the facial landmark regression loss function used for sheep face feature localization. SmoothL1Loss is used to measure the predicted landmarks. With real key points The difference is that λ1 and λ2 are the weights of the bounding box loss and keypoint loss functions, respectively, and the values ​​of λ1 and λ2 are both 0.

5.

6. The method for identifying sheep based on facial features according to claim 1, characterized in that, The detailed steps of step S3 are as follows: S301. The HTL-Net network uses HG-Block as the basic unit for feature extraction, extracts features from the sheep face region output in step S2, and gradually aggregates local and global information in a hierarchical manner to output hierarchical features. S302. Input the hierarchical features output in step S301 into the TripletAttention mechanism, and use a three-branch structure to capture cross-dimensional interaction features between height, width and channels, and output the attention-enhanced features. S303. The attention-enhanced features obtained in step S302 are sequentially aggregated through global average pooling, Dropout regularization, fully connected layer and batch normalization to obtain a 128-dimensional feature vector. The feature vector is then normalized by L2 normalization to output a normalized sheep face feature vector. S304. Using the TripletLoss loss function based on Euclidean distance combined with the cross-entropy loss function, the normalized sheep face feature vector output in step S303 is used as the training target to optimize the intra-class and inter-class distances in the feature space, thus completing the training of the sheep face recognition network.

7. A method for identifying sheep based on facial features according to claim 6, characterized in that, In step S301, the specific steps of the HG-Block hierarchical feature aggregation unit in processing the feature map are as follows: First, the input feature map is divided into multiple sub-feature groups according to the channel dimension. Second, convolution operations are performed on each sub-feature group and aggregated step by step. Finally, local and global information at different levels are fused through hierarchical connection. The HG-Block unit uses LightConv lightweight convolution to replace ordinary convolution for feature extraction.

8. A method for identifying sheep based on facial features according to claim 6, characterized in that, In step S302, the TripletAttention mechanism captures cross-dimensional interactive features of the input feature map in the three dimensions of height-channel, width-channel, and height-width through a three-branch structure, and then averages and aggregates the outputs of the three branches. The specific steps of the average aggregation are as follows: first, a specific dimension is compressed through Z-pooling; second, attention weights are generated through convolution and activation functions; and finally, the attention weights of the three branches are aggregated and applied to the original feature map.

9. A method for identifying sheep based on facial features according to claim 6, characterized in that, In step S304, the formula for calculating the TripletLoss triplet loss function is as follows: Where, in the formula For anchor point samples, As a positive sample, For negative samples, α is a preset interval hyperparameter; the triplet loss function minimizes the feature vectors of the same sheep to make them closer together in the feature space, and the feature vectors of different sheep to make them farther apart, thus forming a more discriminative feature embedding.

10. The method for identifying sheep based on facial features according to claim 1, characterized in that, In step S5, the identity matching process includes: during the recognition process, the Euclidean distance metric method is used to determine whether the sheep face belongs to an identity that already exists in the database. The similarity between the feature vector of the sheep face to be identified and the feature vector stored in the database is calculated. According to the set similarity threshold, if it is less than the threshold, it is determined to be the same sheep; otherwise, it is a different individual.