A method for recognizing daily behaviors of group-raised live pigs based on a SAM2-C2F model
By constructing the SAM2-C2F model and combining it with self-designed modules and feature fusion technology, the problems of single model and high computational cost in pig behavior recognition are solved. This enables efficient and accurate recognition of various behaviors of group-raised pigs, and is suitable for pig house environments.
Patent Information
- Application Number
- CN202510070348.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-16
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2045-01-16
AI Technical Summary
Existing technologies for pig behavior recognition suffer from problems such as single model detection, high computational cost, and slow speed, making them difficult to apply effectively in actual breeding processes.
Using the SAM2-C2F model, a daily behavior recognition model for group-raised pigs was constructed by designing a Symmetric Adapter module and an Adjustment Channel module, combined with a C2F feature fusion module and a dynamic detection head. The feature extraction capability of the SAM2 backbone network was utilized to fuse feature information at different scales, thereby improving the recognition accuracy.
It achieves accurate identification of various daily behaviors of group-raised pigs, improves the model's identification speed and accuracy, is applicable to actual pig house environments, and reduces computational costs.
Smart Images

Figure CN119904913B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the technical field of computer vision and behavior recognition, and specifically relates to a daily behavior recognition method for group-raised live pigs based on a SAM2-C2F model. The technology is particularly suitable for group-raised pig environments and can achieve the recognition of various daily behaviors of multiple pigs. BACKGROUND
[0002] China is a major pig-raising country in the world. In 2021, 670 million live pigs were slaughtered nationwide, and in 2022, nearly 700 million live pigs were slaughtered nationwide. Such a huge slaughter quantity requires more attention to pig safety, so bio-prevention and control in pig farms are more important than ever. Pig farms need to minimize personnel, vehicles, and material flows, and it is inconvenient for management personnel to frequently enter and exit the pig farm. The intelligentization and de-artificialization of pig production management processes have become a pressing problem for pig farms. Among them, accurately identifying the daily behavior of pigs through images or videos can improve pig-raising efficiency and economic benefits while reducing the risk of human-pig disease infection during the pig production process, which is conducive to the health and sustainable development of the breeding industry.
[0003] Pig behavior recognition has important significance in agriculture and animal husbandry. For example: 1) Precision feeding. By recognizing the behavior patterns of pigs, individual needs can be better understood, allowing for personalized feeding plans. For example, adjusting the feed ratio based on the activity level and eating habits of pigs can improve feed utilization. 2) Reducing stress. By monitoring pig behavior, it can be determined which environmental factors or management behaviors cause stress, allowing for optimization of the breeding environment, reducing stress, and improving the quality of life for pigs. 3) Health management. Timely detection of abnormal behaviors such as reduced appetite and slow movement can help early diagnosis of diseases, allowing for preventive measures and reducing the risk of disease transmission.
[0004] Early pig behavior recognition mainly uses electronic sensors and traditional image methods for perception, but has drawbacks such as interfering with pig behavior, complex steps, and slow recognition speed. Subsequent scholars have applied deep learning to pig behavior recognition, achieving relatively good results. However, most researchers recognize only one or two or three types of pig behavior, and the model detects a single type of pig behavior. In addition, many researchers only consider one-sided model detection accuracy or model running speed. If only model detection accuracy is considered, the model will be large, have high computational cost, and slow computing speed, making it difficult to apply to actual feeding processes. If only model running speed is considered, although the model is small, the detection accuracy is low, and it cannot be applied to actual feeding processes.
[0005] In view of the limitations of the prior art, the application provides a group-raised live pig daily behavior recognition method based on a SAM2-C2F model. The method adopts a SAM2 backbone network which is very popular in the field of transfer learning, and on the basis of freezing the pre-training weight parameters of the SAM2 backbone network, a Symmetric Adapter module and an Adjustment Channel module are independently designed, so that the model can fully utilize the powerful feature extraction capability of the SAM2 backbone network to more accurately apply to the downstream task of group-raised live pig daily behavior recognition. In addition, the C2F feature fusion module is fused to fully fuse feature information from different scales to improve the representation ability and task performance of the model; and the dynamic detection head is used to improve the accuracy of group-raised live pig daily behavior recognition.
[0006] The application independently designs a Symmetric Adapter module and an Adjustment Channel module on the basis of the SAM2 backbone network, fuses a C2F feature fusion module and a dynamic detection head, and thus constructs a SAM2-C2F group-raised live pig daily behavior recognition model. Compared with the previous method, the application is the first to introduce the SAM2 backbone network in the field of group-raised live pig daily behavior recognition, can accurately recognize up to seven kinds of daily behaviors of pigs, and provides an innovative solution for group-raised live pig behavior recognition. SUMMARY
[0007] The application aims to provide a group-raised live pig daily behavior recognition method based on a SAM2-C2F model. The technology constructs a pig multi-class behavior dataset through self-collection and self-construction, independently designs a Symmetric Adapter module and an Adjustment Channel module on the basis of freezing the pre-training weight parameters of the SAM2 backbone network, fuses a C2F feature fusion module and a dynamic detection head, and thus constructs a SAM2-C2F group-raised live pig daily behavior recognition model, and realizes very accurate recognition of pig multi-class behaviors.
[0008] A group-raised live pig daily behavior recognition method based on a SAM2-C2F model, for pig multi-class daily behaviors, constructs a recognition model, and realizes automatic recognition of daily behaviors of group-raised live pigs, comprising the following steps:
[0009] S1, using an online camera device, acquiring pig activity data videos in a pigpen at different time periods in a group-raised pig house environment.
[0010] S2, acquiring and arranging pig activity key frame pictures by using a video frame extraction technology, and expanding pig behavior data pictures by using a data enhancement technology.
[0011] S3, the pigs in the picture are labeled according to different behaviors, including using drink (drink), eat (eat), mount (mount), stand (stand), lie (lie), lateral (lateral), and sit (sit) seven daily behaviors, and a pig multi-class daily behavior recognition dataset is constructed, and the dataset is divided into a training set, a validation set and a test set.
[0012] S4, based on the SAM2 backbone network, on the basis of freezing the pre-training weight parameters of the SAM2 backbone network, a Symmetric Adapter (Symmetric Adapter) module and an Adjustment Channel (Adjustment Channel) module are designed, a C2F feature fusion module and a dynamic detection head are fused, and a group of living pigs daily behavior recognition model based on SAM2-C2F model is constructed.
[0013] S5, input the pictures in the training set and the validation set into the SAM2-C2F model for training, and obtain the best weight parameters of the SAM2-C2F model.
[0014] S6, input the test set pictures into the SAM2-C2F model loaded with the best weight model, and output the results of pig behavior recognition in the pig house environment, including the target frame position, behavior category and confidence.
[0015] Further, the online camera device in step S1 is a Hikvision network camera, model DS-IPC-T13HV3-IA, resolution is set to 1920x720, and video frame rate is set to 25fps. A python script is used to record the pig activity in four time periods of 5:00-9:00, 11:00-15:00, 17:00-21:00 and 23:00-3:00 every day in real time, and the pig behavior data video is sorted and saved.
[0016] Further, the representative pig behavior data video is selected in step S2, and the pig behavior data picture is obtained by using the video frame extraction script. The Opencv program is used to extract the key frame picture of pig activity in the video. Since the pig behavior instances in the pig behavior data picture are not evenly distributed, the data enhancement technology in step S2 includes Cutout and Cutmix, which uses Cutout data enhancement method and Cutmix data enhancement method to solve the problem of long tail effect of pig behavior quantity. The Cutout data enhancement method generates a new picture by randomly cutting a part of the image, which is beneficial to make the model more robust and avoid over-reliance on specific areas; the Cutmix data enhancement method generates a new picture by mixing the local areas of two images, which is beneficial to solve the problem of different categories and more efficiently use data.
[0017] Further, the step S3 uses LabelImg to label the behavior category of the pig in the picture, and the file after labeling has txt as the suffix, and the file name is consistent with the picture name. Correspond the image and the label file, and divide the data set into training set, validation set and test set according to the ratio of 8:1:1 to construct the pig multi-class daily behavior recognition data set.
[0018] Further, the Symmetric Adapter module designed in the step S4 is added with an adapter at the top and the low end of the frozen SAM2 backbone network. The adapter is composed of a layer of down-sampling fully connected layer, a layer of activation function layer, a layer of up-sampling fully connected layer and a layer of activation function layer. Since the backbone network is frozen and will not change with the training of the adapter, it can ensure that the feature representation provided by the backbone network is stable, and at the same time let the adapter focus on learning the subtle differences of the specific task of group-living pig daily behavior recognition. The design of the symmetric adapter helps the model to optimize the feature maps of different levels in the frozen SAM2 backbone network, so as to fully utilize the powerful feature extraction capability of the SAM2 backbone network.
[0019] Further, the Adjustment Channel module designed in the step S4 is used to adjust the channel number of the feature maps output by the last three layers of the SAM2 backbone network. It is mainly composed of a channel attention mechanism and a channel adjustment convolution layer. The channel adjustment module can dynamically allocate weights to each channel, highlight the features more important to pig behavior recognition, and suppress irrelevant features. By focusing on important features, the channel adjustment module can make the model more robust and better cope with noise, occlusion or other adverse conditions. In addition, through the channel adjustment module, unnecessary calculations can be reduced, which is convenient for subsequent feature fusion, so as to optimize the use of computing resources of the model.
[0020] Further, the C2F feature fusion module in the step S4 mainly refers to the C2F module designed by predecessors. It is used to integrate feature information from different scales to improve the representation ability and task performance of the model. Feature maps of different scales usually contain information of different levels, and low-level feature maps capture more local details and edge information, while high-level feature maps contain more extensive context information. Specifically, by adopting FPN (Feature Pyramid Network) and PAN (Path Aggregation Network) to integrate the C2F module into the feature fusion network, the model can benefit from both local details and global context by fusing feature maps from different levels, thereby improving the accuracy of the SAM2-C2F model in group-living pig daily behavior recognition.
[0021] Further, the dynamic detection head in the SAM2-C2F model in step S4 mainly includes offset and mask calculation, dynamic convolution, scale attention module and task attention module. The offset and mask are calculated by a convolution layer, which is used to calculate the offset and mask required for dynamic convolution; dynamic convolution is used to process feature maps of different scales; the scale attention module can assign importance to features of different scales; the task attention module is used to adjust the feature map to better adapt to the task requirements.
[0022] Further, the SAM2-C2F model in step S5 includes an input end, a symmetric adapter and a frozen SAM2 backbone network, a channel adjustment module, a C2F feature fusion module and a dynamic detection head connected in turn. The method for training the model by inputting pictures into the SAM2-C2F model is as follows:
[0023] S5-1, the picture is preprocessed by the input end, which includes Mosaic data enhancement and adaptive picture scaling;
[0024] S5-2, the preprocessed picture enters the backbone network composed of the symmetric adapter and the frozen SAM2 backbone network in the SAM2-C2F, and three feature maps of different sizes are obtained after passing through the channel adjustment module;
[0025] S5-3, the three feature maps of different sizes obtained in step S5-2 are input into the C2F feature fusion module to fuse the features from different scales, obtaining three scale feature maps;
[0026] S5-4, the three scale feature maps obtained in step S5-3 are input into the dynamic detection head end to obtain three feature tensors, and then the position of the prediction box and the behavior category are obtained;
[0027] S5-5, in the training phase, the prediction box is matched with the real box to obtain positive and negative samples, then the loss function is calculated to adjust the weight parameter, and at the end of each iteration, the validation set is used to calculate the accuracy and average precision to continuously optimize the model parameters.
[0028] Further, the SAM2-C2F model sets the maximum number of iterations (epoch) for training to 100, the number of input pictures (batch) for each model training to 12, and the initial learning rate of the model to 0.0001; the learning rate is updated by preheating for the first 3 iterations, and then the cosine annealing algorithm is used to update the learning rate.
[0029] Further, the backbone network combined by the ground symmetry adapter and the frozen SAM2 backbone network comprises the SAM2 backbone network and the symmetry adapter. The symmetry adapter is added at the top and low end of the frozen SAM2 backbone network respectively. The adapter is composed of a layer of down-sampling full connection layer, a layer of activation function layer, a layer of up-sampling full connection layer and a layer of activation function layer. The SAM2 backbone network adopts a multi-layer VIT (Vision Transformer) structure, which is used to extract image features by combining with the symmetry adapter, and extracts image features A1, A2 and A3 of 44x44, 22x22 and 11x11 pixels to obtain semantic information of different scales.
[0030] Further, the channel adjustment module mainly comprises a channel attention mechanism and a channel adjustment convolution layer. The channel adjustment module can dynamically assign weights to each channel, highlight features more important for pig behavior recognition, and suppress irrelevant features. By focusing on important features, the channel adjustment module can make the model more robust and better cope with noise, occlusion or other adverse conditions. In addition, the channel adjustment module can help reduce unnecessary calculations and facilitate subsequent feature fusion, thereby optimizing the use of model computing resources. The channel adjustment module adjusts the three feature maps A1, A2 and A3 of different channel numbers to feature maps B1, B2 and B3 with a channel number of 64.
[0031] Further, the C2F feature fusion module adopts FPN (Feature Pyramid Network) and PAN (Path Aggregation Network). FPN uses up-sampling to transmit and fuse information, and PAN transmits deep target positioning from bottom to top. The lightweight neck up-samples the feature map B3, then fuses it with the feature map B2, and then passes through the C2F to obtain the feature map C2 of 22x22; the feature map C2 is up-sampled and fused with the feature map B1, and then passes through the C2F module to obtain the feature map C1 of 44x44; the feature map C1 passes through the Conv module, is fused with the feature map C2, and then passes through the C2F module to obtain the feature map D2 of 22x22; the feature map D2 passes through the Conv module, is fused with the feature map B3, and then passes through the C2F module to obtain the feature map D3 of 11x11.
[0032] Further, the feature maps C1, D2 and D3 are input into the dynamic detection head to obtain three feature tensors of (4xreg_max+nc)x44x44, (4xreg_max+nc)x22x22 and (4xreg_max+nc)x11x11, wherein reg_max is the channel number of each anchor point output, and nc is the behavior class (a total of seven classes). reg_max is 16, and nc is 7.
[0033] Further, the weight parameters are adjusted by classification loss and rectangular frame loss. The classification loss is obtained by using the binary classification cross-entropy loss function on the seven categories and then taking the average. The rectangular frame loss includes DFL loss and CIoU loss. The DFL trains the model by minimizing the cross-entropy between the real label distribution and the model prediction distribution, so that the network focuses on the probability distribution near the target position more quickly, effectively handles difficult samples in the target detection task, and improves the performance and robustness of the model. The CIoU is an improved version of the IoU, which takes the center point distance, overlap rate and minimum outer matrix diagonal length as parameters, and introduces a direction factor to consider the rotation and direction of the bounding box. The CIoU loss calculation formula is as follows, wherein alpha is a weight coefficient, and v represents a parameter for measuring the similarity of the aspect ratio. The position, shape and direction are comprehensively considered, so that the model learns the features of the target frame more comprehensively. The calculation formula is as formula (1)-(3).
[0034]
[0035]
[0036]
[0037] The beneficial effects of the present application are as follows:
[0038] 1. The SAM2 backbone network is first introduced into the group-raised live pig daily behavior recognition field in the form of transfer learning, and a SAM2-C2F group-raised live pig daily behavior recognition model is constructed, which provides a new solution for group-raised live pig behavior recognition.
[0039] 2. The Symmetric Adapter module and the AdjustmentChannel module are independently designed, which helps to fully apply the powerful feature extraction capability of the SAM2 backbone network to the downstream task of group-raised live pig daily behavior recognition, thereby improving the accuracy of pig behavior recognition.
[0040] 3. The pig multi-class daily behavior recognition data set is self-built, which is convenient for other researchers to continue to carry out related research. BRIEF DESCRIPTION OF DRAWINGS
[0041] Figure 1 is the network structure diagram of the group-raised live pig daily behavior recognition model based on the SAM2-C2F model of the present application.
[0042] Figure 2 is a schematic diagram of the training set in the self-built data set of the present application after data processing.
[0043] Figure 3is a schematic diagram of a test set after data processing from a self-built data set of the present application.
[0044] Figure 4 is Figure 3 corresponding identification schematic diagram of the test set in the middle. DETAILED DESCRIPTION
[0045] The principles and features of the present application are described below in conjunction with the accompanying drawings, and the examples are used only to explain the present application and are not intended to limit the scope of the present application.
[0046] As shown in the figure, the present application provides a group-raised live pig daily behavior recognition method based on a SAM2-C2F model, wherein the SAM2-C2F model is a SAM2 main network based on which a Symmetric Adapter module and an Adjustment Channel module are autonomously designed, a C2F feature fusion module and a dynamic detection head are fused, and thus a SAM2-C2F group-raised live pig daily behavior recognition model is constructed.
[0047] The backbone network combined with the symmetric adapter and the frozen SAM2 backbone network is mainly used for multi-level feature extraction of the input image and gradually reduces the spatial size of the image. Since the SAM2 backbone network is frozen and will not change with the training of the adapter, it can ensure that the feature representation provided by the SAM2 backbone network is stable, while allowing the symmetric adapter to focus on learning the subtle differences of the specific task of group-living pig daily behavior recognition, thereby helping the model to optimize the feature maps of different levels in the frozen SAM2 backbone network, thereby making full use of the powerful feature extraction capability of the SAM2 backbone network. The channel adjustment module can dynamically assign weights to each channel after receiving the feature maps with different channel numbers from the backbone network by using the channel attention mechanism and the channel adjustment convolution layer, highlighting the more important features for pig behavior recognition and suppressing irrelevant features. In addition, it can also help to reduce unnecessary calculations and facilitate subsequent feature fusion, thereby optimizing the use of the model's computing resources. The C2F feature fusion module is used to integrate the feature maps from different scales processed by the channel adjustment module to improve the representation ability and task performance of the model. Different scale feature maps usually contain different levels of information, with low-level feature maps capturing more local details and edge information, and high-level feature maps containing more extensive context information. The C2F feature fusion module can benefit the model from both local details and global context by fusing these different scale features, thereby improving the understanding of complex scenes and improving the accuracy of the SAM2-C2F model for group-living pig daily behavior recognition. The dynamic detection head mainly includes offset and mask calculation, dynamic convolution, scale attention module and task attention module. The offset and mask are calculated by a convolution layer, which is used to calculate the offset and mask required for dynamic convolution; dynamic convolution is used to process feature maps of different scales; the scale attention module can assign importance to features of different scales; the task attention module is used to adjust the feature map to better adapt to the task requirements. The dynamic detection head can better process feature maps of different scales by adaptively adjusting its internal parameters and structure, thereby improving the recognition accuracy of pig multi-class behavior recognition.
[0048] The loss function of the SAM2-C2F model includes a classification loss and a rectangular box loss. The classification loss is calculated by pairwise comparison of the seven classes and taking the average of the binary cross-entropy loss function. The rectangular box loss is composed of two losses, DFL and CIoU. DFL minimizes the difference between the real label distribution and the model prediction distribution, enabling the network to focus more quickly on the location near the target, effectively handling samples that are difficult to distinguish, and improving the performance and robustness of the model. CIoU is an improved version of IoU, which not only considers the distance of the center point and the overlap ratio, but also includes the length of the minimum circumscribed matrix diagonal, and introduces a direction factor to consider the rotation angle of the bounding box. In addition, the CIoU loss function introduces a parameter v to measure the similarity of the aspect ratio. Therefore, CIoU considers multiple factors such as position, shape, and direction, enabling the model to learn the features of the target box more comprehensively. The classification loss and the rectangular box loss jointly act on the training process of the model, the former optimizes the classification performance by comparing the loss of multiple classes, and the latter improves the accuracy of the bounding box regression by combining DFL and CIoU. This comprehensive loss function design enables the model to perform better in multi-class object detection tasks in terms of accuracy and robustness.
[0049] The present application is based on a self-built pig multi-class daily behavior recognition dataset, and the SAM2-C2F model is used for daily behavior recognition of pigs in a pig house environment. The pig multi-class daily behavior recognition dataset is a self-built dataset based on LabelImg software, and is the first pig multi-class daily behavior recognition dataset based on the target detection task. It contains 977 high-definition images with a resolution of 1280x720, covering more than 5500 behavior instances, including drink (drink), eat (eat), mount (mount), stand (stand), lie (lie), lateral (lateral), and sit (sit), which are seven daily behaviors of pigs. In order to effectively train and evaluate, the dataset is randomly divided into training set (781), validation set (98) and test set (98) according to the ratio of 8:1:1.
[0050] The application uses Pytorch2.2.2 deep learning framework, Pycharm compiler, python3.8. The hardware environment is Intel Core i7-5930K processor, NVIDIA GeForce RTX 4090 24GB graphics card, operating system Ubuntu20.04.6LTS. After the environment is deployed, the SAM2-C2F model is used for iterative training, the SGD optimizer is used in the experiment process, the initial learning rate is 0.0001, the momentum of the SGD optimizer is 0.937, the weight decay is 0.0005, the iteration is 100 rounds, and the batch-size is 12.
[0051] In order to analyze the performance of the model, the model performance evaluation indexes are precision P, recall R, target precision AP, mean target precision mAP, model volume and parameter quantity, etc. as evaluation indexes. Among them, mAP takes IoU threshold as 0.5 in the experiment, that is, mAP@0.5. The calculation formula of the indexes is as shown in formulas (1)-(4).
[0052]
[0053]
[0054]
[0055]
[0056] Among them, TP represents the number of instances of the model correctly detecting the behavior category of the pig, FP represents the number of instances of the model failing to accurately detect the behavior category of the pig, FN represents the number of instances of the model failing to detect the behavior category of the pig, c represents the total number of behavior categories of the pig, and APi represents the target precision of the ith category.
[0057] The above is an example of the best embodiment of the application, wherein the parts not described in detail are the common knowledge of ordinary skilled in the art. The protection scope of the application is subject to the content of the claims, and any equivalent transformation based on the technical inspiration of the application is also within the protection scope of the application.
Claims
1. A method for recognizing daily behaviors of group-raised live pigs based on a SAM2-C2F model, characterized in that, The application discloses a pig daily behavior recognition model, and relates to the technical field of pig daily behavior recognition. Step S1, using an online camera device, obtaining pig behavior data videos in different time periods in a pigsty; Step S2, using a video frame extraction technology to obtain and organize pig behavior data pictures and expand pig behavior data pictures based on data enhancement; Step S3, using LabelImg software to label seven kinds of pig daily behaviors, and constructing a pig multi-category daily behavior recognition data set; Step S4, based on a SAM2 backbone network, on the basis of freezing the pre-training weight parameters of the SAM2 backbone network, independently designing a symmetric adapter module and a channel adjustment module, fusing a C2F feature fusion module and a dynamic detection head, and constructing a pig daily behavior recognition model based on a SAM2-C2F model; The pig daily behavior recognition model based on the SAM2-C2F model in step S4 comprises an input end, a symmetric adapter module, a frozen SAM2 backbone network, a channel adjustment module, a C2F feature fusion module and a dynamic detection head connected in sequence; wherein the symmetric adapter module is added at the top and the low end of the frozen SAM2 backbone network; the adapter is composed of a layer of down-sampling full connection layer, a layer of activation function layer, a layer of up-sampling full connection layer and a layer of activation function layer; wherein the adapter focuses on learning the subtle differences of the pig daily behavior recognition task, and can help the model to optimize the feature maps of different levels in the frozen SAM2 backbone network; the channel adjustment module is used for adjusting the channel number of the feature maps output by the last three layers of the SAM2 backbone network, and is composed of a channel attention mechanism and a channel adjustment convolution layer; the channel adjustment module can dynamically assign weights to each channel, highlight the features more important for pig behavior recognition, suppress irrelevant features, reduce unnecessary calculations, facilitate subsequent feature fusion, and thus optimize the use of model calculation resources; the C2F feature fusion module adopts an FPN feature pyramid network and a PAN path aggregation network, and is used for integrating feature information from different scales to improve the representation ability and task performance of the model; wherein the feature maps of different scales usually contain information of different levels, the low-level feature maps capture more local details and edge information, and the high-level feature maps contain more extensive context information; the C2F feature fusion module fuses the feature maps of different scales, so that the model benefits from local details and global context at the same time, improves the understanding ability of complex scenes, and thus improves the accuracy of the SAM2-C2F model in pig daily behavior recognition; the dynamic detection head comprises offset and mask calculation, dynamic convolution, a scale attention module and a task attention module; the offset and the mask are calculated through a convolution layer, and are used for calculating the offset and the mask required by the dynamic convolution; the dynamic convolution is used for processing feature maps of different scales; the scale attention module is used for assigning importance to features of different scales; and the task attention module is used for adjusting the feature maps to better adapt to the task requirements. Step S5, training a pig daily behavior recognition model based on the SAM2-C2F model; Step S6, using the trained pig daily behavior recognition model to realize the recognition of the daily behavior of group-housed pigs.
2. The method according to claim 1, wherein the method is a method for recognizing daily behavior of group-housed breeding pigs based on a SAM2-C2F model. In step S1, the Hikvision DS-IPC-T13HV3-IA online camera is installed in the group-housed pig house environment, and the python script is used to record the pig activity in four time periods of 5:00-9:00, 11:00-15:00, 17:00-21:00 and 23:00-3:00 every day in real time, and the pig behavior data video is saved.
3. The method according to claim 1, wherein the method is characterized by, In step S2, representative pig behavior data videos are selected, pig behavior data pictures are obtained by video frame extraction script, and Cutout data enhancement method and Cutmix data enhancement method are used to solve the problem of long-tail effect of pig behavior quantity; wherein the Cutout data enhancement method is to generate new data pictures by randomly cutting a part of the image; the Cutmix data enhancement method is to generate new data pictures by mixing the local areas of two images.
4. The method according to claim 1, wherein the method is characterized by, In step S3, the seven daily behaviors of pigs are drink, eat, mount, stand, lie, lateral and sit; the pig behavior data pictures expanded in step S2 are labeled by using the image labeling tool LabelImg, and a pig multi-class daily behavior recognition data set is constructed.
5. The method according to claim 1, wherein the method is characterized by, The specific steps of step S5 include: S5-1, the pictures are preprocessed through the input end, including Mosaic data enhancement and adaptive picture scaling; S5-2, the preprocessed pictures enter the backbone network composed of the symmetric adapter and the frozen SAM2 backbone network in SAM2-C2F, and three different size feature maps are obtained through the channel adjustment module; S5-3, input the three different size feature maps obtained in step S5-2 into the C2F feature fusion module to fuse the features from different scales, and obtain three scale feature maps; S5-4, input the three scale feature maps obtained in step S5-3 into the dynamic detection head, obtain three feature tensors, and then obtain the position and behavior category of the prediction box; S5-5, in the training stage, the prediction box is matched with the real box to obtain positive and negative samples, then the loss function is calculated to adjust the weight parameter, and at the end of each iteration, the validation set is used to calculate the accuracy and average precision to continuously optimize the model parameters.
6. The group-housed pig daily behavior recognition method based on the SAM2-C2F model according to claim 5, characterized in that: In step S5-1, the number of input pictures is 12 each time, and the initial learning rate of the model is 0.0001; the learning rate is updated by preheating for the first 3 iterations, and then the cosine annealing algorithm is used to update the learning rate; In the step S5-2, the image features A1, A2, A3 of the pixels are extracted 、 and the image features A1, A2, A3 of the pixels are extracted, and semantic information of different scales is obtained; the three feature maps A1, A2, A3 of different channel numbers are uniformly adjusted to feature maps B1, B2, B3 with a channel number of 64 by a channel adjustment module. In step S5-3, feature map B3 is upsampled, then fused with feature map B2, and then processed by C2F to obtain... Feature map C2; feature map C2 is upsampled and fused with feature map B1, then passed through the C2F module to obtain... Feature map C1; feature map C1 is fused with feature map C2 by the Conv module, and then fused with feature map C2 by the C2F module to obtain... Feature map D2; feature map D2 is processed by the Conv module, fused with feature map B3, and then processed by the C2F module to obtain feature map B3. From feature map D3, we finally obtain feature maps C1, D2, and D3 at three scales; In the step S5-4, the feature maps C1, D2 and D3 are input to the dynamic detection head to obtain 、 、 three feature tensors, wherein reg_max is the number of channels output for each anchor point, taking a value of 16; and nc is the behavior category, taking a value of 7. In step S5-5, the weight parameter is adjusted by classification loss and rectangular box loss, wherein the rectangular box loss includes DFL loss and CIoU loss.
Citation Information
Patent Citations
Group health-preserving pig fighting behavior identification method fusing space-time double attention mechanism
CN114299436A
Method for identifying and tracking behaviors of health-preserving pigs in group
CN115223191A