## 3 Proposed Algorithm
### Variational Autoencoders (VAE)  
The VAE is a NN designed to extract spatial feature information of a visible gun from ROI, denoted by \(R\) extracted from both the hands. Its network architecture resembles that of a standard autoencoder (AE) [48], but with a key difference: the encoder (E) in a VAE enforces the latent representation \(z\) to follow a predefined prior probability distribution \(p(z)\) (e.g., \(\mathcal{N}(0,1)\)) (see Figure 7). The decoder (G) then reconstructs realistic data using latent codes sampled from \(p(z)\). In the proposed approach, the VAE is trained using normal data samples. When anomalous patches containing visible gun features extracted from camera are processed, they cause deviations in the latent variables during gradient backpropagation and attention generation. These deviations act as indicators for detecting anomalies associated with the presence of a visible gun. For a datapoint \(R\in D^{d}\), the E and G conditional distributions are denoted as \(q_{\phi}(z|R)\) and \(p_{\theta}(R|z)\), respectively. Here \(d\) is the dimension of the \(R\) extracted from the hands, and \(\theta\) and \(\phi\) are the hidden parameters of E and G, respectively. Since the data distribution \(p_{\theta}(R)\) is intractable through analytical methods, variational inference techniques are employed to optimize the objective function, maximum likelihood estimation of \(\log p_{\theta}(R)\):  
\[ \begin{split}\mathcal{L}(R)&=\log p_{\theta}(R)- KL\Big{[}q_{\phi}(z|R)\parallel p_{\theta}(z|R)\Big{]}\\ &=E_{q_{\phi}(z|R)}\Big{[}\log p_{\theta}(R)+\log p_{\theta}(z|R )-\log q_{\phi}(z|R)\Big{]}\\ &=-KL\Big{(}q_{\phi}(z|R)\|p_{\theta}(z)\Big{)}+E_{q_{\phi}(z|R )}\Big{[}\log p_{\theta}(R|z)\Big{]}\end{split}  \tag{5}\]  
Figure 7: An illustration of the VAE architecture. In the proposed approach, \(R\) represents the flattened input extracted from a 32 \(\times\) 32 patch within the video frame, \(\vec{v}\) denotes the encoded feature vector, and \(\vec{R}^{\prime}\) corresponds to the reconstructed patch generated by the VAE. where \(KL\) denotes the Kullback-Leibler divergence (KLD), which is a metric to quantify similarity between \(q_{\varphi}(z|R)\) and \(p_{\theta}(R|z)\). To optimize the objective function \(\mathcal{L}(R)\), VAE maximizes the evidence variational lower bound (ELBO). To optimize the second term in Equation (5), VAE minimizes the reconstruction errors between input and output using mean squared error (MSE) between the inputs and their reconstructions \(\mathcal{L}_{MSE}(R,R_{r})=\|R-R_{r}\|\). Therefore, the objective function for VAE in Equation (5) can be rewritten as follows:  
\[ \begin{split}\mathcal{L}_{VAE}&=\mathcal{L}_{ MSE}\Big{(}R,G_{\theta}(z)\Big{)}+\lambda\mathcal{L}_{KL}\Big{(}E_{\phi}(R) \Big{)}\\ &=\mathcal{L}_{MSE}(R,R_{r})+\lambda\mathcal{L}_{KL}(\mu,\sigma) \end{split}  \tag{6}\]  
where \(\lambda\) is the scaling hyperparameter to hold the tradeoff between each KLD target term. In Equation (6), \(\mu\) and \(\sigma\) represent the mean and standard deviation of the Gaussian distribution \(q_{\phi}(z|R)\), which defines the parameter vectors estimated by the \(E\) to optimize the KLD:  
\[ \begin{split}\mathcal{L}_{KL}(\mu,\sigma)&=KL \Big{(}q_{\varphi}(z|R)\parallel p_{\theta}(z)\Big{)}\\ &=KL\Big{(}\mathcal{N}\Big{(}z;\mu,\sigma^{2}\Big{)}\parallel \mathcal{N}(z;0,1)\Big{)}\\ &=\int\mathcal{N}\Big{(}z;\mu,\sigma^{2}\Big{)}\text{log}\,\frac{ \mathcal{N}\Big{(}z;\mu,\sigma^{2}\Big{)}}{\mathcal{N}\Big{(}z;0,1\Big{)}}\,dz \\ &=\frac{1}{2}\Big{(}1+\text{log}(\sigma^{2})-\mu^{2}-\sigma^{2} \Big{)}\end{split}  \tag{7}\]  
In the proposed firearm detection approach, a VAE is preferred over a AE because it leverages the reconstruction probability to sample \(z\) from the prior \(p_{\theta}(z)\) for \(L\) iterations. Two fully connected NN are utilized for both the E and D, respectively. During the training of the VAE for feature extraction, patches of variable sizes are resized to a fixed size of \(32\times 32\).