-
Notifications
You must be signed in to change notification settings - Fork 520
/
train.py
163 lines (135 loc) · 5.6 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
#coding=utf-8
# pylint: disable=C0111,too-many-arguments,too-many-instance-attributes,too-many-locals,redefined-outer-name,fixme
# pylint: disable=superfluous-parens, no-member, invalid-name
import sys
sys.path.insert(0, "../../python")
import mxnet as mx
import numpy as np
import cv2, random
from io import BytesIO
from genplate import *
class OCRBatch(object):
def __init__(self, data_names, data, label_names, label):
self.data = data
self.label = label
self.data_names = data_names
self.label_names = label_names
@property
def provide_data(self):
return [(n, x.shape) for n, x in zip(self.data_names, self.data)]
@property
def provide_label(self):
return [(n, x.shape) for n, x in zip(self.label_names, self.label)]
def rand_range(lo,hi):
return lo+r(hi-lo);
def gen_rand():
name = "";
label= [];
label.append(rand_range(0,31));
label.append(rand_range(41,65));
for i in xrange(5):
label.append(rand_range(31,65))
name+=chars[label[0]]
name+=chars[label[1]]
for i in xrange(5):
name+=chars[label[i+2]]
return name,label
def gen_sample(genplate, width, height):
num,label = gen_rand()
img = genplate.generate(num)
img = cv2.resize(img, (width, height))
img = np.multiply(img, 1/255.0)
img = img.transpose(2, 0, 1)
return label, img
class OCRIter(mx.io.DataIter):
def __init__(self, count, batch_size, num_label, height, width):
super(OCRIter, self).__init__()
self.genplate = GenPlate("./font/platech.ttf",'./font/platechar.ttf','./NoPlates')
self.batch_size = batch_size
self.count = count
self.height = height
self.width = width
self.provide_data = [('data', (batch_size, 3, height, width))]
self.provide_label = [('softmax_label', (self.batch_size, num_label))]
print "start"
def __iter__(self):
for k in range(self.count / self.batch_size):
data = []
label = []
for i in range(self.batch_size):
num, img = gen_sample(self.genplate, self.width, self.height)
data.append(img)
label.append(num)
data_all = [mx.nd.array(data)]
label_all = [mx.nd.array(label)]
data_names = ['data']
label_names = ['softmax_label']
data_batch = OCRBatch(data_names, data_all, label_names, label_all)
yield data_batch
def reset(self):
pass
def get_ocrnet():
data = mx.symbol.Variable('data')
label = mx.symbol.Variable('softmax_label')
conv1 = mx.symbol.Convolution(data=data, kernel=(5,5), num_filter=32)
pool1 = mx.symbol.Pooling(data=conv1, pool_type="max", kernel=(2,2), stride=(1, 1))
relu1 = mx.symbol.Activation(data=pool1, act_type="relu")
conv2 = mx.symbol.Convolution(data=relu1, kernel=(5,5), num_filter=32)
pool2 = mx.symbol.Pooling(data=conv2, pool_type="avg", kernel=(2,2), stride=(1, 1))
relu2 = mx.symbol.Activation(data=pool2, act_type="relu")
# conv3 = mx.symbol.Convolution(data=relu2, kernel=(3,3), num_filter=32)
# pool3 = mx.symbol.Pooling(data=conv3, pool_type="avg", kernel=(2,2), stride=(1, 1))
# relu3 = mx.symbol.Activation(data=pool3, act_type="relu")
#
# conv4 = mx.symbol.Convolution(data=relu3, kernel=(3,3), num_filter=32)
# pool4 = mx.symbol.Pooling(data=conv4, pool_type="avg", kernel=(2,2), stride=(1, 1))
# relu4 = mx.symbol.Activation(data=pool4, act_type="relu")
flatten = mx.symbol.Flatten(data = relu2)
fc1 = mx.symbol.FullyConnected(data = flatten, num_hidden = 120)
fc21 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc22 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc23 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc24 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc25 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc26 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc27 = mx.symbol.FullyConnected(data = fc1, num_hidden = 65)
fc2 = mx.symbol.Concat(*[fc21, fc22, fc23, fc24,fc25,fc26,fc27], dim = 0)
label = mx.symbol.transpose(data = label)
label = mx.symbol.Reshape(data = label, target_shape = (0, ))
return mx.symbol.SoftmaxOutput(data = fc2, label = label, name = "softmax")
def Accuracy(label, pred):
label = label.T.reshape((-1, ))
hit = 0
total = 0
for i in range(pred.shape[0] / 7):
ok = True
for j in range(7):
k = i * 7 + j
if np.argmax(pred[k]) != int(label[k]):
ok = False
break
if ok:
hit += 1
total += 1
return 1.0 * hit / total
def train():
network = get_ocrnet()
devs = [mx.gpu(i) for i in range(1)]
model = mx.model.FeedForward(
symbol = network,
num_epoch = 1,
learning_rate = 0.001,
wd = 0.00001,
initializer = mx.init.Xavier(factor_type="in", magnitude=2.34),
momentum = 0.9)
batch_size = 8
data_train = OCRIter(500000, batch_size, 7, 30, 120)
data_test = OCRIter(1000, batch_size,7, 30, 120)
import logging
head = '%(asctime)-15s %(message)s'
logging.basicConfig(level=logging.DEBUG, format=head)
model.fit(X = data_train, eval_data = data_test, eval_metric = Accuracy, batch_end_callback=mx.callback.Speedometer(batch_size, 50))
model.save("cnn-ocr")
print gen_rand()
if __name__ == '__main__':
train();