-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain_s2_classifier.py
333 lines (296 loc) · 16 KB
/
train_s2_classifier.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
from __future__ import print_function
import argparse
import os
import random
import time
import h5py
import numpy as np
import pickle
import torch
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torch.nn.functional as F
# in order to import modules from pointnet folder
from utils.dataset import ORGDataset
from utils.model_supcon import PointNet_SupCon, PointNet_Classifier
from utils.logger import create_logger
from utils.metrics_plots import classify_report, per_class_metric, process_curves
from utils.metrics_plots import calculate_prec_recall_f1, best_swap, save_best_weights, gen_199_classify_report
from eval import kfold_evaluate_two_stage_contrastive_model
from utils.funcs import unify_path, makepath, fix_seed
# GPU check
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def load_data():
"""load train and validation data"""
# load feature and label data
train_dataset = ORGDataset(
root=args.input_path,
logger=logger,
num_fold=num_fold,
k=args.k_fold,
split='train')
val_dataset = ORGDataset(
root=args.input_path,
logger=logger,
num_fold=num_fold,
k=args.k_fold,
split='val')
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.train_batch_size,
shuffle=True, num_workers=int(args.num_workers))
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.val_batch_size,
shuffle=False, num_workers=int(args.num_workers))
train_data_size = len(train_dataset)
val_data_size = len(val_dataset)
logger.info('The training data size is:{}'.format(train_data_size))
logger.info('The validation data size is:{}'.format(val_data_size))
num_classes = len(train_dataset.label_names)
logger.info('The number of classes is:{}'.format(num_classes))
# load label names
train_label_names = train_dataset.obtain_label_names()
val_label_names = val_dataset.obtain_label_names()
assert train_label_names == val_label_names
label_names = train_label_names
label_names_h5 = h5py.File(os.path.join(args.out_path, 'label_names.h5'), 'w')
label_names_h5['y_names'] = label_names
logger.info('The label names are: {}'.format(str(label_names)))
return train_loader, val_loader, label_names, num_classes, train_data_size, val_data_size
def train_val_net(supcon_net, classify_net):
"""train and validation of the network"""
time_start = time.time()
train_num_batch = train_data_size / args.train_batch_size
val_num_batch = val_data_size / args.val_batch_size
# for save training and validating process data
train_loss_lst, val_loss_lst, train_acc_lst, val_acc_lst, \
train_precision_lst, val_precision_lst, train_recall_lst, val_recall_lst, \
train_f1_lst, val_f1_lst = [], [], [], [], [], [], [], [], [], []
# for best metrics
best_acc, best_f1_mac = 0, 0
best_acc_epoch, best_f1_epoch = 1, 1
best_acc_wts, best_f1_wts = None, None
best_acc_val_labels_lst, best_f1_val_labels_lst = [], []
best_acc_val_pred_lst, best_f1_val_pred_lst = [], []
for epoch in range(args.epoch):
train_start_time = time.time()
# epoch starts from 1
epoch += 1
total_train_loss, total_val_loss = 0, 0
train_labels_lst, train_predicted_lst = [], []
total_train_correct, total_val_correct = 0, 0
val_labels_lst, val_predicted_lst = [], []
# training
for i, data in enumerate(train_loader, 0):
points, label = data # points [B, N, 3]
label = label[:, 0] # [B,1] rank2 to (, B) rank1
points = points.transpose(2, 1) # points [B, 3, N]
points, label = points.to(device), label.to(device)
optimizer.zero_grad()
supcon_net = supcon_net.eval()
classify_net = classify_net.train()
with torch.no_grad():
features = supcon_net.encoder(points)
pred = classify_net(features.detach())
loss = F.nll_loss(pred, label)
loss.backward()
optimizer.step()
if args.scheduler == 'wucd':
scheduler.step(epoch - 1 + i / train_num_batch)
_, pred_idx = torch.max(pred, dim=1)
correct = pred_idx.eq(label.data).cpu().sum()
# for calculating training accuracy and loss
total_train_correct += correct.item()
total_train_loss += loss.item()
# for calculating training weighted and macro metrics
label = label.cpu().detach().numpy().tolist()
train_labels_lst.extend(label)
pred_idx = pred_idx.cpu().detach().numpy().tolist()
train_predicted_lst.extend(pred_idx)
if args.scheduler == 'step':
scheduler.step()
# train accuracy loss
avg_train_acc = total_train_correct / float(train_data_size)
avg_train_loss = total_train_loss / float(train_num_batch)
train_acc_lst.append(avg_train_acc)
train_loss_lst.append(avg_train_loss)
# train macro p, r, f1
mac_train_precision, mac_train_recall, mac_train_f1 = calculate_prec_recall_f1(train_labels_lst,
train_predicted_lst)
train_precision_lst.append(mac_train_precision)
train_recall_lst.append(mac_train_recall)
train_f1_lst.append(mac_train_f1)
train_end_time = time.time()
train_time = round(train_end_time - train_start_time, 2)
logger.info('{} epoch [{}/{}] time: {}s train loss: {} accuracy: {} f1: {}'.format(
script_name, epoch, args.epoch, train_time, round(avg_train_loss, 4), round(avg_train_acc, 4), round(mac_train_f1, 4)))
# validation
with torch.no_grad():
val_start_time = time.time()
for j, data in (enumerate(val_loader, 0)):
points, label = data
label = label[:, 0]
points = points.transpose(2, 1)
points, label = points.to(device), label.to(device)
supcon_net = supcon_net.eval()
classify_net = classify_net.eval()
features = supcon_net.encoder(points)
pred = classify_net(features)
loss = F.nll_loss(pred, label)
_, pred_idx = torch.max(pred, dim=1)
correct = pred_idx.eq(label.data).cpu().sum()
# for calculating validation accuracy and loss
total_val_correct += correct.item()
total_val_loss += loss.item()
# for calculating validation weighted and macro metrics
label = label.cpu().detach().numpy().tolist()
val_labels_lst.extend(label)
pred_idx = pred_idx.cpu().detach().numpy().tolist()
val_predicted_lst.extend(pred_idx)
# calculate the validation accuracy and loss for the epoch
avg_val_acc = total_val_correct / float(val_data_size)
avg_val_loss = total_val_loss / float(val_num_batch)
val_acc_lst.append(avg_val_acc)
val_loss_lst.append(avg_val_loss)
# calculate the validation macro metrics
mac_val_precision, mac_val_recall, mac_val_f1 = calculate_prec_recall_f1(val_labels_lst, val_predicted_lst)
val_precision_lst.append(mac_val_precision)
val_recall_lst.append(mac_val_recall)
val_f1_lst.append(mac_val_f1)
val_end_time = time.time()
val_time = round(val_end_time - val_start_time, 2)
logger.info('{} epoch [{}/{}] time: {}s val loss: {} accuracy: {} f1: {}'.format(
script_name, epoch, args.epoch, val_time, round(avg_val_loss, 4), round(avg_val_acc, 4), round(mac_val_f1, 4)))
# swap and save the best metric
if avg_val_acc > best_acc:
best_acc, best_acc_epoch, best_acc_wts, best_acc_val_labels_lst, best_acc_val_pred_lst = \
best_swap(avg_val_acc, epoch, classify_net, val_labels_lst, val_predicted_lst)
if mac_val_f1 > best_f1_mac:
best_f1_mac, best_f1_epoch, best_f1_wts, best_f1_val_labels_lst, best_f1_val_pred_lst = \
best_swap(mac_val_f1, epoch, classify_net, val_labels_lst, val_predicted_lst)
# save best weights
save_best_weights(classify_net, best_acc_wts, args.out_path, 'acc', best_acc_epoch, best_acc, logger)
save_best_weights(classify_net, best_f1_wts, args.out_path, 'f1', best_f1_epoch, best_f1_mac, logger)
# calculate classification report and plot class analysis curves for different metrics
label_names_str = [label_name.decode() for label_name in label_names]
# accuracy
classify_report(best_acc_val_labels_lst, best_acc_val_pred_lst, label_names_str, logger, args.out_path, 'acc')
per_class_metric(best_acc_val_labels_lst, best_acc_val_pred_lst, label_names_str, val_data_size, logger,
args.out_path, 'acc')
# macro f1
classify_report(best_f1_val_labels_lst, best_f1_val_pred_lst, label_names_str, logger, args.out_path, 'f1')
per_class_metric(best_f1_val_labels_lst, best_f1_val_pred_lst, label_names_str, val_data_size, logger,
args.out_path, 'f1')
if args.redistribute_class:
gen_199_classify_report(best_acc_val_labels_lst, best_acc_val_pred_lst, label_names_str, logger, args.out_path,
'acc')
gen_199_classify_report(best_f1_val_labels_lst, best_f1_val_pred_lst, label_names_str, logger, args.out_path,
'f1')
# plot process curves
process_curves(args.epoch, train_loss_lst, val_loss_lst, train_acc_lst, val_acc_lst,
train_precision_lst, val_precision_lst, train_recall_lst, val_recall_lst,
train_f1_lst, val_f1_lst, best_acc, best_acc_epoch, best_f1_mac, best_f1_epoch, args.out_path)
# total processing time
time_end = time.time()
total_time = round(time_end - time_start, 2)
logger.info('Total processing time is {}s'.format(total_time))
if __name__ == '__main__':
# Variable Space
parser = argparse.ArgumentParser(description="Train classification model in stage 2, and evaluate on validation data for entire 1 million streamlines",
epilog="by Tengfei Xue [email protected]")
# Paths
parser.add_argument('--input_path', type=str, required=True, help='Input training data and labels')
parser.add_argument('--out_path_base', type=str, required=True, default='./ModelWeights', help='Save trained models')
# evaluation params
parser.add_argument('--input_eval_data_path', type=str, default='./', help='Input entire data (all 1 million streamlines) for evaluation ')
parser.add_argument('--best_metric', type=str, default='f1', help='evaluation metric')
parser.add_argument('--stage1_weight_path_base', type=str, required=True, default='', help='stage1 trained weight path')
parser.add_argument('--supcon_epoch', type=int, default=150, required=True, help='The epoch of encoder model')
# parameters
parser.add_argument('--k_fold', type=int, default=5, help='fold of cross-validation')
parser.add_argument('--num_workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--lr', type=float, default=1e-3, help='learning rate')
parser.add_argument('--opt', type=str, required=True, help='type of optimizer')
parser.add_argument('--weight_decay', type=float, default=0, help='weight decay for Adam')
parser.add_argument('--momentum', type=float, default=0, help='momentum for SGD')
parser.add_argument('--scheduler', type=str, default='step', help='type of learning rate scheduler')
parser.add_argument('--step_size', type=int, default=20, help='Period of learning rate decay')
parser.add_argument('--decay_factor', type=float, default=0.5, help='Multiplicative factor of learning rate decay')
parser.add_argument('--T_0', type=int, default=10, help='Number of iterations for the first restart (for wucd)')
parser.add_argument('--T_mult', type=int, default=2, help='A factor increases Ti after a restart (for wucd)')
parser.add_argument('--train_batch_size', type=int, default=128, help='batch size')
parser.add_argument('--val_batch_size', type=int, default=128, help='batch size')
parser.add_argument('--epoch', type=int, default=10, help='the number of epochs')
parser.add_argument('--eval_fold_zero', default=False, action='store_true', help='eval on fold 0, train on fold 1 2 3 4')
parser.add_argument('--redistribute_class', default=False, action='store_true', help="redistribute classes to 199 classes when generate reports")
args = parser.parse_args()
args.manualSeed = 0 # fix seed
print("Random Seed: ", args.manualSeed)
fix_seed(args.manualSeed)
script_name = '<train_stage2_classifier>'
args.input_path = unify_path(args.input_path)
args.input_eval_data_path = unify_path(args.input_eval_data_path)
args.out_path_base = unify_path(args.out_path_base)
args.stage1_weight_path_base = unify_path(args.stage1_weight_path_base)
if args.eval_fold_zero:
fold_lst = [0]
else:
fold_lst = [i for i in range(args.k_fold)]
# eval on the entire data
with open(os.path.join(args.stage1_weight_path_base, 'stage1_params.pickle'), 'rb') as f: # stage 1
stage1_params = pickle.load(f)
f.close()
with open(os.path.join(*args.out_path_base.split('/')[:-1], 'encoder_params.pickle'), 'rb') as f: # stage 2, encoder with contrastive learning
encoder_params = pickle.load(f)
f.close()
if args.eval_fold_zero:
# force classifier to only train on fold 1
encoder_params['fold_lst'] = [0]
fold_lst = encoder_params['fold_lst']
for num_fold in fold_lst:
num_fold = num_fold + 1
args.out_path = os.path.join(args.out_path_base, str(num_fold))
makepath(args.out_path)
# Record the training process and values
logger = create_logger(args.out_path)
logger.info('=' * 55)
logger.info(args)
logger.info('=' * 55)
logger.info('Implement {} fold experiment'.format(num_fold))
# load data
train_loader, val_loader, label_names, \
num_classes, train_data_size, val_data_size = load_data()
# model setting
supcon_model = PointNet_SupCon(head=encoder_params['head_name'], feat_dim=encoder_params['encoder_feat_num'])
# encoder weight path base
args.encoder_weight_path_base = os.path.join(*args.out_path_base.split('/')[:-1])
encoder_weight_path = os.path.join(args.encoder_weight_path_base, str(num_fold),
'epoch_{}_model.pth'.format(args.supcon_epoch))
supcon_model.load_state_dict(torch.load(encoder_weight_path))
# classifier setting
classifier = PointNet_Classifier(num_classes=num_classes)
# optimizers
if args.opt == 'Adam':
optimizer = optim.Adam(classifier.parameters(), lr=args.lr, betas=(0.9, 0.999), weight_decay=args.weight_decay)
elif args.opt == 'SGD':
optimizer = optim.SGD(classifier.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
else:
optimizer = None
exit()
# schedulers
if args.scheduler == 'step':
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=args.step_size, gamma=args.decay_factor)
elif args.scheduler == 'wucd':
scheduler = optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0=args.T_0, T_mult=args.T_mult)
else:
scheduler = None
exit()
supcon_model.to(device)
classifier.to(device)
# train and eval net
train_val_net(supcon_model, classifier)
# clean the logger
logger.handlers.clear()
# use the entire data (1 million streamlines) for evaluation
args.input_path = args.input_eval_data_path
kfold_evaluate_two_stage_contrastive_model(stage1_params, encoder_params, args, device, 'evaluate_net')