-
Notifications
You must be signed in to change notification settings - Fork 12
/
mmocr_teds_acc_mp.py
423 lines (357 loc) · 16.1 KB
/
mmocr_teds_acc_mp.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
import json
import time
import pickle
from metric import TEDS
from multiprocessing import Pool
import glob
import re
import copy
import sys
def deal_isolate_span(thead_part):
"""
Deal with isolate span cases in this function.
It causes by wrong prediction in structure recognition model.
eg. predict <td rowspan="2"></td> to <td></td> rowspan="2"></b></td>.
:param thead_part:
:return:
"""
# 1. find out isolate span tokens.
isolate_pattern = "<td></td> rowspan=\"(\d)+\" colspan=\"(\d)+\"></b></td>|" \
"<td></td> colspan=\"(\d)+\" rowspan=\"(\d)+\"></b></td>|" \
"<td></td> rowspan=\"(\d)+\"></b></td>|" \
"<td></td> colspan=\"(\d)+\"></b></td>"
isolate_iter = re.finditer(isolate_pattern, thead_part)
isolate_list = [i.group() for i in isolate_iter]
# 2. find out span number, by step 1 results.
span_pattern = " rowspan=\"(\d)+\" colspan=\"(\d)+\"|" \
" colspan=\"(\d)+\" rowspan=\"(\d)+\"|" \
" rowspan=\"(\d)+\"|" \
" colspan=\"(\d)+\""
corrected_list = []
for isolate_item in isolate_list:
span_part = re.search(span_pattern, isolate_item)
spanStr_in_isolateItem = span_part.group()
# 3. merge the span number into the span token format string.
if spanStr_in_isolateItem is not None:
corrected_item = '<td{}></td>'.format(spanStr_in_isolateItem)
corrected_list.append(corrected_item)
else:
corrected_list.append(None)
# 4. replace original isolated token.
for corrected_item, isolate_item in zip(corrected_list,isolate_list):
if corrected_item is not None:
thead_part = thead_part.replace(isolate_item, corrected_item)
else:
pass
return thead_part
def deal_duplicate_bb(thead_part):
"""
Deal duplicate <b> or </b> after replace.
Keep one <b></b> in a <td></td> token.
:param thead_part:
:return:
"""
# 1. find out <td></td> in <thead></thead>.
td_pattern = "<td rowspan=\"(\d)+\" colspan=\"(\d)+\">(.+?)</td>|" \
"<td colspan=\"(\d)+\" rowspan=\"(\d)+\">(.+?)</td>|" \
"<td rowspan=\"(\d)+\">(.+?)</td>|" \
"<td colspan=\"(\d)+\">(.+?)</td>|" \
"<td>(.*?)</td>"
td_iter = re.finditer(td_pattern, thead_part)
td_list = [t.group() for t in td_iter]
# 2. is multiply <b></b> in <td></td> or not?
new_td_list = []
for td_item in td_list:
if td_item.count('<b>') > 1 or td_item.count('</b>') > 1:
# multiply <b></b> in <td></td> case.
# 1. remove all <b></b>
td_item = td_item.replace('<b>','').replace('</b>','')
# 2. replace <tb> -> <tb><b>, </tb> -> </b></tb>.
td_item = td_item.replace('<td>', '<td><b>').replace('</td>', '</b></td>')
new_td_list.append(td_item)
else:
new_td_list.append(td_item)
# 3. replace original thead part.
for td_item, new_td_item in zip(td_list, new_td_list):
thead_part = thead_part.replace(td_item, new_td_item)
return thead_part
def deal_bb(result_token, tag_='thead'):
"""
In our opinion, <b></b> always occurs in <thead></thead> text's context.
This function will find out all tokens in <thead></thead> and insert <b></b> by manual.
:param result_token:
:param tag_:
:return:
"""
# find out <thead></thead> parts.
thead_pattern = '<' + tag_ + '>(.*?)</' + tag_ + '>'
if re.search(thead_pattern, result_token) is None:
return result_token
thead_part = re.search(thead_pattern, result_token).group()
origin_thead_part = copy.deepcopy(thead_part)
# check "rowspan" or "colspan" occur in <thead></thead> parts or not .
span_pattern = "<td rowspan=\"(\d)+\" colspan=\"(\d)+\">|<td colspan=\"(\d)+\" rowspan=\"(\d)+\">|<td rowspan=\"(\d)+\">|<td colspan=\"(\d)+\">"
span_iter = re.finditer(span_pattern, thead_part)
span_list = [s.group() for s in span_iter]
has_span_in_head = True if len(span_list) > 0 else False
if not has_span_in_head:
# <thead></thead> not include "rowspan" or "colspan" branch 1.
# 1. replace <td> to <td><b>, and </td> to </b></td>
# 2. it is possible to predict text include <b> or </b> by Text-line recognition,
# so we replace <b><b> to <b>, and </b></b> to </b>
thead_part = thead_part.replace('<td>', '<td><b>')\
.replace('</td>', '</b></td>')\
.replace('<b><b>', '<b>')\
.replace('</b></b>', '</b>')
else:
# <thead></thead> include "rowspan" or "colspan" branch 2.
# Firstly, we deal rowspan or colspan cases.
# 1. replace > to ><b>
# 2. replace </td> to </b></td>
# 3. it is possible to predict text include <b> or </b> by Text-line recognition,
# so we replace <b><b> to <b>, and </b><b> to </b>
# Secondly, deal ordinary cases like branch 1
# replace ">" to "<b>"
replaced_span_list = []
for sp in span_list:
replaced_span_list.append(sp.replace('>', '><b>'))
for sp, rsp in zip(span_list, replaced_span_list):
thead_part = thead_part.replace(sp, rsp)
# replace "</td>" to "</b></td>"
thead_part = thead_part.replace('</td>', '</b></td>')
# remove duplicated <b> by re.sub
mb_pattern = "(<b>)+"
single_b_string = "<b>"
thead_part = re.sub(mb_pattern, single_b_string, thead_part)
mgb_pattern = "(</b>)+"
single_gb_string = "</b>"
thead_part = re.sub(mgb_pattern, single_gb_string, thead_part)
# ordinary cases like branch 1
thead_part = thead_part.replace('<td>', '<td><b>').replace('<b><b>', '<b>')
# convert <tb><b></b></tb> back to <tb></tb>, empty cell has no <b></b>.
# but space cell(<tb> </tb>) is suitable for <td><b> </b></td>
thead_part = thead_part.replace('<td><b></b></td>', '<td></td>')
# deal with duplicated <b></b>
thead_part = deal_duplicate_bb(thead_part)
# deal with isolate span tokens, which causes by wrong predict by structure prediction.
# eg.PMC5994107_011_00.png
thead_part = deal_isolate_span(thead_part)
# replace original result with new thead part.
result_token = result_token.replace(origin_thead_part, thead_part)
return result_token
def merge_span_token(master_token_list):
"""
Merge the span style token (row span or col span).
:param master_token_list:
:return:
"""
new_master_token_list = []
pointer = 0
if master_token_list[-1] != '</tbody>':
master_token_list.append('</tbody>')
while pointer < len(master_token_list) and master_token_list[pointer] != '</tbody>':
try:
if master_token_list[pointer] == '<td':
if (pointer + 5) <= len(master_token_list) and (master_token_list[pointer+2].startswith(' colspan=') or
master_token_list[pointer+2].startswith(' rowspan=')):
"""
example:
pattern <td rowspan="2" colspan="3">
'<td' + 'rowspan=" "' + 'colspan=" "' + '>' + '</td>'
"""
# tmp = master_token_list[pointer] + master_token_list[pointer+1] + \
# master_token_list[pointer+2] + master_token_list[pointer+3] + master_token_list[pointer+4]
tmp = ''.join(master_token_list[pointer:pointer+4+1])
pointer += 5
new_master_token_list.append(tmp)
elif (pointer + 4) <= len(master_token_list) and \
(master_token_list[pointer+1].startswith(' colspan=') or
master_token_list[pointer+1].startswith(' rowspan=')):
"""
example:
pattern <td colspan="3">
'<td' + 'colspan=" "' + '>' + '</td>'
"""
# tmp = master_token_list[pointer] + master_token_list[pointer+1] + master_token_list[pointer+2] + \
# master_token_list[pointer+3]
tmp = ''.join(master_token_list[pointer:pointer+3+1])
pointer += 4
new_master_token_list.append(tmp)
else:
new_master_token_list.append(master_token_list[pointer])
pointer += 1
else:
new_master_token_list.append(master_token_list[pointer])
pointer += 1
except:
print("Break in merge...")
break
new_master_token_list.append('</tbody>')
return new_master_token_list
def deal_eb_token(master_token):
"""
post process with <eb></eb>, <eb1></eb1>, ...
emptyBboxTokenDict = {
"[]": '<eb></eb>',
"[' ']": '<eb1></eb1>',
"['<b>', ' ', '</b>']": '<eb2></eb2>',
"['\\u2028', '\\u2028']": '<eb3></eb3>',
"['<sup>', ' ', '</sup>']": '<eb4></eb4>',
"['<b>', '</b>']": '<eb5></eb5>',
"['<i>', ' ', '</i>']": '<eb6></eb6>',
"['<b>', '<i>', '</i>', '</b>']": '<eb7></eb7>',
"['<b>', '<i>', ' ', '</i>', '</b>']": '<eb8></eb8>',
"['<i>', '</i>']": '<eb9></eb9>',
"['<b>', ' ', '\\u2028', ' ', '\\u2028', ' ', '</b>']": '<eb10></eb10>',
}
:param master_token:
:return:
"""
master_token = master_token.replace('<eb></eb>', '<td></td>')
master_token = master_token.replace('<eb1></eb1>', '<td> </td>')
master_token = master_token.replace('<eb2></eb2>', '<td><b> </b></td>')
master_token = master_token.replace('<eb3></eb3>', '<td>\u2028\u2028</td>')
master_token = master_token.replace('<eb4></eb4>', '<td><sup> </sup></td>')
master_token = master_token.replace('<eb5></eb5>', '<td><b></b></td>')
master_token = master_token.replace('<eb6></eb6>', '<td><i> </i></td>')
master_token = master_token.replace('<eb7></eb7>', '<td><b><i></i></b></td>')
master_token = master_token.replace('<eb8></eb8>', '<td><b><i> </i></b></td>')
master_token = master_token.replace('<eb9></eb9>', '<td><i></i></td>')
master_token = master_token.replace('<eb10></eb10>', '<td><b> \u2028 \u2028 </b></td>')
return master_token
def insert_text_to_token(master_token_list, cell_content_list):
"""
Insert OCR text result to structure token.
:param master_token_list:
:param cell_content_list:
:return:
"""
master_token_list = merge_span_token(master_token_list)
merged_result_list = []
text_count = 0
for master_token in master_token_list:
if master_token.startswith('<td'):
if text_count > len(cell_content_list)-1:
text_count += 1
continue
else:
master_token = master_token.replace('><', '>{}<'.format(cell_content_list[text_count]))
text_count += 1
master_token = deal_eb_token(master_token)
merged_result_list.append(master_token)
return ''.join(merged_result_list)
def text_to_list(master_token):
# insert virtual master token
master_token_list = master_token.split(',')
if master_token_list[-1] == '<td></td>':
master_token_list.append('</tr>')
master_token_list.append('</tbody>')
elif master_token_list[-1] != '</tbody>':
master_token_list.append('</tbody>')
if master_token_list[-2] != '</tr>':
master_token_list.insert(-1, '</tr>')
return master_token_list
def pickle_load(path, prefix='end2end'):
if os.path.isfile(path):
data = pickle.load(open(path, 'rb'))
elif os.path.isdir(path):
data = dict()
search_path = os.path.join(path, '{}_*.pkl'.format(prefix))
pkls = glob.glob(search_path)
for pkl in pkls:
this_data = pickle.load(open(pkl, 'rb'))
data.update(this_data)
else:
raise ValueError
return data
def htmlPostProcess(text):
text = '<html><body><table>' + text + '</table></body></html>'
return text
def singleEvaluation(teds, file_name, context, gt_context):
# save problem log
# save_folder = ''
# html format process
htmlContext = htmlPostProcess(context)
htmlGtContext = gt_context
# Evaluate
score = teds.evaluate(htmlContext, htmlGtContext)
print("FILENAME : {}".format(file_name))
print("SCORE : {}".format(score))
return score
if __name__ == "__main__":
epoch_id = int(sys.argv[1])
t_start = time.time()
pool = Pool(64)
start_time = time.time()
predFile = '/home2/nam/nam_data/work_dir/1114_TableMASTER_structure_seq500_cell150_batch4/structure_test_result_epoch_' + str(epoch_id)
gtJsonFile = '/home2/nam/nam_data/pubtabnet/icdar-task-b/final_eval.json'
# Initialize TEDS object
teds = TEDS(structure_only=True, n_jobs=1) #, ignore_nodes='b')
predDict = pickle_load(predFile, prefix='structure')
with open(gtJsonFile, 'r') as f:
gtValDict = json.load(f)
# assert len(predDict) == len(gtValDict) == 9115
# # cut 10 to debug
# file_names = [p for p in predDict.keys()][:10]
# cut_predDict = dict()
# for file_name in file_names:
# cut_predDict.setdefault(file_name, predDict[file_name])
# predDict = cut_predDict
scores_simple = []
scores_complex = []
caches = dict()
for idx, (file_name, context) in enumerate(predDict.items()):
# loading html of prediction
pred_text = context['text']
pred_cells = context['cell']
pred_html = insert_text_to_token(text_to_list(pred_text), pred_cells)
pred_html = deal_bb(pred_html, 'thead')
pred_html = deal_bb(pred_html, 'tbody')
# file_name = os.path.basename(file_path)
gt_context = gtValDict[file_name]
# print(file_name)
score = pool.apply_async(func=singleEvaluation, args=(teds, file_name, pred_html, gt_context['html'],))
if gt_context['type'] == 'simple':
scores_simple.append(score)
else:
scores_complex.append(score)
tmp = {'score':score, 'gt':gt_context['html'], 'pred':context}
caches.setdefault(file_name, tmp)
pool.close()
pool.join() # 进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。
pool.terminate()
# get score from scores
cal_scores = []
cal_scores_simple = []
for score in scores_simple:
cal_scores.append(score.get())
cal_scores_simple.append(score.get())
cal_scores_complex = []
for score in scores_complex:
cal_scores.append(score.get())
cal_scores_complex.append(score.get())
print('AVG TEDS score: {}'.format(sum(cal_scores)/len(cal_scores)))
print('AVG TEDS Simple score: {}'.format(sum(cal_scores_simple)/len(cal_scores_simple)))
print('AVG TEDS Complex score: {}'.format(sum(cal_scores_complex)/len(cal_scores_complex)))
print('TEDS cost time: {}s'.format(time.time()-start_time))
print('Number sample: {}'.format(len(cal_scores)))
with open(predFile + '/cfg.txt', 'a') as f:
f.write('TEDS struc AVG TEDS score: {}'.format(sum(cal_scores)/len(cal_scores)) + '\n')
f.write('AVG TEDS Simple score: {}'.format(sum(cal_scores_simple)/len(cal_scores_simple)) + '\n')
f.write('AVG TEDS Complex score: {}'.format(sum(cal_scores_complex)/len(cal_scores_complex)) + '\n')
f.write('TEDS cost time: {}s'.format(time.time()-start_time) + '\n')
f.write('Number sample: {}'.format(len(cal_scores)) + '\n')
# print("Save cache for analysis.")
# save_folder = '/disks/strg16-176/nam/work_dir/ted_caches_epoch_17'
# for file_name in caches.keys():
# info = caches[file_name]
# if info['score']._value < 1.0:
# f = open(os.path.join(save_folder, file_name.replace('.png', '.txt')), 'w')
# f.write(file_name+'\n'+'\n')
# f.write('Score:'+'\n')
# f.write(str(info['score']._value)+'\n'+'\n')
# f.write('Pred:'+'\n')
# f.write(info['pred']+'\n'+'\n')
# f.write('Gt:' + '\n')
# f.write(info['gt']+'\n'+'\n')