-
Notifications
You must be signed in to change notification settings - Fork 5
/
crnn-test.py
140 lines (115 loc) · 5.93 KB
/
crnn-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
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17-9-25 下午3:56
# @Author : Luo Yao
# @Site : http://github.com/TJCVRS
# @File : test_shadownet.py
# @IDE: PyCharm Community Edition
"""
Test shadow net script
"""
import os
import os.path as ops
from typing import Tuple
import tensorflow as tf
import matplotlib.pyplot as plt
import argparse
import numpy as np
import math
from utils import data_utils
from utils.config_utils import load_config
from utils.log_utils import compute_accuracy
from models.crnn import crnn_model
from easydict import EasyDict
def init_args() -> Tuple[argparse.Namespace, EasyDict]:
"""
:return: parsed arguments and (updated) config.cfg object
"""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dataset_dir', type=str,
help='Directory containing test_features.tfrecords')
parser.add_argument('-c', '--chardict_dir', type=str,
help='Directory where character dictionaries for the dataset were stored')
parser.add_argument('-w', '--weights_path', type=str, required=True,
help='Path to pre-trained weights')
parser.add_argument('-n', '--num_classes', type=int, required=True,
help='Force number of character classes to this number. '
'Use 37 to run with the demo data. '
'Set to 0 for auto (read from files in charset_dir)')
parser.add_argument('-f', '--config_file', type=str,
help='Use this global configuration file')
parser.add_argument('-v', '--visualize', type=bool, default=False,
help='Whether to display images')
parser.add_argument('-b', '--one_batch', default=False,
action='store_true', help='Test only one batch of the dataset')
parser.add_argument('-j', '--num_threads', type=int,
default=int(os.cpu_count() / 2),
help='Number of threads to use in batch shuffling')
args = parser.parse_args()
config = load_config(args.config_file)
if args.dataset_dir:
config.cfg.PATH.TFRECORDS_DIR = args.dataset_dir
if args.chardict_dir:
config.cfg.PATH.CHAR_DICT_DIR = args.chardict_dir
return args, config.cfg
def test_shadownet(dataset_dir: str, weights_path: str, cfg: EasyDict, visualize: bool, process_all_data: bool=True, num_threads: int=4, num_classes: int=0):
"""
:param tfrecords_dir: Directory with test_feature.tfrecords
:param charset_dir: Path to char_dict.json and ord_map.json (generated with write_text_features.py)
:param weights_path: Path to stored weights
:param cfg: configuration EasyDict (e.g. global_config.config.cfg)
:param visualize: whether to display the images
:param process_all_data:
:param num_threads: Number of threads for tf.train.(shuffle_)batch
:param num_classes: Number of different characters in the dataset
"""
# Initialize the record decoder
decoder = data_utils.TextFeatureIO().reader
images_t, labels_t, imagenames_t = decoder.read_features(ops.join(dataset_dir, 'train_feature.tfrecords'), cfg.TEST.BATCH_SIZE, num_threads)
# build shadownet
num_classes = len(decoder.char_dict) + 1 if num_classes == 0 else num_classes
net = crnn_model.ShadowNet(phase='Test', hidden_nums=cfg.ARCH.HIDDEN_UNITS,
layers_nums=cfg.ARCH.HIDDEN_LAYERS,
num_classes=num_classes)
with tf.variable_scope('shadow'):
net_out = net.build_shadownet(inputdata=images_t)
decoded, _ = tf.nn.ctc_beam_search_decoder(net_out,
cfg.ARCH.SEQ_LENGTH * np.ones(cfg.TEST.BATCH_SIZE),
merge_repeated=False)
# config tf session
sess_config = tf.ConfigProto()
sess_config.gpu_options.per_process_gpu_memory_fraction = cfg.TRAIN.GPU_MEMORY_FRACTION
sess_config.gpu_options.allow_growth = cfg.TRAIN.TF_ALLOW_GROWTH
# config tf saver
saver = tf.train.Saver()
sess = tf.Session(config=sess_config)
test_sample_count = sum(1 for _ in tf.python_io.tf_record_iterator(
ops.join(cfg.PATH.TFRECORDS_DIR, 'test_feature.tfrecords')))
num_iterations = int(math.ceil(test_sample_count / cfg.TEST.BATCH_SIZE)) if process_all_data \
else 1
with sess.as_default():
saver.restore(sess=sess, save_path=weights_path)
print('Start predicting...')
accuracy = 0
for epoch in range(num_iterations):
predictions, images, labels, imagenames = sess.run([decoded, images_t, labels_t, imagenames_t])
imagenames = np.reshape(imagenames, newshape=imagenames.shape[0])
imagenames = [tmp.decode('utf-8') for tmp in imagenames]
labels = decoder.sparse_tensor_to_str(labels)
predictions = decoder.sparse_tensor_to_str(predictions[0])
accuracy += compute_accuracy(labels, predictions, display=False)
for index, image in enumerate(images):
print('Predict {:s} image with gt label: {:s} **** predicted label: {:s}'.format(
imagenames[index], labels[index], predictions[index]))
# avoid accidentally displaying for the whole dataset
if visualize and not process_all_data:
plt.imshow(image[:, :, (2, 1, 0)])
plt.show()
# We compute a mean of means, so we need the sample sizes to be constant
# (BATCH_SIZE) for this to equal the actual mean
accuracy /= num_iterations
print('Mean test accuracy is {:5f}'.format(accuracy))
if __name__ == '__main__':
args, cfg = init_args()
test_shadownet(dataset_dir=args.dataset_dir,weights_path=args.weights_path, cfg=cfg, process_all_data=not args.one_batch,
visualize=args.visualize, num_threads=args.num_threads, num_classes=args.num_classes)