# Weed Detection Method Based on Lightweight and Contextual Information Fusion
## 2 Materials and Methods
### Enhanced YOLOv5 Model
#### 2.2.4 Lightweight CARAFE Upsampling Operator  
To further enhance the recognition capabilities for small weed targets, CARAFE is employed as a replacement for the nearest-neighbor interpolation upsampling used in the original YOLOv5. The nearest-neighbor interpolation upsampling relies solely on pixel spatial positions to determine the upsampling kernel, without effectively leveraging semantic information within the feature map. This limitation results in a small perceptual field. In contrast, CARAFE excels in aggregating contextual information from the image, thereby increasing the receptive field size. CARAFE dynamically generates adaptive kernels  
Figure 4: Context aggregation block. Permutations of feature maps are represented as their dimensions, e.g., C \(\times\) H \(\times\) W indicates a matrix with a number of channels C, height H, and width W. \(\otimes\) and \(\odot\) denote batched matrix multiplication and broadcast Hadamard product. Convolution layers used for attention map generation, feature mapping, and context refinement are annotated as blue, red, and green.  
for the feature map and then employs them for feature reassembly, facilitating seamless feature map integration and preserving feature map integrity.  
CARAFE comprises two primary modules: the upsampling kernel prediction module and the feature reassembly module. The upsampling kernel prediction module analyzes the input feature map to predict the sizes of corresponding upsampling kernels at different positions. The feature reassembly module utilizes the upsampling kernels obtained from the prediction module to perform the upsampling operation. The module structure of CARAFE is illustrated in Figure 5.  
In the upsampling kernel prediction module, to reduce computational complexity, an initial \(1\times 1\) convolutional layer is applied to compress the input feature map of shape \(H\times W\times C\) into a compact form with dimensions \(H\times W\times C_{m}\). Subsequently, an inner encoding sub-module employs convolutional layers with kernel sizes of \(k_{encoder}\times k_{encoder}\) to perform the prediction task, yielding upsampling kernels of size \(\sigma H\times\sigma W\times k_{up}^{2}\). Here, \(\sigma\) represents the upsampling factor, and \(k_{up}^{2}\) denotes the size of an individual upsampling kernel for a single feature point. The upsampling kernels are then subject to softmax normalization to ensure that the sum of kernel weights equals 1. This process adaptively generates corresponding upsampling kernels for each distinct feature point.  
In the feature reassembly module, each feature point in the output feature map is mapped to the input feature map, considering a region of size \(k_{up}\times k_{up}\) centered on that feature point. The dot product between this region and the associated upsampling kernel results in an output feature map of dimensions \(\sigma H\times\sigma W\times C\). Thus, compared to the original nearest-neighbor interpolation upsampling, CARAFE upsampling preserves the integrity of image features, enlarges the feature's receptive field, and enhances the feature pyramid's feature extraction and fusion capabilities.