-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
203 lines (173 loc) · 7.71 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import math
import time
import argparse
import datetime
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from tqdm import tqdm
from nets.retinaface import RetinaFace
from nets.retinaface_training import (DataGenerator, MultiBoxLoss,
detection_collate)
from utils.anchors import Anchors
from utils.config import cfg_mnet, cfg_re50
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def fit_one_epoch(model, net, criterion, optimizer, epoch, epoch_size, gen, Epoch, anchors, cfg, cuda):
total_r_loss = 0
total_c_loss = 0
total_landmark_loss = 0
with tqdm(total=epoch_size, desc=f'Epoch {epoch + 1}/{Epoch}', postfix=dict, mininterval=0.3) as pbar:
for iteration, batch in enumerate(gen):
if iteration >= epoch_size:
break
images, targets = batch[0], batch[1]
if len(images) == 0:
continue
with torch.no_grad():
if cuda:
images = Variable(torch.from_numpy(images).type(torch.FloatTensor)).cuda()
targets = [Variable(torch.from_numpy(ann).type(torch.FloatTensor)).cuda() for ann in targets]
else:
images = Variable(torch.from_numpy(images).type(torch.FloatTensor))
targets = [Variable(torch.from_numpy(ann).type(torch.FloatTensor)) for ann in targets]
#----------------------#
# 清零梯度
#----------------------#
optimizer.zero_grad()
#----------------------#
# 前向传播
#----------------------#
out = net(images)
# ----------------------#
# 计算损失
# ----------------------#
r_loss, c_loss, landm_loss = criterion(out, anchors, targets)
loss = cfg['loc_weight'] * r_loss + c_loss + landm_loss
loss.backward()
optimizer.step()
total_c_loss += c_loss.item()
total_r_loss += cfg['loc_weight'] * r_loss.item()
total_landmark_loss += landm_loss.item()
pbar.set_postfix(**{'Conf Loss': total_c_loss / (iteration + 1),
'Regression Loss': total_r_loss / (iteration + 1),
'LandMark Loss': total_landmark_loss / (iteration + 1),
'lr': get_lr(optimizer)})
pbar.update(1)
print('Saving state, iter:', str(epoch + 1))
torch.save(model.state_dict(), 'logs/Epoch%d-Total_Loss%.4f.pth' % (
(epoch + 1), (total_c_loss + total_r_loss + total_landmark_loss) / (epoch_size + 1)))
return
if __name__ == '__main__':
num_classes = 2
#-------------------------------#
# 是否使用Cuda
# 没有GPU可以设置成False
#-------------------------------#
Cuda = True
#--------------------------------#
# 获得训练用的人脸标签与坐标
#--------------------------------#
training_dataset_path = './data/widerface/train/label.txt'
#-------------------------------#
# 主干特征提取网络的选择
# mobilenet或者resnet50
#-------------------------------#
backbone = "mobilenet"
#-------------------------------#
# 是否使用主干特征提取网络
# 的预训练权重
#-------------------------------#
pretrained = False
if backbone == 'mobilenet':
cfg = cfg_mnet
elif backbone == 'resnet50':
cfg = cfg_re50
else:
raise ValueError('Unsupported backbone - `{}`, Use mobilenet, resnet50.'.format(backbone))
img_dim = cfg['train_image_size']
#-------------------------------#
# 获得先验框anchors
#-------------------------------#
anchors = Anchors(cfg, image_size=(img_dim, img_dim)).get_anchors()
if Cuda:
anchors = anchors.cuda()
model = RetinaFace(cfg=cfg, pretrained=pretrained).train()
#-------------------------------------------#
# 权值文件的下载请看README
# 权值和主干特征提取网络一定要对应
#-------------------------------------------#
model_path = "model_data/Retinaface_mobilenet0.25.pth"
print('Loading weights into state dict...')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_dict = model.state_dict()
pretrained_dict = torch.load(model_path, map_location=device)
# for k, v in pretrained_dict.items():
# print(k)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
print('Finished!')
net = model
if Cuda:
net = torch.nn.DataParallel(model)
cudnn.benchmark = True
net = net.cuda()
criterion = MultiBoxLoss(num_classes, 0.35, 7, Cuda)
#------------------------------------------------------#
# 主干特征提取网络特征通用,冻结训练可以加快训练速度
# 也可以在训练初期防止权值被破坏。
# Init_Epoch为起始世代
# Freeze_Epoch为冻结训练的世代
# Epoch总训练世代
# 提示OOM或者显存不足请调小Batch_size
#------------------------------------------------------#
if True:
#--------------------------------------------#
# BATCH_SIZE不要太小,不然训练效果很差
#--------------------------------------------#
lr = 1e-3
Batch_size = 8
Init_Epoch = 0
Freeze_Epoch = 50
optimizer = optim.Adam(net.parameters(), lr, weight_decay=5e-4)
lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92)
train_dataset = DataGenerator(training_dataset_path, img_dim)
gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=detection_collate)
epoch_size = train_dataset.get_len()//Batch_size
#------------------------------------#
# 冻结一定部分训练
#------------------------------------#
for param in model.body.parameters():
param.requires_grad = False
for epoch in range(Init_Epoch, Freeze_Epoch):
fit_one_epoch(model, net, criterion, optimizer, epoch, epoch_size, gen, Freeze_Epoch, anchors, cfg, Cuda)
lr_scheduler.step()
if True:
#--------------------------------------------#
# BATCH_SIZE不要太小,不然训练效果很差
#--------------------------------------------#
lr = 1e-4
Batch_size = 8
Freeze_Epoch = 50
Unfreeze_Epoch = 100
optimizer = optim.Adam(net.parameters(), lr, weight_decay=5e-4)
lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.92)
train_dataset = DataGenerator(training_dataset_path, img_dim)
gen = DataLoader(train_dataset, shuffle=True, batch_size=Batch_size, num_workers=4, pin_memory=True,
drop_last=True, collate_fn=detection_collate)
epoch_size = train_dataset.get_len() // Batch_size
# ------------------------------------#
# 解冻后训练
# ------------------------------------#
for param in model.body.parameters():
param.requires_grad = True
for epoch in range(Freeze_Epoch, Unfreeze_Epoch):
fit_one_epoch(model, net, criterion, optimizer, epoch, epoch_size, gen, Unfreeze_Epoch, anchors, cfg, Cuda)
lr_scheduler.step()