# AI-Based Smart Monitoring Framework for Livestock Farms
## 4 Data Inspection and RT-DETR Model
### RT-DETR Model  
Recent advancements in Transformer-based object detection have led to models such as DETR and Deformable DETR, which formulate object detection as a direct set prediction problem, eliminating the need for hand-designed anchors and Non-Maximum Suppression (NMS). However, the slow convergence and inference speed of these models have hindered their practical deployment in real-time applications [16; 17; 18; 19]. To address these limitations, RT-DETR (Real-Time Detection Transformer) was proposed, which is a real-time object detection framework that maintains the end-to-end simplicity of DETR while significantly improving the inference speed and computational efficiency. RT-DETR introduces a hybrid encoder that processes multi-scale feature maps extracted from a CNN backbone [19; 20]. Within the encoder, two novel modules are employed:  
AIFI (Attention-based Intra-scale Feature Interaction) applies lightweight self-attention exclusively to the highest-resolution feature map (S\({}_{5}\)) to enhance intra-scale contextual information with minimal computational cost.  
CCFF (CNN-based Cross-scale Feature Fusion) efficiently fuses adjacent scale features (S\({}_{3}\), S\({}_{4}\), S\({}_{5}\)) using convolutional operations, enabling effective multi-scale feature integration without relying on attention mechanisms.  
The RT-DETR (Real-Time Detection Transformer) is an extended version of DETR that removes NMS (Non-Maximum Suppression) which negatively affects real-time object detection [20]. It is required for the optimal model to reduce computational costs, optimize query initialization, and show high performance.  
Figure 6 shows the RT-DETR pipeline, in which the last three stages of the backbone [S\({}_{3}\), S\({}_{4}\), S\({}_{5}\)] are used as inputs to the encoder. An efficient hybrid encoder converts multi-scale features into a sequence of image features through intra-scale feature interactions and cross-scale feature fusion. Then, query selection with minimal uncertainty is used to select a fixed number of encoders to be used as initial object queries for the decoder. Finally, the decoder, equipped with auxiliary prediction heads, iteratively optimizes the object queries to generate classifications and bounding boxes. AIFI performs intra-scale interaction only at the S\({}_{5}\) stage using a single-scale Transformer encoder, which significantly reduces computational cost while maintaining performance. CCFF is an optimized cross-scale fusion module that utilizes a fusion block composed of multiple convolutional layers. The role of the fusion block is to fuse adjacent features into new representations, enabling more effective multi-scale feature integration. High-level features containing meaningful information related to objects are extracted from low-level features, and redundant computations are performed for feature interaction for multi-scale features. In order to prove that it is inefficient to perform intra-scale and cross-scale feature interaction simultaneously, various types of encoders were designed, as shown in Figure 7. Multi-scale features ([S\({}_{3}\), S\({}_{4}\), S\({}_{5}\)]) are taken as input. AIFI employs a single-scale Transformer encoder to perform intra-scale interaction only at the S\({}_{5}\) level, which helps to reduce computational cost. In Figure 8, the fusion block is designed to fuse two adjacent features into a new representation, effectively integrating information across different scales.  
The hybrid encoder process can be mathematically formulated as follows:  
\[Q=K=V=Flatten(S_{5}),\]  
\[F_{5}=Reshape(AIFI(Q,K,V)),\]  
\[O=CCFF(\{S_{3},S_{4},F_{5}\}),\]