-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_baseline.py
126 lines (110 loc) · 5.09 KB
/
train_baseline.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
import re
import torch.nn.functional as F
import voc12.dataloader
import argparse
import torch
import os
from torch.utils.data import DataLoader
from misc import pyutils, torchutils
from net.resnet50_cam import Net
def validate(model, data_loader):
print('validating ... ', flush=True, end='')
val_loss_meter = pyutils.AverageMeter('loss1', 'loss2')
model.eval()
with torch.no_grad():
for pack in data_loader:
img = pack['img'].cuda(non_blocking=True)
label = pack['label'].cuda(non_blocking=True)
x, _ = model(img)
loss1 = F.multilabel_soft_margin_loss(x, label)
val_loss_meter.add({'loss1': loss1.item()})
model.train()
vloss = val_loss_meter.pop('loss1')
print('loss: %.4f' % vloss)
return vloss
def train(config):
seed = config['seed']
train_list = config['train_list']
val_list = config['val_list']
voc12_root = config['voc12_root']
cam_batch_size = config['cam_batch_size']
cam_num_epoches = config['cam_num_epoches']
cam_learning_rate = config['cam_learning_rate']
sty_learning_rate = cam_learning_rate
cam_weight_decay = config['cam_weight_decay']
model_root = config['model_root']
cam_weights_name = config['laste_cam_weights_name']
num_workers = config['num_workers']
cam_crop_size = config['cam_crop_size']
alpha = config['alpha']
cam_weight_path = os.path.join(model_root, cam_weights_name)
pyutils.seed_all(seed)
cls_model = Net()
train_dataset = voc12.dataloader.VOC12ClassificationDataset(train_list,
voc12_root=voc12_root,
resize_long=(
320, 640),
hor_flip=True,
crop_size=cam_crop_size,
crop_method="random")
train_data_loader = DataLoader(train_dataset,
batch_size=cam_batch_size,
shuffle=True,
num_workers=num_workers,
pin_memory=True,
drop_last=True)
max_step = (len(train_dataset) // cam_batch_size) * cam_num_epoches
val_dataset = voc12.dataloader.VOC12ClassificationDataset(val_list,
voc12_root=voc12_root,
crop_size=cam_crop_size)
val_data_loader = DataLoader(val_dataset,
batch_size=cam_batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True,
drop_last=True)
param_groups = cls_model.trainable_parameters()
optimizer = torchutils.PolyOptimizer([
{'params': param_groups[0], 'lr': cam_learning_rate,
'weight_decay': cam_weight_decay},
{'params': param_groups[1], 'lr': 10 * cam_learning_rate,
'weight_decay': cam_weight_decay}
], lr=cam_learning_rate, weight_decay=cam_weight_decay, max_step=max_step)
cls_model.train()
cls_model = torch.nn.DataParallel(cls_model).cuda()
avg_meter = pyutils.AverageMeter()
timer = pyutils.Timer()
for ep in range(cam_num_epoches):
print('Epoch %d/%d' % (ep+1, cam_num_epoches))
for step, pack in enumerate(train_data_loader):
img = pack['img'].cuda(non_blocking=True)
label = pack['label'].cuda(non_blocking=True)
x = cls_model(img)
loss = F.multilabel_soft_margin_loss(x, label)
avg_meter.add({'loss': loss.item()})
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (optimizer.global_step - 1) % 100 == 0:
timer.update_progress(optimizer.global_step / max_step)
print('step:%5d/%5d' % (optimizer.global_step - 1, max_step),
'loss:%.4f' % (avg_meter.pop('loss')),
'imps:%.1f' % (
(step + 1) * cam_batch_size / timer.get_stage_elapsed()),
'lr: %.4f' % (optimizer.param_groups[0]['lr']),
'etc:%s' % (timer.str_estimated_complete()), flush=True)
# validation
validate(cls_model, val_data_loader)
else:
timer.reset_stage()
# empty cache
torch.save(cls_model.module.state_dict(), cam_weight_path)
torch.cuda.empty_cache()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Front Door Semantic Segmentation')
parser.add_argument('--config', type=str,
help='YAML config file path', required=True)
args = parser.parse_args()
config = pyutils.parse_config(args.config)
train(config)