Cow behavior recognition method based on improved YOLOv8n algorithm

By improving the YOLOv8n algorithm and combining it with the CVMamba module, multi-head bidirectional recursive attention mechanism, and multi-scale pooling kernel, a cosine similarity loss function was designed. This solved the problems of detection accuracy and robustness in complex environments for dairy cow behavior recognition, achieving efficient multi-dairy cow behavior recognition and improving agricultural production efficiency.

CN122176378APending Publication Date: 2026-06-09TIANJIN UNIV OF SCI & TECH

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
TIANJIN UNIV OF SCI & TECH
Filing Date
2026-03-05
Publication Date
2026-06-09

AI Technical Summary

Technical Problem

Existing dairy cow behavior recognition technologies lack sufficient accuracy and robustness in complex environments, making it difficult to simultaneously recognize the behavior of multiple dairy cows. Furthermore, traditional methods are time-consuming, labor-intensive, or prone to equipment interference with the cows.

Method used

An improved YOLOv8n algorithm is adopted, which combines the CVMamba module, multi-head bidirectional recursive attention mechanism, multi-scale pooling kernel and cosine similarity-based loss function to optimize the model and improve recognition accuracy and robustness.

Benefits of technology

It significantly improves the model's behavior recognition accuracy and generalization ability in complex environments, enabling efficient identification of different behaviors of multiple dairy cows, reducing equipment interference, and improving agricultural production efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122176378A_ABST
    Figure CN122176378A_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of computer vision and dairy cattle breeding, and in particular, a dairy cow behavior recognition method based on an improved YOLOv8n algorithm is provided, and specifically, the present application effectively solves the problem of insufficient long-distance dependency modeling by constructing a C2f-Vision Mamba module; a multi-head bidirectional recursive attention mechanism is designed, which greatly improves the expression ability and classification precision of fine-grained features; the perception ability of local and global features is enhanced by introducing multi-scale pooling kernels and average pooling through improving the SPPF module; the innovative design of the multi-scale hybrid module enables the model to maintain feature consistency and integrity in complex environments; the CoIoU loss function combining IoU, center point distance and shape consistency penalty is used to further optimize the prediction accuracy of the bounding box. The model of the present application is significantly better than the original YOLOv8 and other comparative models in key indicators, and realizes accurate recognition of dairy cow behavior, providing reliable technical support for intelligent breeding.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of computer vision technology and dairy farming technology, and more specifically, to a method for recognizing dairy cow behavior based on an improved YOLOv8n algorithm. Background Technology

[0002] In the era of modern smart agriculture, studying dairy cow behavior is crucial for understanding how cows adapt to their environment and managing their health and welfare. Routine behaviors such as feeding, standing, and lying down typically indicate a comfortable physiological state; while other behaviors, such as licking back, resisting, and mounting, may reflect health problems or poor environmental adaptation. Efficient and accurate identification of dairy cow behavior allows for the timely detection of abnormal individuals and appropriate human intervention, thereby reducing potential economic losses and improving agricultural productivity.

[0003] Traditional behavioral monitoring methods include manual observation and contact sensors. While these methods can obtain information about cow behavior to some extent, they have significant limitations. Manual observation is time-consuming and labor-intensive, and is often limited to specific time periods, making it impossible to achieve continuous, real-time behavioral monitoring around the clock. Although contact sensors can record movement data, wearing the devices may cause discomfort or interference to the cows. In addition to the vulnerability of the sensors to damage, the data may also be affected by external environmental noise, all of which limit their widespread adoption in practical applications.

[0004] In recent years, with the rapid development of computer vision and deep learning technologies, non-contact image recognition technology has been increasingly widely used in monitoring dairy cow behavior. This technology analyzes video images to identify the behavioral states of dairy cows in real time and automatically without physical contact. In particular, object detection algorithms such as YOLO have shown significant advantages in animal behavior classification due to their fast detection speed and high accuracy. Wang et al. increased the receptive field of the network by incorporating a multi-scale attention module into the YOLOv5 model to collect more comprehensive feature information, thereby achieving accurate detection of individual cattle. Saifudin A et al. proposed a dairy cow behavior recognition method based on the YOLOv8 algorithm, studying the grazing, standing, lying down, and rumination behaviors of dairy cows. They also proposed a solution for common problems in complex environments, providing technical support for intelligent agricultural management. Jeong K et al., combining daytime and nighttime dairy cow activity datasets, proposed an IoT system based on the YOLOv8 model for 24-hour real-time monitoring of cattle mounting behavior. Wang et al. proposed an improved Estrus-YOLO model based on YOLOv8n by replacing the loss function, introducing a contextual information enhancement module and a ternary attention module. They also innovatively labeled individual cows in estrus, thereby achieving accurate identification of cows in estrus.

[0005] Although YOLO-based algorithms have been applied to some extent in dairy cow behavior recognition, most research focuses on recognizing single behaviors and analyzing individual cow behaviors. However, the actual dairy farming environment is complex, often involving multiple cows in the same image with varying behaviors. Therefore, dairy cow behavior recognition still faces many challenges, requiring further model optimization to improve detection accuracy and robustness. Summary of the Invention

[0006] In view of this, the present invention proposes a method for recognizing cow behavior based on an improved YOLOv8n algorithm to solve the problems existing in the prior art.

