Target
将CBAM加入到Face embedding model训练出一个可识别戴口罩人脸的人脸识别模型。
综合来说,就是在week06作业的基础上,在训练集中添加戴口罩的数据集。
Steps
数据集
训练数据集选择:VGGFACE2 的训练集 或者 VGGFACE2的测试集
- 下载完成vggfrace2_test.zip解压缩,得到vggface2_train文件夹
- 由于人脸检测,对齐需要预先处理,所以我们运行week06/image_processing.py,对图片进行检测对齐加口罩处理
- 这个时间耗时会非常长,注意时间安排。
- 代码功能一:对齐后的图片保存到vggface2_train_notmask文件夹内
- 代码功能二:对齐后的图片加口罩,保存到vggface2_train_mask文件夹内
- 再运行make_csv_file.py,生成csv文件,为生成triplet做准备。
- 运行 train_dataset.py 检查数据集是否正常。
和上周一样的数据集,感觉是直接修改路径比较方便。到时候看下。
加注意力机制模组,希望训练过程中,focus on没有口罩遮挡的部分。所以应该是要在上周的训练模型的基础上,增加CBAM,再使用带口罩的训练集去fine tune。
为了熟悉工程 决定直接把整个工程仓库clone下来。
README
目标是针对戴口罩的数据集训练一个模型。查看readme,
要求包含CBAM模组,并且输入是戴口罩的人脸,满足这两点的只有V1 V2 V9,因为输入是已经处理完毕的图片,所以只能是第二个版本或者第九个版本。目前还不清楚所谓的不同版本的face_attention提取层具体是指什么,先暂定采用V2试一下。
目标:看懂并跑通train_maskV2.py
1. import包和文件
# 路径置顶
import sys
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
sys.path.append(os.getcwd())
# 导入包
from torch.nn.modules.distance import PairwiseDistance
import torch.nn as nn
from tqdm import tqdm
import numpy as np
import torch
import time
# 导入文件
# from Models.Model_for_facenet import model, optimizer_model, start_epoch, flag_train_multi_gpu
from Data_loader.Data_loader_facenet_mask import test_dataloader,V9_train_dataloader
from Data_loader.Data_loader_facenet_mask import LFWestMask_dataloader
from Losses.Triplet_loss import TripletLoss
from validate_on_LFW import evaluate_lfw
from Data_loader.Data_loader_train_notmask import TrainDataset
from config_mask import config
from Models.CBAM_Face_attention_Resnet_maskV2 import resnet18_cbam, resnet50_cbam, resnet101_cbam, resnet34_cbam, \
resnet152_cbam
2. GPU环境配置、模型选择
注意这里为了避免通过外网下载resnet预训练模型造成速度缓慢,将pretrained参数设置为False了。实际应用的时候下载预训练模型是必要的
没有’Model_training_checkpoints’文件夹,新建一个;
pwd = os.path.abspath('./')
print("Using {} model architecture.".format(config['model']))
start_epoch = 0
#
if config['model'] == 18:
model = resnet18_cbam(pretrained=True, showlayer= False,num_classes=128) # 设置为False
elif config['model'] == 34:
model = resnet34_cbam(pretrained=True, showlayer= False, num_classes=128)
elif config['model'] == 50:
model = resnet50_cbam(pretrained=True, showlayer= False, num_classes=128)
elif config['model'] == 101:
model = resnet101_cbam(pretrained=True, showlayer= False, num_classes=128)
elif config['model'] == 152:
model = resnet152_cbam(pretrained=True, showlayer= False, num_classes=128)
model_path = os.path.join(pwd, 'Model_training_checkpoints')
x = [int(i.split('_')[4]) for i in os.listdir(model_path) if 'V2' in i]
x.sort()
for i in os.listdir(model_path):
if (len(x)!=0) and ('epoch_'+str(x[-1]) in i) and ('V2' in i):
model_pathi = os.path.join(model_path, i)
break
if os.path.exists(model_pathi) and ('V2' in model_pathi):
# model_state = torch.load(model_pathi)
# model.load_state_dict(model_state['model_state_dict'])
# start_epoch = model_state['epoch']
# print('loaded %s' % model_pathi)
model_state = torch.load(model_pathi)
# model.load_state_dict(model_state['model_state_dict'])
start_epoch = model_state['epoch']
now_state_dict = model.state_dict()
state_dict = {
k: v for k, v in model_state.items() if (k in now_state_dict.keys()) and \
('fc.weight' not in now_state_dict.keys())}
now_state_dict.update(state_dict)
# now_state_dict.update(pretrained_state_dict)
model.load_state_dict(now_state_dict)
print('loaded %s' % model_pathi)
else:
print('不存在预训练模型!')
flag_train_gpu = torch.cuda.is_available()
flag_train_multi_gpu = False
if flag_train_gpu and torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.cuda()
flag_train_multi_gpu = True
print('Using multi-gpu training.')
elif flag_train_gpu and torch.cuda.device_count() == 1:
model.cuda()
print('Using single-gpu training.')
3. 学习率(不管)
def adjust_learning_rate(optimizer, epoch):
if epoch<19:
lr = 0.125
elif (epoch>=19) and (epoch<60):
lr = 0.0625
elif (epoch >= 60) and (epoch < 90):
lr = 0.0155
elif (epoch >= 90) and (epoch < 120):
lr = 0.003
elif (epoch>=120) and (epoch<160):
lr = 0.0001
else:
lr = 0.00006
for param_group in optimizer.param_groups:
param_group['lr'] = lr
4. 优化器(不管)
def create_optimizer(model, new_lr):
# setup optimizer
if config['optimizer'] == "sgd":
optimizer_model = torch.optim.SGD(model.parameters(), lr = new_lr,
momentum=0.9