-
Notifications
You must be signed in to change notification settings - Fork 63
/
train_Kfold_CV.py
93 lines (71 loc) · 3.22 KB
/
train_Kfold_CV.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
import argparse
import collections
import numpy as np
from data_loader.data_loaders import *
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from parse_config import ConfigParser
from trainer import Trainer
from utils.util import *
import torch
import torch.nn as nn
# fix random seeds for reproducibility
SEED = 123
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
def weights_init_normal(m):
if type(m) == nn.Conv2d:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif type(m) == nn.Conv1d:
torch.nn.init.normal_(m.weight.data, 0.0, 0.02)
elif type(m) == nn.BatchNorm1d:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
def main(config, fold_id):
batch_size = config["data_loader"]["args"]["batch_size"]
logger = config.get_logger('train')
# build model architecture, initialize weights, then print to console
model = config.init_obj('arch', module_arch)
model.apply(weights_init_normal)
logger.info(model)
# get function handles of loss and metrics
criterion = getattr(module_loss, config['loss'])
metrics = [getattr(module_metric, met) for met in config['metrics']]
# build optimizer
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = config.init_obj('optimizer', torch.optim, trainable_params)
data_loader, valid_data_loader, data_count = data_generator_np(folds_data[fold_id][0],
folds_data[fold_id][1], batch_size)
weights_for_each_class = calc_class_weight(data_count)
trainer = Trainer(model, criterion, metrics, optimizer,
config=config,
data_loader=data_loader,
fold_id=fold_id,
valid_data_loader=valid_data_loader,
class_weights=weights_for_each_class)
trainer.train()
if __name__ == '__main__':
args = argparse.ArgumentParser(description='PyTorch Template')
args.add_argument('-c', '--config', default="config.json", type=str,
help='config file path (default: None)')
args.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
args.add_argument('-d', '--device', default="0", type=str,
help='indices of GPUs to enable (default: all)')
args.add_argument('-f', '--fold_id', type=str,
help='fold_id')
args.add_argument('-da', '--np_data_dir', type=str,
help='Directory containing numpy files')
CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
options = []
args2 = args.parse_args()
fold_id = int(args2.fold_id)
config = ConfigParser.from_args(args, fold_id, options)
if "shhs" in args2.np_data_dir:
folds_data = load_folds_data_shhs(args2.np_data_dir, config["data_loader"]["args"]["num_folds"])
else:
folds_data = load_folds_data(args2.np_data_dir, config["data_loader"]["args"]["num_folds"])
main(config, fold_id)