-
Notifications
You must be signed in to change notification settings - Fork 139
/
test.py
139 lines (98 loc) · 3.4 KB
/
test.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
from __future__ import print_function
###
# Copyright 2018 Edgard Chammas. All Rights Reserved.
# Licensed under the Creative Commons Attribution-NonCommercial International Public License, Version 4.0.
# You may obtain a copy of the License at https://creativecommons.org/licenses/by-nc/4.0/legalcode
###
#!/usr/bin/python
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
import sys
import os
import cv2
import numpy as np
import codecs
import math
try:
reload(sys) # Python 2
sys.setdefaultencoding('utf8')
except NameError:
pass # Python 3
from config import cfg
from util import LoadClasses
from util import LoadModel
from util import ReadData
from util import LoadList
from cnn import CNN
from cnn import WND_HEIGHT
from cnn import WND_WIDTH
from cnn import MPoolLayers_H
from rnn import RNN
if cfg.WriteDecodedToFile == True:
DecodeLog = codecs.open("decoded.txt", "w", "utf-8")
Classes = LoadClasses(cfg.CHAR_LIST)
NClasses = len(Classes)
FilesList = LoadList(cfg.TEST_LIST)
WND_SHIFT = WND_WIDTH - 2
VEC_PER_WND = WND_WIDTH / math.pow(2, MPoolLayers_H)
phase_train = tf.Variable(True, name='phase_train')
x = tf.placeholder(tf.float32, shape=[None, WND_HEIGHT, WND_WIDTH])
SeqLens = tf.placeholder(shape=[cfg.BatchSize], dtype=tf.int32)
x_expanded = tf.expand_dims(x, 3)
Inputs = CNN(x_expanded, phase_train, 'CNN_1')
logits = RNN(Inputs, SeqLens, 'RNN_1')
# CTC Beam Search Decoder to decode pred string from the prob map
decoded, log_prob = tf.nn.ctc_beam_search_decoder(logits, SeqLens)
#Reading test data...
InputListTest, SeqLensTest, _ = ReadData(cfg.TEST_LOCATION, cfg.TEST_LIST, cfg.TEST_NB, WND_HEIGHT, WND_WIDTH, WND_SHIFT, VEC_PER_WND, '')
print('Initializing...')
session = tf.Session()
session.run(tf.global_variables_initializer())
LoadModel(session, cfg.SaveDir+'/')
try:
session.run(tf.assign(phase_train, False))
randIxs = range(0, len(InputListTest))
start, end = (0, cfg.BatchSize)
batch = 0
while end <= len(InputListTest):
batchInputs = []
batchSeqLengths = []
for batchI, origI in enumerate(randIxs[start:end]):
batchInputs.extend(InputListTest[origI])
batchSeqLengths.append(SeqLensTest[origI])
feed = {x: batchInputs, SeqLens: batchSeqLengths}
del batchInputs, batchSeqLengths
Decoded = session.run([decoded], feed_dict=feed)[0]
del feed
trans = session.run(tf.sparse_tensor_to_dense(Decoded[0]))
for i in range(0, cfg.BatchSize):
fileIndex = cfg.BatchSize * batch + i
filename = FilesList[fileIndex].strip()
decodedStr = " "
for j in range(0, len(trans[i])):
if trans[i][j] == 0:
if (j != (len(trans[i]) - 1)):
if trans[i][j+1] == 0: break
else: decodedStr = "%s%s" % (decodedStr, Classes[trans[i][j]])
else:
break
else:
if trans[i][j] == (NClasses - 2):
if (j != 0): decodedStr = "%s " % (decodedStr)
else: continue
else:
decodedStr = "%s%s" % (decodedStr, Classes[trans[i][j]])
decodedStr = decodedStr.replace("<SPACE>", " ")
decodedStr = filename + decodedStr[:] + "\n"
if cfg.WriteDecodedToFile == True: DecodeLog.write(decodedStr)
else: print(decodedStr, end=' ')
start += cfg.BatchSize
end += cfg.BatchSize
batch += 1
DecodeLog.close()
except (KeyboardInterrupt, SystemExit, Exception) as e:
print("[Error/Interruption] %s" % str(e))
print("Clossing TF Session...")
session.close()
print("Terminating Program...")
sys.exit(0)