[0007] To achieve the above objectives, this invention proposes a method for recognizing cow behavior based on an improved YOLOv8n algorithm, comprising the following steps: Collect video data of dairy cow behavior and construct a dairy cow behavior dataset; The dairy cow behavior dataset is preprocessed; An improved YOLOv8n model was constructed, and the improved YOLOv8n model was trained using a preprocessed dairy cow behavior dataset to obtain a dairy cow behavior recognition model. The trained dairy cow behavior recognition model is used to identify dairy cow behavior; The improved YOLOv8n model specifically includes: introducing the CVMamba module and combining it with the original YOLOv8 C2f module to form the C2f-VisionMamba module, which replaces the C2f module in the backbone network; Design a multi-head bidirectional recursive attention mechanism and integrate it with the channel attention mechanism in the convolutional block attention module to construct a multi-head convolutional block attention module. Multi-scale pooling kernels and average pooling are introduced to improve the SPPF module in the backbone network, forming the MSPPF_A module; A multi-scale hybrid module is introduced and added to the last layer of the backbone network; We designed a loss function CoIoU based on cosine similarity to optimize the bounding box fitting ability of the original YOLOv8 model.

[0008] Furthermore, the data acquisition device for the cow behavior video is installed in the feeding area and the resting fence area at a fixed height of 4.5 meters. The acquisition resolution is 1280×720 pixels, the frame rate is 30 frames / second, and the video acquisition period is from 08:00 to 18:00 every day, with a frame acquisition interval of 3 seconds.

[0009] Furthermore, the preprocessing includes denoising, filtering, labeling, and data augmentation; the labeled behavior categories include six types: feeding, standing, lying down, licking back, mounting, and resistance; the labeled information is saved in txt format, and the training set and validation set are divided in an 8:2 ratio; the data augmentation operations include translation, mirroring, random cropping, random scaling, and random rotation.

[0010] Furthermore, the C2f-VisionMamba module introduces a Visual State Space (VSS) module. The VSS module employs a multi-directional selective scanning mechanism, performing forward and reverse scans in the horizontal and vertical directions respectively to capture contextual information from different directions, as shown in the following equation: in, Indicates contextual information, Scan horizontal and Scan vertical These represent selective scanning operations in the horizontal and vertical directions, respectively.

[0011] Furthermore, the multi-head bidirectional recursive attention mechanism includes: dividing the query, key, and value into multiple heads, performing attention calculation independently on each head, concatenating the weighted output of the multi-head attention mechanism for each head with its corresponding value, and then restoring the concatenated result to the original embedding dimension dmodel through a linear transformation to generate the final output.

[0012] Furthermore, the MSPPF_A module introduces pooling kernels of different sizes. Specifically, it uses small pooling kernels to capture local details of the cow's body, large pooling kernels to obtain the overall shape and posture changes of the cow, and average pooling layers to further supplement feature representation, thus fusing feature information at different scales.

[0013] Furthermore, the multi-scale hybrid module is based on a multi-scale pooling and convolution kernel combination strategy, employing 1×1, 3×3, and 5×5 multi-scale convolution kernels and depthwise separable convolution to capture subtle changes in target features and contextual information from local to global perspectives. It further coordinates feature representations at different scales through an adaptive feature integration mechanism. In addition, the multi-scale hybrid module introduces a multi-dimensional fusion strategy to enable interaction between feature maps at different scales, reducing information loss.

[0014] Furthermore, the CoIoU loss function combines IoU, center point distance penalty term, and shape consistency penalty term based on cosine similarity to improve bounding box prediction accuracy; The IoU is used to calculate the overlap between the predicted bounding box and the ground truth bounding box. The larger the IoU, the higher the matching degree between the two boxes and the smaller the loss. The center point distance penalty term is used to measure the distance between the center points of the predicted bounding box and the ground truth bounding box; the greater the distance, the greater the penalty. The shape consistency penalty term based on cosine similarity calculates the aspect ratio difference between the predicted and ground truth bounding boxes and smooths it using a cosine function to prevent excessive fluctuations when the aspect ratio difference is large. Simultaneously, it uses weight parameters... α The impact of controlling shape consistency penalty.

[0015] Furthermore, the CoIoU loss function is expressed as follows:

[0016] in, Let CoIoU be the loss function. Center point distance penalty item, c The diagonal length of the smallest bounding box that encloses the predicted box and the ground truth box. This represents a shape consistency penalty term based on cosine similarity.

[0017] Compared with the prior art, the beneficial effects of the present invention are as follows: This invention constructs a high-quality dairy cow behavior dataset by scientifically deploying data acquisition equipment and combining optimized acquisition parameters. On this basis, redundant images are removed through noise reduction processing to ensure data diversity. The CVAT tool is used to accurately label six types of behaviors: feeding, standing, lying down, licking, mounting, and resistance. Multi-dimensional data augmentation operations such as translation, mirroring, and random cropping are introduced, which not only effectively expands the number of samples and accelerates model convergence, but also significantly improves the model's generalization ability and robustness in complex environments, laying a solid data foundation for subsequent high-precision behavior recognition.

[0018] This invention optimizes and improves the YOLOv8n model, mainly by: introducing a C2f-Vision Mamba module that combines the CVMamba module with the original YOLOv8 C2f module to form a C2f-Vision Mamba module, replacing the C2f module in the backbone network to solve the problem of insufficient long-distance dependency modeling in cow behavior classification; designing a multi-head bidirectional recursive attention mechanism and integrating it with the channel attention mechanism in the convolutional block attention module to construct a novel multi-head convolutional block attention module, significantly improving the model's feature representation ability and classification accuracy; introducing pooling kernels of different sizes and average pooling to improve the SPPF module in the backbone network to capture local and global features of cow behavior; introducing a new multi-scale hybrid module and adding it to the last layer of the model's backbone network, enabling the model to effectively extract local details and overall behavioral features of cows in complex environments; and designing a cosine similarity-based loss function by combining IoU, center point distance, and shape consistency penalty to further improve the accuracy of cow behavior recognition. Attached Figure Description

[0019] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. In the drawings: Figure 1 This is a sample diagram of dairy cow behavior according to the present invention.

