- The paper introduces PigFormer, a two-stage RGB-D framework that directly regresses swine tissue metrics from ceiling-mounted imagery.
- It employs a geometric front-end and transformer-based encoder, achieving MAEs of 2.43 mm for backfat and 5.01 mm for loin muscle depth.
- The system offers high throughput and cross-site generalization, presenting a scalable alternative to labor-intensive ultrasound assessments.
Automated Swine Body Condition Estimation with PigFormer
Motivation and Background
Assessing sow body condition is critical for optimizing feed management, lactation performance, and piglet survival. Traditional approaches such as visual scoring and caliper-based assessments exhibit low correlation with actual subcutaneous fat and muscle composition as measured by ultrasound, which remains the gold standard. However, ultrasound acquisition is laborious, costly, and requires specialized personnel, limiting its scalability for large-scale, routine monitoring in commercial settings. These constraints highlight the need for an automated, high-throughput, non-contact solution for accurate body condition estimation.
Advances in computer vision have enabled 3D and RGB-D systems to estimate gross body weight and growth in pigs. However, direct estimation of tissue-level metrics such as backfat thickness and loin muscle depth from camera-based sensor data, particularly in an end-to-end and multi-target fashion, had not previously been realized at production-relevant accuracy and throughput.
The paper introduces PigFormer, a two-stage, end-to-end system for regressing anatomically-relevant tissue measurementsโbackfat thickness, loin muscle depth, and total tissue depth at the rib-siteโfrom raw ceiling-mounted RGB-D imagery. The system is comprehensively designed to handle geometric and pose variability, maximize spatial context, and support joint multi-target regression.
Stage 1: Geometric Front-End
Raw depth frames are processed to segment the pig, upper body, and ground plane using a distillation pipeline from SAM3 to MaskDINO, operating on depth alone for deployment portability. The pig's ground-relative dorsal profile is extracted and spatially normalized by rotating each map according to heading, ensuring pose invariance and canonical feature alignment. The result is a standardized 96ร224 dorsal height map per frame.

Figure 1: Pipeline overview for PigFormer, from raw RGB-D input through geometric normalization and segmentation, to height map generation and sequence-based regression.
Stage 2: Slice Attention Encoder
The height map is processed as a sequence of 224 cross-sectional "slice" tokens, each representing the height profile at a longitudinal spine position. A transformer with rotary positional encoding enables global attention across distant segments of the back, essential for capturing anatomical relationships relevant to the regression targets. The encoder employs mean/max dual pooling before a final MLP head jointly regresses all three anatomical measurements. Supervision is via Huber loss on each target with total tissue depth (= backfat + loin) included explicitly to enforce target consistency.

Figure 2: Segmentation pipeline using SAM3 and MaskDINO distillation to generate pig, ground, and upper body masks from depth-only input.


Figure 3: RGB-D point cloud of a pig as captured by the ceiling-mounted sensor, before geometric transformation.

Figure 4: Heading normalization via upper-body mask, standardizing left-to-right orientation to eliminate pose ambiguity.
Empirical Evaluation
The dataset comprises 319 instances (from two distinct US research facilities) with 6,705 depth frames, fully annotated with site-matched ultrasound-derived fat and loin measurements under an instance-level split protocol (no animal overlap across splits). Strong baselines include single-stage ResNet-18 and ViT-small models taking raw (preprocessed) depth input, using only z-score normalization, to establish the independent effects of PigFormerโs geometric front-end.
Results demonstrate PigFormer achieves 2.43 mm MAE for backfat, 5.01 mm for loin, and an overall MAE of 3.87 mm across all three targets, outperforming ResNet-18 and ViT baselines by 22% and 39% in overall MAE, respectively. Notably, PigFormerโs backfat MAE approaches the intra-operator variability of manual ultrasound (1.30 mm), indicating prediction accuracy near the practical limits of measurement repeatability.
Alternative geometric front-endsโa pruned MaskDINO segmenter and a lightweight UNetโachieve comparable accuracy (within 0.08 mm MAE of the original) while improving throughput, with the UNet-based pipeline running in approximately 7 ms/frame on A100, enabling real-time deployment.
Model Analysis and Attention Attribution
Critical ablation and attribution analyses confirm the necessity of spatially structured attention. Classical regression and CNN alternatives on handcrafted or local geometric features drastically underperform, indicating that regional anatomical attention, not global statistics, are required for precise tissue-depth estimation.
Interpretability studies using SmoothGrad-ร-Input highlight that the encoderโs attention is anatomically localizedโfat and loin predictions concentrate on the dorsal regions near the rib and rump, mirroring the zones evaluated by expert ultrasound measurement.

Figure 5: Population-averaged normalized column importance (NCI) demonstrating anatomical localization of model attention along the dorsal axisโrib and rump receive high attribution, disproving reliance on global shape regression.

Figure 6: Per-spine-column importance for one pig; peak attention for loin depth aligns with the last-rib anatomical region, confirming spatial specificity in tissue estimation targets.
Comparison with Conventional Scoring and Generalization
Correlation analysis between ultrasound-based measurements and production-caliber caliper scores reveals only modest agreement (r2=0.56 for backfat, r2=0.44 for loin), reinforcing the discrepancy between traditional scoring tools and direct tissue quantification.
PigFormer demonstrates strong cross-site generalization only when full data diversity is included during training; single-site models fail to extrapolate accurately due to marked differences in animal phenotype and management across sites.
Practical Implications and Future Directions
PigFormer constitutes a practical and rigorous alternative to ultrasound and caliper-based tissue estimation. The architectureโmodular and robustโsupports deployment in production environments, balancing out-of-distribution robustness (with a detection-based geometric front end) and inference speed as prioritized. The transformer backbone, with attention mechanisms attuned to swine anatomical structure, establishes a template for future extension to related tasks (e.g., multi-site generalization, integration of temporal or behavioral cues, application to other livestock).
Further developments may include large-scale, multi-farm datasets, broader breed and management representation, and benchmarking against continuous measurements in commercial-grade barns. Additionally, extensions to automated longitudinal monitoring and integration with nutrition management platforms could optimize feed efficiency and reproductive outcomes at scale.
Conclusion
The presented study details a fully automated, two-stage RGB-D vision framework for direct regression of continuous swine body composition phenotypes. PigFormer achieves state-of-the-art performance approaching reference-level measurement repeatability, demonstrates controllable trade-offs between robustness and throughput, and localizes prediction attention to anatomically meaningful regions. This advance bridges the gap between direct measurement and scalable monitoring, supporting improved decision-making in commercial swine production.
(2606.05611)