-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_patch.py
181 lines (167 loc) · 6.67 KB
/
train_patch.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
from __future__ import print_function, division
import os
import time
import shutil
import pickle
import logging
import argparse
import datetime
import cv2
import mxnet as mx
from mxnet import nd, autograd as ag, gluon as gl
from mxnet.gluon import nn
import numpy as np
import pandas as pd
from tensorboardX import SummaryWriter
from lib.config import cfg
from lib.dataset import FashionAIPatchDataSet
from lib.model import PatchRefineNet
from lib.utils import get_logger, Recorder
class SumL2Loss(gl.loss.Loss):
def __init__(self, weight=1., batch_axis=0, **kwargs):
super(SumL2Loss, self).__init__(weight, batch_axis, **kwargs)
def hybrid_forward(self, F, pred, label, mask):
pred = F.broadcast_mul(pred, mask)
label = F.broadcast_mul(label, mask)
label = gl.loss._reshape_like(F, label, pred)
loss = F.square(pred - label)
loss = gl.loss._apply_weighting(F, loss, self._weight/2, None)
return F.sum(loss, axis=self._batch_axis, exclude=True)
def forward_backward(net, criterion, ctx, packet, is_train=True):
data, ht, mask = packet
data = gl.utils.split_and_load(data, ctx)
ht = gl.utils.split_and_load(ht, ctx)
mask = gl.utils.split_and_load(mask, ctx)
# run
ag.set_recording(is_train)
ag.set_training(is_train)
losses = []
for data_, ht_, mask_ in zip(data, ht, mask):
pred_ = net(data_)
losses_ = [criterion(ht_, pred_, mask_)]
losses.append(losses_)
if is_train:
ag.backward(losses_)
ag.set_recording(False)
ag.set_training(False)
return losses
def reduce_losses(losses):
n = len(losses)
m = len(losses[0])
ret = np.zeros(m)
for i in range(n):
for j in range(m):
ret[j] += losses[i][j].mean().asscalar()
ret /= n
return ret
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=str, default='0')
parser.add_argument('--epoches', type=int, default=100)
parser.add_argument('--batch-size', type=int, default=16)
parser.add_argument('--freq', type=int, default=50)
parser.add_argument('--lr', type=float, default=1e-4)
parser.add_argument('--wd', type=float, default=1e-4)
parser.add_argument('--optim', type=str, default='adam', choices=['sgd', 'adam'])
parser.add_argument('--seed', type=int, default=666)
parser.add_argument('--steps', type=str, default='1000')
parser.add_argument('--lr-decay', type=float, default=0.1)
parser.add_argument('--model-path', type=str, default='')
parser.add_argument('--prefix', type=str, default='default', help='model description')
args = parser.parse_args()
# seed
mx.random.seed(args.seed)
np.random.seed(args.seed)
# hyper parameters
ctx = [mx.gpu(int(x)) for x in args.gpu.split(',')]
data_dir = cfg.DATA_DIR
lr = args.lr
wd = args.wd
optim = args.optim
batch_size = args.batch_size
epoches = args.epoches
freq = args.freq
steps = [int(x) for x in args.steps.split(',')]
lr_decay = args.lr_decay
base_name = 'refine'
filename = './tmp/refine.log'
logger = get_logger(fn=filename)
logger.info(args)
# data
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'val.csv'))
traindata = FashionAIPatchDataSet(df_train, is_train=True)
testdata = FashionAIPatchDataSet(df_test, is_train=False)
trainloader = gl.data.DataLoader(traindata, batch_size=batch_size, shuffle=True, last_batch='discard', num_workers=4)
testloader = gl.data.DataLoader(testdata, batch_size=batch_size, shuffle=False, last_batch='discard', num_workers=4)
epoch_size = len(trainloader)
# model
num_kps = cfg.NUM_LANDMARK
net = PatchRefineNet(num_kps=num_kps)
net.initialize(mx.init.Normal(), ctx=ctx)
net.hybridize()
criterion = SumL2Loss()
criterion.hybridize()
# trainer
steps = [epoch_size * x for x in steps]
lr_scheduler = mx.lr_scheduler.MultiFactorScheduler(step=steps, factor=lr_decay)
if optim == 'sgd':
trainer = gl.trainer.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr, 'wd': wd, 'momentum': 0.9, 'lr_scheduler': lr_scheduler})
else:
trainer = gl.trainer.Trainer(net.collect_params(), 'adam', {'learning_rate': lr, 'wd': wd, 'lr_scheduler': lr_scheduler})
# logger
log_dir = './log-refine'
if os.path.exists(log_dir):
shutil.rmtree(log_dir)
sw = SummaryWriter(log_dir)
rds = [Recorder('ht', freq)]
# meta info
global_step = 0
# forward and backward
for epoch_idx in range(epoches):
# train part
tic = time.time()
for rd in rds:
rd.reset()
sw.add_scalar('lr', trainer.learning_rate, global_step)
for batch_idx, packet in enumerate(trainloader):
# [(l1, l2, ...), (l1, l2, ...)]
losses = forward_backward(net, criterion, ctx, packet, is_train=True)
trainer.step(batch_size)
# reduce to [l1, l2, ...]
ret = reduce_losses(losses)
for rd, loss in zip(rds, ret):
rd.update(loss)
if batch_idx % freq == freq - 1:
for rd in rds:
name, value = rd.get()
sw.add_scalar('train/' + name, value, global_step)
logger.info('[Epoch %d][Batch %d] %s = %f', epoch_idx + 1, batch_idx + 1, name, value)
global_step += 1
toc = time.time()
speed = (batch_idx + 1) * batch_size / (toc - tic)
logger.info('[Epoch %d][Batch %d] Speed = %.2f sample/sec', epoch_idx + 1, batch_idx + 1, speed)
toc = time.time()
logger.info('[Epoch %d] Global step %d', epoch_idx + 1, global_step - 1)
logger.info('[Epoch %d] Train Cost %.0f sec', epoch_idx + 1, toc - tic)
# test part
tic = time.time()
for rd in rds:
rd.reset()
for batch_idx, packet in enumerate(testloader):
losses = forward_backward(net, criterion, ctx, packet, is_train=False)
ret = reduce_losses(losses)
for rd, loss in zip(rds, ret):
rd.update(loss)
for rd in rds:
name, value = rd.get()
sw.add_scalar('test/' + name, value, global_step)
logger.info('[Epoch %d][Test] %s = %f', epoch_idx + 1, name, value)
toc = time.time()
logger.info('[Epoch %d] Test Cost %.0f sec', epoch_idx + 1, toc - tic)
# save part
save_path = './output/%s-%04d.params' % (base_name, epoch_idx + 1)
net.save_params(save_path)
logger.info('[Epoch %d] Saved to %s', epoch_idx + 1, save_path)
if __name__ == '__main__':
main()