Face Mask Wearing Detection Based on YOLOv5

Yunshan Xie, Zhi‐Yi Hu, Jun YuInternational Journal of Advanced Network Monitoring and Controls 2022 · DOI

TL;DR

This paper presents a real-time face mask detection system leveraging the YOLOv5s architecture. By integrating mosaic data enhancement, CIOU loss, and weighted Non-Maximum Suppression (NMS), the authors achieved a mean Average Precision (mAP) of 93.5%, significantly outperforming previous YOLOv3 and YOLOv4 iterations in both accuracy and speed.

What Problem Does This Paper Address?

In the wake of the COVID-19 pandemic, monitoring mask-wearing in crowded public spaces became a critical public health requirement. However, manual monitoring is notoriously "time-consuming and laborious," and it puts the monitors themselves at risk of infection.

The challenge for computer vision researchers is to create a system that is not only accurate but also fast enough to process live camera feeds in real-time. While previous object detection models like YOLOv3 and YOLOv4 provided a foundation, they often struggled with the trade-off between detection speed and the ability to identify small objects (like faces in a distant crowd) or correctly handle overlapping bounding boxes in dense environments.

Methods

The authors opted for the YOLOv5s (small) variant, prioritizing inference speed for real-world deployment. Their implementation introduces several key technical refinements:

  • Mosaic Data Enhancement: At the input layer, the model splices four training images into one. This "mosaic" approach forces the network to identify objects at different scales and enriches the background, which is particularly effective for detecting small targets.
  • Adaptive Image Scaling: To avoid the computational waste of "black edges" often found in standard letterboxing, the authors modified the scaling function to adaptively reduce redundancy, thereby increasing processing speed.
  • Backbone & Neck Architecture: The model utilizes a Focus structure (which uses slicing operations to reduce FLOPs) and CSP (Cross Stage Partial) structures to enhance gradient flow. The "neck" of the model uses a combination of FPN (Feature Pyramid Network) and PAN (Path Aggregation Network) to fuse low-level spatial features with high-level semantic features.
  • CIOU Loss & Weighted NMS: Instead of standard IOU (Intersection over Union), the authors used CIOU (Complete IOU) loss, which accounts for the overlap area, center distance, and aspect ratio of bounding boxes. Furthermore, they replaced standard NMS with Weighted NMS, which helps the model better manage adjacent targets by weighting confidence scores rather than simply deleting overlapping boxes.

Datasets

The study utilized a dataset of 8,000 preprocessed images categorized into two classes: "mask" and "no mask." * Training Set: 80% (6,400 images) * Test Set: 20% (1,600 images) The images were normalized and preprocessed before being fed into the PyTorch-based training pipeline for 200 epochs.

Key Insights and Ideas

  • Efficiency is King: The choice of YOLOv5s over larger variants (m, l, x) highlights a pragmatic engineering choice: in public safety monitoring, a slight dip in absolute accuracy is a worthy trade-off for the high frame rates required for live video.
  • Solving the "Crowd" Problem: By using Weighted NMS and Mosaic enhancement, the authors specifically targeted the "crowded public place" scenario where people often overlap or appear very small in the frame.
  • Refined Bounding Box Logic: The transition to CIOU loss suggests that simply looking at overlap (IOU) isn't enough for precise mask detection; the geometry (aspect ratio) of the face matters significantly for stable tracking.

Key Results

The YOLOv5s model showed a dramatic improvement over its predecessors on the test set:

Model Precision Recall [email protected]
YOLOv5s 94.8% 89.0% 93.5%
YOLOv4 76.2% 85.4% 51.2%
YOLOv3 73.6% 82.3% 48.9%

The authors noted that the YOLOv5 architecture is roughly 90% smaller than YOLOv4, making it much more suitable for deployment on edge devices or standard surveillance hardware.

Limitations and Future Work

While the system is highly effective at binary classification (mask vs. no mask), the authors acknowledge several areas for improvement:

  1. Non-standard Wearing: The current model may struggle to distinguish between a mask worn correctly and one pulled down below the nose or chin.
  2. Model Lightening: Further optimization is needed to "lighten" the model even more for low-power embedded systems.
  3. Real-world Verification: The authors aim to further verify and improve the network's performance in diverse, unpredictable practical applications beyond the initial dataset.

Summary updated

Abstract

Abstract In recent years, COVID-19 has swept the world, and people in crowded public places are usually large. In order to reduce the risk of virus transmission, stop the spread of the epidemic and reduce cross-infection, wearing masks correctly has become an important measure to prevent the virus. Aiming at the time-consuming and laborious situation of wearing masks manually, this paper proposes a mask wearing detection method based on yolov5. The input layer is mainly used for mosaic data enhancement, that is, adaptive anchor box and adaptive image scaling technology; Yolov5 in backbone mainly adopts focus and CSP (cross stage partial) structure; The neck layer adopts spp (spatial pyramid pooling) module and FPN (feature pyramid networks) + pan (pixel aggregation network) structure; The output mainly adopts ciou for the bounding box loss function_Loss is the average index of NMS (non maximum suppression). This method uses 8000 preprocessed images as the data set and trains 200 epochs to get the final model. The algorithm visually displays the training and test results through tensor board, and inputs the pictures captured by the camera into the model to detect whether the face wears a mask. The accuracy, recall and mean accuracy (map) of the algorithm on the test set are 94.8%, 89.0% and 93.5% respectively, which are higher than the detection results of yolov3 and yolov4 algorithms.
BibTeX
@article{xie2022face,
  author = {Yunshan Xie and Zhi‐Yi Hu and Jun Yu},
  title = {{Face Mask Wearing Detection Based on YOLOv5}},
  journal = {International Journal of Advanced Network Monitoring and Controls},
  year = {2022},
  doi = {10.2478/ijanmc-2022-0017},
}
Figure