Abstract
Accurate pig instance segmentation plays a fundamental role in intelligent livestock farming, providing reliable visual information for downstream applications such as individual identification, behavior analysis, and precision management. However, practical pig-house environments remain highly challenging due to severe adhesion between adjacent pigs, frequent occlusion, complex background structures, and illumination variations. Although Mask2Former has demonstrated strong performance in generic instance segmentation, its query-based representation is prone to instance ambiguity in densely distributed pig groups, while the extraction of fine-grained boundary information is still insufficient under complex farming conditions. To address these challenges, this paper proposes MGB-Former, an enhanced Mask2Former framework for robust pig instance segmentation. Instead of modifying the overall segmentation paradigm, MGB-Former improves instance representation learning from three complementary perspectives. Following MP-Former, its mask-piloted auxiliary training mechanism is incorporated into the proposed framework to provide explicit instance-level guidance during training and promote more reliable query-instance correspondence in crowded pig scenes. An LSKBlock-based feature enhancement module is incorporated into the pixel decoder to strengthen multi-scale spatial representation. Furthermore, a lightweight Auxiliary CNN Branch, used only during training, is designed to reinforce local spatial and foreground feature learning without increasing the inference complexity of the model. Extensive experiments on the PigLife dataset demonstrate the effectiveness of the proposed framework. MGB-Former achieves 72.54% AP, 91.39% AP50, and 81.21% AR, consistently outperforming the baseline Mask2Former and several representative instance segmentation methods. Comprehensive ablation studies, attention mechanism comparisons, and scenario-wise evaluations further verify the contribution of each proposed component and show that the proposed framework achieves consistent segmentation improvements across the dense, low-light, and occlusion-related subsets evaluated within the PigLife dataset.