[0020] Figure 2 A flowchart for constructing the dataset of this invention.

[0021] Figure 3 This is a distribution diagram of the frequency and bounding box of dairy cow behavior categories according to the present invention.

[0022] Figure 4 This is a structural diagram of the CVMamba module of the present invention.

[0023] Figure 5 This is a structural diagram of the VSS module of the present invention.

[0024] Figure 6 This is a schematic diagram of the multi-directional selective scanning of the present invention.

[0025] Figure 7 This is a structural diagram of the MCB module of the present invention.

[0026] Figure 8 This is a structural diagram of the MHBRA module of the present invention.

[0027] Figure 9 This is a structural diagram of the MSPPF_A module of the present invention.

[0028] Figure 10 This is a structural diagram of the MSH module of the present invention.

[0029] Figure 11 This is the CVMamba-MYOLOv8 network structure of the present invention.

[0030] Figure 12 This is a comparative analysis chart of the ablation experiment results of the present invention.

[0031] Figure 13 This is a comparison chart showing the accuracy of different models for recognizing cow behavior in this invention.

[0032] Figure 14 This is a comparison chart of the overall performance of the dairy cow behavior recognition model of the present invention.

[0033] Figure 15 This is a performance comparison of the key indicators of the present invention as a function of Epoch.

[0034] Figure 16 This is a visual comparison chart of the present invention.

[0035] Figure 17 This is for the model visualization evaluation of the present invention. Detailed Implementation

[0036] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the disclosure to those skilled in the art. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0037] This embodiment proposes a method for recognizing cow behavior based on an improved YOLOv8n algorithm, including the following steps: The CVMamba-MYOLOv8 dairy cow behavior recognition model, which is an improvement on YOLOv8n, is characterized by the following steps: Step 1: Two data acquisition devices were installed at the ranch of an animal breeding group in Tianjin to collect dairy cow behavior data. To ensure the integrity of the dataset, the devices were fixed at a height of approximately 4.5 meters above the ground. To ensure a sufficient number of cows were captured, the devices were also installed in the feeding area and resting fence area to capture the complete movement trajectories of the cows. The dataset collection period was from April 19th to August 25th, 2024. To obtain clear images of dairy cow behavior, the video resolution was set to 1280×720 pixels, the frame rate to 30 frames per second, and the video collection period was from 08:00 to 18:00 daily. Considering that dairy cows move slowly and with small amplitude when there is no external interference, a frame acquisition interval that is too short would lead to insufficient differences between images. Therefore, the frame acquisition interval was set to 3 seconds to improve the difference in dairy cow behavior between images.

[0038] Step 2: The image data collected in Step 1 was first denoised to remove images with high similarity, ensuring dataset diversity. Then, CVAT annotation was used to annotate the images, categorizing behaviors into six types: grazing, standing, lying down, licking, mounting, and resisting. The annotation information was saved in a txt file and divided into training and validation sets in an 8:2 ratio. Simultaneously, considering the complexity of the cattle farm environment, excessive occlusion, and uneven lighting, to enhance data diversity, a portion of images from the training set were randomly selected for data augmentation operations such as translation, mirroring, random cropping, random scaling, and random rotation. Training the model using the data-augmented dataset indirectly increases the number of samples, accelerates model convergence, and improves its generalization ability.

[0039] Step 3: The improved YOLOv8n model mainly includes: introducing the CVMamba module and combining it with the original YOLOv8 C2f module to form the C2f-VisionMamba module, replacing the C2f module in the backbone network to solve the problem of insufficient long-distance dependency modeling in cow behavior classification; designing a multi-head bidirectional recursive attention mechanism and integrating it with the channel attention mechanism in the convolutional block attention module to construct a brand-new multi-head convolutional block attention module, which significantly improves the model's feature representation ability and classification accuracy; introducing pooling kernels of different sizes and average pooling to improve the SPPF module in the backbone network to capture local and global features of cow behavior; introducing a new multi-scale hybrid module and adding it to the last layer of the model's backbone network, enabling the model to effectively extract local details and overall behavioral features of cows in complex environments; and designing a loss function based on cosine similarity by combining IoU, center point distance, and shape consistency penalty to further improve the accuracy of cow behavior recognition.

[0040] In step 3 above, the CVMamba module and the original YOLOv8 C2f module are combined to form the C2f-VisionMamba module, which replaces the C2f module in the backbone network to solve the problem of insufficient long-distance dependency modeling in cow behavior classification.

[0041] Building upon YOLOv8's C2f module, a parallel VisionStateSpace (VSS) module is added to capture global contextual information of the input features. The module's workflow is as follows: First, the input features are processed through an initial convolutional layer to generate a basic feature map. Then, the map is divided into several branches, each processed by an independent VSS module. The VSS module captures global information through a multi-directional selective scanning mechanism and concatenates the features from each branch into a single overall feature map at the output stage. This map is then passed through a convolutional layer to generate the module's final output.

[0042] Mamba is a lightweight sequence modeling module based on a state-space model, originally designed for handling one-dimensional time series tasks. Mamba's core advantage lies in capturing long-range dependencies in a sequence through explicit state update rules, while maintaining linear complexity O(n). Compared to traditional attention mechanisms, such as Transformer, Mamba is more efficient and particularly suitable for long sequences and high-resolution tasks. In visual tasks, the Mamba module flattens the two-dimensional feature map in one dimension and processes the features using state update rules, as shown in equation (1): (1) in, hi This is a hidden state used to store historical information about the sequence; A and B These are the state transition matrix and the input projection matrix, respectively, used to capture the dynamic relationships between inputs; C and D These are the output projection matrix and the direct input mapping matrix, used to generate the final output features. In applications of two-dimensional image features, Mamba typically unfolds the two-dimensional feature map into a one-dimensional sequence for processing. However, in vision tasks, directly flattening the feature map may lead to the loss of local spatial structure information. Therefore, to adapt to two-dimensional image feature processing, the Visual State Space (VSS) module is introduced, which extends and optimizes the Mamba module.

