This project involves building a Convolutional Neural Network (CNN) to classify images as either showing a person wearing a mask or not wearing a mask. It was developed using Python and various machine learning and image processing libraries. Below is a breakdown of the project:
- Data Source: The dataset is downloaded from Kaggle, containing images of people with and without masks.
- Data Organization: The dataset is split into two main folders:
with_maskandwithout_mask, containing labeled images.
- Loading and Resizing Images: Images are loaded using
PILand resized to 128x128 pixels. - Labeling: Images from the
with_maskfolder are labeled1, and images from thewithout_maskfolder are labeled0. - Normalization: Image data is normalized by dividing pixel values by 255 to scale them between 0 and 1.
- The dataset is split into training and testing sets using
train_test_splitfromsklearnwith an 80-20 ratio.
- Layers:
Conv2DandMaxPooling2Dfor feature extraction.Flattenfor converting 2D data to 1D.Denselayers withReLUactivation andDropoutfor regularization.- Final
Denselayer withsigmoidactivation for binary classification.
- Compilation:
- Optimizer:
Adam - Loss function:
sparse_categorical_crossentropy - Metrics:
accuracy
- Optimizer:
- The model is trained for 5 epochs with an 80-20 validation split.
- Training metrics (accuracy and loss) are tracked and displayed.
- The model achieves an accuracy of ~90.5% on the test data.
- Plotting: Training and validation loss/accuracy are plotted to visualize model performance.
- Users can input an image path for prediction.
- The input image is read using
cv2, resized, normalized, and reshaped before being passed to the model for prediction. - The model outputs whether the person in the image is wearing a mask or not.
numpy,matplotlib,PIL,cv2,tensorflow,sklearn
- Training accuracy: ~89.2%
- Validation accuracy: ~91.8%
- Test accuracy: ~90.5%
- Training and validation loss/accuracy curves are plotted for better insights.
This project serves as a practical implementation for learning about CNNs in computer vision and image classification tasks.