AI Era Survival: Using YOLOv3 for Accurate Pig Detection

The article explains how YOLOv3’s architectural upgrades—Darknet‑53 backbone, three‑scale feature fusion, refined anchors and multi‑label classification, plus dynamic input sizing—enable a pig‑recognition model trained on 2,456 images to achieve up to 20% higher detection rates and AP scores of 0.673–0.981.

xkx's Tech General Store
xkx's Tech General Store
xkx's Tech General Store
AI Era Survival: Using YOLOv3 for Accurate Pig Detection

YOLOv3 Core Improvements

Backbone upgrade : Darknet‑53 (53 convolutional layers with residual blocks) replaces Darknet‑19, providing deeper feature extraction and smoother gradient flow.

Three‑scale feature fusion : Inspired by FPN, YOLOv3 outputs 13×13, 26×26 and 52×52 feature maps. The 52×52 map preserves fine details for tiny piglets, the 26×26 map handles medium‑sized pigs, and the 13×13 map captures large adult pigs.

Anchor refinement : Each scale receives three anchors (nine total) generated by separate K‑means clustering, better matching target sizes at each scale.

Classification head : Softmax is replaced by a sigmoid + binary cross‑entropy loss, enabling multi‑label classification such as “pig + marked pig”.

Dynamic input resizing : Input size is randomly chosen among multiples of 32 (e.g., 320, 416, 608) instead of a fixed 416×416, improving generalisation to different camera distances and pen sizes.

Pig Detection Model

Dataset: 2,456 pig images split into 1,720 training, 491 validation and 245 test samples.

class Yolov3(nn.Module):
    def __init__(self, cfg, is_val=False) -> None:
        super(Yolov3, self).__init__()
        self.cfg = cfg
        self.num_classes = cfg.num_classes
        self.topk_candidates = cfg.val_topk if is_val else cfg.test_topk
        self.conf_thresh = cfg.val_conf_thresh if is_val else cfg.test_conf_thresh
        self.nms_thresh = cfg.val_nms_thresh if is_val else cfg.test_nms_thresh
        self.no_multi_labels = False if is_val else True
        # Backbone
        self.backbone = Yolov3Backbone(cfg)
        self.pyramid_feat_dims = self.backbone.feat_dims[-3:]
        # Neck: SPPF
        self.neck = SPPF(cfg, self.pyramid_feat_dims[-1], self.pyramid_feat_dims[-1])
        self.pyramid_feat_dims[-1] = self.neck.out_dim
        # Neck: FPN
        self.fpn = Yolov3FPN(cfg, self.pyramid_feat_dims)
        # Head
        self.head = Yolov3DetHead(cfg, self.fpn.out_dims)
        # Prediction layer
        self.pred = Yolov3DetPredLayer(cfg)

Training command:

nohup python -u train.py --cuda --dataset custom --root dataset/MonitorPigDataset --model yolov3_s --batch_size 16 >> train.log 2>&1 &

Training Results

After 300 epochs the model converged. Validation metrics (averaged) are:

Averaged stats: lr: 0.000010 size: 640 gnorm: 5.5 loss_obj: 1.4137 (1.4709) loss_cls: 0.3966 (0.3938) loss_box: 0.1408 (0.1403) losses: 0.6211 (0.6415)
AP@[0.50:0.95] = 0.673
[email protected] = 0.981
[email protected] = 0.829
AP small = 0.021
AP medium = 0.655
AP large = 0.686
AR@[0.50:0.95] (maxDets=100) = 0.750
AR small = 0.333
AR medium = 0.749
AR large = 0.751

Medium and large pigs achieve AP above 0.65, while small piglets obtain AP of 0.021. Compared with a YOLOv2 baseline the three‑scale fusion improves detection of small targets by more than 20 %.

Conclusion

The deeper Darknet‑53 backbone, multi‑scale feature maps, refined anchors and sigmoid‑based classification together make YOLOv3 well‑suited for animal detection tasks. With a modest dataset and the described training strategy, high‑precision pig detection is achievable without resorting to custom heavyweight networks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

computer visionDeep Learningobject detectionModel TrainingYOLOv3Pig Detection
xkx's Tech General Store
Written by

xkx's Tech General Store

Code with the left hand, enjoy with the right; a keystroke sweeps away worries.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.