[0043] Based on the Mamba module design, the VSS module effectively solves the balance problem between long-distance dependence and local feature extraction in visual tasks through multi-directional selective scanning and two-dimensional state modeling. Specifically, the 2D selective scanning submodule extends Mamba's state space modeling to two-dimensional image features, enabling simultaneous capture of both local and global features. Its state update rule formulas are shown in equations (2) and (3): (2) (3) in, (i,j) Represents pixel coordinates on a two-dimensional feature map. A, B, C and D These are the state transition matrix and projection matrix. Unlike the Mamba module, the SS2D submodule incorporates deep convolution in its state update to capture local features, while simultaneously combining global state updates to enhance its ability to model contextual information. This allows it to effectively analyze the dynamic changes of the cow's entire body and identify and associate motion features across multiple body regions, while capturing local details and incorporating global contextual information.

[0044] To further enhance the ability to capture global information, the VSS module employs a multi-directional selective scanning mechanism. Specifically, the VSS module performs forward and reverse scans in the horizontal and vertical directions, respectively, to capture context information from different directions, as shown in equation (4): (4) in, Scan horizontal and Scan verticalThese represent selective scanning operations in the horizontal and vertical directions, respectively. This mechanism allows the VSS module to simultaneously monitor both the horizontal and vertical changes in the overall posture of the cow. By integrating these contextual features from different directions, an enhanced global feature map is ultimately generated, thereby improving the model's performance in complex behavior classification tasks.

[0045] In step 3 above, a multi-head bidirectional recursive attention mechanism (MHBRA) was designed and integrated with the channel attention mechanism in the convolutional block attention module to construct a novel multi-head convolutional block attention module (MCB). This enhances the accuracy of feature selection and the ability to capture contextual dependencies. The MHBRA module processes global and local features simultaneously through the multi-head attention mechanism, with each head independently capturing different feature dependencies. This improves the model's ability to recognize fine-grained features in complex scenes, enhances the aggregation of global and local information, and improves recognition accuracy and robustness.

[0046] Specifically, the MHBRA module first transforms the input features through a linear transformation. Figure X Split into queries (Query, Q) ,key (Key,K) ,value (Value, V) ,at the same time Q, K, V It will be divided into multiple heads so that attention calculations can be performed in different subspaces. This allows the model to capture different features in parallel. The specific formulas are shown in equations (5), (6), and (7): (5) (6) (7) in, W q 、W k and W v It is a learnable weight matrix. Subsequently, the generated... Q, K, V The matrix is ​​reshaped to fit the computational requirements of the multi-head attention mechanism, and an independent feature subspace is assigned to each attention head. Specific formulas are shown in equations (8) and (9): (8) (9) Where h is the number of heads. reshape The shape after (N,T,d model ) Become (N,h,T,d k )This means that each head independently processes its own query, key, and value. Secondly, in the improved multi-head attention mechanism, since each head can independently process different feature subspaces, Top-K filtering is no longer used. Instead, the query, key, and value are divided into multiple heads, and each head performs attention computation independently. This improvement allows the model to process different features in parallel, thereby capturing richer contextual information. In the multi-head mechanism, each head independently computes its own... Q, K, V And attention score. The specific formula is shown in equation (10): (10) in, Q i and K i S and are the query matrix and key matrix of the i-th head; is the scaling factor; Softmax is used to calculate the dot product. Q i and K i The similarity score describes the relevance between the two. Then, the weighted output of the multi-head attention mechanism for each head is used. attn i Its corresponding V i After weighted summation, the results are concatenated, and then a linear transformation is applied to restore the concatenated result to the original embedding dimension. d model The final output is generated. The specific formula is shown in equation (11): (11) In step 3 above, a multi-scale pyramid pooling module incorporating average pooling is proposed to enhance the model's ability to perceive targets at different scales and improve the overall robustness of detection. The original SPPF module only uses pooling kernels of a fixed size for feature extraction, limiting its sensitivity to targets at different scales. In contrast, the MSPPF_A module, by introducing pooling kernels of different sizes and combining max pooling and average pooling operations, can capture local and global features more comprehensively. Specifically, smaller pooling kernels help capture local details of the cow's body, such as ears and tail, while larger pooling kernels are more adaptable to changes in overall shape and posture. The introduction of the average pooling layer further supplements the feature representation, allowing for better fusion of feature information at different scales.

[0047] In step 3 above, a multi-scale hybrid module (MSH) was designed to enhance the model's feature capture efficiency and robustness in complex scenes by optimizing multi-scale feature fusion and representation capabilities. The MSH module is designed based on a multi-scale pooling and convolution kernel combination strategy, employing 1×1, 3×3, and 5×5 multi-scale convolution kernels and depthwise separable convolutions to accurately capture subtle changes and contextual information of target features from local to global perspectives. The module further coordinates feature representations at different scales through an adaptive feature integration mechanism, ensuring that target features maintain consistency and integrity under conditions of multiple camera angles and complex background interference. Furthermore, the module optimizes feature transfer efficiency by introducing a multi-dimensional fusion strategy to achieve efficient interaction between feature maps at different scales, minimizing information loss and significantly improving the model's ability to analyze multi-dimensional features without significantly increasing computational costs.

