# Improving Moving Insect Detection with Difference of Features Maps in YOLO Architecture
## 2 Materials and Methods
### Method
After completing this initial training phase, we transform the model into a Siamese architecture, that is, the model consists of two identical branches with shared weights that process two input frames in parallel -the main frame and the previous frame, as illustrated in Figure 7. Specifically, the inputs are the image at time \(t\) and the image at time \(t-1\), denoted as \(I_{t}\) and \(I_{t-1}\), respectively, in Figure 7.  
In Figure 7, each block in the network backbone before SPPF block is labeled as \(B_{i}\) where \(1\leq i\leq 9\). The motion module can be inserted after any of these blocks. In the example shown, it is placed after block \(B_{5}\). Both input images, \(I_{t}\) and \(I_{t-1}\) are processed in parallel through identical network branches up to block \(B_{n}\), where \(n\) is the block preceding the placement of the motion module. At this point, the motion module integrates the motion information derived from the two branches into the feature map corresponding to \(I_{t}\), effectively ending the Siamese structure. From this point onward, the original YOLOv8 pipeline resumes, using the enhanced features as input. If the neck of the network receives input from a block preceding the motion module-i.e., a stage still within the Siamese structure-it only utilizes the output from the branch processing \(I_{t}\), ignoring the features from the \(I_{t-1}\) branch. This ensures consistency with the original single-frame processing design of YOLOv8. All blocks in the trained YOLOv8 model, except for the motion module, are frozen during the second training phase. As illustrated in Figure 7, the blocks highlighted in blue indicate layers whose weights remain unchanged throughout this phase. Only parameters of motion module are updated, since its weights are initialized randomly. Freezing the rest of the network prevents the large gradients-expected during the initial updates of the motion module-from adversely affecting the pretrained weights, thereby preserving the knowledge previously learned by the base YOLOv8 model. Therefore, during the second training phase, we train the model for additional 100 epochs, updating only the parameters of the motion module. This selective fine-tuning allows the module to effectively learn and encode motion information, while preserving the knowledge already acquired by the rest of the network. In the following section, we compare the results obtained on the modified dataset using our method with those obtained by the method presented in [29]. To provide context for this comparison, we first offer a brief explanation of the referenced method. In [29], the authors incorporate a preprocessing step that enhances images with motion information prior to feeding them into the neural network. To generate a motion-enhanced image, three consecutive frames are used. These RGB frames are first converted to grayscale and then blurred using a Gaussian kernel of size \(5\times 5\) pixels. The resulting grayscale and blurred frame at time index \(k\in\mathbb{N}\) is denoted as \(B_{k}\). To enhance frame \(k\), the adjacent frames \(k-1\) and \(k+1\) are also considered. Two frame differences are then computed: the difference between \(B_{k}\) and \(B_{k-1}\), denoted as \(\Delta B_{k}[i,j]\), and the difference between \(B_{k+1}\) and  
Figure 6: Motion module. \(B_{k}\), denoted as \(\Delta B_{k+1}[i,j]\), where \([i,j]\in[1...N]\times[1...M]\) are the pixel coordinates. These computations are formalized in Equations (2) and (3), respectively. \[\Delta B_{k}[i,j]=B_{k}[i,j]-B_{k-1}[i,j] \tag{2}\]  
\[\Delta B_{k+1}[i,j]=B_{k+1}[i,j]-B_{k}[i,j] \tag{3}\]  
Then, using this information, a motion likelihood, \(L_{k}^{3}[i,j]\), is created by summing the absolute values of \(\Delta B_{k}[i,j]\) and \(\Delta B_{k+1}[i,j]\), as shown in Equation (4). Figure 6.0  
Next, the RGB channels of the original color image at time \(k\) are modified to generate the motion enhanced image, denoted as \(M\).