-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
286 lines (234 loc) · 10.4 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
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
import math
import os
import argparse
import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import DataLoader
from models.models import HandWritingPredictionNet, HandWritingSynthesisNet
from generate import generate_conditional_sequence, generate_unconditional_seq
from utils.data_utils import data_denormalization
from utils.model_utils import compute_nll_loss
from utils.dataset import HandwritingDataset
from utils.constants import Global
from utils import plot_stroke
def argparser():
parser = argparse.ArgumentParser(
description="PyTorch Handwriting Synthesis Model")
parser.add_argument("--hidden_size", type=int, default=400, metavar="",
help="#hidden states for LSTM layer")
parser.add_argument("--n_layers", type=int, default=3, metavar="",
help="#LSTM layer")
parser.add_argument("--batch_size", type=int, default=32, metavar="",
help="size of training batch")
parser.add_argument("--step_size", type=int, default=100, metavar="",
help="step size for learning rate decay")
parser.add_argument("--n_epochs", type=int, default=100, metavar="",
help="#Training epochs")
parser.add_argument("--lr", type=float, default=0.001, metavar="",
help="learning rate")
parser.add_argument("--patience", type=int, default=15, metavar="",
help="patience for early stopping")
parser.add_argument("--model_type", type=str, default="prediction",
metavar="", help="train model type")
parser.add_argument("--data_path", type=str, default="./data/", metavar="",
help="path to processed training data")
parser.add_argument("--save_path", type=str, default="./logs/", metavar="",
help="path where training weights are stored")
parser.add_argument("--text_req", action="store_true",
help="flag indicating to fetch text data also")
parser.add_argument("--data_aug", action="store_true",
help="flag to whether data augmentation required")
parser.add_argument("--seed", type=int, default=212, metavar="",
help="random seed")
args = parser.parse_args()
return args
def train_epoch(model, optimizer, epoch, train_loader, device, model_type):
avg_loss = 0.0
model.train()
for i, mini_batch in enumerate(train_loader):
if model_type == "prediction":
inputs, targets, mask = mini_batch
else:
inputs, targets, mask, text, text_mask = mini_batch
text = text.to(device)
text_mask = text_mask.to(device)
inputs = inputs.to(device)
targets = targets.to(device)
mask = mask.to(device)
batch_size = inputs.shape[0]
optimizer.zero_grad()
if model_type == "prediction":
initial_hidden = model.init_hidden(batch_size, device)
y_hat, state = model.forward(inputs, initial_hidden)
else:
initial_hidden, window_vector, kappa = model.init_hidden(
batch_size, device)
y_hat, state, window_vector, kappa = model.forward(
inputs, text, text_mask, initial_hidden, window_vector, kappa
)
loss = compute_nll_loss(targets, y_hat, mask)
# Output gradient clipping
y_hat.register_hook(lambda grad: torch.clamp(grad, -100, 100))
loss.backward()
# LSTM params gradient clipping
if model_type == "prediction":
nn.utils.clip_grad_value_(model.parameters(), 10)
else:
nn.utils.clip_grad_value_(model.lstm_1.parameters(), 10)
nn.utils.clip_grad_value_(model.lstm_2.parameters(), 10)
nn.utils.clip_grad_value_(model.lstm_3.parameters(), 10)
nn.utils.clip_grad_value_(model.window_layer.parameters(), 10)
optimizer.step()
avg_loss += loss.item()
# print every 10 mini-batches
if i % 10 == 0:
print("\t[MiniBatch: {:3d}] loss: {:.3f}".format(
i + 1, loss / batch_size))
avg_loss /= len(train_loader.dataset)
return avg_loss
def validation(model, valid_loader, device, epoch, model_type):
avg_loss = 0.0
model.eval()
with torch.no_grad():
for i, mini_batch in enumerate(valid_loader):
if model_type == "prediction":
inputs, targets, mask = mini_batch
else:
inputs, targets, mask, text, text_mask = mini_batch
text = text.to(device)
text_mask = text_mask.to(device)
inputs = inputs.to(device)
targets = targets.to(device)
mask = mask.to(device)
batch_size = inputs.shape[0]
if model_type == "prediction":
initial_hidden = model.init_hidden(batch_size, device)
y_hat, state = model.forward(inputs, initial_hidden)
else:
initial_hidden, window_vector, kappa = model.init_hidden(
batch_size, device
)
y_hat, state, window_vector, kappa = model.forward(
inputs, text, text_mask, initial_hidden, window_vector,
kappa
)
loss = compute_nll_loss(targets, y_hat, mask)
avg_loss += loss.item()
# print every 10 mini-batches
if i % 10 == 0:
print(
"[Epoch: {:d}, MiniBatch: {:3d}] loss: {:.3f}".format(
epoch + 1, i + 1, loss / batch_size
)
)
avg_loss /= len(valid_loader.dataset)
return avg_loss
def train(model, train_loader, valid_loader, batch_size, n_epochs, lr,
patience, step_size, device, model_type, save_path):
model_path = save_path + "model_" + model_type + ".pt"
model = model.to(device)
if os.path.isfile(model_path):
model.load_state_dict(torch.load(model_path))
print(f"[ACTION] Loaded model weights from '{model_path}'")
else:
print("[INFO] No saved weights found, training from scratch.")
optimizer = optim.Adam(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=step_size, gamma=0.1)
train_losses = []
valid_losses = []
best_loss = math.inf
best_epoch = 0
k = 0
for epoch in range(n_epochs):
start_time = time.time()
print(f"[Epoch {epoch + 1}/{n_epochs}]")
print("[INFO] Training Model.....")
train_loss = train_epoch(model, optimizer, epoch, train_loader,
device, model_type)
print("[INFO] Validating Model....")
valid_loss = validation(model, valid_loader, device, epoch, model_type)
train_losses.append(train_loss)
valid_losses.append(valid_loss)
print(f"[RESULT] Epoch {epoch + 1}/{n_epochs}"
f"\tTrain loss: {train_loss:.3f}\tVal loss: {valid_loss:.3f}")
if step_size != -1:
scheduler.step()
if valid_loss < best_loss:
best_loss = valid_loss
best_epoch = epoch + 1
print("[SAVE] Saving weights at epoch: {}".format(epoch + 1))
torch.save(model.state_dict(), model_path)
if model_type == "prediction":
gen_seq = generate_unconditional_seq(model_path, 700, device,
bias=10.0, style=None,
prime=False)
else:
gen_seq = generate_conditional_sequence(
model_path,
"Hello world!",
device,
train_loader.dataset.char_to_id,
train_loader.dataset.idx_to_char,
bias=10.0,
prime=False,
prime_seq=None,
real_text=None
)
# denormalize the generated offsets using train set mean and std
gen_seq = data_denormalization(
Global.train_mean, Global.train_std, gen_seq)
# plot the sequence
plot_stroke(
gen_seq[0],
save_name=save_path + model_type +
"_seq_" + str(best_epoch) + ".png",
)
k = 0
elif k > patience:
print("Best model was saved at epoch: {}".format(best_epoch))
print("Early stopping at epoch {}".format(epoch))
break
else:
k += 1
total_time_taken = time.time() - start_time
print('Time taken per epoch: {:.2f}s\n'.format(total_time_taken))
if __name__ == "__main__":
args = argparser()
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
# fix random seed
torch.manual_seed(args.seed)
np.random.seed(args.seed)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('--ARGUMENTS--')
for arg in vars(args):
print(f"[{arg}] = {getattr(args, arg)}", end=", ")
print("")
model_type = args.model_type
batch_size = args.batch_size
n_epochs = args.n_epochs
# Load the data and text
train_dataset = HandwritingDataset(args.data_path, split="train",
text_req=args.text_req,
data_aug=args.data_aug)
valid_dataset = HandwritingDataset(args.data_path, split="valid",
text_req=args.text_req,
data_aug=args.data_aug)
train_loader = DataLoader(
train_dataset, batch_size=batch_size, shuffle=True)
valid_loader = DataLoader(
valid_dataset, batch_size=batch_size, shuffle=False)
if model_type == "prediction":
model = HandWritingPredictionNet(
hidden_size=400, n_layers=3, output_size=121, input_size=3
)
elif model_type == "synthesis":
model = HandWritingSynthesisNet(hidden_size=400, n_layers=3,
output_size=121,
window_size=train_dataset.vocab_size)
train(model, train_loader, valid_loader, batch_size, n_epochs, args.lr,
args.patience, args.step_size, device, model_type, args.save_path)