[0048] In step 3 above, a novel CoIoU loss function is proposed to optimize the bounding box fitting ability of the original YOLOv8 model. The CoIoU loss function effectively improves the model's bounding box prediction accuracy, convergence speed, and robustness by refining the modeling of the positional relationships and geometric characteristics of the target boxes, thereby significantly enhancing detection performance in complex environments. CoIoU combines IoU, center point distance penalty, and shape consistency penalty, and specifically includes the following components.

[0049] First, IoU is a basic metric for measuring the degree of overlap between the predicted bounding box and the ground truth bounding box, and the specific formula is as follows (12): (12) In CoIoU, IoU is used to calculate the overlap between the predicted bounding box and the ground truth bounding box, and is a fundamental component of the loss function. The larger the IoU, the higher the matching degree between the two boxes, and the smaller the loss. Secondly, there is a penalty term for the center point distance. To further optimize the position fitting of the bounding boxes, CoIoU introduces a penalty term for the center point distance, as shown in equation (13): (13) Where x and y are the coordinates of the center point of the prediction box. x gt ,y gt The coordinates of the center point of the true bounding box. c This is the diagonal length of the smallest bounding box that encloses both the predicted and ground truth boxes. This term measures the distance between the center points of the predicted and ground truth boxes; the greater the distance, the larger the penalty. Finally, there is the shape consistency penalty term. The core improvement of CoIoU lies in using cosine similarity to measure the aspect ratio consistency between the predicted and ground truth boxes. The specific formula is shown in equation (14): (14) This method calculates the aspect ratio difference between the predicted and ground truth bounding boxes and smooths them using a cosine function to prevent excessive fluctuations when the aspect ratio difference is large. Simultaneously, the weight parameters... α Controlling the impact of shape consistency penalty α The specific formula is shown in equation (15): (15) This design ensures that... IoU When the value is large, the effect of the shape penalty is small, while when IoU When the aspect ratio is smaller, the influence of shape differences will increase accordingly, thus achieving a more balanced aspect ratio processing. Based on the above, the CoIoU loss function is finally obtained, and the specific formula is shown in equation (16): (16) By combining IoU, center point distance, and shape consistency penalty, the model considers both the overlapping area of ​​the bounding boxes and the differences in the position and shape of the boxes, making the comprehensive evaluation of bounding boxes more accurate in the cow behavior recognition task.

[0050] To address the aforementioned technical solution, this embodiment proposes specific implementation examples and provides concrete experimental data to demonstrate the superiority of the model, such as... Figure 1 As shown. Figure 1 This is a sample image of cow behavior according to the present invention. Two data acquisition devices were installed in the ranch of an animal breeding group company in Tianjin to collect cow behavior data. To ensure the integrity of the dataset, the data acquisition devices were fixed at a height of approximately 4.5 meters above the ground. Simultaneously, to ensure that a sufficient number of cows were captured and their complete activity trajectories were obtained, the two data acquisition devices were respectively installed in the feeding area and the resting fence area of ​​the ranch. Regarding data acquisition parameters, to obtain clear images of cow behavior, the video resolution was set to 1280×720 pixels, the frame rate to 30 frames per second, and the video acquisition period was from 08:00 to 18:00 daily; the dataset was collected from April 19, 2024 to August 25, 2024.

[0051] like Figure 2 As shown. Figure 2The flowchart for constructing the dataset for this invention is as follows: The collected image data is first denoised to remove images with excessive similarity, ensuring dataset diversity. Then, the CVAT annotation tool is used to annotate the images, categorizing behaviors into six types: feeding, standing, lying down, licking, mounting, and resisting. The annotation information is saved in a txt file and divided into training and validation sets in an 8:2 ratio. Simultaneously, considering the complexity of the cattle farm environment, excessive occlusion, and uneven lighting, to enhance data diversity, a portion of images from the training set are randomly selected for data augmentation operations such as translation, mirroring, random cropping, random scaling, and random rotation. Training the model using the data-augmented dataset indirectly increases the number of samples, accelerates model convergence, and improves its generalization ability.

[0052] like Figure 3 As shown. Figure 3 This is a distribution diagram of the frequency and bounding box of dairy cow behavior categories according to the present invention. The processed dairy cow behavior recognition dataset includes 4640 images with a total of 37175 bounding boxes. There are some differences in the number of bounding boxes for the six behavior categories. Since dairy cows spend most of their time standing or lying down, and their feeding time is concentrated and long-lasting, standing, lying down, and feeding behaviors have the most bounding boxes. In contrast, abnormal behaviors such as licking back, mounting, and resistance occur for shorter periods and at lower frequencies, resulting in fewer bounding boxes. The number of bounding boxes for each behavior category is consistent with the usual distribution of dairy cow behavior, and can well reflect the characteristics of dairy cow behavior distribution.

[0053] like Figure 4 As shown, Figure 4 This is a structural diagram of the CVMamba module of the present invention. To address the challenges of diverse background interference, long-distance feature dependencies, and insufficient feature modeling in dairy cow behavior recognition tasks within complex pasture environments, a CVMamba module is proposed to replace the C2f module in the YOLOv8 backbone network, enhancing the model's ability to capture global contextual information. This module, based on the YOLOv8 C2f module, incorporates a parallel Vision State Space (VSS) module to capture the global contextual information of the input features. The module's workflow is as follows: First, the input features are processed through an initial convolutional layer to generate a basic feature map, which is then divided into several branches. Each branch undergoes feature processing through an independent VSS module. The VSS module captures global information through a multi-directional selective scanning mechanism and concatenates the features from each branch into a single overall feature map at the output stage. This final output is then generated through a convolutional layer.

