WMSP-RTDETR: An RT-DETR with Weighted Multi-Scale Fusion and Position-Enhanced Attention for Pig Detection in Crowded Barn Scenes
Meng Han, Shicong Li, Le Yang, Shuai Xu, Wangli Hao, Hua Yang, Yifan ChenAccurate pig detection in crowded barns is hindered by frequent body contact, similar appearance, overlapping torso boundaries, and scale variations, which weaken instance separation and bounding-box localisation. In the original Real-Time DEtection Transformer (RT-DETR) architecture, limited interactions between shallow boundary details and deep semantic features, together with insufficient complementary positional modelling in the encoder, may lead to missed, merged, or shifted detections in crowded pig scenes. A detector that strengthens both cross-scale feature interaction and spatial positional representation without excessive parameter growth is therefore needed. This study proposes the Weighted Multi-Scale and Position-Enhanced RT-DETR (WMSP-RTDETR), an RT-DETR-based detector for crowded pig scenes. The Weighted Multi-Scale Context Network (WMSC-Net) uses weighted cross-scale fusion and is designed to transfer shallow boundary cues to semantic features and enhance scale-aware representation. The Positional-Enhanced Mamba-Inspired Linear Attention Encoder (PE-MILA Encoder) employs linear attention, Rotary Position Embedding (RoPE), Locally Enhanced Positional Encoding (LePE), and Convolutional Position Enhancement (CPE), and is designed to enhance global context modelling and spatial localisation with limited parameter growth. Experiments on 8070 farm images show that WMSP-RTDETR reaches 61.1% AP, 95.4% AP50, and 63.6% AP75. Compared with ResNet-50-based RT-DETR (R50-RTDETR), the proposed model improves AP, AP50, and AP75 by 1.6, 0.5, and 2.0 percentage points, respectively. Parameters decline from 42.7 M to 29.1 M, and computational complexity declines from 137.7 to 99.1 giga floating-point operations (GFLOPs), whereas inference speed decreases from 21 to 16 frames per second (FPS). These results suggest improved overall detection and high-IoU localisation accuracy under the evaluated conditions. The proposed detector provides an accuracy-oriented solution for automated pig perception in crowded barn scenes.