-
Notifications
You must be signed in to change notification settings - Fork 3
/
crnn_main.py
207 lines (177 loc) · 6.72 KB
/
crnn_main.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
from __future__ import print_function
import argparse
import random
import torch
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
from warpctc_pytorch import CTCLoss
import os
import utils
import dataset
import models.crnn as crnn
import re
import params
training_Loss = []
testing_Rate = []
def init_args():
args = argparse.ArgumentParser()
args.add_argument('--trainroot', help='path to dataset', default='./to_lmdb/train_hand')
args.add_argument('--valroot', help='path to dataset', default='./to_lmdb/test_hand')
args.add_argument('--cuda', action='store_true', help='enables cuda', default=False)
return args.parse_args()
# custom weights initialization called on crnn
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def val(net, dataset, criterion, max_iter=100):
print('Start val')
for p in crnn.parameters():
p.requires_grad = False
net.eval()
data_loader = torch.utils.data.DataLoader(
dataset, shuffle=True, batch_size=params.batchSize, num_workers=int(params.workers))
val_iter = iter(data_loader)
i = 0
n_correct = 0
loss_avg = utils.averager()
max_iter = min(max_iter, len(data_loader))
for i in range(max_iter):
data = val_iter.next()
i += 1
cpu_images, cpu_texts = data
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
preds = crnn(image)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
cost = criterion(preds, text, preds_size, length) / batch_size
loss_avg.add(cost)
_, preds = preds.max(2)
preds = preds.transpose(1, 0).contiguous().view(-1)
sim_preds = converter.decode(preds.data, preds_size.data, raw=False)
list_1 = []
for i in cpu_texts:
list_1.append(i.decode('utf-8', 'strict'))
for pred, target in zip(sim_preds, list_1):
if pred == target:
n_correct += 1
raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:params.n_test_disp]
for raw_pred, pred, gt in zip(raw_preds, sim_preds, list_1):
print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt))
print(n_correct)
print(max_iter * params.batchSize)
accuracy = n_correct / float(max_iter * params.batchSize)
print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
testing_Rate.append(accuracy)
def trainBatch(crnn, criterion, optimizer, train_iter):
data = train_iter.next()
cpu_images, cpu_texts = data
# For Debug
image_np = np.array(cpu_images)
image_np = np.squeeze(image_np)
plt.imshow(image_np)
plt.show()
batch_size = cpu_images.size(0)
utils.loadData(image, cpu_images)
t, l = converter.encode(cpu_texts)
utils.loadData(text, t)
utils.loadData(length, l)
preds = crnn(image)
preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size))
cost = criterion(preds, text, preds_size, length) / batch_size
crnn.zero_grad()
cost.backward()
optimizer.step()
return cost
def training(crnn,train_loader,criterion,optimizer):
for total_steps in range(params.niter):
train_iter = iter(train_loader)
i = 0
print(len(train_loader))
while i < len(train_loader):
for p in crnn.parameters():
p.requires_grad = True
crnn.train()
cost = trainBatch(crnn, criterion, optimizer, train_iter)
loss_avg.add(cost)
i += 1
if i % params.displayInterval == 0:
print('[%d/%d][%d/%d] Loss: %f' %
(total_steps, params.niter, i, len(train_loader), loss_avg.val()))
training_Loss.append(loss_avg.val())
loss_avg.reset()
if i % params.valInterval == 0:
val(crnn, test_dataset, criterion)
if (total_steps + 1) % params.saveInterval == 0:
torch.save(crnn.state_dict(), '{0}/crnn_Rec_done_{1}_{2}.pth'.format(params.experiment, total_steps, i))
if __name__ == '__main__':
args = init_args()
manualSeed = random.randint(1, 10000) # fix seed
random.seed(manualSeed)
np.random.seed(manualSeed)
torch.manual_seed(manualSeed)
cudnn.benchmark = True
# store model path
if not os.path.exists('./expr'):
os.mkdir('./expr')
# read train set
train_dataset = dataset.lmdbDataset(root=args.trainroot)
assert train_dataset
if not params.random_sample:
sampler = dataset.randomSequentialSampler(train_dataset, params.batchSize)
else:
sampler = None
# images will be resize to 32*160
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=params.batchSize,
shuffle=True, sampler=sampler,
num_workers=int(params.workers),
collate_fn=dataset.alignCollate(imgH=params.imgH, imgW=params.imgW, keep_ratio=params.keep_ratio))
# read test set
# images will be resize to 32*160
test_dataset = dataset.lmdbDataset(
root=args.valroot, transform=dataset.resizeNormalize((320, 32)))
nclass = len(params.alphabet) + 1
nc = 1
converter = utils.strLabelConverter(params.alphabet)
criterion = CTCLoss()
#criterion = torch.nn.CTCLoss()
# cnn and rnn
image = torch.FloatTensor(params.batchSize, 3, params.imgH, params.imgH)
text = torch.IntTensor(params.batchSize * 5)
length = torch.IntTensor(params.batchSize)
crnn = crnn.CRNN(params.imgH, nc, nclass, params.nh)
if args.cuda:
crnn.cuda()
image = image.cuda()
criterion = criterion.cuda()
crnn.apply(weights_init)
if params.crnn != '':
print('loading pretrained model from %s' % params.crnn)
crnn.load_state_dict(torch.load(params.crnn))
image = Variable(image)
text = Variable(text)
length = Variable(length)
# loss averager
loss_avg = utils.averager()
# setup optimizer
if params.adam:
optimizer = optim.Adam(crnn.parameters(), lr=params.lr,
betas=(params.beta1, 0.999))
elif params.adadelta:
optimizer = optim.Adadelta(crnn.parameters(), lr=params.lr)
else:
optimizer = optim.RMSprop(crnn.parameters(), lr=params.lr)
training(crnn,train_loader,criterion,optimizer)
np.save('loss.npy', training_Loss)
np.save('Accuracy.npy', testing_Rate)