[0054] like Figure 5 As shown, Figure 5This is a structural diagram of the VSS module of the present invention. The VSS module effectively solves the balance problem of long-distance dependence and local feature extraction in visual tasks through multi-directional selective scanning and two-dimensional state modeling. The 2D selective scanning submodule extends Mamba's state space modeling to two-dimensional image features, enabling simultaneous capture of local and global features. By extending state space modeling to a two-dimensional feature map, it can effectively analyze the dynamic changes of the cow's entire body while capturing local details, combined with global contextual information. This design can identify and associate motion features of multiple body regions, demonstrating stronger modeling capabilities for complex behaviors that require integrating limb region information, such as mounting and licking.

[0055] like Figure 6 As shown, Figure 6 This is a schematic diagram of the multi-directional selective scanning of the present invention. To further enhance its ability to capture global information, the VSS module employs a multi-directional selective scanning mechanism. Specifically, the VSS module performs forward and reverse scans in the horizontal and vertical directions respectively to capture contextual information from different directions. This mechanism allows the VSS module to simultaneously focus on the horizontal and vertical changes in the overall posture of the cow. For example, horizontal scanning is suitable for capturing the relative positional relationship between the head and tail, while vertical scanning focuses more on dynamic information related to body height. By integrating these contextual features from different directions, an enhanced global feature map is ultimately generated, thereby improving the model's performance in complex behavioral classification tasks.

[0056] like Figure 7 , Figure 8 As shown, Figure 7 This is a structural diagram of the MCB module of the present invention. Figure 8 This is a structural diagram of the MHBRA module of the present invention. Because spatiotemporal features are complex and difficult to distinguish in dairy cow identification tasks, a multi-head bidirectional recursive attention mechanism (MHBRA) was designed and integrated with the channel attention mechanism in the convolutional block attention module to construct a novel multi-head convolutional block attention module (MCB). This enhances the accuracy of feature selection and the ability to capture contextual dependencies. Through the multi-head attention mechanism, it simultaneously processes global and local features, with each head independently capturing different feature dependencies. This improves the model's ability to recognize fine-grained features in complex scenes, enhances the aggregation of global and local information, and improves recognition accuracy and robustness.

[0057] like Figure 9 As shown, Figure 9This is a structural diagram of the MSPPF_A module of the present invention. To address the complexities of target behavior recognition tasks, such as pose variations, partial occlusion, and scale differences, a multi-scale pyramid pooling module incorporating average pooling is proposed. This module aims to enhance the model's ability to perceive targets at different scales and improve the overall robustness of detection. By introducing pooling kernels of different sizes and combining max pooling and average pooling operations, both local and global features can be captured more comprehensively.

[0058] like Figure 10 As shown, Figure 10 This is a structural diagram of the MSH module of the present invention. In the task of recognizing dairy cow behavior, the target usually exhibits characteristics of multiple perspectives, pose changes, and complex behavioral patterns, which places higher demands on the accuracy and breadth of feature extraction. To address this issue, this paper innovatively designs a multi-scale hybrid module, MSH, which aims to enhance the model's feature capture efficiency and robustness in complex scenes by optimizing multi-scale feature fusion and representation capabilities. This module is based on a multi-scale pooling and convolution kernel combination strategy, employing 1×1, 3×3, and 5×5 multi-scale convolution kernels and depthwise separable convolutions to accurately capture subtle changes in target features and contextual information from local to global perspectives. Furthermore, the module coordinates feature representations at different scales through an adaptive feature integration mechanism, ensuring that target features maintain consistency and integrity under conditions of multiple camera angles and complex background interference.

[0059] like Figure 11 As shown, Figure 11 This invention presents the CVMamba-MYOLOv8 network structure. The CVMamba-MYOLOv8 model is built upon the YOLOv8 base model, and by combining a visual state space CVMamba module, a multi-head attention mechanism (MCB) module, a multi-scale feature optimization module, and an improved loss function, it significantly enhances the model's ability to recognize cow behavior and its robustness. To comprehensively evaluate the model's performance, the following key metrics were used: mean precision, precision, and recall. These metrics can comprehensively quantify the model's detection effectiveness and computational complexity from different perspectives. The specific calculation formulas are shown below.

[0060] (17) (18) (19) in, TP The number of actual positive classes predicted as positive. FP This represents the number of actual negative classes predicted as positive. FN This represents the number of actual positive classes predicted as negative. TNThe actual number of negative classes predicted as negative is AP, which is the area under the precision-recall curve for a specific class. P It is used to evaluate the proportion of samples that are actually positive when the model predicts them to be positive, and can reflect the reliability of the model's predictions. R This represents the proportion of correctly identified objects by the model among all actual positive samples, used to measure the model's coverage capability. `mAP@0.5` represents the average AP value calculated for all images of each class when IoU is set to 0.5. `mAP@0.5-0.95` represents the average mAP across different IoU thresholds (from 0.5 to 0.95, with a step size of 0.05). A higher mAP value indicates better detection performance of the object detection model. `mAP` is a key indicator for evaluating the performance of object detection algorithms, calculated by combining precision and recall through the area under the PR curve.

[0061] To ensure the fairness and consistency of the model training results, all experiments were conducted in a unified environment, with the following specific configuration: operating system: Windows 11; CPU: 12th Gen Intel Core i7-12650H; GPU: NVIDIA GeForce RTX 4060; programming language: Python version 3.12.7; CUDA version: 12.1; deep learning framework: PyTorch version 2.5.1+cu121.

[0062] Hyperparameters play a crucial role in the training process of the model. The hyperparameter settings for this training are as follows: batch size is 16, initial learning rate and final learning rate are both set to 0.01, weight decay coefficient is 0.0005, and learning rate momentum is 0.937.

