-
Notifications
You must be signed in to change notification settings - Fork 1
/
continuous_hw_training.py
212 lines (165 loc) · 7.12 KB
/
continuous_hw_training.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
import torch
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torch.nn import CTCLoss
from hw import hw_dataset
from hw import cnn_lstm
from hw.hw_dataset import HwDataset
from utils.dataset_wrapper import DatasetWrapper
from utils import safe_load
import numpy as np
import cv2
import sys
import json
import os
from utils import string_utils, error_rates
import time
import random
import yaml
from utils.dataset_parse import load_file_list
def training_step(config):
hw_network_config = config['network']['hw']
train_config = config['training']
allowed_training_time = train_config['hw']['reset_interval']
init_training_time = time.time()
char_set_path = hw_network_config['char_set_path']
with open(char_set_path) as f:
char_set = json.load(f)
idx_to_char = {}
for k,v in char_set['idx_to_char'].items():
idx_to_char[int(k)] = v
print("Number of outputs:", len(idx_to_char)+1)
training_set_list = load_file_list(train_config['training_set'])
train_dataset = HwDataset(training_set_list,
char_set['char_to_idx'], augmentation=True,
img_height=hw_network_config['input_height'])
train_dataloader = DataLoader(train_dataset,
batch_size=train_config['hw']['batch_size'],
shuffle=False, num_workers=0,
collate_fn=hw_dataset.collate)
batches_per_epoch = int(train_config['hw']['images_per_epoch']/train_config['hw']['batch_size'])
train_dataloader = DatasetWrapper(train_dataloader, batches_per_epoch)
test_set_list = load_file_list(train_config['validation_set'])
test_dataset = HwDataset(test_set_list,
char_set['char_to_idx'],
img_height=hw_network_config['input_height'],
random_subset_size=train_config['hw']['validation_subset_size'])
test_dataloader = DataLoader(test_dataset,
batch_size=train_config['hw']['batch_size'],
shuffle=False, num_workers=0,
collate_fn=hw_dataset.collate)
hw = cnn_lstm.create_model(hw_network_config)
hw_path = os.path.join(train_config['snapshot']['best_validation'], "hw.pt")
hw_state = safe_load.torch_state(hw_path)
hw.load_state_dict(hw_state)
hw.cuda()
criterion = CTCLoss(zero_infinity=True)
dtype = torch.cuda.FloatTensor
lowest_loss = np.inf
lowest_loss_i = 0
for epoch in range(100000000):
sum_loss = 0.0
steps = 0.0
hw.eval()
for x in test_dataloader:
sys.stdout.flush()
with torch.no_grad():
line_imgs = Variable(x['line_imgs'].type(dtype))
labels = Variable(x['labels'])
label_lengths = Variable(x['label_lengths'])
preds = hw(line_imgs).cpu()
output_batch = preds.permute(1,0,2)
out = output_batch.data.cpu().numpy()
# print(out)
for i, gt_line in enumerate(x['gt']):
logits = out[i,...]
pred, raw_pred = string_utils.naive_decode(logits)
pred_str = string_utils.label2str_single(pred, idx_to_char, False)
cer = error_rates.cer(gt_line, pred_str)
sum_loss += cer
steps += 1
if epoch == 0:
print("First Validation Step Complete")
print("Benchmark Validation CER:", sum_loss/steps)
lowest_loss = sum_loss/steps
hw = cnn_lstm.create_model(hw_network_config)
hw_path = os.path.join(train_config['snapshot']['current'], "hw.pt")
hw_state = safe_load.torch_state(hw_path)
hw.load_state_dict(hw_state)
hw.cuda()
optimizer = torch.optim.Adam(hw.parameters(), lr=train_config['hw']['learning_rate'])
optim_path = os.path.join(train_config['snapshot']['current'], "hw_optim.pt")
if os.path.exists(optim_path):
print("Loading Optim Settings")
optimizer.load_state_dict(safe_load.torch_state(optim_path))
else:
print("Failed to load Optim Settings")
if lowest_loss > sum_loss/steps:
lowest_loss = sum_loss/steps
print("Saving Best")
dirname = train_config['snapshot']['best_validation']
if not len(dirname) != 0 and os.path.exists(dirname):
os.makedirs(dirname)
save_path = os.path.join(dirname, "hw.pt")
torch.save(hw.state_dict(), save_path)
lowest_loss_i = epoch
print("Test Loss", sum_loss/steps, lowest_loss)
print("")
if allowed_training_time < (time.time() - init_training_time):
print("Out of time: Exiting...")
break
print("Epoch", epoch)
sum_loss = 0.0
steps = 0.0
hw.train()
for i, x in enumerate(train_dataloader):
with torch.no_grad():
line_imgs = Variable(x['line_imgs'].type(dtype))
labels = Variable(x['labels'])
label_lengths = Variable(x['label_lengths'])
preds = hw(line_imgs).cpu()
output_batch = preds.permute(1,0,2)
out = output_batch.data.cpu().numpy()
# if i == 0:
# for i in xrange(out.shape[0]):
# pred, pred_raw = string_utils.naive_decode(out[i,...])
# pred_str = string_utils.label2str_single(pred_raw, idx_to_char, True)
# print pred_str
for i, gt_line in enumerate(x['gt']):
logits = out[i,...]
pred, raw_pred = string_utils.naive_decode(logits)
pred_str = string_utils.label2str_single(pred, idx_to_char, False)
# print(gt_line, pred_str)
cer = error_rates.cer(gt_line, pred_str)
sum_loss += cer
steps += 1
batch_size = preds.size(1)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
loss = criterion(preds, labels, preds_size, label_lengths)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("Train Loss", sum_loss/steps)
print("Real Epoch", train_dataloader.epoch)
## Save current snapshots for next iteration
print("Saving Current")
dirname = train_config['snapshot']['current']
if not len(dirname) != 0 and os.path.exists(dirname):
os.makedirs(dirname)
save_path = os.path.join(dirname, "hw.pt")
torch.save(hw.state_dict(), save_path)
optim_path = os.path.join(dirname, "hw_optim.pt")
torch.save(optimizer.state_dict(), optim_path)
# sys.exit()
if __name__ == "__main__":
config_path = sys.argv[1]
with open(config_path) as f:
config = yaml.safe_load(f)
cnt = 0
if True:
print("")
print("Full Step", cnt)
print("")
cnt += 1
training_step(config)
# sys.exit()