forked from breezedeus/CnOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnocr_evaluate.py
More file actions
175 lines (151 loc) · 5.68 KB
/
cnocr_evaluate.py
File metadata and controls
175 lines (151 loc) · 5.68 KB
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
# coding: utf-8
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
""" An example of predicting CAPTCHA image data with a LSTM network pre-trained with a CTC loss"""
from __future__ import print_function
import sys
import os
import time
import logging
import argparse
from operator import itemgetter
from pathlib import Path
from collections import Counter
import mxnet as mx
import Levenshtein
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from cnocr import CnOcr
from cnocr.utils import set_logger
logger = set_logger(log_level=logging.INFO)
def evaluate():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-name", help="model name", type=str, default='densenet-lite-lstm'
)
parser.add_argument("--model-epoch", type=int, default=None, help="model epoch")
parser.add_argument(
"-i",
"--input-fp",
default='test.txt',
help="the file path with image names and labels",
)
parser.add_argument(
"--image-prefix-dir", default='.', help="图片所在文件夹,相对于索引文件中记录的图片位置"
)
parser.add_argument("--batch-size", type=int, default=128, help="batch size")
parser.add_argument(
"-v",
"--verbose",
action='store_true',
help="whether to print details to screen",
)
parser.add_argument(
"-o",
"--output-dir",
default=False,
help="the output directory which records the analysis results",
)
args = parser.parse_args()
ocr = CnOcr(model_name=args.model_name, model_epoch=args.model_epoch)
alphabet = ocr._alphabet
fn_labels_list = read_input_file(args.input_fp)
miss_cnt, redundant_cnt = Counter(), Counter()
model_time_cost = 0.0
start_idx = 0
bad_cnt = 0
badcases = []
while start_idx < len(fn_labels_list):
logger.info('start_idx: %d', start_idx)
batch = fn_labels_list[start_idx : start_idx + args.batch_size]
batch_img_fns = []
batch_labels = []
batch_imgs = []
for fn, labels in batch:
batch_labels.append(labels)
img_fp = os.path.join(args.image_prefix_dir, fn)
batch_img_fns.append(img_fp)
img = mx.image.imread(img_fp, 1).asnumpy()
batch_imgs.append(img)
start_time = time.time()
batch_preds = ocr.ocr_for_single_lines(batch_imgs)
model_time_cost += time.time() - start_time
for bad_info in compare_preds_to_reals(
batch_preds, batch_labels, batch_img_fns, alphabet
):
if args.verbose:
logger.info('\t'.join(bad_info))
distance = Levenshtein.distance(bad_info[1], bad_info[2])
bad_info.insert(0, distance)
badcases.append(bad_info)
miss_cnt.update(list(bad_info[-2]))
redundant_cnt.update(list(bad_info[-1]))
bad_cnt += 1
start_idx += args.batch_size
badcases.sort(key=itemgetter(0), reverse=True)
output_dir = Path(args.output_dir)
if not output_dir.exists():
os.makedirs(output_dir)
with open(output_dir / 'badcases.txt', 'w') as f:
f.write(
'\t'.join(
[
'distance',
'image_fp',
'real_words',
'pred_words',
'miss_words',
'redundant_words',
]
)
+ '\n'
)
for bad_info in badcases:
f.write('\t'.join(map(str, bad_info)) + '\n')
with open(output_dir / 'miss_words_stat.txt', 'w') as f:
for word, num in miss_cnt.most_common():
f.write('\t'.join([word, str(num)]) + '\n')
with open(output_dir / 'redundant_words_stat.txt', 'w') as f:
for word, num in redundant_cnt.most_common():
f.write('\t'.join([word, str(num)]) + '\n')
logger.info(
"number of total cases: %d, time cost per image: %f, number of bad cases: %d"
% (len(fn_labels_list), model_time_cost / len(fn_labels_list), bad_cnt)
)
def read_input_file(in_fp):
fn_labels_list = []
with open(in_fp) as f:
for line in f:
fields = line.strip().split()
fn_labels_list.append((fields[0], fields[1:]))
return fn_labels_list
def compare_preds_to_reals(batch_preds, batch_reals, batch_img_fns, alphabet):
for preds, reals, img_fn in zip(batch_preds, batch_reals, batch_img_fns):
reals = [alphabet[int(_id)] for _id in reals if _id != '0'] # '0' is padding id
if preds == reals:
continue
preds_set, reals_set = set(preds), set(reals)
miss_words = reals_set.difference(preds_set)
redundant_words = preds_set.difference(reals_set)
yield [
img_fn,
''.join(reals),
''.join(preds),
''.join(miss_words),
''.join(redundant_words),
]
if __name__ == '__main__':
evaluate()