CheneyWong's picture
canonical OTSL ground truth: scripts/build_norm_pack.py
2239a78 verified
Raw History Blame Contribute Delete
4.96 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""用**规范 OTSL**(PaddleOCR-VL 1.6 原生约定)重建评测 pack 的 GT 列。
为什么
----
现有两个 pack 都有问题:
- /autodl-tmp/reshard : 数据集自带 OTSL —— 补 `<ecel>` 到固定列数,
且含 `<ulcel>`(1.6 词表里没有, 模型吐不出来)
- /autodl-tmp/reshard_fixgt : 我们用 html_to_otsl 重建的 —— 不补了, 但
rowspan 被拼成 `<fcel><ulcel>...`(同行标记),
续接行没有占位, 跟 1.6 的解析语义对不上
实测 `<ulcel>` 出现在: pubtables1m 37% / ccocr 33% / omnidocbench16 25% /
pubtabnet 23% / tablet 22% / omnidocbench15 20% / ocrbenchv2 17% 的 GT 行里,
而评测器 `canonical_teds.py` 的 OTSL_KNOWN_FOREIGN 没收录它 → 被当成**正文**吞掉。
做法: 从 pack 的 html 列按 1.6 官方语义重建(rowspan 续接是独立的 `<ucel>` 行),
写进新的 `otsl` 列, 图片等列原样搬运。定位符 `shard#rg#idx` 保持不变,
所以历史预测可以直接 join 回来重打分。
用法:
python3 src/build_norm_pack.py --src /autodl-tmp/reshard_fixgt --out /autodl-tmp/reshard_normgt
"""
import argparse
import multiprocessing as mp
import os
import sys
import time
sys.path.insert(0, "/autodl-fs/data/povl_v5/src")
import re
import pyarrow as pa
import pyarrow.parquet as pq
from otsl_norm import html_to_otsl, otsl_stats
# 有些 cohort 的 pack 里 html 列只是**空壳**(文字在源 OTSL 里):
# synthtabnet 29990 行没有 <table> / fintabnet 9569 行 html 全是 <td></td> /
# tabrecset_en 2162 行 / tabrecset_zh 全部。
# 从空壳重建会把 GT 的文字全冲掉(实测 tabrecset_en 0.8275 -> 0.3362)。
# 所以规则: **只有当 HTML 重建出的文字量不显著少于源 OTSL 时才用它**,
# 否则保留源 OTSL(这些源的源 OTSL 本身没有 <ulcel>, 是干净的)。
_ALNUM = re.compile(r"[0-9A-Za-z\u4e00-\u9fff]")
def _text_len(s):
return len(_ALNUM.findall(s or ""))
def work(job):
src, dst, f = job
t0 = time.time()
pf = pq.ParquetFile(f"{src}/{f}")
writer = None
n = n_html = n_ulcel = n_keep = 0
try:
for rg in range(pf.metadata.num_row_groups):
t = pf.read_row_group(rg)
cols = t.column_names
htmls = t.column("html").to_pylist() if "html" in cols else None
otsls = t.column("otsl").to_pylist() if "otsl" in cols else None
new = []
for i in range(t.num_rows):
o = otsls[i] if otsls else ""
h = htmls[i] if htmls else ""
if h and "<table" in h.lower():
c = html_to_otsl(h)
if c:
ot = _text_len(o)
ct = _text_len(c)
if ct >= 0.5 * ot or ot <= 20:
new.append(c)
n_html += 1
if "<ulcel>" in c:
n_ulcel += 1
continue
n_keep += 1
new.append(o or "")
n += 1
arrays = []
for c in cols:
if c == "otsl":
arrays.append(pa.array(new, type=pa.string()))
else:
arrays.append(t.column(c))
nt = pa.Table.from_arrays(arrays, names=cols)
if writer is None:
writer = pq.ParquetWriter(f"{dst}/{f}", nt.schema)
writer.write_table(nt)
finally:
if writer is not None:
writer.close()
return f, n, n_html, n_ulcel, n_keep, time.time() - t0
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--src", default="/autodl-tmp/reshard_fixgt")
ap.add_argument("--out", default="/autodl-tmp/reshard_normgt")
ap.add_argument("--workers", type=int, default=8)
a = ap.parse_args()
os.makedirs(a.out, exist_ok=True)
files = sorted(f for f in os.listdir(a.src) if f.endswith(".parquet"))
print(f"{len(files)} 个 shard, {a.workers} workers -> {a.out}")
jobs = [(a.src, a.out, f) for f in files]
t0 = time.time()
tot = tot_html = tot_ul = tot_keep = 0
with mp.Pool(a.workers) as pool:
for i, (f, n, nh, nu, nk, dt) in enumerate(pool.imap_unordered(work, jobs)):
tot += n
tot_html += nh
tot_ul += nu
tot_keep += nk
print(f" [{i+1}/{len(files)}] {f} html重建 {nh} 沿用旧 {n} "
f"(html文字不足而保留 {nk}) 重建里仍带<ulcel> {nu} {dt:.0f}s", flush=True)
print(f"完成 {time.time()-t0:.0f}s: html重建 {tot_html} 条, 沿用旧 {tot} 条 "
f"(其中 html文字不足而保留 {tot_keep} 条), 重建结果仍含 <ulcel> {tot_ul} 条")
if __name__ == "__main__":
main()