# Sustainable Parking Space Management Using Machine Learning and Swarm Theory--The SPARK System
## 3 Methodology Concept
### Applied Algorithms and Technologies in This Study
#### 3.3.3 The You Only Look Once (YOLO) Algorithm for Object Segmentation in Images  
In this study, a pre-trained YOLO v9e segmentation model was fine-tuned to perform precise segmentation of a single object class: cars. By leveraging this pre-trained model, training time and computational resources were minimized while enhancing segmentation accuracy for the specific class. A custom dataset comprising images prominently featuring cars was assembled. Figure 4: Map of the study area, showing the extent of parking coverage captured by the cameras and the reference points used for transformations. (Source: Author’s own elaboration). The dataset included 1129 images with pixel-level annotations for the car class. Images were sourced from Ermak [82], ensuring diversity in backgrounds, lighting conditions, and perspectives, supplemented with custom car image registration originating from the study area. To align the images with the model's input requirements, images were resized to a resolution of 640 \(\times\) 640 pixels and normalized. Normalization involved scaling pixel values from the range of 0-255 to 0-1 by dividing each value by 255. Additionally, for each color channel (Red, Green, and Blue), we calculated the mean and standard deviation across the entire training dataset; each pixel value then had the corresponding channel mean subtracted and was divided by the channel's standard deviation. This standardization ensured that each channel had a mean of zero and a standard deviation of one, facilitating model convergence during training. Data augmentation techniques were applied to enhance model robustness and prevent overfitting. Horizontal flipping reflected images to simulate different orientations, random cropping introduced variability in object positioning, color jittering adjusted brightness and contrast to mimic different lighting conditions, and Gaussian noise was added to improve resilience to image imperfections. Each original image underwent four augmentations, with each augmentation randomly selected from the aforementioned techniques, resulting in an expanded dataset of 4516 images. The augmented dataset was split into training, validation, and test sets with a ratio of 70:15:15. This study examined the adaptation of the YOLO v9e architecture for advanced segmentation of the \"car\" class. The YOLOv9-seg e-model was pre-trained on the MS COCO (Microsoft Common Objects in Context) dataset, which includes 80 object classes, including \"car\". By restructuring the neural network's final layers, it was possible to generate precise segmentation masks exclusively for cars, allowing their isolation while maintaining high prediction accuracy. To tailor the model for exclusive vehicle detection, transfer learning was employed by freezing the weights of the backbone layers responsible for extracting general visual features (such as points, edges, and textures). This allowed the training process to focus on the final network layers, which were adjusted to the specific task of vehicle detection. The output layer was modified to reduce the number of classification neurons to a single class, \"vehicle\". Binary Cross-Entropy was used as the loss function, enabling effective training in the context of binary classification: \"vehicle\" vs. \"non-vehicle\". To maximize accuracy and efficiency, the segmentation mask decoding module underwent extensive optimization. Dynamic learning rate adjustment was employed using the StepLR schedule, reducing the rate by a factor of 0.1 every 20 epochs. Additionally, an early stopping mechanism was implemented to prevent overfitting, halting training if no significant improvement in validation loss occurred over five consecutive epochs. Training was conducted using the PyTorch framework on an NVIDIA 3090 GPU (Graphics Processing Unit) with 24 GB of memory and finished after 87 epochs, based on the analysis of loss function values for both the training and validation datasets (Figure 5).