-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
166 lines (138 loc) · 4.91 KB
/
train.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
"""
The active learning training process
"""
from argparse import Namespace
import os
import logging
import numpy as np
import torch
import torch.optim as optim
from tensorboardX import SummaryWriter
from dataset.get_data import get_dataset
from model.get_models import get_net
from model.nn_utils import initialize_weights
from oracle.get_oracles import get_oracle
from query_strategies.get_strategy import get_strategy
from parsing import parse_args
def train_model(
args: Namespace = None,
logger: logging.Logger = None,
writer: SummaryWriter = None):
"""
Start a active training process
:param args: arguments
:param logger: logger
:param writer: tensorboard writer
"""
if logger is not None:
debug, info = logger.debug, logger.info
else:
debug = info = print
# set seed
use_cuda = torch.cuda.is_available()
if args.seed is not None:
np.random.seed(args.seed)
if not use_cuda:
torch.manual_seed(args.seed)
else:
np.random.seed()
torch.backends.cudnn.enabled = False
# Construct data set
data_train, data_test, idxs_lb, n_test = get_dataset(args)
info('number of labeled pool: {}'.format(sum(idxs_lb)))
info('number of unlabeled pool: {}'.format(len(data_train) - sum(idxs_lb)))
info('number of testing pool: {}'.format(n_test))
# Define parts we need
net = get_net(args)
device = torch.device("cuda" if use_cuda else "cpu")
net = net(args, device)
initialize_weights(net)
if args.parameters is not None:
net.load_state_dict(torch.load(args.param))
optimizer = optim.Adam(
params=net.parameters(),
lr=args.learning_rate,
betas=(args.adam_beta_1, args.adam_beta_2),
eps=1e-08,
weight_decay=0,
amsgrad=False
)
lr_schedule = optim.lr_scheduler.ExponentialLR(
optimizer=optimizer,
gamma=args.learning_rate_decay_rate
)
oracle = get_oracle(args)
strategy = get_strategy(args)
strategy = strategy(data_train, idxs_lb, net, optimizer, lr_schedule, args, logger, writer)
# Logging information of this task
info(f'Starting running task: {args.task}')
info('SEED {}'.format(args.seed))
info(type(strategy).__name__)
losses = []
n_iter = 0
# The first round training
n_iter = strategy.train(n_iter)
mse, mae, maxae = strategy.evaluate(data_test)
losses.append(mae)
info('Round 0\ntesting MAE {}'.format(mae))
if writer is not None:
writer.add_scalar('query_mse', mse, 0)
writer.add_scalar('query_mae', mae, 0)
writer.add_scalar('query_maxae', maxae, 0)
# Starting querying and training
for rd in range(1, args.round + 1):
assert sum(idxs_lb) < len(data_train)
info('Round {}'.format(rd))
# query
q_idxs = strategy.query(args.query)
data_train, idxs_lb = oracle(data_train, idxs_lb, q_idxs)
# update
strategy.update(idxs_lb)
n_iter = strategy.train(n_iter)
# round accuracy
mse, mae, maxae = strategy.evaluate(data_test)
losses.append(mae)
info('Round {}\ntesting MAE {}'.format(rd, mae))
if writer is not None:
writer.add_scalar('query_mse', mse, rd)
writer.add_scalar('query_mae', mae, rd)
writer.add_scalar('query_maxae', maxae, rd)
strategy.save_net(rd)
# print results
info('SEED {}'.format(args.seed))
info(type(strategy).__name__)
info(losses)
def set_logger(logger: logging.Logger, save_dir: str = None, quiet: bool = False):
"""
Sets up a logger with a stream handler and two file handlers.
The stream handler prints to the screen depending on the value of `quiet`.
One file handler (verbose.log) saves all logs, the other (quiet.log) only saves important info.
:param logger: A logger.
:param save_dir: The directory in which to save the logs.
:param quiet: Whether the stream handler should be quiet (i.e. print only important info).
"""
# Set logger depending on desired verbosity
ch = logging.StreamHandler()
if quiet:
ch.setLevel(logging.INFO)
else:
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
if save_dir is not None:
fh_v = logging.FileHandler(os.path.join(save_dir, 'verbose.log'))
fh_v.setLevel(logging.DEBUG)
fh_q = logging.FileHandler(os.path.join(save_dir, 'quiet.log'))
fh_q.setLevel(logging.INFO)
logger.addHandler(fh_v)
logger.addHandler(fh_q)
if __name__ == '__main__':
args = parse_args()
logger = logging.getLogger('train')
logger.setLevel(logging.DEBUG)
logger.propagate = False
set_logger(logger, args.log_path, args.quiet)
try:
writer = SummaryWriter(log_dir=args.log_path)
except:
writer = SummaryWriter(logdir=args.log_path)
train_model(args, logger, writer)