Matlab simulation of personnel mask recognition algorithm based on Alexnet deep learning network

Table of contents

1. Preview of algorithm operation renderings

2.Algorithm running software version

3. Some core programs

4. Overview of algorithm theory

5. Algorithm complete program engineering


1. Preview of algorithm operation renderings

2.Algorithm running software version

matlab2022a

3. Some core programs

file_path1    =  'test\mask\';% 图像文件夹路径  

%获取测试图像文件夹下所有jpg格式的图像文件
img_path_list = dir(strcat(file_path1,'*.png'));
idx=0;%初始化索引
for i = 1:20%对每张测试图像进行预测并可视化
    idx           = idx+1; %索引+1
    II            = imread([file_path1,img_path_list(i).name]);%读取测试图像
    II            = imresize(II,[227 227]);%将测试图像大小缩放为预训练模型的输入大小
    Features      = activations(net,II,featureLayer,'OutputAs','rows'); %提取测试图像的特征
    II2           = predict(classifier,Features);%使用分类器对测试图像进行分类
    subplot(4,10,idx) %在第一行的左侧位置显示测试图像和分类结果
    disp(char(II2));%输出测试图像的分类结果
    imshow(II); %显示测试图像
    title(char(II2));%显示测试图像的分类结果
end


file_path1    =  'test\no mask\';% 图像文件夹路径  
img_path_list = dir(strcat(file_path1,'*.png'));%获取测试图像文件夹下所有jpg格式的图像文件
 
for i = 1:20%对每张测试图像进行预测并可视化
    idx           = idx+1;%索引+1
    II            = imread([file_path1,img_path_list(i).name]); %读取测试图像
    II            = imresize(II,[227 227]);%将测试图像大小缩放为预训练模型的输入大小
    Features      = activations(net,II,featureLayer,'OutputAs','rows');%提取测试图像的特征
    II2           = predict(classifier,Features); %使用分类器对测试图像进行分类
    subplot(4,10,idx)%在第一行的右侧位置显示测试图像和分类结果
    disp(char(II2)); %输出测试图像的分类结果
    imshow(II);%显示测试图像
    title(char(II2));%显示测试图像的分类结果
end
59    

4. Overview of algorithm theory

       The personnel mask recognition algorithm is an image classification problem based on deep learning. In this problem, we need to detect and identify whether a person is wearing a mask in an image. To solve this problem, we can use the AlexNet model, which is a deep learning network that is widely used in image recognition tasks.

AlexNet model

         AlexNet is a deep learning network consisting of two parts: a shared layer and a task-specific layer. The shared layer includes 5 convolutional layers (conv1 to conv5) and 3 fully connected layers (fc6, fc7, fc8). Task-specific layers include a softmax layer (fc8) for classification and fc6-fc7-fc8 layers for position regression. AlexNet uses ReLU as the activation function, dropout to prevent overfitting, and L2 regularization to enhance the generalization ability of the model.

Personnel Mask Recognition Algorithm

       We can apply the AlexNet model to the task of person mask recognition. First, we need to collect a dataset containing images of people wearing masks and not wearing masks. We then use the AlexNet model to train and test on the images.

         During the training phase, we feed the input images and corresponding labels (masked or not) into the AlexNet model. The output of the model is a probability value that represents the probability that the image is wearing a mask or not. We use a cross-entropy loss function and a stochastic gradient descent (SGD) optimizer to update model parameters to minimize the difference between predicted and actual values.

        In the testing phase, we feed the input image into the already trained AlexNet model and output the prediction results. If the prediction result is greater than a certain threshold, we determine it as wearing a mask; otherwise, we determine it as not wearing a mask.

The following are some core formulas of the AlexNet model:

  1. The output size calculation formula of the convolutional layer is: O=(I−F+2P)/S+1, where I is the input size, F is the convolution kernel size, P is the padding size, and S is the step size.
  2. The formula of ReLU activation function: f(x)=max(0,x).
  3. The formula of cross entropy loss function: L=−∑i=1N​yi​log(yi​^​)+(1−yi​)log(1−yi​^​), where N is the number of samples and y is the true label, y^​ is the predicted label.
  4. The formula of the SGD optimizer: θ=θ−η∇L(θ), where θ is the parameter, eta is the learning rate, and ∇L(θ) is the gradient of the loss function.

        Deep learning models work by learning the mapping relationship from input to output. In this process, the model will learn some useful feature representations, allowing it to better understand and predict the properties of the input data. In the person mask recognition task, the AlexNet model can accurately identify whether a person is wearing a mask by learning the mapping relationship from the image to the probability of wearing a mask or not wearing a mask.

5. Algorithm complete program engineering

OOOOO

OOO

O

Guess you like

Origin blog.csdn.net/aycd1234/article/details/132724593