[0063] To systematically evaluate the impact of the proposed improvements on the performance of the YOLOv8 model, we designed and implemented a series of ablation experiments. The experiments examined the contributions of the MCB attention mechanism, MSH module, MSPPF_A module, CoIoU loss function, and CVMamba module to model accuracy, recall, and mean precision. Table 1 shows the specific settings and results for each group of experiments, where “√” indicates the introduction of the corresponding module.

[0064] Table 1 ; Experimental results show that the initial YOLOv8 model achieved the following performance: mean precision (MPT) of 90.2%, recall of 91.4%, mAP50 of 92.8%, and mAP50-95 of 74.8%. After introducing various improvements, all performance metrics of the model improved. Firstly, the introduction of the MCB attention mechanism improved both precision and recall, reaching an MPT of 92.5%. Adding the MSH module maintained a relatively good improvement in MPT and recall, reaching 91.7% and 92.8% respectively, indicating that this module has a positive effect on model performance. The introduction of the MSPPF_A module also improved MPT and recall, demonstrating its optimization effect on precision and recall. Adding the CoIoU loss function improved MPT by 1.8% compared to the original YOLOv8 model. Finally, the introduction of the CVMamba module improved the model's MPT to 92.3% and recall to 92.9%. Ultimately, when all improved modules were introduced simultaneously, the model achieved optimal overall performance, with mean precision reaching 94.2%, recall at 93.8%, mAP50 at 94.4%, and mAP50-95 at 78.3%. Compared to the initial YOLOv8 model, precision improved by 4.0%, recall by 2.4%, mAP50 by 1.6%, and mAP50-95 by 3.5%. Overall, the ablation experiments validated the effectiveness of each module. By introducing these improvements, the model significantly improved precision, recall, and mean precision in cow behavior recognition, and the synergistic effect between modules further enhanced overall performance. These experimental results fully demonstrate that the proposed improvement method not only effectively improves model performance but also verifies the practical application effects of these improvement schemes.

[0065] like Figure 12 As shown, Figure 12 This is a comparative analysis chart of the ablation experiment results of the present invention. This chart more intuitively shows the performance of different models on the main performance indicators in the ablation experiments. A, B, C, D, E, F, and G correspond to the seven groups of experiments in the table. (a) The chart uses a standardized histogram to compare the performance of models A to G on four indicators: mean precision, recall, mAP50, and mAP50-95. It can be seen from the chart that model G has a significant advantage in all indicators, indicating that the improved model performs best in terms of accuracy. (b) The radar chart further visualizes the overall performance of the model under multiple indicators. It can be found that model G not only performs outstandingly in accuracy but also has the most balanced performance, demonstrating good overall performance.

[0066] To evaluate and compare the performance of different YOLO models on various behavior recognition tasks, we selected multiple versions from YOLOv5 to YOLOv11, as well as the CVMamba-MYOLOv8 model, an improvement on YOLOv8. We then comprehensively compared their precision, recall, mean precision, mAP50, and mAP50-95 across six behavior classifications: feeding, standing, lying down, licking, mounting, and resisting. The performance of each YOLO version is shown in Table 2.

[0067] Table 2 ; Comparative experiments show that among all models, the proposed CVMamba-MYOLOv8 exhibits the best overall performance, particularly in terms of precision and recall. In various behavior recognition tasks, CVMamba-MYOLOv8 significantly outperforms other YOLO versions. For example, in the precision of the eating action, CVMamba-MYOLOv8 reaches 94.6%, nearly 7 percentage points higher than YOLOv8. In the standing and lying-down actions, its precision is 97.1% and 99.5% respectively, demonstrating stronger recognition capabilities. Furthermore, CVMamba-MYOLOv8 also improves its performance in the mAP50-95 range, reaching 78.3%, indicating that the model's overall performance under different IoU thresholds has been optimized.

[0068] like Figure 13 As shown, Figure 13 This chart compares the accuracy of different models in recognizing cow behaviors according to the present invention. The chart clearly shows the differences in performance among the different models across the six cow behavior recognition tasks. Compared to other models, the CVMamba-MYOLOv8 model exhibits higher accuracy across all behavior categories, particularly in complex behaviors such as licking and mounting, where its performance is significantly superior. This indicates that CVMamba-MYOLOv8 is better suited for behavioral feature extraction and classification tasks in complex scenarios, demonstrating stronger robustness and recognition capabilities.

[0069] like Figure 14 As shown, Figure 14 This is a performance comparison chart of the cow behavior recognition models of this invention. It clearly shows the comparison results of different models in four performance metrics: average precision, recall, mAP50, and mAP50-95. The results show that the CVMamba-MYOLOv8 model performs best in all metrics, especially demonstrating a significant advantage in mAP50-95, fully illustrating its superior detection capability in complex scenarios. Overall, this model possesses higher robustness and comprehensiveness, making it suitable for diverse behavior recognition tasks.

[0070] like Figure 15 As shown, Figure 15 This figure compares the performance of the key metrics of this invention over epochs. It clearly shows that CVMamba-MYOLOv8 not only outperforms other models in performance but also demonstrates a significant advantage in training convergence speed. Especially after the 50th epoch, this model consistently maintains a leading position across multiple evaluation metrics, indicating that CVMamba-MYOLOv8 can reach convergence faster and exhibits strong performance from the early stages of training. This demonstrates that the model not only excels in accuracy but also achieves high efficiency in a shorter training cycle. In summary, CVMamba-MYOLOv8 not only performs exceptionally well in action recognition tasks in complex scenes but also possesses significant advantages in training speed and convergence performance.

