## 4 Training and Evaluation of Threat-Detection AI Model
### Theoretical Background to Object-Detection Models  
#### 4.1.1 You Only Look Once Version 4 (YOLOv4)  
YOLOv4 is a state-of-the-art object-detection model that uses a deep convolutional neural network to detect and localize objects in an input image [26]. It belongs to the family of deep learning models that offers a good equilibrium between speed and accuracy. Indeed, it can offer real-time performance when deployed on systems with limited computational resources while still delivering top accuracy. The architecture of the YOLOv4 model is designed to be able to detect objects at different scales (i.e., small, medium, large) to compensate the object's perceived size in an image based on its distance to the camera. The YOLOv4 model consists of a backbone network, a neck network, and a head network. The backbone network is based on the CSPDarknet53 architecture, which uses a Cross-Stage Partial (CSP) network module to improve the flow of information across different layers of the network. The neck network uses a Spatial Pyramid Pooling (SPP) module to allow the model to detect objects of different sizes. The head network uses a YOLOv3-style detection head to predict bounding boxes and object classes.  
The YOLOv4 model works by processing an input image in three grids (one for each detection scale) through the backbone network, neck network, and head network to generate a set of bounding boxes and object classes. The bounding boxes are represented as four coordinates \((x,y,w,h)\), where \((x,y)\) is the center of the box, \(w\) is the width of the box, and \(h\) is the height of the box. The object classes are represented as a probability distribution over a set of predefined classes. The output of the YOLOv4 model can be represented as a set of bounding boxes and corresponding object classes (see Equation (1)).  
\[Y=(x_{i},y_{i},w_{i},h_{i},c_{i})_{i=1}^{N}, \tag{1}\]  
where \(N\) is the number of detected objects, \(x_{i}\), \(y_{i}\), \(w_{i}\), and \(h_{i}\) are the coordinates of the \(i\)-th bounding box, and \(c_{i}\) is the probability distribution over the set of predefined classes for the \(i\)-th object.  
The YOLOv4 model is trained using a combination of a classification loss and a localization loss. The classification loss measures the difference between the predicted class probabilities and the true class probabilities, while the localization loss measures the difference between the predicted bounding boxes and the ground truth bounding boxes. The total loss is a weighted sum of the classification loss and the localization loss (see Equation (2)).  
\[L=\lambda_{cls}L_{cls}+\lambda_{loc}L_{loc}, \tag{2}\]  
where \(L_{cls}\) is the classification loss, \(L_{loc}\) is the localization loss, and \(\lambda_{cls}\) and \(\lambda_{loc}\) are hyper-parameters that control the relative importance of the two losses.  
The localization loss is calculated using the mean-squared error (MSE) between the predicted bounding boxes and the ground truth bounding boxes (see Equation (3)).  
\[L_{loc}=\frac{1}{N_{obj}}\sum_{i=1}^{N_{obj}}\sum_{j\in x,y,w,h}\lambda_{j}\cdot \text{MSE}(y_{i}^{j},y_{i}^{j}), \tag{3}\]  
where \(N_{obj}\) is the number of objects in the image, \(y_{i}\) and \(\hat{y}_{i}\) are the ground truth and predicted values of the \(i\)-th bounding box, \(\lambda_{j}\) is a hyperparameter that controls the relative importance of the \(j\)-th coordinate, and \(\text{MSE}(a,b)=(a-b)^{2}\) is the mean-squared error between \(a\) and \(b\).  
To remove duplicate detections of the same object, the Non-Maximal Suppression (NMS) technique is applied. If there is an Intersection over Union (IoU) between two bounding boxes \(B_{m}\) and \(B_{n}\) greater than a predefined NMS threshold \(\gamma\), i.e., if \(\text{IoU}(B_{m},B_{n})>\gamma\), then the bounding box with the least object confidence score is removed.