# Meta-Learning Approach for Adaptive Anomaly Detection from Multi-Scenario Video Surveillance
## 3 Methodology
### Anomaly Detector Model
#### 3.4.1 Meta Training on Dataset  
The meta training process in meta-learning consists of several key steps designed to help a model learn how to quickly adapt to new tasks (image embeddings) with limited data. A meta-learning approach is illustrated in Figure 4.  
Task Sampling: The sample task method was designed to generate a dataset sample for an anomaly detection task. Here, a random scenario from a predefined list was selected. Within that scenario, it identified normal and anomalous images by cross-referencing scenario-specific indices with label-specific indices. Once these images were identified, it randomly selected a fixed number (k_shot + k_query) of normal and anomalous samples. These selected images were then split into two sets: the support set, which consisted of k_shot normal and k_shot anomalous images for training, and the query set, which contained k_query normal and k_query aroma-lous images for evaluation. Each image was assigned to its respective label, either normal or anomalous. To avoid bias, the method shuffled both the support and query sets before returning them. Then, the function ultimately ensured a balanced, struc-tured dataset that can be used in meta-learning tasks, particularly for detecting anomalies within specific scenarios. Inner Loop: The method began by creating a temporary copy of the model to ensure that updates do not affect the original model. The copied model was then placed in training mode, and a Binary Cross Entropy with Logits Loss function was defined for classification. Then, a series of inner-loop updates was performed over self-inner steps iterations. In each iteration, it made predictions using the support set, calculat-ed the loss, and computed gradients with respect to the model's parameters. Instead of using an optimizer, it manually updated each parameter using stochastic gradient descent (SGD) by subtracting the gradient scaled by the inner learning rate. This adapted model, then fine-tuned to the support set, was returned. The purpose of this method was to allow a model to quickly adapt to a new task using only a few examples. This approach maintained the computational graph so that the outer update can backpropagate through the inner adaptation steps. in-tialized the loss function, optimizer, and storage variables for query labels and pre-dictions. Then, it iterated over tasks, moving data to the appropriate device and per-forming an inner-loop update, where the model adapted to the task using the support set. After adaptation, the model made predictions on the query set, and the binary cross-entropy loss was computed. The predicted probabilities and true query labels were stored for later analysis. The total meta-loss was averaged across tasks, and outer-loop optimization was performed by backpropagating the loss. Finally, the main model's weights were updated using the adapted model, and the function re-turned the final loss along with stored query labels and predictions. This process helped the model learn a generalizable initialization that can quickly adapt to new tasks with minimal updates. * Meta Validation: A meta-learning model was then evaluated by iterating over a set of tasks, each containing a support set for fine-tuning and a query set for evaluation. It first initialized the total loss and prepared storage for true labels and predictions. As it looped through tasks, it moved data to the appropriate device (CPU/GPU) and fi-ne-tuned a copy of the model on the support set using an inner-loop update function (inner update). After adaptation, it made predictions on the query set, applied a sig-moid activation function to convert logits into probabilities, and stored both true and predicted labels. Then, the function computed the binary cross-entropy loss for each task, accumulated the losses, and then averaged them over all tasks. Finally, it in-voked an early stopping mechanism, which can determine if the model should be saved or training should halt based on the meta-loss trend.