# Lightweight Anomaly Detection in Digit Recognition Using Federated Learning
## 4 Autoencoder Deployment on ESP32-CAM
### Model Architecture and Training Strategy  
We implemented and evaluated seven small-scale fully connected AE architectures suitable for embedded deployment. Each model consists of an encoder and decoder surrounding one or more hidden layers that encode a latent representation of the input. The input consists of \(14\times 14\) grayscale images flattened to 196 features. The encoder compresses this input to a lower-dimensional latent space, and the decoder reconstructs it back to the original input size. All models use ReLU as the activation function, selected for its simplicity and efficiency, and the Adam optimizer with a learning rate of 0.001, which is commonly used for autoencoders due to its fast convergence. We evaluated multiple models with varying numbers of hidden layers and neurons to determine the best balance between model complexity, reconstruction performance, and resource constraints. The model architectures are as follows:  
* Model 1: Single hidden layer with 16 neurons--architecture: [196, 16, 196]. * Model 2: Single hidden layer with 32 neurons--architecture: [196, 32, 196]. * Model 3: Single hidden layer with 64 neurons--architecture: [196, 64, 196]. * Model 4: Two hidden layers with 32 neurons each--architecture: [196, 32, 32, 196]. * Model 5: Three hidden layers with a narrow bottleneck of 8 neurons--architecture: [196, 32, 8, 32, 196]. * Model 6: Three hidden layers with a narrow bottleneck of 16 neurons--architecture: [196, 64, 16, 64, 196]. * Model 7: Five hidden layers with 32-neuron bottlenecks and wider 64-neuron outer layers--architecture: [196, 64, 32, 32, 64, 196]. In on-device training scenarios, where data is collected locally and its characteristics are unknown beforehand, it is challenging to predefine the optimal number of training epochs. Combined with the strict power and time constraints of edge devices, this requires mechanisms to avoid unnecessary computation. To address this, we implemented a dual-phase early stopping strategy, inspired by TensorFlow's EarlyStopping mechanism [50], which monitors training or validation loss and stops training when no significant improvement (defined by _min delta_) is observed over a specified number of epochs (_patience_). Our approach extends this concept by using two different _patience_ values for two training phases. Initially, in the first phase, the model is allowed to train with a higher patience value (_patience phase_1) until a predefined _acceptable loss_ threshold is reached. After this point, a lower patience value (_patience phase_2) is applied, allowing the training to terminate quickly when further improvements are minimal (smaller than _min delta_). This mechanism is particularly suitable for on-device training scenarios, where the optimal number of epochs cannot be known in advance and both training time and energy consumption must be minimized. Unlike typical early stopping strategies that save and restore the best model after training--often requiring the storage for multiple checkpoints--our approach is optimized for memory-constrained embedded devices. Since saving earlier model states is impractical on such platforms, the second phase uses a very low patience value (typically below 5) combined with an appropriate _min delta_ threshold. This setup ensures that, once the loss reaches an acceptable level in the first phase and stops improving significantly in the second phase, training is halted immediately. This prevents potential degradation that could occur if training continued through a plateau or local minimum. As a result, the final model is effectively \"frozen\" at a satisfactory point, without risking performance loss or requiring rollback, making the process suitable for training on embedded devices. AD was performed by measuring the reconstruction error using mean squared error (MSE) between the input and the reconstructed output image as the MSE provides a standard and effective metric for quantifying reconstruction discrepancies in image data.