游戏公司官方网站模版,网页传奇私,微信小程序开发教程书籍,网站域名备案代理概述
基于深度学习的人脸表情识别#xff0c;数据集采用公开数据集fer2013#xff0c;可直接运行#xff0c;效果良好#xff0c;可根据需求修改训练代码#xff0c;自己训练模型。
详细 一、概述
本项目以PyTorch为框架#xff0c;搭建卷积神经网络模型#xff0c;训…概述
基于深度学习的人脸表情识别数据集采用公开数据集fer2013可直接运行效果良好可根据需求修改训练代码自己训练模型。
详细 一、概述
本项目以PyTorch为框架搭建卷积神经网络模型训练后可直接调用py文件进行人脸检测与表情识别默认开启摄像头实时检测识别。效果良好可根据个人需求加以修改。
二、演示效果 三、实现过程
1. 搭建网络
def __init__(self):super(FaceCNN, self).__init__()# 第一次卷积、池化self.conv1 nn.Sequential(nn.Conv2d(in_channels1, out_channels64, kernel_size3, stride1, padding1), # 卷积层# BatchNorm2d进行数据的归一化处理这使得数据在进行Relu之前不会因为数据过大而导致网络性能的不稳定nn.BatchNorm2d(num_features64), # 归一化nn.RReLU(inplaceTrue), # 激活函数nn.MaxPool2d(kernel_size2, stride2), # 最大值池化)# 第二次卷积、池化self.conv2 nn.Sequential(nn.Conv2d(in_channels64, out_channels128, kernel_size3, stride1, padding1),nn.BatchNorm2d(num_features128),nn.RReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),)# 第三次卷积、池化self.conv3 nn.Sequential(nn.Conv2d(in_channels128, out_channels256, kernel_size3, stride1, padding1),nn.BatchNorm2d(num_features256),nn.RReLU(inplaceTrue),nn.MaxPool2d(kernel_size2, stride2),)# 参数初始化self.conv1.apply(gaussian_weights_init)self.conv2.apply(gaussian_weights_init)self.conv3.apply(gaussian_weights_init)# 全连接层self.fc nn.Sequential(nn.Dropout(p0.2),nn.Linear(in_features256 * 6 * 6, out_features4096),nn.RReLU(inplaceTrue),nn.Dropout(p0.5),nn.Linear(in_features4096, out_features1024),nn.RReLU(inplaceTrue),nn.Linear(in_features1024, out_features256),nn.RReLU(inplaceTrue),nn.Linear(in_features256, out_features7),)
2. 训练模型
# 载入数据并分割batch
train_loader data.DataLoader(train_dataset, batch_size)
# 损失函数
loss_function nn.CrossEntropyLoss()
# 学习率衰减
# scheduler optim.lr_scheduler.StepLR(optimizer, step_size10, gamma0.8)
device cuda if torch.cuda.is_available() else cpu
# 构建模型
model FaceCNN().to(device)
# 优化器
optimizer optim.SGD(model.parameters(), lrlearning_rate, weight_decaywt_decay)
# 逐轮训练
for epoch in range(epochs):if (epoch 1) % 10 0:learning_rate learning_rate * 0.1# 记录损失值loss_rate 0# scheduler.step() # 学习率衰减model.train() # 模型训练for images, labels in train_loader:images, labels images.to(device), labels.to(device)# 梯度清零optimizer.zero_grad()# 前向传播output model.forward(images)# 误差计算loss_rate loss_function(output, labels)# 误差的反向传播loss_rate.backward()# 更新参数optimizer.step()
3. 模型预测
with torch.no_grad():pred model(face)probability torch.nn.functional.softmax(pred, dim1)probability np.round(probability.cpu().detach().numpy(), 3)max_prob np.max(probability)# print(max_prob)predicted classes[torch.argmax(pred[0])]cv2.putText(img, predicted str(max_prob), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 255, 0), 1, cv2.LINE_AA)
cv2.imshow(frame, img)