forked from igormq/ctc_tensorflow_example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
executable file
·158 lines (121 loc) · 4.55 KB
/
common.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
# Copyright (c) 2016 Matthew Earl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
Definitions that don't fit elsewhere.
"""
import glob
import numpy
import cv2
import numpy as np
# Constants
import time
SPACE_INDEX = 0
# FIRST_INDEX = ord('0') - 1 # 0 is reserved to space
FIRST_INDEX = 1 # 0 is reserved to space
SPACE_TOKEN = '<space>'
__all__ = (
'DIGITS',
'sigmoid',
'softmax',
'CHARS'
)
DIGITS = "0123456789"
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CHARS = list(DIGITS + LETTERS)
LENGTHS = [6, 6] # the number of digits varies from LENGTHS[0] to LENGTHS[1] in a image
TEST_SIZE = 100
ADD_BLANK = True # if add a blank between digits
LEARNING_RATE_DECAY_FACTOR = 0.9 # The learning rate decay factor
INITIAL_LEARNING_RATE = 1e-3
DECAY_STEPS = 5000
# parameters for bdlstm ctc
BATCH_SIZE = 64
BATCHES = 100
OUTPUT_SHAPE = (BATCH_SIZE, 256)
TRAIN_SIZE = BATCH_SIZE * BATCHES
MOMENTUM = 0.9
REPORT_STEPS = 1000
# Hyper-parameters
num_epochs = 2000
num_hidden = 128
num_layers = 2
# Some configs
# Accounting the 0th indice + space + blank label = 28 characters
# num_classes = ord('9') - ord('0') + 1 + 1 + 1
num_classes = len(CHARS) + 1 + 1 # 10 digits + blank + ctc blank
print(num_classes)
def softmax(a):
exps = numpy.exp(a.astype(numpy.float64))
return exps / numpy.sum(exps, axis=-1)[:, numpy.newaxis]
def sigmoid(a):
return 1. / (1. + numpy.exp(-a))
"""
{"dirname":{"fname":(im,code)}}
"""
data_set = {}
def load_data_set(dirname):
fname_list = glob.glob(dirname + "/*.png")
result = dict()
print("loading", dirname)
for fname in sorted(fname_list):
im = cv2.imread(fname)[:, :, 0].astype(numpy.float32) / 255.
code = list(fname.split("/")[1].split("_")[1])
index = fname.split("/")[1].split("_")[0]
result[index] = (im, code)
data_set[dirname] = result
def read_data_for_lstm_ctc(dirname, start_index=None, end_index=None):
start = time.time()
fname_list = []
if dirname not in data_set.keys():
load_data_set(dirname)
if start_index is None:
f_list = glob.glob(dirname + "/*.png")
fname_list = [fname.split("/")[1].split("_")[0] for fname in f_list]
else:
for i in range(start_index, end_index):
fname_index = "{:08d}".format(i)
# print(fname_index)
fname_list.append(fname_index)
# print("regrex time ", time.time() - start)
start = time.time()
dir_data_set = data_set.get(dirname)
for fname in sorted(fname_list):
# im = cv2.imread(fname)[:, :, 0].astype(numpy.float32) / 255.
# code = list(fname.split("/")[1].split("_")[1])
im, code = dir_data_set.get(fname)
yield im, numpy.asarray(
[SPACE_INDEX if x == SPACE_TOKEN else (CHARS.index(x) + FIRST_INDEX) for x in list(code)])
# print("get time ", time.time() - start)
# print numpy.asarray([SPACE_INDEX if x == SPACE_TOKEN else (CHARS.index(x) + FIRST_INDEX) for x in list(code)])
def convert_original_code_train_code(code):
return numpy.asarray([SPACE_INDEX if x == SPACE_TOKEN else (CHARS.index(x) - FIRST_INDEX) for x in code])
def unzip(b):
xs, ys = zip(*b)
xs = numpy.array(xs)
ys = numpy.array(ys)
return xs, ys
if __name__ == '__main__':
train_inputs, train_codes = unzip(list(read_data_for_lstm_ctc("test"))[:2])
print(train_inputs.shape)
print(train_codes)
print("train_codes", train_codes)
targets = np.asarray(train_codes).flat[:]
print(targets)
print(list(read_data_for_lstm_ctc("test", 0, 10)))