# A New Hybrid ConvViT Model for Dangerous Farm Insect Detection
## 3 Materials and Methods
### Proposed Hybrid ConvViT Model  
Artificial intelligence technologies' innovations in image processing come to the forefront in automatically detecting agricultural pests. It is essential for sustainable agriculture and ecology that artificial intelligence methods prevent using traditional pesticides. Due to their ability to process large-scale and complex datasets and detect complex patterns, deep learning models enable the capture of distinct morphological features such as wing patterns, body shapes, and antenna structures that distinguish agricultural pest species from each other. However, the shooting angle of the image and the objects in the background are a challenge in image classification problems. ViTs, which have self-attention mechanisms, efficiently analyze the image's dependencies and relationships between regions. CNNs are efficient in extracting local features. Since the ConvViT model created using a CNN and a ViT will have the features of both extracting local features and capturing dependencies and relationships between regions in the images, it strengthens the contextual understanding and increases the classification accuracy. The model selections made in the study are mainly based on efficiency and computational cost, generalization ability, and proven success criteria. A ViT is a suitable option, especially in systems operating with limited resources, due to offering high accuracy and low computational cost. A ViT was preferred due to its superior success in modeling the global context and various tasks. The CNN and ViT models have been widely tested in the literature and have shown high performance. Alternative models were also evaluated in the initial phase of our study. CNN alternatives such as ResNet, DenseNet, and MobileNet were also analyzed. Vision transformer derivatives such as the Swin transformer and DeiT were also examined, but the tests conducted with the basic ViT model provided sufficient performance for the target application scenarios. In the combination of the models used in our study, the aim was to effectively blend local and global features. In particular, the local features learned by the CNN were combined in a way that was compatible with the global context modeling capability of the ViT. The study extracted the dataset's image files and class labels during pre-processing. Since there was an imbalance between the classes in the dataset, random oversampling was applied to balance the classes. After the class-balancing process, 80% of the dataset was split for training and 20% for testing. A total of 10% of the training dataset was split to determine the model hyper-parameters. The hyper-parameters of all compared models were determined using a grid search. Table 2 shows the optimized hyper-parameters for each model. Data augmentation techniques such as translation, rotation, and brightness adjustments were applied. Resizing, random rotations, sharpness adjustments, tensor transformation, and normalization were performed on the dataset. The model's output layer was set for multi-class classification according to the number of labels. For model evaluation, argmax was applied to calculate the accuracy and select the highest probability class for predictions. Data were prepared for model training by stacking pixel values and converting labels to tensors. This study created the ConvViT hybrid model and compared it with EfficientNetB0, DenseNet201, ResNet-50, VGG-16, and the ViT model. ConvViT was created to take advantage of the influential and prominent features of a CNN and a ViT. The CNN was used to extract local patterns and structural features, while the ViT was used to extract global context. ConvViT was designed to extract specific details and spatial relationships to classify insect species with similar physical appearance accurately. The research model employs hybrid ConvViT architecture by unifying CNN and ViT features. The EfficientNet-B0 architecture is this study's building block for convolutional neural networks.