To achieve precise detection of sow feeding behavior, this study adopted the YOLO v5-based object detection algorithm. The following sections will elaborate on the design principles of the algorithm and its application in this research.
2.2.1. Building the Object Detection Model
YOLO v5 is a single-stage object detection algorithm that builds on the YOLO series, incorporating several innovative designs to enhance performance. Compared to other models in the YOLO series and two-stage object detection models such as Faster R-CNN, YOLO v5 offers faster inference speed and lower computational resource requirements. YOLO v5 utilizes a lightweight network architecture while introducing deeper and wider network structures along with several improvements, enabling it to better handle small and dense objects while maintaining fast inference speed. Its efficient detection capabilities make YOLO v5 particularly suitable for large-scale pig farms [
18,
19]. Therefore, this study adopts the YOLO v5 algorithm to construct a model for detecting the feeding behavior of lactating sows.
Currently, YOLO v5 offers four main object detection networks: YOLO v5s, YOLO v5m, YOLO v5l, and YOLO v5x, primarily differentiated by their network depth. YOLO v5s is a variant of the YOLO v5 object detection algorithm and is a relatively smaller model within the YOLO v5 series. The network architecture of YOLO v5s is illustrated in
Figure 2. The red-framed section illustrates the schematic diagram of the main network structure, while the orange-framed section depicts the schematic diagram of the model components. YOLO v5s adopts the overall network architecture of the YOLO v5 series, comprising the Backbone, Neck, and Head networks. With its smaller network depth, YOLO v5s is suitable for resource-constrained environments. Considering the practical production limitations in pig farms, and to facilitate subsequent software and hardware deployment, the lightweight model YOLO v5s was chosen. The optimized version of this model will serve as the detection model for the feeding behavior of lactating sows.
Due to the presence of piglets entering the trough, causing occlusion and affecting feed and water conditions, the probability of missed and incorrect detections has increased, to some extent. Therefore, this study proposes improvements to the YOLO v5s model. The specific improvements are as follows: (1) adding an SE (Squeeze-and-Excitation) attention mechanism module, and (2) replacing the loss function with WIoU (Weighted Intersection over Union). The SE attention mechanism enhances the model’s focus on important features, improving its generalization ability and accuracy across various tasks, particularly in resource-constrained environments like pig farms. Combined with the WIoU loss function, which optimizes bounding box regression and reduces detection errors, the model can more accurately identify and localize targets in complex backgrounds, thereby boosting overall performance in terms of detection precision and localization accuracy.
The SE attention mechanism enhances the model’s representational capacity by explicitly modelling the interdependencies between channels. It achieves this through the following two steps: Squeeze: first, a global average pooling operation compresses the spatial information of each channel’s features, converting the feature maps into a global descriptor vector. Excitation: next, a fully connected layer applies a nonlinear transformation to the global descriptor vector, generating weights for each channel. These weights enable the model to dynamically adjust the importance of different channels, thereby capturing key features more effectively [
20,
21]. By enhancing the model’s focus on important features, particularly in resource-constrained environments, the SE attention mechanism improves the model’s generalization ability and accuracy across various tasks. In this study, the integration of the SE mechanism allows the model to more effectively identify features that are highly relevant to the target task, leading to improved detection or classification accuracy. The SE attention module is shown in
Figure 3a and is fused with the C3 module structure into an improved C3SE module, which is shown in
Figure 3b.
Given that the backbone network of YOLO v5 is responsible for feature extraction and the neck network handles feature fusion, integrating the SE attention module into the backbone can facilitate the extraction of more pertinent features, thus enhancing the detection model’s accuracy. The impact of inserting the SE attention module into different structural parts of the network varies in terms of performance and detection speed. In this study, two methods are employed: first, adding the SE attention module to the last layer of the original YOLO v5s model’s backbone, and second, replacing all C3 modules in the backbone with SE modules. The network models derived from these methods are designated as YOLO v5s-SE_Backbone and YOLO v5s-C3SE, respectively. The network structures of these models are depicted in
Figure 3c,d.
- 2.
Replacing the loss function with WIoU.
Since training data inevitably contains low-quality samples, geometric factors such as distance and aspect ratio exacerbate the penalty on these low-quality examples, thereby reducing the model’s generalization performance. A good loss function mitigates the impact of geometric factors when the anchor box and the target box overlap well, and minimal training intervention will result in better generalization capabilities of the model. An anchor box, also known as a prior box or default box, is a predefined bounding box of a specific aspect ratio and scale used in object detection algorithms like YOLO. These boxes serve as reference points for the model to predict the location and size of objects within an image. During training, the model adjusts these anchor boxes to better fit the ground truth (target) boxes, which represent the actual location of objects in the image. A target box, also known as a ground truth box, is the bounding box that defines the precise location and dimensions of an object within an image. It is manually labelled in the dataset and serves as the standard against which the model’s predictions (adjusted anchor boxes) are compared during training. The goal of the model is to predict bounding boxes that closely match these target boxes.
Focal-EIoU (Extended Intersection over Union) v1 was proposed to address the BBR (Bounding Box Regression) balance issue arising from varying sample quality. BBR is a process used in object detection algorithms to refine the predicted bounding boxes to more accurately match the ground truth boxes (target boxes) in an image. During this process, the model predicts adjustments (offsets) to the coordinates of anchor boxes or prior boxes to better align them with the actual location and size of the object in the image. The goal of bounding box regression is to minimize the difference between the predicted boxes and the ground truth boxes, improving the precision of object localization. This process typically involves predicting the center coordinates, width, and height of the bounding boxes.
However, due to its static FM (Focusing Mechanism), the potential of non-monotonic FM was not fully utilized. WIoU was proposed, based on this concept, introducing a dynamic non-monotonic FM. The attention-based loss WIoUv1 for BBR has demonstrated lower regression error in simulation tests compared to the state-of-the-art SIoU (Scalable Intersection over Union). Additionally, WIoU includes WIoUv2 with a monotonic FM and WIoUv3 with a dynamic non-monotonic FM. Detailed studies on the impact of WIoUv3 on low-quality samples have demonstrated the effectiveness and efficiency of the dynamic non-monotonic FM [
22]. The dynamic non-monotonic FM uses the outlier degree instead of IoU to evaluate the quality of anchor boxes and provides a wise gradient gain allocation strategy. This strategy reduces the competitiveness of high-quality anchor boxes while also reducing the harmful gradient generated by low-quality examples. This allows WIoU to focus on ordinary-quality anchor boxes and improve the detector’s overall performance.
In the WIoUv3 version, the outlier measure- and loss-function formulas are shown below as Equations (1) and (2):
where
represents the original bounding box loss, (
,
y) denotes the center point of the predicted box, (
) denotes the center point of the ground truth box,
and
are the width and height of the smallest enclosing box that contains both the ground truth and predicted boxes,
β is the outlier measure used to evaluate the quality of the anchor box—the smaller the
β, the higher the quality of the box—and
r is the non-monotonic focusing coefficient.
α and
δ are hyperparameters that can be adjusted according to different models and datasets. To prevent
from generating gradients that hinder convergence,
and
are detached from the computation graph (indicated by the superscript *). This effective elimination of factors that hinder convergence ensures that no new metrics, such as aspect ratio, are introduced.
In resource-constrained environments, such as applications in pig farms, the WIoU loss function aids the model in more accurately localizing targets, even in complex backgrounds. By optimizing bounding box regression, WIoU reduces detection errors, thereby enhancing the overall performance of the model, particularly in terms of detection precision and localization accuracy.
2.2.2. Experimental Setup
The experiments of this research were conducted using an Intel i7 8700K CPU (manufactured by Intel, Santa Clara, CA, USA). The system had 16 GB of RAM and an NVIDIA GeForce GTX 1080Ti GPU with 11 GB of memory (manufactured by Nvidia, Santa Clara, CA, USA). The chosen model framework was PyTorch 2.1.2, with CUDA 12.0 and Python 3.9 for implementation.
Table 2 lists some parameters used in the experiments; the early stopping mechanism of the training process was not disabled.
In this study, P (Precision), R (Recall), and mAP (mean Average Precision) are used as the metrics for evaluating the model’s performance. The metrics are defined in Equations (3) and (4), to validate the effectiveness of the model’s lightweight design [
23].
where
TP is the number of true positives,
FP is the number of false positives, and
FN is the number of false negatives.
C represents the number of target categories, with
C = 4 in this study.
N represents the number of images in the test set that contain targets of these
C categories.
P evaluates the model’s ability to accurately recognize the feeding behavior of lactating sows. R assesses the model’s coverage in identifying the feeding behavior of lactating sows.
[email protected] represents the mean average precision of all target categories (four categories in total) when the threshold is 0.50.
[email protected]:0.95 represents the mean average precision of all target categories when the threshold ranges from 0.50 to 0.95 [
19].