[0071] like Figure 16 As shown, Figure 16 This is a visualization comparison of the present invention. To explore the improvement effect, a comparative analysis of the visualization results of the original YOLOv8 model and the improved CVMamba-MYOLOv8 model in the cow behavior recognition task was conducted. The left column shows the detection results of the original YOLOv8 model, and the right column shows the detection results of the CVMamba-MYOLOv8 model. It can be seen that the improved model has improved detection confidence, indicating that the improved network structure more accurately represents the features. The figure shows that in complex scenes containing multiple cows, the bounding boxes of the CVMamba-MYOLOv8 model are more accurate and can clearly capture the behavioral features of the cows, while the detection results of the original YOLOv8 model are slightly insufficient.

[0072] like Figure 17 As shown, Figure 17 This paper presents a visualization evaluation of the model used in this invention. A visualization analysis of the CVMamba-MYOLOv8 model for cow behavior recognition was performed, demonstrating its detection performance across various cow behaviors, including feeding, standing, lying down, resisting, licking, and mounting. The figures show that the model can accurately and efficiently identify the behavior category when the cow's body shape is complete and its position is clear. Furthermore, even when the cow is at the edge of the image or partially occluded, the model can still accurately determine its specific behavior by extracting local features. This indicates that CVMamba-MYOLOv8 has high stability and accuracy when handling behavior detection tasks in complex scenes.

[0073] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that modifications or equivalent substitutions can still be made to the specific implementation of the present invention. Any modifications or equivalent substitutions that do not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.

Claims

1. A method for recognizing cow behavior based on an improved YOLOv8n algorithm, characterized in that, Includes the following steps: Collect video data of dairy cow behavior and construct a dairy cow behavior dataset; The dairy cow behavior dataset is preprocessed; An improved YOLOv8n model was constructed, and the improved YOLOv8n model was trained using a preprocessed dairy cow behavior dataset to obtain a dairy cow behavior recognition model. The trained dairy cow behavior recognition model is used to identify dairy cow behavior; The improved YOLOv8n model specifically includes: introducing the CVMamba module and combining it with the original YOLOv8 C2f module to form the C2f-VisionMamba module, which replaces the C2f module in the backbone network; Design a multi-head bidirectional recursive attention mechanism and integrate it with the channel attention mechanism in the convolutional block attention module to construct a multi-head convolutional block attention module. Multi-scale pooling kernels and average pooling are introduced to improve the SPPF module in the backbone network, forming the MSPPF_A module; A multi-scale hybrid module is introduced and added to the last layer of the backbone network; We designed a loss function CoIoU based on cosine similarity to optimize the bounding box fitting ability of the original YOLOv8 model.

2. The method according to claim 1, characterized in that, The equipment for collecting video data on cow behavior is installed in the feeding area and the resting fence area at a fixed height of 4.5 meters. The collection resolution is 1280×720 pixels, the frame rate is 30 frames / second, and the video collection period is from 08:00 to 18:00 every day, with a frame collection interval of 3 seconds.

3. The method according to claim 1, characterized in that, The preprocessing includes denoising, filtering, labeling, and data augmentation; the labeled behavior categories include six types: feeding, standing, lying down, licking back, mounting, and resistance; the labeled information is saved in txt format, and the training set and validation set are divided in an 8:2 ratio; the data augmentation operations include translation, mirroring, random cropping, random scaling, and random rotation.

4. The method according to claim 1, characterized in that, The C2f-VisionMamba module introduces a Visual State Space (VSS) module. The VSS module employs a multi-directional selective scanning mechanism, performing forward and reverse scans in the horizontal and vertical directions respectively to capture contextual information from different directions, as shown in the following equation: , in, Indicates contextual information, Scan horizontal and Scan vertical These represent selective scanning operations in the horizontal and vertical directions, respectively.

5. The method according to claim 1, characterized in that, The multi-head bidirectional recursive attention mechanism includes: dividing the query, key, and value into multiple heads, performing attention calculation independently on each head, concatenating the weighted output of the multi-head attention mechanism for each head with its corresponding value, and then restoring the concatenated result to the original embedding dimension dmodel through a linear transformation to generate the final output.

6. The method according to claim 1, characterized in that, The MSPPF_A module introduces pooling kernels of different sizes. Specifically, it uses small pooling kernels to capture local details of the cow's body, large pooling kernels to obtain the overall shape and posture changes of the cow, and average pooling layers to further supplement feature representation, thus fusing feature information at different scales.

7. The method according to claim 1, characterized in that, The multi-scale hybrid module is based on a multi-scale pooling and convolution kernel combination strategy, employing 1×1, 3×3, and 5×5 multi-scale convolution kernels and depthwise separable convolution to capture subtle changes in target features and contextual information from local to global perspectives. Furthermore, it coordinates feature representations at different scales through an adaptive feature integration mechanism. In addition, the multi-scale hybrid module introduces a multi-dimensional fusion strategy to enable interaction between feature maps at different scales, reducing information loss.

8. The method according to claim 1, characterized in that, The CoIoU loss function combines IoU, center point distance penalty term and shape consistency penalty term based on cosine similarity to improve bounding box prediction accuracy; The IoU is used to calculate the overlap between the predicted bounding box and the ground truth bounding box. The larger the IoU, the higher the matching degree between the two boxes and the smaller the loss. The center point distance penalty term is used to measure the distance between the center points of the predicted bounding box and the ground truth bounding box; the greater the distance, the greater the penalty. The shape consistency penalty term based on cosine similarity calculates the aspect ratio difference between the predicted and ground truth bounding boxes and smooths it using a cosine function to prevent excessive fluctuations when the aspect ratio difference is large. Simultaneously, it uses weight parameters... α The impact of controlling shape consistency penalty.

9. The method according to claim 8, characterized in that, The CoIoU loss function is expressed as follows: , in, Let CoIoU be the loss function. Center point distance penalty item, c The diagonal length of the smallest bounding box that encloses the predicted box and the ground truth box. This represents a shape consistency penalty term based on cosine similarity.