-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
241 lines (212 loc) · 12.2 KB
/
app.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
from argparse import ArgumentParser
from torchtext import data
import torch
import numpy as np
import random
import os
import sys
import uuid
class TrainAPP:
def __init__(self, args, fields, include_test, build_vocab_params=None,
batch_size_fn_train = lambda new, count, sofar: count,
batch_size_fn_valid = lambda new, count, sofar: count,
batch_size_fn_test = lambda new, count, sofar: count,
train_shuffle=True,
sort_within_batch=False,
sort_key=None):
self.args = args
torch.manual_seed(self.args.seed)
torch.backends.cudnn.deterministic = True
if not self.args.cuda:
self.args.gpu = -1
if torch.cuda.is_available() and self.args.cuda:
print("Note: You are using GPU for training")
torch.cuda.set_device(self.args.gpu)
torch.cuda.manual_seed(self.args.seed)
if torch.cuda.is_available() and not self.args.cuda:
print("Warning: You have Cuda but not use it. You are using CPU for training.")
np.random.seed(self.args.seed)
random.seed(self.args.seed)
self.fields = fields
self.train_data, self.valid_data, self.test_data = data.TabularDataset.splits(path=self.args.dataset_path,
train=self.args.train_txt, validation=self.args.valid_txt, test=self.args.test_txt,
format='tsv', fields=self.fields)
if build_vocab_params ==None:
build_vocab_params = [{}] * len(fields)
for i in range(len(self.fields)):
setattr(self, self.fields[i][0], self.fields[i][1])
if self.fields[i][1].use_vocab:
if include_test[i]:
self.fields[i][1].build_vocab(self.train_data, self.valid_data, self.test_data, build_vocab_params[i])
else:
self.fields[i][1].build_vocab(self.train_data, self.valid_data, build_vocab_params[i])
self.train_iter = data.Iterator(self.train_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_train, train=True, repeat=False, sort=False,
shuffle=train_shuffle, sort_within_batch=sort_within_batch, sort_key=sort_key)
self.valid_iter = data.Iterator(self.valid_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_valid, train=False, repeat=False, sort=False,
shuffle=False, sort_within_batch=sort_within_batch, sort_key=sort_key)
self.test_iter = data.Iterator(self.test_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_test, train=False, repeat=False, sort=False,
shuffle=False, sort_within_batch=sort_within_batch, sort_key=sort_key)
self.config = self.args
def prepare(self, model, optimizer, criterion, evaluator,
metrics_comparison, log_printer):
self.model = model(self.config)
if self.args.cuda:
device = torch.device("cuda:{}".format(self.args.gpu))
self.model = self.model.to(device)
print("Shift model to GPU")
self.parameter = filter(lambda p: p.requires_grad, self.model.parameters())
self.optimizer = optimizer(self.parameter, self.config)
self.criterion = criterion
self.evaluator = evaluator
self.metrics_comparison = metrics_comparison
os.makedirs(self.args.save_path, exist_ok=True)
self.snapshot_path = os.path.join(self.args.save_path, self.args.prefix+"_best_model_"+str(uuid.uuid4()))
self.patience = self.args.patience # TODO: calculate the patience
self.log_printer = log_printer
def train(self):
early_stop = False
epoch = 0
iterations = 0
best_metrics = None
iters_not_improved = 0
batch_num = len(self.train_iter)
while True:
if early_stop:
print("Early Stoping")
break
epoch += 1
self.train_iter.init_epoch()
self.optimizer.schedule()
for batch_idx, batch in enumerate(self.train_iter):
iterations += 1
self.model.train()
self.optimizer.zero_grad()
output = self.model(batch)
# We generate metrics for each batch, not all batches so far
metrics = self.evaluator("train", (output, batch))
loss = self.criterion(output, batch)
loss.backward()
self.optimizer.step()
if iterations % self.args.valid_every == 1:
self.model.eval()
self.valid_iter.init_epoch()
valid_result = []
valid_loss = 0
for valid_batch_idx, valid_batch in enumerate(self.valid_iter):
valid_output = self.model(valid_batch)
valid_result.append((valid_output, valid_batch))
valid_loss += self.criterion(valid_output, valid_batch).item()
valid_metrics = self.evaluator("valid", valid_result)
self.log_printer("valid", epoch=epoch, iters="{}/{}".format(batch_idx, batch_num),
metrics=valid_metrics, loss=valid_loss)
if self.metrics_comparison(valid_metrics, best_metrics):
iters_not_improved = 0
best_metrics = valid_metrics
torch.save(self.model, self.snapshot_path)
print("Saving model to {}".format(self.snapshot_path))
else:
iters_not_improved += 1
if iters_not_improved >= self.patience:
early_stop = True
break
if iterations % self.args.log_every == 0:
self.log_printer("train", epoch=epoch, iters="{}/{}".format(batch_idx, batch_num), metrics=metrics, loss=loss.data[0])
class TestAPP:
def __init__(self, args, fields, include_test, build_vocab_params=None,
batch_size_fn_train=lambda new, count, sofar: count,
batch_size_fn_valid=lambda new, count, sofar: count,
batch_size_fn_test=lambda new, count, sofar: count):
self.args = args
if not self.args.trained_model:
print("Error: You need to provide a option 'trained_model' to load the model")
sys.exit(1)
torch.manual_seed(self.args.seed)
torch.backends.cudnn.deterministic = True
if not self.args.cuda:
self.args.gpu = -1
if torch.cuda.is_available() and self.args.cuda:
print("Note: You are using GPU for training")
torch.cuda.set_device(self.args.gpu)
torch.cuda.manual_seed(self.args.seed)
if torch.cuda.is_available() and not self.args.cuda:
print("Warning: You have Cuda but not use it. You are using CPU for training.")
np.random.seed(self.args.seed)
random.seed(self.args.seed)
self.fields = fields
self.train_data, self.valid_data, self.test_data = data.TabularDataset.splits(path=self.args.dataset_path,
train=self.args.train_txt, validation=self.args.valid_txt,
test=self.args.test_txt,
format='tsv', fields=self.fields)
if build_vocab_params ==None:
build_vocab_params = [{}] * len(fields)
for i in range(len(self.fields)):
setattr(self, self.fields[i][0], self.fields[i][1])
if self.fields[i][1].use_vocab:
if include_test[i]:
self.fields[i][1].build_vocab(self.train_data, self.valid_data, self.test_data, build_vocab_params[i])
else:
self.fields[i][1].build_vocab(self.train_data, self.valid_data, build_vocab_params[i])
self.train_iter = data.Iterator(self.train_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_train, train=True, repeat=False, sort=False,
shuffle=False, sort_within_batch=False)
self.valid_iter = data.Iterator(self.valid_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_valid, train=False, repeat=False, sort=False,
shuffle=False, sort_within_batch=False)
self.test_iter = data.Iterator(self.test_data, batch_size=self.args.batch_size, device=self.args.gpu,
batch_size_fn=batch_size_fn_test, train=False, repeat=False, sort=False,
shuffle=False, sort_within_batch=False)
if self.args.cuda:
self.model = torch.load(self.args.trained_model, map_location=lambda storage,
location: storage.cuda(self.args.gpu))
else:
self.model = torch.load(self.args.trained_model, map_location=lambda storage, location: storage)
def prepare(self, evaluator, log_printer, output_parser=None):
self.evaluator = evaluator
self.log_printer = log_printer
self.output_parser = output_parser
def predict(self, dataset_iter, dataset_name):
print("Dataset : {}".format(dataset_name))
self.model.eval()
dataset_iter.init_epoch()
test_result = []
for test_batch_idx, test_batch in enumerate(dataset_iter):
results = self.model(test_batch)
test_result.append((results, test_batch))
test_metrics = self.evaluator(dataset_name, test_result)
self.log_printer(dataset_name, metrics=test_metrics, loss=None)
if self.output_parser != None:
os.makedirs(self.args.result_path, exist_ok=True)
self.output_parser(dataset_name, test_result)
def test(self):
self.predict(dataset_iter=self.valid_iter, dataset_name='valid')
self.predict(dataset_iter=self.test_iter, dataset_name='test')
class ArgParser:
def __init__(self, description, gpu=0, batch_size=32, seed=3435,
dev_every=300, log_every=30, patience=5,
dataset_path='data', train_txt='train.txt', valid_txt='valid.txt', test_txt='test.txt',
save_path='saves', result_path='results'):
self.parser = ArgumentParser(description=description)
self.parser.add_argument('--no_cuda', action='store_false', dest='cuda')
self.parser.add_argument('--gpu', type=int, default=gpu)
self.parser.add_argument('--batch_size', type=int, default=batch_size)
self.parser.add_argument('--seed', type=int, default=seed)
self.parser.add_argument('--valid_every', type=int, default=dev_every)
self.parser.add_argument('--log_every', type=int, default=log_every)
self.parser.add_argument('--patience', type=int, default=patience)
self.parser.add_argument('--dataset_path', type=str, default=dataset_path)
self.parser.add_argument('--train_txt', type=str, default=train_txt)
self.parser.add_argument('--valid_txt', type=str, default=valid_txt)
self.parser.add_argument('--test_txt', type=str, default=test_txt)
self.parser.add_argument('--save_path', type=str, default=save_path)
self.parser.add_argument('--prefix', type=str, default="exp")
# Tester
self.parser.add_argument('--trained_model', type=str, default='')
self.parser.add_argument('--result_path', type=str, default=result_path)
self.parser.add_argument('--output_valid', type=str, default='valid.txt')
self.parser.add_argument('--output_test', type=str, default='test.txt')
def get_args(self):
self.args = self.parser.parse_args()
return self.args