-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_silhouette.py
350 lines (271 loc) · 11.7 KB
/
train_silhouette.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import math
import argparse
import pprint
import tqdm
import sys
# sys.path.append('configs')
import pandas as pd
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from models import get_model
from losses import get_loss, get_center_loss
from optimizers import get_optimizer, get_center_optimizer
from schedulers import get_scheduler
from sampler import get_sampler
import utils
from utils.checkpoint import get_initial_checkpoint, load_checkpoint, save_checkpoint
import utils.metrics
from utils import get_initial, get_collate_fn
from sklearn.preprocessing import LabelEncoder
# change training parameters from py dictionary to
class Train(object):
def __init__(self, config):
self.config = config
self.model = None
self.optimizer = None
self.optimizer_center = None # reserved for center loss
self.scheduler = None
self.writer = None
self.label = None
self.label_encoder = None
self.sampler = None
self.loss_function = None
self.loss_center = None # reserved for center loss
self.writer = None
self.data_loader = None
self.data_loader_val = None
self.dataset = None
self.more_label = None
self.collate_fn = None
self.weight = self.config.train.weight
self.num_epochs = self.config.train.num_epochs
self.num_workers = self.config.data.num_workers
self.sample_type = 'random'
self.last_epoch = 0
self.step = -1
self.iteration = 0
if self.writer is not None:
self.writer = SummaryWriter(self.config.writer)
self.more_label = self.load_new_label()
def load_new_label(self):
data = pd.read_csv("./data/label.csv")
data = data.drop(columns=['ID'])
return data
def initialization(self):
self.dataset, test_dataset, val_dataset, self.label = get_initial(self.config, train = True) # return dataset instance
self.label_encoder = LabelEncoder()
self.label_encoder.fit(self.label)
torch.cuda.empty_cache()
self.model = get_model(self.config)
self.optimizer = get_optimizer(self.config, self.model.parameters())
checkpoint = get_initial_checkpoint(self.config)
if torch.cuda.device_count() >1:
self.model = torch.nn.DataParallel(self.model)
self.model = self.model.cuda()
if checkpoint is not None:
self.last_epoch, self.step = load_checkpoint(self.model, self.optimizer, self.optimizer_center, checkpoint)
print("from checkpoint {} last epoch: {}".format(checkpoint, self.last_epoch))
self.sampler = get_sampler(self.dataset, self.config)
self.loss_function = get_loss(self.config)
if self.config.loss.name is 'softmax_center':
self.loss_center = get_center_loss(class_num = 86, feature_num = 512)
self.optimizer_center = get_center_optimizer(self.loss_center.parameters(), self.config.optimizer.params.lr)
self.collate_fn = get_collate_fn(self.config, self.config.data.frame_num, self.sample_type) #
if self.sampler is not None:
self.data_loader = DataLoader(
dataset=self.dataset,
batch_sampler=self.sampler,
collate_fn=self.collate_fn,
num_workers=self.num_workers)
else:
self.data_loader = DataLoader(
dataset=self.dataset,
batch_size=self.config.train.batch_size.batch1,
collate_fn=self.collate_fn,
num_workers=self.num_workers,
drop_last= self.config.data.drop_last,
shuffle= self.config.data.pid_shuffle,
)
def validation(self, epoch):
self.model.eval()
with torch.no_grad():
epoch_val = 10
acc_sample = 0
count_all = 0
all_loss = 0
for epoch_i in range(epoch_val):
for seq, date, label, _ in self.data_loader_val:
count_all += len(label)
seq = torch.Tensor(seq).float().cuda()
fc, out = self.model(seq)
label = self.label_encoder.transform(label)
label = torch.Tensor(label).long().cuda()
loss = self.loss_function(out, label)
pred = torch.max(out, 1)[1]
acc = (pred == label).sum()
acc_sample += acc.item()
all_loss += loss
acc_ii = acc_sample/count_all
if self.writer is not None:
self.writer.add_scalar("data/val_loss", all_loss, epoch)
self.writer.add_scalar("data/val_acc", acc_ii, epoch)
print("validated result, in epoch :{}, acc = {}, loss={}".format(epoch, acc_ii, all_loss))
self.model.train()
def find_label(self, date, label):
# cloth: normal, coat, skirt: 0,1,2
# activity = walk, phone:0,1
# gender = male, female : 0,1
# carry = no, bag, small, big : 0,1,2,3
# path = straight, curve :0,1
# upper = short, long : 0,1
cloth = []
activity = []
gender = []
carry = []
path = []
for i in range(len(date)):
value = self.more_label.loc[(self.more_label['id'] == label[i]) & (self.more_label['date'] == date[i]) ].values[0]
cloth.append(int(value[0]))
activity.append(int(value[1]))
gender.append(int(value[2]))
carry.append(int(value[3]))
path.append(int(value[4]))
cloth = np.asarray(cloth)
activity = np.asarray(activity)
gender = np.asarray(gender)
carry = np.asarray(carry)
path = np.asarray(path)
# print(cloth)
return cloth,activity,gender,carry,path
def train_sigle_iteration(self, seq, date, label, _):
self.optimizer.zero_grad()
if self.optimizer_center is not None:
self.optimizer_center.zero_grad()
# generate new label
cloth, activity, gender, carry, path = self.find_label(date, label)
seq = torch.Tensor(seq).float().cuda()
fc, out, out_cloth, out_activity, out_gender, out_carry, out_path = self.model(seq)
label = self.label_encoder.transform(label)
label = torch.Tensor(label).long().cuda()
cloth = torch.Tensor(cloth).long().cuda()
activity = torch.Tensor(activity).long().cuda()
gender = torch.Tensor(gender).long().cuda()
carry = torch.Tensor(carry).long().cuda()
path = torch.Tensor(path).long().cuda()
loss = self.loss_function(out, label) # loss of identification.
pred = torch.max(out, 1)[1]
acc = (pred == label).sum()
loss_cloth = self.loss_function(out_cloth, cloth)
loss_activity = self.loss_function(out_activity, activity)
loss_gender = self.loss_function(out_gender, gender)
# loss_carry = self.loss_function(out_carry, carry)
# loss_path = self.loss_function(out_path, path)
# NOTE:The following code is for Experiments of gait covariate.
# You can customize the following code for you case.
loss = loss + self.weight*loss_cloth +self.weight*loss_activity + self.weight*loss_gender #+ loss_carry + loss_path
loss_temp = loss.item()
loss.backward()
self.optimizer.step() # update parameters
if self.loss_center is not None:
self.optimizer_center.step()
return acc.item(), loss_temp
def train_weigh(self):
acc_sample = 0
count_all = 0
all_loss = 0
total_num = len(self.dataset)
batch_size = self.config.train.batch_size.batch1 * self.config.train.batch_size.batch2
step_num = math.ceil(total_num/batch_size)
epoch = self.last_epoch
iteration = epoch*step_num
# print("step number is ", step_num)
for seq, date, label, _ in self.data_loader:
iteration += 1
count_all += len(label)
acc_i, loss = self.train_sigle_iteration(seq, date, label, _)
all_loss += loss
acc_sample += acc_i
# print("iteration is {}, epoch is {}".format(iteration, epoch))
if iteration % step_num == step_num-1 :
epoch += 1
if self.writer is not None:
self.writer.add_scalar("train_loss", all_loss, epoch)
acc_epoch = acc_sample * 1.0 / count_all
print("training in epoch :{}, the acc is {}% ,\n the loss is {}".format(epoch, acc_epoch * 100, all_loss))
acc_sample = 0
count_all = 0
all_loss = 0
# if epoch % 10 == 9:
# self.validation(epoch)
if epoch > self.config.train.num_epochs:
break
if epoch % 200 == 199:
save_checkpoint(self.config, self.model, self.optimizer, self.optimizer_center, epoch, self.step)
def train_single_epoch(self, epoch ):
acc_sample = 0
count_all = 0
all_loss = 0
for seq, date, label, _ in self.data_loader:
count_all += len(label)
acc_i, loss = self.train_sigle_iteration(seq, date, label, _)
all_loss += loss
acc_sample += acc_i
if self.writer is not None:
self.writer.add_scalar("train_loss", all_loss, epoch)
acc_epoch = acc_sample * 1.0 / count_all
print("training in epoch :{}, the acc is {}% ,\n the loss is {}".format(epoch, acc_epoch * 100, all_loss))
print("learning rate: ",self.optimizer.param_groups[0]['lr'])
def run(self):
# checkpoint
self.scheduler = get_scheduler(self.config, self.optimizer)
self.model.train()
postfix_dic = {
'lr': 0.0,
'acc' : 0.0,
'loss' : 0.0,
}
if self.config.data.sampler== "weight":
self.train_weigh()
else:
for epoch in range(self.last_epoch, self.num_epochs):
self.train_single_epoch(epoch)
if epoch % 200 == 199:
save_checkpoint(self.config, self.model, self.optimizer, self.optimizer_center, epoch, self.step)
# if epoch % 10 ==9:
# self.validation(epoch)
# saving model every 200 epoch
self.scheduler.step()
if epoch > self.config.train.num_epochs:
break
def parse_args():
parser = argparse.ArgumentParser(description='config file')
parser.add_argument('--config', dest='config_file',
help='configuration filename',
default="./configs/YOUR_CONFIG.yml", type=str)
parser.add_argument('--GPU_num', dest='GPU_num',
help='GPU number',
default="0", type=str)
return parser.parse_args()
def main():
args = parse_args()
if args.config_file is None:
raise Exception("no configuration file.")
config = utils.config.load(args.config_file)
config.train.dir = os.path.join(config.train.dir, os.path.basename(args.config_file)[:-4])
if args.GPU_num is not None:
config.CUDA_VISIBLE_DEVICES = args.GPU_num
print("GPU is ", config.CUDA_VISIBLE_DEVICES)
print(config.train.dir)
trainer = Train(config)
trainer.initialization()
trainer.run()
print("Training complete!")
if __name__ == '__main__':
main()