-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_convnext.py
378 lines (303 loc) · 14.1 KB
/
train_convnext.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import argparse
import time
import numpy as np
from collections import OrderedDict
import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)
import torch
import torch.nn as nn
from torch.optim.lr_scheduler import ReduceLROnPlateau
from my_lib.network.rtpose_convnext import get_model
from my_lib.datasets import coco, transforms, datasets
from my_lib.config import update_config
DATA_DIR = 'J:/coco2017/'
checkpoints = 'checkpoints.txt'
ANNOTATIONS_TRAIN = [os.path.join(DATA_DIR, 'annotations', item) for item in ['person_keypoints_train2017.json']]
ANNOTATIONS_VAL = os.path.join(DATA_DIR, 'annotations', 'person_keypoints_val2017.json')
IMAGE_DIR_TRAIN = os.path.join(DATA_DIR, 'train2017')
IMAGE_DIR_VAL = os.path.join(DATA_DIR, 'val2017')
def train_cli(parser):
group = parser.add_argument_group('dataset and loader')
group.add_argument('--train-annotations', default=ANNOTATIONS_TRAIN)
group.add_argument('--train-image-dir', default=IMAGE_DIR_TRAIN)
group.add_argument('--val-annotations', default=ANNOTATIONS_VAL)
group.add_argument('--val-image-dir', default=IMAGE_DIR_VAL)
group.add_argument('--pre-n-images', default=8000, type=int,
help='number of images to sampe for pretraining')
group.add_argument('--n-images', default=None, type=int,
help='number of images to sample')
group.add_argument('--duplicate-data', default=None, type=int,
help='duplicate data')
group.add_argument('--loader-workers', default=4, type=int,
help='number of workers for data loading')
group.add_argument('--batch-size', default=8, type=int,
help='batch size')
group.add_argument('--lr', '--learning-rate', default=0.1, type=float,
metavar='LR', help='initial learning rate')
group.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
group.add_argument('--weight-decay', '--wd', default=0.000, type=float,
metavar='W', help='weight decay (default: 1e-4)')
group.add_argument('--nesterov', dest='nesterov', default=True, type=bool)
group.add_argument('--print_freq', default=20, type=int, metavar='N',
help='number of iterations to print the training statistics')
def train_factory(args, preprocess, target_transforms):
train_datas = [datasets.CocoKeypoints(
root=args.train_image_dir,
annFile=item,
preprocess=preprocess,
image_transform=transforms.image_transform_train,
target_transforms=target_transforms,
n_images=args.n_images,
) for item in args.train_annotations]
train_data = torch.utils.data.ConcatDataset(train_datas)
train_loader = torch.utils.data.DataLoader(
train_data, batch_size=args.batch_size, shuffle=True,
pin_memory=args.pin_memory, num_workers=args.loader_workers, drop_last=True)
val_data = datasets.CocoKeypoints(
root=args.val_image_dir,
annFile=args.val_annotations,
preprocess=preprocess,
image_transform=transforms.image_transform_train,
target_transforms=target_transforms,
n_images=args.n_images,
)
val_loader = torch.utils.data.DataLoader(
val_data, batch_size=args.batch_size, shuffle=False,
pin_memory=args.pin_memory, num_workers=args.loader_workers, drop_last=True)
return train_loader, val_loader, train_data, val_data
def cli():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
train_cli(parser)
parser.add_argument('-o', '--output', default=None,
help='output file')
parser.add_argument('--stride-apply', default=1, type=int,
help='apply and reset gradients every n batches')
parser.add_argument('--epochs', default=1000, type=int,
help='number of epochs to train')
parser.add_argument('--freeze-base', default=0, type=int,
help='number of epochs to train with frozen base')
parser.add_argument('--pre-lr', type=float, default=1e-4,
help='pre learning rate')
parser.add_argument('--update-batchnorm-runningstatistics',
default=False, action='store_true',
help='update batch norm running statistics')
parser.add_argument('--square-edge', default=384, type=int,
help='square edge of input images')
parser.add_argument('--ema', default=1e-3, type=float,
help='ema decay constant')
parser.add_argument('--debug-without-plots', default=False, action='store_true',
help='enable debug but dont plot')
parser.add_argument('--resume',default=False,action='store_true',help='load_from_checkpoints')
parser.add_argument('--disable-cuda', action='store_true',
help='disable CUDA')
parser.add_argument('--model_path', default='./network/weight/', type=str, metavar='DIR',
help='path to where the model saved')
args = parser.parse_args()
# add args.device
args.device = torch.device('cpu')
args.pin_memory = False
if not args.disable_cuda and torch.cuda.is_available():
args.device = torch.device('cuda')
args.pin_memory = True
return args
args = cli()
print("Loading dataset...")
# load train data
preprocess = transforms.Compose([
transforms.Normalize(),
transforms.RandomApply(transforms.HFlip(), 0.5),
transforms.RescaleRelative(),
transforms.Crop(args.square_edge),
transforms.CenterPad(args.square_edge),
])
train_loader, val_loader, train_data, val_data = train_factory(args, preprocess, target_transforms=None)
def build_names():
names = []
for j in range(1, 7):
for k in range(1, 3):
names.append('loss_stage%d_L%d' % (j, k))
return names
def get_loss(saved_for_loss, heat_temp, vec_temp):
names = build_names()
saved_for_log = OrderedDict()
criterion = nn.MSELoss(reduction='mean').cuda()
total_loss = 0
for j in range(6):
pred1 = saved_for_loss[2 * j]
pred2 = saved_for_loss[2 * j + 1]
# Compute losses
# print(vec_temp.shape)
# exit()
loss1 = criterion(pred1, vec_temp)
loss2 = criterion(pred2, heat_temp)
total_loss += loss1
total_loss += loss2
# print(total_loss)
# Get value from Variable and save for log
saved_for_log[names[2 * j]] = loss1.item()
saved_for_log[names[2 * j + 1]] = loss2.item()
saved_for_log['max_ht'] = torch.max(
saved_for_loss[-1].data[:, 0:-1, :, :]).item()
saved_for_log['min_ht'] = torch.min(
saved_for_loss[-1].data[:, 0:-1, :, :]).item()
saved_for_log['max_paf'] = torch.max(saved_for_loss[-2].data).item()
saved_for_log['min_paf'] = torch.min(saved_for_loss[-2].data).item()
return total_loss, saved_for_log
def train(train_loader, model, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
meter_dict = {}
for name in build_names():
meter_dict[name] = AverageMeter()
meter_dict['max_ht'] = AverageMeter()
meter_dict['min_ht'] = AverageMeter()
meter_dict['max_paf'] = AverageMeter()
meter_dict['min_paf'] = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, (img, heatmap_target, paf_target) in enumerate(train_loader):
# measure data loading time
#writer.add_text('Text', 'text logged at step:' + str(i), i)
#for name, param in model.named_parameters():
# writer.add_histogram(name, param.clone().cpu().data.numpy(),i)
data_time.update(time.time() - end)
img = img.cuda()
heatmap_target = heatmap_target.cuda()
# print(heatmap_target.shape)
# exit()
paf_target = paf_target.cuda()
# compute output
_,saved_for_loss = model(img)
total_loss, saved_for_log = get_loss(saved_for_loss, heatmap_target, paf_target)
for name,_ in meter_dict.items():
meter_dict[name].update(saved_for_log[name], img.size(0))
losses.update(total_loss.item(), img.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print_string = 'Epoch: [{0}][{1}/{2}]\t'.format(epoch, i, len(train_loader))
print_string +='Data time {data_time.val:.3f} ({data_time.avg:.3f})\t'.format( data_time=data_time)
print_string += 'Loss {loss.val:.4f} ({loss.avg:.4f})'.format(loss=losses)
for name, value in meter_dict.items():
print_string+='{name}: {loss.val:.4f} ({loss.avg:.4f})\t'.format(name=name, loss=value)
print(print_string)
return losses.avg
def validate(val_loader, model, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
meter_dict = {}
for name in build_names():
meter_dict[name] = AverageMeter()
meter_dict['max_ht'] = AverageMeter()
meter_dict['min_ht'] = AverageMeter()
meter_dict['max_paf'] = AverageMeter()
meter_dict['min_paf'] = AverageMeter()
# switch to train mode
model.eval()
end = time.time()
for i, (img, heatmap_target, paf_target) in enumerate(val_loader):
# measure data loading time
data_time.update(time.time() - end)
img = img.cuda()
heatmap_target = heatmap_target.cuda()
paf_target = paf_target.cuda()
# compute output
_,saved_for_loss = model(img)
total_loss, saved_for_log = get_loss(saved_for_loss, heatmap_target, paf_target)
#for name,_ in meter_dict.items():
# meter_dict[name].update(saved_for_log[name], img.size(0))
losses.update(total_loss.item(), img.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print_string = 'Epoch: [{0}][{1}/{2}]\t'.format(epoch, i, len(val_loader))
print_string +='Data time {data_time.val:.3f} ({data_time.avg:.3f})\t'.format( data_time=data_time)
print_string += 'Loss {loss.val:.4f} ({loss.avg:.4f})'.format(loss=losses)
for name, value in meter_dict.items():
print_string+='{name}: {loss.val:.4f} ({loss.avg:.4f})\t'.format(name=name, loss=value)
print(print_string)
return losses.avg
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
# model
model = get_model(trunk='convnext')
model = torch.nn.DataParallel(model).cuda()
if args.resume:
with open(checkpoints,'r') as fp:
data = fp.readline()
print(os.path.join(args.model_path,data))
resume_model = os.path.join(args.model_path,data)
dicts = torch.load(resume_model,map_location=torch.device('cpu'))
model.load_state_dict(dicts)
start = int(data.split("_")[1].replace('.pth',''))
else:
start = 0
print(model)
# exit()
# load pretrained
# use_vgg(model)
# Fix the VGG weights first, and then the weights will be released
# for i in range(20):i
for name,param in model.module.model0.named_parameters():
if 'layers.0' in name or 'layers.1' in name:
param.requires_grad = True
# trainable_vars = [param for param in model.parameters() if param.requires_grad]
# optimizer = torch.optim.SGD(trainable_vars, lr=args.lr,
# momentum=args.momentum,
# weight_decay=args.weight_decay,
# nesterov=args.nesterov)
# for epoch in range(5):
# train for one epoch
# train_loss = train(train_loader, model, optimizer, epoch)
# evaluate on validation set
# val_loss = validate(val_loader, model, epoch)
# Release all weights
# for param in model.module.parameters():
# param.requires_grad = True
trainable_vars = [param for param in model.parameters() if param.requires_grad]
optimizer = torch.optim.SGD(trainable_vars, lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
nesterov=args.nesterov)
lr_scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.8, patience=5, verbose=True, threshold=0.0001, threshold_mode='rel', cooldown=3, min_lr=0, eps=1e-08)
best_val_loss = np.inf
model_save_filename = './network/weight/convnext/best_pose.pth'
for epoch in range(start, args.epochs):
# train for one epoch
train_loss = train(train_loader, model, optimizer, epoch)
# evaluate on validation set
val_loss = validate(val_loader, model, epoch)
lr_scheduler.step(val_loss)
is_best = val_loss<best_val_loss
best_val_loss = min(val_loss, best_val_loss)
if is_best:
torch.save(model.state_dict(), model_save_filename)
torch.save(model.state_dict(),os.path.join(args.model_path,'model_%s.pth'%epoch))
with open(checkpoints,'w') as fp:
fp.write('model_%s.pth'%epoch)