File size: 89,582 Bytes
d82db76 | 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 | """Training recipe generation for recognizer and corrector models."""
from __future__ import annotations
import json
import math
import random
import shlex
import shutil
import subprocess
import tarfile
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Sequence
from .admission import assert_recognizer_family_not_stopped
from .datasets import audit_recognizer_corpus
from .errors import DataValidationError
from .manifest import ManifestEntry, load_manifest, prepare_hf_correction_pairs, sha256_file, sha256_text, write_manifest
from .metrics import cer, exact_line_accuracy, wer
from .models import audit_model_card, write_model_card
from .normalization import dictionary_from_texts, normalize_ocr_text
PADDLE_DEVANAGARI_PPOCRV3_PRETRAINED = (
"https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/"
"devanagari_PP-OCRv3_mobile_rec_pretrained.pdparams"
)
# Current-generation default. The Devanagari PP-OCRv3 mobile family above is a confirmed
# dead end (recognizer-failure-review -> stop_family) and is wrong for non-Devanagari
# scripts. PP-OCRv5 mobile rec (SVTR_LCNet / PPLCNetV3 + CTC/NRTR) is the documented
# fine-tune base; its SVTR backbone transfers across scripts while the head re-inits for a
# changed character dictionary.
PADDLE_PPOCRV5_MOBILE_REC_PRETRAINED = (
"https://paddle-model-ecology.bj.bcebos.com/paddlex/official_pretrained_model/"
"PP-OCRv5_mobile_rec_pretrained.pdparams"
)
PADDLE_RECOGNIZER_MAIN_INDICATORS = frozenset({"acc", "norm_edit_dis"})
def write_recognizer_recipe(
train_manifest: str | Path,
eval_manifest: str | Path,
output_dir: str | Path,
*,
base_model: str = PADDLE_PPOCRV5_MOBILE_REC_PRETRAINED,
dictionary_path: str | Path | None = None,
train_batch_size: int = 64,
eval_batch_size: int = 64,
learning_rate: float = 0.001,
epochs: float | None = None,
warmup_epoch: float = 5.0,
use_gpu: bool = True,
train_num_workers: int = 2,
eval_num_workers: int = 2,
train_drop_last: bool = True,
main_indicator: str = "acc",
eval_batch_step: int = 200,
failure_review: str | Path | None = None,
run: bool = False,
) -> Path:
return write_paddle_recognizer_recipe(
train_manifest,
eval_manifest,
output_dir,
base_model=base_model,
dictionary_path=dictionary_path,
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
learning_rate=learning_rate,
epochs=epochs,
warmup_epoch=warmup_epoch,
use_gpu=use_gpu,
train_num_workers=train_num_workers,
eval_num_workers=eval_num_workers,
train_drop_last=train_drop_last,
main_indicator=main_indicator,
eval_batch_step=eval_batch_step,
failure_review=failure_review,
run=run,
)
def write_paddle_recognizer_recipe(
train_manifest: str | Path,
eval_manifest: str | Path,
output_dir: str | Path,
*,
base_model: str = PADDLE_PPOCRV5_MOBILE_REC_PRETRAINED,
dictionary_path: str | Path | None = None,
train_batch_size: int = 64,
eval_batch_size: int = 64,
learning_rate: float = 0.001,
epochs: float | None = None,
warmup_epoch: float = 5.0,
use_gpu: bool = True,
train_num_workers: int = 2,
eval_num_workers: int = 2,
train_drop_last: bool = True,
main_indicator: str = "acc",
eval_batch_step: int = 200,
failure_review: str | Path | None = None,
run: bool = False,
) -> Path:
if train_batch_size <= 0:
raise DataValidationError("train_batch_size must be positive")
if eval_batch_size <= 0:
raise DataValidationError("eval_batch_size must be positive")
if learning_rate <= 0:
raise DataValidationError("learning_rate must be positive")
if epochs is not None and epochs <= 0:
raise DataValidationError("epochs must be positive when provided")
if warmup_epoch < 0:
raise DataValidationError("warmup_epoch must be non-negative")
if train_num_workers < 0 or eval_num_workers < 0:
raise DataValidationError("num_workers must be non-negative")
if eval_batch_step <= 0:
raise DataValidationError("eval_batch_step must be positive")
if main_indicator not in PADDLE_RECOGNIZER_MAIN_INDICATORS:
allowed = ", ".join(sorted(PADDLE_RECOGNIZER_MAIN_INDICATORS))
raise DataValidationError(f"main_indicator must be one of: {allowed}")
if failure_review is not None:
assert_recognizer_family_not_stopped(failure_review, backend="paddleocr", base_model=base_model)
train_entries = load_manifest(train_manifest)
eval_entries = load_manifest(eval_manifest)
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
labels_dir = out / "labels"
labels_dir.mkdir(exist_ok=True)
train_label = labels_dir / "train.txt"
eval_label = labels_dir / "eval.txt"
_write_paddle_label_file(train_entries, train_label)
_write_paddle_label_file(eval_entries, eval_label)
dict_path = Path(dictionary_path) if dictionary_path else out / "nep_eng_dict.txt"
if dictionary_path is None:
chars = dictionary_from_texts([entry.text for entry in [*train_entries, *eval_entries]])
dict_path.write_text("\n".join(chars) + "\n", encoding="utf-8")
elif not dict_path.exists():
raise DataValidationError(f"dictionary_path does not exist: {dict_path}")
dictionary_chars = [line for line in dict_path.read_text(encoding="utf-8").splitlines() if line]
_validate_paddle_dictionary_coverage([*train_entries, *eval_entries], dictionary_chars, dict_path)
config_path = out / "paddle_recognizer.yml"
config_path.write_text(
_recognizer_config_text(
train_label=train_label,
eval_label=eval_label,
dictionary_path=dict_path,
base_model=base_model,
output_dir=out / "checkpoints",
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
learning_rate=learning_rate,
epochs=epochs,
warmup_epoch=warmup_epoch,
use_gpu=use_gpu,
train_num_workers=train_num_workers,
eval_num_workers=eval_num_workers,
train_drop_last=train_drop_last,
main_indicator=main_indicator,
eval_batch_step=eval_batch_step,
),
encoding="utf-8",
)
summary_path = out / "recognizer-training-summary.json"
summary_path.write_text(
json.dumps(
_recognizer_training_summary(
train_manifest=Path(train_manifest),
eval_manifest=Path(eval_manifest),
train_entries=train_entries,
eval_entries=eval_entries,
train_label=train_label,
eval_label=eval_label,
dictionary_path=dict_path,
dictionary_chars=dictionary_chars,
config_path=config_path,
base_model=base_model,
train_batch_size=train_batch_size,
eval_batch_size=eval_batch_size,
learning_rate=learning_rate,
epochs=epochs,
warmup_epoch=warmup_epoch,
use_gpu=use_gpu,
train_num_workers=train_num_workers,
eval_num_workers=eval_num_workers,
train_drop_last=train_drop_last,
main_indicator=main_indicator,
eval_batch_step=eval_batch_step,
),
ensure_ascii=False,
indent=2,
)
+ "\n",
encoding="utf-8",
)
(out / "README.md").write_text(
"\n".join(
[
"# Recognizer Training Recipe",
"",
f"Training summary: `{summary_path}`",
f"Base model: `{base_model}`",
f"Train labels: `{train_label}`",
f"Eval labels: `{eval_label}`",
f"Dictionary: `{dict_path}`",
f"Train batch size: `{train_batch_size}`",
f"Eval batch size: `{eval_batch_size}`",
f"Learning rate: `{learning_rate}`",
f"Epochs: `{epochs if epochs is not None else 'PaddleOCR default'}`",
f"Warmup epochs: `{warmup_epoch}`",
f"Use GPU: `{use_gpu}`",
f"Train num workers: `{train_num_workers}`",
f"Eval num workers: `{eval_num_workers}`",
f"Train drop last: `{train_drop_last}`",
f"Main indicator: `{main_indicator}`",
f"Eval batch step: `{eval_batch_step}`",
"",
"Run with a checked-out PaddleOCR repo:",
"",
"```bash",
f"python PaddleOCR/tools/train.py -c {config_path}",
"```",
"",
]
),
encoding="utf-8",
)
if run:
completed = subprocess.run(["python", "PaddleOCR/tools/train.py", "-c", str(config_path)], check=False)
if completed.returncode != 0:
raise DataValidationError(f"PaddleOCR training failed with exit code {completed.returncode}")
return config_path
def _recognizer_training_summary(
*,
train_manifest: Path,
eval_manifest: Path,
train_entries: list[ManifestEntry],
eval_entries: list[ManifestEntry],
train_label: Path,
eval_label: Path,
dictionary_path: Path,
dictionary_chars: list[str],
config_path: Path,
base_model: str,
train_batch_size: int,
eval_batch_size: int,
learning_rate: float,
epochs: float | None,
warmup_epoch: float,
use_gpu: bool,
train_num_workers: int,
eval_num_workers: int,
train_drop_last: bool,
main_indicator: str,
eval_batch_step: int,
) -> dict[str, Any]:
pack_content_sha256 = _training_pack_content_sha256(
train_manifest=train_manifest,
eval_manifest=eval_manifest,
train_entries=train_entries,
eval_entries=eval_entries,
)
return {
"backend": "paddleocr",
"base_model": base_model,
"pack_content_sha256": pack_content_sha256,
"training_pack": {
"schema": "ocrtech.recognizer-training-pack.v1",
"pack_content_sha256": pack_content_sha256,
},
"manifests": {
"train": _manifest_summary(train_manifest, train_entries),
"eval": _manifest_summary(eval_manifest, eval_entries),
},
"artifacts": {
"config": _file_summary(config_path),
"train_label": _file_summary(train_label),
"eval_label": _file_summary(eval_label),
"dictionary": {
**_file_summary(dictionary_path),
"character_count": len(dictionary_chars),
"contains_space": " " in dictionary_chars,
"contains_ascii": any(char.isascii() and char.isalnum() for char in dictionary_chars),
"contains_devanagari": any("\u0900" <= char <= "\u097f" for char in dictionary_chars),
},
},
"hyperparameters": {
"train_batch_size": train_batch_size,
"eval_batch_size": eval_batch_size,
"learning_rate": learning_rate,
"epochs": epochs,
"warmup_epoch": warmup_epoch,
"use_gpu": use_gpu,
"train_num_workers": train_num_workers,
"eval_num_workers": eval_num_workers,
"train_drop_last": train_drop_last,
"main_indicator": main_indicator,
"eval_batch_step": eval_batch_step,
},
}
def _manifest_summary(path: Path, entries: list[ManifestEntry]) -> dict[str, Any]:
texts = [entry.text for entry in entries]
chars = dictionary_from_texts(texts)
return {
"path": str(path),
"sha256": sha256_file(path) if path.exists() else None,
"sample_count": len(entries),
"dataset_counts": _count_values(entry.dataset for entry in entries),
"slice_counts": _slice_counts(entries),
"character_count": len(chars),
"contains_ascii": any(char.isascii() and char.isalnum() for char in chars),
"contains_devanagari": any("\u0900" <= char <= "\u097f" for char in chars),
}
def _file_summary(path: Path) -> dict[str, Any]:
return {
"path": str(path),
"sha256": sha256_file(path) if path.exists() and path.is_file() else None,
}
def _slice_counts(entries: list[ManifestEntry]) -> dict[str, int]:
counts: dict[str, int] = {}
for entry in entries:
for slice_name in _entry_slices(entry):
counts[slice_name] = counts.get(slice_name, 0) + 1
return dict(sorted(counts.items()))
def _entry_slices(entry: ManifestEntry) -> list[str]:
metadata = entry.metadata or {}
values: set[str] = set()
for key in ("slice", "script", "document_type", "language"):
value = metadata.get(key)
if isinstance(value, str) and value:
values.add(value)
for key in ("slices", "scripts", "tags"):
value = metadata.get(key)
if isinstance(value, list):
values.update(str(item) for item in value if str(item))
elif isinstance(value, str) and value:
values.add(value)
return sorted(values)
def _count_values(values: object) -> dict[str, int]:
counts: dict[str, int] = {}
for value in values:
key = str(value)
counts[key] = counts.get(key, 0) + 1
return dict(sorted(counts.items()))
def _write_paddle_label_file(entries: list[ManifestEntry], path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as handle:
for entry in entries:
if "\t" in entry.image_path or "\n" in entry.image_path or "\r" in entry.image_path:
raise DataValidationError(f"image_path contains a tab or newline and cannot be serialized for PaddleOCR: {entry.sample_id}")
handle.write(f"{entry.image_path}\t{_paddle_label_text(entry.text)}\n")
def _validate_paddle_dictionary_coverage(
entries: list[ManifestEntry],
dictionary_chars: list[str],
dictionary_path: Path,
) -> None:
allowed = set(dictionary_chars)
allowed.add(" ")
missing: dict[str, list[str]] = {}
for entry in entries:
label = _paddle_label_text(entry.text)
missing_chars = sorted({char for char in label if char not in allowed})
if missing_chars:
missing[entry.sample_id] = missing_chars
if missing:
details = [
f"{sample_id}: {''.join(chars)}"
for sample_id, chars in list(missing.items())[:10]
]
suffix = "; ".join(details)
raise DataValidationError(
f"dictionary_path is missing label characters for {len(missing)} samples: {dictionary_path}; {suffix}"
)
def _paddle_label_text(text: str) -> str:
label = normalize_ocr_text(text).replace("\t", " ")
return " ".join(label.split())
def _yaml_float(value: float) -> str:
if not math.isfinite(value):
raise DataValidationError(f"PaddleOCR YAML float must be finite: {value!r}")
if value == 0:
return "0.0"
text = f"{value:.12f}".rstrip("0").rstrip(".")
if "." not in text:
text += ".0"
return text
def _recognizer_config_text(
*,
train_label: Path,
eval_label: Path,
dictionary_path: Path,
base_model: str,
output_dir: Path,
train_batch_size: int,
eval_batch_size: int,
learning_rate: float,
epochs: float | None,
warmup_epoch: float,
use_gpu: bool,
train_num_workers: int,
eval_num_workers: int,
train_drop_last: bool,
main_indicator: str,
eval_batch_step: int,
) -> str:
epoch_value: int | float | None
if epochs is None:
epoch_value = None
elif float(epochs).is_integer():
epoch_value = int(epochs)
else:
epoch_value = epochs
epoch_line = f" epoch_num: {epoch_value}\n" if epoch_value is not None else ""
learning_rate_value = _yaml_float(float(learning_rate))
warmup_epoch_value = _yaml_float(float(warmup_epoch))
use_gpu_value = "true" if use_gpu else "false"
train_drop_last_value = "true" if train_drop_last else "false"
return f"""# Generated by ocrtech train-recognizer.
# PP-OCRv5 mobile recognizer recipe for PaddleOCR's tools/train.py.
# Keep in sync with PaddleOCR configs/rec/PP-OCRv5/multi_language/devanagari_PP-OCRv5_mobile_rec.yaml.
# Fine-tune from a PP-OCRv5 mobile rec checkpoint: the SVTR backbone transfers across
# scripts; the recognition head re-inits for a changed character dictionary (new script).
Global:
model_name: PP-OCRv5_mobile_rec
debug: false
use_gpu: {use_gpu_value}
{epoch_line} log_smooth_window: 20
print_batch_step: 10
pretrained_model: {base_model}
character_dict_path: {dictionary_path}
save_model_dir: {output_dir}
save_epoch_step: 1
eval_batch_step:
- 0
- {eval_batch_step}
cal_metric_during_train: true
use_space_char: true
max_text_length: &max_text_length 128
infer_mode: false
distributed: false
save_res_path: {output_dir.parent / "predicts.txt"}
Optimizer:
name: Adam
beta1: 0.9
beta2: 0.999
lr:
name: Cosine
learning_rate: {learning_rate_value}
warmup_epoch: {warmup_epoch_value}
regularizer:
name: L2
factor: 3.0e-05
Architecture:
model_type: rec
algorithm: SVTR_LCNet
Transform:
Backbone:
name: PPLCNetV3
scale: 0.95
Head:
name: MultiHead
head_list:
- CTCHead:
Neck:
name: svtr
dims: 120
depth: 2
hidden_dims: 120
kernel_size:
- 1
- 3
use_guide: true
Head:
fc_decay: 1.0e-05
- NRTRHead:
nrtr_dim: 384
max_text_length: *max_text_length
Loss:
name: MultiLoss
loss_config_list:
- CTCLoss:
- NRTRLoss:
PostProcess:
name: CTCLabelDecode
Metric:
name: RecMetric
main_indicator: {main_indicator}
ignore_space: false
Train:
dataset:
name: SimpleDataSet
data_dir: .
ext_op_transform_idx: 1
label_file_list:
- {train_label}
transforms:
- DecodeImage:
img_mode: BGR
channel_first: false
- RecConAug:
prob: 0.5
ext_data_num: 2
image_shape:
- 48
- 320
- 3
max_text_length: *max_text_length
- RecAug:
- MultiLabelEncode:
gtc_encode: NRTRLabelEncode
- RecResizeImg:
image_shape:
- 3
- 48
- 320
- KeepKeys:
keep_keys:
- image
- label_ctc
- label_gtc
- length
- valid_ratio
loader:
shuffle: true
batch_size_per_card: {train_batch_size}
drop_last: {train_drop_last_value}
num_workers: {train_num_workers}
Eval:
dataset:
name: SimpleDataSet
data_dir: .
label_file_list:
- {eval_label}
transforms:
- DecodeImage:
img_mode: BGR
channel_first: false
- MultiLabelEncode:
gtc_encode: NRTRLabelEncode
- RecResizeImg:
image_shape:
- 3
- 48
- 320
- KeepKeys:
keep_keys:
- image
- label_ctc
- label_gtc
- length
- valid_ratio
loader:
shuffle: false
batch_size_per_card: {eval_batch_size}
drop_last: false
num_workers: {eval_num_workers}
"""
@dataclass(slots=True)
class HfRecognizerTrainingConfig:
train_manifest: str
eval_manifest: str
output_dir: str
base_model: str
max_target_length: int
per_device_train_batch_size: int
per_device_eval_batch_size: int
learning_rate: float
num_train_epochs: float
warmup_steps: int
logging_steps: int
save_total_limit: int
predict_with_generate: bool
generation_max_length: int
def to_dict(self) -> dict[str, Any]:
return {
"train_manifest": self.train_manifest,
"eval_manifest": self.eval_manifest,
"output_dir": self.output_dir,
"base_model": self.base_model,
"max_target_length": self.max_target_length,
"per_device_train_batch_size": self.per_device_train_batch_size,
"per_device_eval_batch_size": self.per_device_eval_batch_size,
"learning_rate": self.learning_rate,
"num_train_epochs": self.num_train_epochs,
"warmup_steps": self.warmup_steps,
"logging_steps": self.logging_steps,
"save_total_limit": self.save_total_limit,
"predict_with_generate": self.predict_with_generate,
"generation_max_length": self.generation_max_length,
}
@classmethod
def from_path(cls, path: str | Path) -> "HfRecognizerTrainingConfig":
payload = json.loads(Path(path).read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise DataValidationError("hf recognizer config must be a JSON object")
return cls(
train_manifest=str(payload.get("train_manifest") or ""),
eval_manifest=str(payload.get("eval_manifest") or ""),
output_dir=str(payload.get("output_dir") or ""),
base_model=str(payload.get("base_model") or ""),
max_target_length=int(payload.get("max_target_length", 128)),
per_device_train_batch_size=int(payload.get("per_device_train_batch_size", 8)),
per_device_eval_batch_size=int(payload.get("per_device_eval_batch_size", 8)),
learning_rate=float(payload.get("learning_rate", 5e-5)),
num_train_epochs=float(payload.get("num_train_epochs", 3.0)),
warmup_steps=int(payload.get("warmup_steps", 0)),
logging_steps=int(payload.get("logging_steps", 10)),
save_total_limit=int(payload.get("save_total_limit", 2)),
predict_with_generate=bool(payload.get("predict_with_generate", True)),
generation_max_length=int(payload.get("generation_max_length", payload.get("max_target_length", 128))),
)
@dataclass(slots=True)
class HfTextCorrectorTrainingConfig:
train_pairs: str
eval_pairs: str
output_dir: str
base_model: str
max_source_length: int
max_target_length: int
per_device_train_batch_size: int
per_device_eval_batch_size: int
learning_rate: float
num_train_epochs: float
warmup_steps: int
logging_steps: int
save_total_limit: int
predict_with_generate: bool
generation_max_length: int
def to_dict(self) -> dict[str, Any]:
return {
"train_pairs": self.train_pairs,
"eval_pairs": self.eval_pairs,
"output_dir": self.output_dir,
"base_model": self.base_model,
"max_source_length": self.max_source_length,
"max_target_length": self.max_target_length,
"per_device_train_batch_size": self.per_device_train_batch_size,
"per_device_eval_batch_size": self.per_device_eval_batch_size,
"learning_rate": self.learning_rate,
"num_train_epochs": self.num_train_epochs,
"warmup_steps": self.warmup_steps,
"logging_steps": self.logging_steps,
"save_total_limit": self.save_total_limit,
"predict_with_generate": self.predict_with_generate,
"generation_max_length": self.generation_max_length,
}
@classmethod
def from_path(cls, path: str | Path) -> "HfTextCorrectorTrainingConfig":
payload = json.loads(Path(path).read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise DataValidationError("hf text corrector config must be a JSON object")
return cls(
train_pairs=str(payload.get("train_pairs") or ""),
eval_pairs=str(payload.get("eval_pairs") or ""),
output_dir=str(payload.get("output_dir") or ""),
base_model=str(payload.get("base_model") or ""),
max_source_length=int(payload.get("max_source_length", 256)),
max_target_length=int(payload.get("max_target_length", 256)),
per_device_train_batch_size=int(payload.get("per_device_train_batch_size", 16)),
per_device_eval_batch_size=int(payload.get("per_device_eval_batch_size", 16)),
learning_rate=float(payload.get("learning_rate", 5e-5)),
num_train_epochs=float(payload.get("num_train_epochs", 3.0)),
warmup_steps=int(payload.get("warmup_steps", 0)),
logging_steps=int(payload.get("logging_steps", 10)),
save_total_limit=int(payload.get("save_total_limit", 2)),
predict_with_generate=bool(payload.get("predict_with_generate", True)),
generation_max_length=int(payload.get("generation_max_length", payload.get("max_target_length", 256))),
)
@dataclass(slots=True)
class RecognizerTrainingBundle:
archive_path: str
manifest_path: str
readme_path: str
file_count: int
total_bytes: int
def to_dict(self) -> dict[str, Any]:
return {
"archive_path": self.archive_path,
"manifest_path": self.manifest_path,
"readme_path": self.readme_path,
"file_count": self.file_count,
"total_bytes": self.total_bytes,
}
@dataclass(slots=True)
class HfRecognizerFinalization:
run_dir: str
package_dir: str
model_card_path: str
audit_path: str
report_json_path: str
report_md_path: str
audit_passed: bool
def to_dict(self) -> dict[str, Any]:
return {
"run_dir": self.run_dir,
"package_dir": self.package_dir,
"model_card_path": self.model_card_path,
"audit_path": self.audit_path,
"report_json_path": self.report_json_path,
"report_md_path": self.report_md_path,
"audit_passed": self.audit_passed,
}
def write_hf_recognizer_recipe(
train_manifest: str | Path,
eval_manifest: str | Path,
output_dir: str | Path,
*,
base_model: str = "microsoft/trocr-small-printed",
max_target_length: int = 128,
per_device_train_batch_size: int = 8,
per_device_eval_batch_size: int = 8,
learning_rate: float = 5e-5,
num_train_epochs: float = 3.0,
warmup_steps: int = 0,
logging_steps: int = 10,
save_total_limit: int = 2,
run: bool = False,
) -> Path:
train_entries = load_manifest(train_manifest)
eval_entries = load_manifest(eval_manifest)
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
config = HfRecognizerTrainingConfig(
train_manifest=str(Path(train_manifest)),
eval_manifest=str(Path(eval_manifest)),
output_dir=str(out),
base_model=base_model,
max_target_length=max_target_length,
per_device_train_batch_size=per_device_train_batch_size,
per_device_eval_batch_size=per_device_eval_batch_size,
learning_rate=learning_rate,
num_train_epochs=num_train_epochs,
warmup_steps=warmup_steps,
logging_steps=logging_steps,
save_total_limit=save_total_limit,
predict_with_generate=True,
generation_max_length=max_target_length,
)
config_path = out / "hf_recognizer_config.json"
config_path.write_text(json.dumps(config.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
summary_path = out / "recognizer-training-summary.json"
summary_path.write_text(
json.dumps(
_hf_recognizer_training_summary(
train_manifest=Path(train_manifest),
eval_manifest=Path(eval_manifest),
train_entries=train_entries,
eval_entries=eval_entries,
config_path=config_path,
base_model=base_model,
max_target_length=max_target_length,
per_device_train_batch_size=per_device_train_batch_size,
per_device_eval_batch_size=per_device_eval_batch_size,
learning_rate=learning_rate,
num_train_epochs=num_train_epochs,
warmup_steps=warmup_steps,
logging_steps=logging_steps,
save_total_limit=save_total_limit,
),
ensure_ascii=False,
indent=2,
)
+ "\n",
encoding="utf-8",
)
(out / "README.md").write_text(
"\n".join(
[
"# Hugging Face Recognizer Training Recipe",
"",
f"Training summary: `{summary_path}`",
f"Base model: `{base_model}`",
f"Train manifest: `{train_manifest}` ({len(train_entries)} samples)",
f"Eval manifest: `{eval_manifest}` ({len(eval_entries)} samples)",
"",
"Install the backend dependencies first:",
"",
"```bash",
"python -m pip install -e '.[hf-recognizer]'",
"```",
"",
"Run in-process:",
"",
"```bash",
_hf_recognizer_train_command(config, run=True),
"```",
]
)
+ "\n",
encoding="utf-8",
)
if run:
run_hf_recognizer_training(config_path)
return config_path
def _hf_recognizer_train_command(config: HfRecognizerTrainingConfig, *, run: bool = False) -> str:
parts = [
"ocrtech",
"train-recognizer",
"--backend",
"hf-vision-encoder-decoder",
"--base-model",
config.base_model,
"--train-manifest",
config.train_manifest,
"--eval-manifest",
config.eval_manifest,
"--out",
config.output_dir,
"--max-target-length",
str(config.max_target_length),
"--train-batch-size",
str(config.per_device_train_batch_size),
"--eval-batch-size",
str(config.per_device_eval_batch_size),
"--learning-rate",
_format_float(config.learning_rate),
"--epochs",
_format_float(config.num_train_epochs),
]
if run:
parts.append("--run")
return " ".join(shlex.quote(part) for part in parts)
def _format_float(value: float) -> str:
return f"{value:.12g}"
def _hf_recognizer_training_summary(
*,
train_manifest: Path,
eval_manifest: Path,
train_entries: list[ManifestEntry],
eval_entries: list[ManifestEntry],
config_path: Path,
base_model: str,
max_target_length: int,
per_device_train_batch_size: int,
per_device_eval_batch_size: int,
learning_rate: float,
num_train_epochs: float,
warmup_steps: int,
logging_steps: int,
save_total_limit: int,
) -> dict[str, Any]:
texts = [entry.text for entry in [*train_entries, *eval_entries]]
chars = dictionary_from_texts(texts)
pack_content_sha256 = _training_pack_content_sha256(
train_manifest=train_manifest,
eval_manifest=eval_manifest,
train_entries=train_entries,
eval_entries=eval_entries,
)
return {
"backend": "hf_vision_encoder_decoder",
"base_model": base_model,
"pack_content_sha256": pack_content_sha256,
"training_pack": {
"schema": "ocrtech.recognizer-training-pack.v1",
"pack_content_sha256": pack_content_sha256,
},
"manifests": {
"train": _manifest_summary(train_manifest, train_entries),
"eval": _manifest_summary(eval_manifest, eval_entries),
},
"artifacts": {
"config": _file_summary(config_path),
"dictionary": {
"character_count": len(chars),
"contains_space": " " in chars,
"contains_ascii": any(char.isascii() and char.isalnum() for char in chars),
"contains_devanagari": any("\u0900" <= char <= "\u097f" for char in chars),
},
},
"hyperparameters": {
"max_target_length": max_target_length,
"per_device_train_batch_size": per_device_train_batch_size,
"per_device_eval_batch_size": per_device_eval_batch_size,
"learning_rate": learning_rate,
"num_train_epochs": num_train_epochs,
"warmup_steps": warmup_steps,
"logging_steps": logging_steps,
"save_total_limit": save_total_limit,
"generation_max_length": max_target_length,
},
}
def _training_pack_content_sha256(
*,
train_manifest: Path,
eval_manifest: Path,
train_entries: list[ManifestEntry],
eval_entries: list[ManifestEntry],
) -> str:
payload = {
"schema": "ocrtech.recognizer-training-pack.v1",
"manifests": {
"train": {
"path": str(train_manifest),
"sha256": sha256_file(train_manifest) if train_manifest.exists() else None,
"sample_count": len(train_entries),
},
"eval": {
"path": str(eval_manifest),
"sha256": sha256_file(eval_manifest) if eval_manifest.exists() else None,
"sample_count": len(eval_entries),
},
},
"rows": {
"train": [_training_pack_row(index, entry) for index, entry in enumerate(train_entries)],
"eval": [_training_pack_row(index, entry) for index, entry in enumerate(eval_entries)],
},
}
return sha256_text(json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":")))
def _training_pack_row(index: int, entry: ManifestEntry) -> dict[str, Any]:
return {
"index": index,
"sample_id": entry.sample_id,
"dataset": entry.dataset,
"split": entry.split,
"image_path": entry.image_path,
"text": _paddle_label_text(entry.text),
"sha256": entry.sha256,
"metadata": entry.metadata,
}
def run_hf_recognizer_training(config_path: str | Path) -> Path:
config = HfRecognizerTrainingConfig.from_path(config_path)
train_entries = load_manifest(config.train_manifest)
eval_entries = load_manifest(config.eval_manifest)
if not train_entries:
raise DataValidationError("hf recognizer training requires non-empty train_manifest")
if not eval_entries:
raise DataValidationError("hf recognizer training requires non-empty eval_manifest")
try:
import torch
except ImportError as exc:
raise DataValidationError("hf recognizer training requires torch. Install ocr-tech[hf-recognizer].") from exc
try:
from PIL import Image
except ImportError as exc:
raise DataValidationError("hf recognizer training requires Pillow. Install ocr-tech[hf-recognizer].") from exc
try:
from transformers import Seq2SeqTrainer, Seq2SeqTrainingArguments, TrOCRProcessor, VisionEncoderDecoderModel
except ImportError as exc:
raise DataValidationError("hf recognizer training requires transformers. Install ocr-tech[hf-recognizer].") from exc
output_dir = Path(config.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
processor = _load_trocr_processor(TrOCRProcessor, config.base_model)
model = VisionEncoderDecoderModel.from_pretrained(config.base_model)
tokenizer = processor.tokenizer
decoder_start_token_id = _read_token_id(model.config, "decoder_start_token_id")
pad_token_id = _read_token_id(model.config, "pad_token_id")
if decoder_start_token_id is None:
decoder_start_token_id = tokenizer.cls_token_id or tokenizer.bos_token_id
if pad_token_id is None:
pad_token_id = tokenizer.pad_token_id
if decoder_start_token_id is None or pad_token_id is None:
raise DataValidationError("hf recognizer base model tokenizer must define decoder_start_token_id and pad_token_id")
_write_token_ids(model, decoder_start_token_id=decoder_start_token_id, pad_token_id=pad_token_id)
model.generation_config.max_length = config.generation_max_length
train_dataset = _VisionTextDataset(train_entries, processor, Image, config.max_target_length)
eval_dataset = _VisionTextDataset(eval_entries, processor, Image, config.max_target_length)
training_args = Seq2SeqTrainingArguments(
output_dir=str(output_dir / "checkpoints"),
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=config.learning_rate,
per_device_train_batch_size=config.per_device_train_batch_size,
per_device_eval_batch_size=config.per_device_eval_batch_size,
num_train_epochs=config.num_train_epochs,
warmup_steps=config.warmup_steps,
logging_steps=config.logging_steps,
save_total_limit=config.save_total_limit,
predict_with_generate=config.predict_with_generate,
remove_unused_columns=False,
report_to=[],
)
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
processing_class=processor,
data_collator=_VisionTextCollator(processor, torch),
compute_metrics=_hf_compute_metrics_factory(processor),
)
trainer.train()
final_model_dir = output_dir / "final-model"
trainer.save_model(str(final_model_dir))
processor.save_pretrained(str(final_model_dir))
metrics = trainer.evaluate(max_length=config.generation_max_length)
metrics_path = output_dir / "training_metrics.json"
metrics_path.write_text(json.dumps(metrics, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
return final_model_dir
def bundle_recognizer_training(
recipe_dir: str | Path,
output_dir: str | Path,
*,
train_manifest: str | Path,
eval_manifest: str | Path,
archive_name: str = "recognizer-training-bundle.tar.gz",
base_dir: str | Path = ".",
require_corpus_audit: bool = False,
min_train_samples: int = 1000,
min_eval_samples: int = 100,
min_train_english: int = 200,
min_train_devanagari: int = 200,
min_train_mixed: int = 25,
min_eval_english: int = 50,
min_eval_devanagari: int = 50,
min_eval_mixed: int = 10,
min_train_latin_only: int = 50,
min_train_devanagari_only: int = 50,
min_eval_latin_only: int = 10,
min_eval_devanagari_only: int = 10,
min_train_real: int = 100,
min_train_synthetic: int = 100,
require_eval_real: bool = False,
failure_review: str | Path | None = None,
) -> RecognizerTrainingBundle:
base = Path(base_dir).resolve()
recipe_path = _resolve_under_base(recipe_dir, base=base)
train_path = _resolve_under_base(train_manifest, base=base)
eval_path = _resolve_under_base(eval_manifest, base=base)
if not recipe_path.is_dir():
raise DataValidationError(f"recipe_dir does not exist or is not a directory: {recipe_path}")
if not train_path.is_file():
raise DataValidationError(f"train_manifest does not exist or is not a file: {train_path}")
if not eval_path.is_file():
raise DataValidationError(f"eval_manifest does not exist or is not a file: {eval_path}")
training_summary_path = recipe_path / "recognizer-training-summary.json"
if failure_review is not None:
if not training_summary_path.is_file():
raise DataValidationError(f"recognizer training summary does not exist for stopped-family guard: {training_summary_path}")
summary_payload = _read_json_object(training_summary_path, "recognizer training summary")
assert_recognizer_family_not_stopped(
failure_review,
backend=str(summary_payload.get("backend") or ""),
base_model=str(summary_payload.get("base_model") or ""),
)
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
corpus_audit_payload: dict[str, Any] = {"required": require_corpus_audit, "present": False}
if require_corpus_audit:
corpus_audit = audit_recognizer_corpus(
train_path,
eval_path,
out / "recognizer-corpus-audit",
min_train_samples=min_train_samples,
min_eval_samples=min_eval_samples,
min_train_english=min_train_english,
min_train_devanagari=min_train_devanagari,
min_train_mixed=min_train_mixed,
min_eval_english=min_eval_english,
min_eval_devanagari=min_eval_devanagari,
min_eval_mixed=min_eval_mixed,
min_train_latin_only=min_train_latin_only,
min_train_devanagari_only=min_train_devanagari_only,
min_eval_latin_only=min_eval_latin_only,
min_eval_devanagari_only=min_eval_devanagari_only,
min_train_real=min_train_real,
min_train_synthetic=min_train_synthetic,
require_eval_real=require_eval_real,
)
corpus_audit_payload = {
"required": True,
"present": True,
"passed": corpus_audit.passed,
"path": "recognizer-corpus-audit/recognizer-corpus-audit.json",
}
if not corpus_audit.passed:
raise DataValidationError(
"recognizer corpus audit failed; refusing to bundle training job: "
+ "; ".join(corpus_audit.issues)
)
archive_path = out / archive_name
manifest_path = out / "recognizer-training-bundle.json"
readme_path = out / "recognizer-training-bundle.md"
train_entries = load_manifest(train_path)
eval_entries = load_manifest(eval_path)
with tempfile.TemporaryDirectory(prefix="ocrtech-recognizer-bundle-") as tmp_dir_name:
staging = Path(tmp_dir_name)
recipe_arcname = _relative_to_base(recipe_path, base=base)
train_arcname = _relative_to_base(train_path, base=base)
eval_arcname = _relative_to_base(eval_path, base=base)
shutil.copytree(recipe_path, staging / recipe_arcname)
rewritten_train = _stage_bundle_manifest(train_entries, staging, split_name="train", base=base)
rewritten_eval = _stage_bundle_manifest(eval_entries, staging, split_name="eval", base=base)
write_manifest(rewritten_train, staging / train_arcname)
write_manifest(rewritten_eval, staging / eval_arcname)
_write_paddle_label_file(rewritten_train, staging / recipe_arcname / "labels" / "train.txt")
_write_paddle_label_file(rewritten_eval, staging / recipe_arcname / "labels" / "eval.txt")
_rewrite_staged_paddle_config_for_bundle(
staging / recipe_arcname / "paddle_recognizer.yml",
recipe_arcname=recipe_arcname,
recipe_path=recipe_path,
staging=staging,
base=base,
)
staged_files = sorted(path for path in staging.rglob("*") if path.is_file())
total_bytes = sum(path.stat().st_size for path in staged_files)
staged_summary = staging / recipe_arcname / "recognizer-training-summary.json"
summary_payload = _bundle_file_reference(staged_summary, staging)
recipe_backend, run_command = _bundle_recipe_run_command(staging / recipe_arcname, recipe_arcname=recipe_arcname)
with tarfile.open(archive_path, "w:gz") as archive:
for source in staged_files:
archive.add(source, arcname=str(source.relative_to(staging)))
payload = {
"archive_path": str(archive_path),
"base_dir": str(base),
"recipe_dir": str(_relative_to_base(recipe_path, base=base)),
"train_manifest": str(_relative_to_base(train_path, base=base)),
"eval_manifest": str(_relative_to_base(eval_path, base=base)),
"portable_image_root": "bundle_assets/images",
"recipe_backend": recipe_backend,
"run_command": run_command,
"training_summary": summary_payload,
"recognizer_corpus_audit": corpus_audit_payload,
"file_count": len(staged_files),
"total_bytes": total_bytes,
"files": [str(path.relative_to(staging)) for path in staged_files],
}
manifest_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
readme_path.write_text(
"# Recognizer Training Bundle\n\n"
f"Archive: `{archive_path}`\n"
f"Training summary: `{summary_payload.get('path') or 'not present'}`\n"
f"Recognizer corpus audit: `{corpus_audit_payload.get('path') or 'not required'}`\n"
f"Files: `{len(staged_files)}`\n"
f"Bytes before compression: `{total_bytes}`\n\n"
"Extract this archive at the root of an ocr-tech workspace or an empty training workspace. "
"The recipe uses relative paths, so run training from the extraction root.\n\n"
"```bash\n"
f"tar -xzf {archive_path.name}\n"
f"{run_command}\n"
"```\n",
encoding="utf-8",
)
return RecognizerTrainingBundle(
archive_path=str(archive_path),
manifest_path=str(manifest_path),
readme_path=str(readme_path),
file_count=len(staged_files),
total_bytes=total_bytes,
)
def _bundle_recipe_run_command(recipe_dir: Path, *, recipe_arcname: Path) -> tuple[str, str]:
paddle_config = recipe_dir / "paddle_recognizer.yml"
hf_config = recipe_dir / "hf_recognizer_config.json"
if paddle_config.is_file():
return "paddleocr", f"python PaddleOCR/tools/train.py -c {recipe_arcname / 'paddle_recognizer.yml'}"
if hf_config.is_file():
config = HfRecognizerTrainingConfig.from_path(hf_config)
return "hf_vision_encoder_decoder", _hf_recognizer_train_command(config, run=True)
return "unknown", f"# no supported recognizer training config found under {recipe_dir}"
def _rewrite_staged_paddle_config_for_bundle(
config_path: Path,
*,
recipe_arcname: Path,
recipe_path: Path,
staging: Path,
base: Path,
) -> None:
if not config_path.exists():
return
train_label = recipe_arcname / "labels" / "train.txt"
eval_label = recipe_arcname / "labels" / "eval.txt"
output_dir = recipe_arcname / "checkpoints"
save_res_path = recipe_arcname / "predicts.txt"
lines = config_path.read_text(encoding="utf-8").splitlines()
rewritten: list[str] = []
section: str | None = None
in_label_file_list = False
for line in lines:
stripped = line.strip()
if stripped == "Train:":
section = "train"
in_label_file_list = False
elif stripped == "Eval:":
section = "eval"
in_label_file_list = False
elif line and not line.startswith(" "):
section = None
in_label_file_list = False
if stripped == "label_file_list:":
in_label_file_list = True
rewritten.append(line)
continue
if in_label_file_list and stripped.startswith("- "):
label_path = train_label if section == "train" else eval_label if section == "eval" else None
if label_path is not None:
rewritten.append(f" - {label_path}")
continue
if in_label_file_list and stripped and not stripped.startswith("- "):
in_label_file_list = False
if line.startswith(" character_dict_path: "):
original = line.split(": ", 1)[1]
dictionary_rel = _stage_bundle_dictionary(original, recipe_arcname=recipe_arcname, recipe_path=recipe_path, staging=staging, base=base)
rewritten.append(f" character_dict_path: {dictionary_rel}")
continue
if line.startswith(" save_model_dir: "):
rewritten.append(f" save_model_dir: {output_dir}")
continue
if line.startswith(" save_res_path: "):
rewritten.append(f" save_res_path: {save_res_path}")
continue
rewritten.append(line)
config_path.write_text("\n".join(rewritten) + "\n", encoding="utf-8")
def _stage_bundle_dictionary(
value: str,
*,
recipe_arcname: Path,
recipe_path: Path,
staging: Path,
base: Path,
) -> Path:
source = _resolve_bundle_config_path(value, base=base)
if not source.is_file():
raise DataValidationError(f"PaddleOCR character_dict_path does not exist for training bundle: {source}")
try:
return recipe_arcname / source.relative_to(recipe_path)
except ValueError:
target_rel = recipe_arcname / "dictionaries" / source.name
target = staging / target_rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, target)
return target_rel
def _resolve_bundle_config_path(value: str, *, base: Path) -> Path:
source = Path(value)
if source.is_absolute():
return source.resolve()
cwd_relative = source.resolve()
if cwd_relative.exists():
return cwd_relative
return (base / source).resolve()
def _bundle_file_reference(path: Path, staging_root: Path) -> dict[str, Any]:
if not path.exists() or not path.is_file():
return {"present": False, "path": str(path.relative_to(staging_root))}
return {
"present": True,
"path": str(path.relative_to(staging_root)),
"sha256": sha256_file(path),
}
def _read_json_object(path: Path, label: str) -> dict[str, Any]:
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError as exc:
raise DataValidationError(f"{label} does not exist: {path}") from exc
except json.JSONDecodeError as exc:
raise DataValidationError(f"invalid {label} JSON {path}: {exc}") from exc
if not isinstance(payload, dict):
raise DataValidationError(f"{label} must be a JSON object: {path}")
return payload
def _stage_bundle_manifest(entries: list[ManifestEntry], staging: Path, *, split_name: str, base: Path) -> list[ManifestEntry]:
rewritten: list[ManifestEntry] = []
for entry in entries:
image_path = Path(entry.image_path)
if not image_path.is_absolute():
image_path = base / image_path
image_path = image_path.resolve()
if not image_path.exists():
raise DataValidationError(f"manifest image_path does not exist for {entry.sample_id}: {image_path}")
if not image_path.is_file():
raise DataValidationError(f"recognizer training bundle only supports file image_path entries: {entry.sample_id}: {image_path}")
suffix = image_path.suffix or ".img"
target_rel = Path("bundle_assets") / "images" / split_name / f"{_safe_sample_id(entry.sample_id)}{suffix}"
target = staging / target_rel
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(image_path, target)
rewritten.append(
ManifestEntry(
sample_id=entry.sample_id,
dataset=entry.dataset,
split=entry.split,
image_path=str(target_rel),
text=entry.text,
sha256=entry.sha256,
metadata=dict(entry.metadata),
)
)
return rewritten
def _safe_sample_id(sample_id: str) -> str:
safe = "".join(char if char.isalnum() or char in {"-", "_", "."} else "_" for char in sample_id)
return safe or "sample"
def _resolve_under_base(path: str | Path, *, base: Path) -> Path:
value = Path(path)
resolved = value if value.is_absolute() else base / value
resolved = resolved.resolve()
try:
resolved.relative_to(base)
except ValueError as exc:
raise DataValidationError(f"path must be inside base_dir {base}: {path}") from exc
return resolved
def _relative_to_base(path: Path, *, base: Path) -> Path:
try:
return path.resolve().relative_to(base)
except ValueError as exc:
raise DataValidationError(f"path must be inside base_dir {base}: {path}") from exc
def _load_trocr_processor(processor_cls: type[object], source: str | Path) -> object:
attempts = ({"backend": "pil"}, {"use_fast": False})
for kwargs in attempts:
try:
return processor_cls.from_pretrained(str(source), **kwargs)
except TypeError as exc:
if any(key in str(exc) for key in kwargs):
continue
raise
except ImportError as exc:
raise DataValidationError("TrOCR processor loading requires sentencepiece and protobuf. Install ocr-tech[hf-recognizer].") from exc
except ValueError as exc:
message = str(exc)
if "SentencePiece" in message or "sentencepiece" in message or "tiktoken" in message or "protobuf" in message:
raise DataValidationError("TrOCR processor loading requires sentencepiece and protobuf. Install ocr-tech[hf-recognizer].") from exc
raise
return processor_cls.from_pretrained(str(source))
def _read_token_id(config: object, name: str) -> int | None:
value = getattr(config, name, None)
if value is not None:
return int(value)
nested_decoder = getattr(config, "decoder", None)
nested_value = getattr(nested_decoder, name, None)
return int(nested_value) if nested_value is not None else None
def _set_attr_if_possible(target: object, name: str, value: int) -> None:
try:
setattr(target, name, value)
except AttributeError:
return
def _write_token_ids(model: object, *, decoder_start_token_id: int, pad_token_id: int) -> None:
config = getattr(model, "config", None)
if config is not None:
_set_attr_if_possible(config, "decoder_start_token_id", decoder_start_token_id)
_set_attr_if_possible(config, "pad_token_id", pad_token_id)
nested_decoder = getattr(config, "decoder", None)
if nested_decoder is not None:
_set_attr_if_possible(nested_decoder, "decoder_start_token_id", decoder_start_token_id)
_set_attr_if_possible(nested_decoder, "pad_token_id", pad_token_id)
generation_config = getattr(model, "generation_config", None)
if generation_config is not None:
_set_attr_if_possible(generation_config, "decoder_start_token_id", decoder_start_token_id)
_set_attr_if_possible(generation_config, "pad_token_id", pad_token_id)
def write_corrector_recipe(
output_dir: str | Path,
*,
clean_manifest: str | Path | None = None,
pairs_source: str | Path | None = None,
hf_dataset: str | None = None,
hf_split: str = "train",
limit: int | None = None,
seed: int = 13,
) -> Path:
sources = [clean_manifest is not None, pairs_source is not None, hf_dataset is not None]
if sum(1 for item in sources if item) != 1:
raise DataValidationError("train-corrector requires exactly one of --clean-manifest, --pairs-source, or --hf-dataset")
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
pairs_path = out / "correction_pairs.jsonl"
if pairs_source is not None:
_copy_pairs(pairs_source, pairs_path, limit=limit)
elif hf_dataset is not None:
prepare_hf_correction_pairs(hf_dataset, pairs_path, split=hf_split, limit=limit)
else:
entries = load_manifest(clean_manifest or "")
pairs = _synthetic_pairs(entries, limit=limit, seed=seed)
with pairs_path.open("w", encoding="utf-8") as handle:
for noisy, clean in pairs:
handle.write(json.dumps({"noisy_text": noisy, "clean_text": clean}, ensure_ascii=False) + "\n")
config_path = out / "corrector_config.json"
config_path.write_text(
json.dumps(
{
"task": "ocr_text_correction",
"pairs": str(pairs_path),
"recommended_model_family": "compact seq2seq or encoder-decoder text corrector",
"metrics": ["cer", "wer", "exact_line_accuracy"],
},
ensure_ascii=False,
indent=2,
)
+ "\n",
encoding="utf-8",
)
(out / "README.md").write_text(
"# Corrector Training Recipe\n\n"
f"Pairs: `{pairs_path}`\n\n"
"Use these noisy-clean pairs with a compact text-to-text model. Keep Nepali and English validation sets separate.\n",
encoding="utf-8",
)
return config_path
def generate_correction_pairs(
manifest_path: str | Path,
output_path: str | Path,
*,
engine_name: str = "tesseract",
model_config: str | Path | None = None,
limit: int | None = None,
only_errors: bool = False,
) -> Path:
from .engines import create_engine
entries = load_manifest(manifest_path)
if not entries:
raise DataValidationError("generate-correction-pairs requires a non-empty manifest")
engine_kwargs: dict[str, Any] = {}
if engine_name in {"candidate", "ours"}:
engine_kwargs["model_config"] = model_config
engine = create_engine(engine_name, **engine_kwargs)
out = Path(output_path)
out.parent.mkdir(parents=True, exist_ok=True)
written = 0
with out.open("w", encoding="utf-8") as handle:
for entry in entries:
if limit is not None and written >= limit:
break
recognized = engine.recognize(Path(entry.image_path))
noisy = normalize_ocr_text("\n".join(line.text for page in recognized.pages for line in page.text_lines))
clean = normalize_ocr_text(entry.text)
if only_errors and noisy == clean:
continue
handle.write(
json.dumps(
{
"sample_id": entry.sample_id,
"engine": engine_name,
"noisy_text": noisy,
"clean_text": clean,
"slices": list(entry.metadata.get("slices") or []) if entry.metadata else [],
},
ensure_ascii=False,
)
+ "\n"
)
written += 1
if written == 0:
raise DataValidationError("No correction pairs generated")
return out
def write_hf_text_corrector_recipe(
pairs_source: str | Path,
output_dir: str | Path,
*,
base_model: str = "google/byt5-small",
max_source_length: int = 256,
max_target_length: int = 256,
per_device_train_batch_size: int = 16,
per_device_eval_batch_size: int = 16,
learning_rate: float = 5e-5,
num_train_epochs: float = 3.0,
warmup_steps: int = 0,
logging_steps: int = 10,
save_total_limit: int = 2,
eval_ratio: float = 0.1,
seed: int = 13,
run: bool = False,
) -> Path:
pairs = _load_correction_pairs(Path(pairs_source))
if len(pairs) < 2:
raise DataValidationError("hf text corrector training requires at least two correction pairs")
if eval_ratio <= 0 or eval_ratio >= 1:
raise DataValidationError("hf text corrector eval_ratio must be between 0 and 1")
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
rng = random.Random(seed)
shuffled = list(pairs)
rng.shuffle(shuffled)
eval_count = min(max(1, round(len(shuffled) * eval_ratio)), len(shuffled) - 1)
eval_pairs = shuffled[:eval_count]
train_pairs = shuffled[eval_count:]
train_pairs_path = out / "train_pairs.jsonl"
eval_pairs_path = out / "eval_pairs.jsonl"
_write_correction_pairs_jsonl(train_pairs, train_pairs_path)
_write_correction_pairs_jsonl(eval_pairs, eval_pairs_path)
config = HfTextCorrectorTrainingConfig(
train_pairs=str(train_pairs_path),
eval_pairs=str(eval_pairs_path),
output_dir=str(out),
base_model=base_model,
max_source_length=max_source_length,
max_target_length=max_target_length,
per_device_train_batch_size=per_device_train_batch_size,
per_device_eval_batch_size=per_device_eval_batch_size,
learning_rate=learning_rate,
num_train_epochs=num_train_epochs,
warmup_steps=warmup_steps,
logging_steps=logging_steps,
save_total_limit=save_total_limit,
predict_with_generate=True,
generation_max_length=max_target_length,
)
config_path = out / "hf_text_corrector_config.json"
config_path.write_text(json.dumps(config.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
(out / "README.md").write_text(
"\n".join(
[
"# Hugging Face Text Corrector Training Recipe",
"",
f"Base model: `{base_model}`",
f"Pairs source: `{pairs_source}` ({len(pairs)} pairs)",
f"Train pairs: `{train_pairs_path}` ({len(train_pairs)} pairs)",
f"Eval pairs: `{eval_pairs_path}` ({len(eval_pairs)} pairs)",
"",
"Install the backend dependencies first:",
"",
"```bash",
"python -m pip install -e '.[hf-recognizer]'",
"```",
"",
"Run in-process:",
"",
"```bash",
f"ocrtech train-corrector --backend hf-seq2seq --pairs-source {pairs_source} --out {out} --run",
"```",
]
)
+ "\n",
encoding="utf-8",
)
if run:
run_hf_text_corrector_training(config_path)
return config_path
def run_hf_text_corrector_training(config_path: str | Path) -> Path:
config = HfTextCorrectorTrainingConfig.from_path(config_path)
train_pairs = _load_correction_pairs(Path(config.train_pairs))
eval_pairs = _load_correction_pairs(Path(config.eval_pairs))
if not train_pairs:
raise DataValidationError("hf text corrector training requires non-empty train_pairs")
if not eval_pairs:
raise DataValidationError("hf text corrector training requires non-empty eval_pairs")
try:
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, DataCollatorForSeq2Seq, Seq2SeqTrainer, Seq2SeqTrainingArguments
except ImportError as exc:
raise DataValidationError("hf text corrector training requires transformers. Install ocr-tech[hf-recognizer].") from exc
output_dir = Path(config.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
tokenizer = AutoTokenizer.from_pretrained(config.base_model, use_fast=False)
model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model)
model.generation_config.max_length = config.generation_max_length
train_dataset = _TextCorrectionDataset(train_pairs, tokenizer, config.max_source_length, config.max_target_length)
eval_dataset = _TextCorrectionDataset(eval_pairs, tokenizer, config.max_source_length, config.max_target_length)
training_args = Seq2SeqTrainingArguments(
output_dir=str(output_dir / "checkpoints"),
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=config.learning_rate,
per_device_train_batch_size=config.per_device_train_batch_size,
per_device_eval_batch_size=config.per_device_eval_batch_size,
num_train_epochs=config.num_train_epochs,
warmup_steps=config.warmup_steps,
logging_steps=config.logging_steps,
save_total_limit=config.save_total_limit,
predict_with_generate=config.predict_with_generate,
generation_max_length=config.generation_max_length,
remove_unused_columns=False,
report_to=[],
)
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
processing_class=tokenizer,
data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model),
compute_metrics=_hf_text_corrector_metrics_factory(tokenizer),
)
trainer.train()
final_model_dir = output_dir / "final-model"
trainer.save_model(str(final_model_dir))
tokenizer.save_pretrained(str(final_model_dir))
metrics = trainer.evaluate(max_length=config.generation_max_length)
metrics_path = output_dir / "training_metrics.json"
metrics_path.write_text(json.dumps(metrics, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
return final_model_dir
def write_recognizer_export_recipe(
training_config: str | Path,
checkpoint: str | Path,
output_dir: str | Path,
*,
inference_dir: str | Path | None = None,
paddleocr_dir: str | Path = "PaddleOCR",
export_options: Sequence[str] | None = None,
run: bool = False,
) -> Path:
config_path = Path(training_config)
checkpoint_path = _paddle_checkpoint_prefix(Path(checkpoint))
if not config_path.exists():
raise DataValidationError(f"training_config does not exist: {config_path}")
if run and not _paddle_checkpoint_exists(checkpoint_path):
raise DataValidationError(f"checkpoint does not exist: {checkpoint_path}")
out = Path(output_dir)
out.mkdir(parents=True, exist_ok=True)
save_dir = Path(inference_dir) if inference_dir else out / "inference"
options = list(export_options or [])
command = [
"python",
str(Path(paddleocr_dir) / "tools" / "export_model.py"),
"-c",
str(config_path),
"-o",
f"Global.pretrained_model={checkpoint_path}",
f"Global.save_inference_dir={save_dir}",
*options,
]
export_spec = {
"training_config": str(config_path),
"checkpoint": str(checkpoint_path),
"paddleocr_dir": str(paddleocr_dir),
"save_inference_dir": str(save_dir),
"export_options": options,
"command": command,
}
export_path = out / "export-recognizer.json"
export_path.write_text(json.dumps(export_spec, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
(out / "EXPORT.md").write_text(
"# Recognizer Export\n\n"
"Run this after training has produced a PaddleOCR checkpoint.\n\n"
"```bash\n"
+ " ".join(export_spec["command"])
+ "\n```\n",
encoding="utf-8",
)
if run:
completed = subprocess.run(export_spec["command"], check=False)
if completed.returncode != 0:
raise DataValidationError(f"PaddleOCR export failed with exit code {completed.returncode}")
return export_path
def _paddle_checkpoint_prefix(checkpoint: Path) -> Path:
if checkpoint.name.endswith(".pdparams"):
return checkpoint.with_name(checkpoint.name[: -len(".pdparams")])
return checkpoint
def _paddle_checkpoint_exists(checkpoint_prefix: Path) -> bool:
return checkpoint_prefix.exists() or checkpoint_prefix.with_name(f"{checkpoint_prefix.name}.pdparams").is_file()
def package_paddleocr_model(
inference_dir: str | Path,
output_dir: str | Path,
*,
model_id: str,
dictionary_path: str | Path,
base_model: str = PADDLE_PPOCRV5_MOBILE_REC_PRETRAINED,
paddle_lang: str = "ne",
text_recognition_model_name: str = "PP-OCRv5_mobile_rec",
train_manifest: str | Path | None = None,
eval_manifest: str | Path | None = None,
training_summary: str | Path | None = None,
metrics_report: str | Path | None = None,
admission_validation_report: str | Path | None = None,
recognition_mode: str = "full_page",
line_mode_max_height: int = 256,
line_mode_min_aspect_ratio: float = 3.0,
source_archive: str | Path | None = None,
source_checkpoint: str | Path | None = None,
source_training_config: str | Path | None = None,
export_recipe: str | Path | None = None,
) -> Path:
source_dir = Path(inference_dir)
dictionary = Path(dictionary_path)
if not source_dir.exists() or not source_dir.is_dir():
raise DataValidationError(f"inference_dir does not exist or is not a directory: {source_dir}")
if not dictionary.exists() or not dictionary.is_file():
raise DataValidationError(f"dictionary_path does not exist or is not a file: {dictionary}")
normalized_recognition_mode = recognition_mode.strip().lower()
if normalized_recognition_mode == "recognition_only":
normalized_recognition_mode = "line"
if normalized_recognition_mode not in {"full_page", "line", "auto"}:
raise DataValidationError(f"unsupported PaddleOCR recognition_mode: {recognition_mode!r}")
model_files = _find_paddle_inference_files(source_dir)
out = Path(output_dir)
artifacts_dir = out / "artifacts"
artifacts_dir.mkdir(parents=True, exist_ok=True)
copied: list[Path] = []
for file_path in model_files:
target = artifacts_dir / file_path.name
shutil.copy2(file_path, target)
copied.append(target)
dict_target = artifacts_dir / dictionary.name
shutil.copy2(dictionary, dict_target)
copied.append(dict_target)
card_path = out / "model-card.json"
backend_kwargs: dict[str, object] = {
"lang": paddle_lang,
"text_recognition_model_name": text_recognition_model_name,
"text_recognition_model_dir": str(artifacts_dir.relative_to(out)),
}
if normalized_recognition_mode != "full_page":
backend_kwargs.update(
{
"recognition_mode": normalized_recognition_mode,
"line_mode_max_height": int(line_mode_max_height),
"line_mode_min_aspect_ratio": float(line_mode_min_aspect_ratio),
}
)
provenance = _optional_path_provenance(
{
"source_archive": source_archive,
"source_checkpoint": source_checkpoint,
"source_training_config": source_training_config,
"export_recipe": export_recipe,
}
)
write_model_card(
card_path,
model_id=model_id,
backend="paddleocr",
base_model=base_model,
artifact_paths=[path.relative_to(out) for path in copied],
backend_kwargs=backend_kwargs,
train_manifest=train_manifest,
eval_manifest=eval_manifest,
training_summary=training_summary,
metrics_report=metrics_report,
admission_validation_report=admission_validation_report,
artifact_base_path=out,
provenance=provenance,
)
(out / "PACKAGE.md").write_text(
"# Packaged PaddleOCR Candidate\n\n"
f"Model card: `{card_path}`\n"
f"Artifacts: `{artifacts_dir}`\n\n"
"Audit before benchmarking:\n\n"
"```bash\n"
f"ocrtech audit-model {card_path}\n"
"```\n",
encoding="utf-8",
)
return card_path
def _optional_path_provenance(paths: dict[str, str | Path | None]) -> dict[str, dict[str, object]]:
provenance: dict[str, dict[str, object]] = {}
for key, raw_path in paths.items():
if raw_path is None:
continue
path = Path(raw_path)
provenance[key] = {
"path": str(path),
"exists": path.exists(),
"sha256": sha256_file(path) if path.is_file() else None,
"size_bytes": path.stat().st_size if path.exists() and path.is_file() else None,
}
return provenance
def package_hf_recognizer_model(
model_dir: str | Path,
output_dir: str | Path,
*,
model_id: str,
base_model: str,
train_manifest: str | Path | None = None,
eval_manifest: str | Path | None = None,
training_summary: str | Path | None = None,
metrics_report: str | Path | None = None,
admission_validation_report: str | Path | None = None,
max_new_tokens: int = 128,
) -> Path:
source_dir = Path(model_dir)
if not source_dir.exists() or not source_dir.is_dir():
raise DataValidationError(f"model_dir does not exist or is not a directory: {source_dir}")
out = Path(output_dir)
artifacts_dir = out / "artifacts" / "model"
if artifacts_dir.exists():
shutil.rmtree(artifacts_dir)
artifacts_dir.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(source_dir, artifacts_dir)
card_path = out / "model-card.json"
write_model_card(
card_path,
model_id=model_id,
backend="hf_vision_encoder_decoder",
base_model=base_model,
artifact_paths=[artifacts_dir.relative_to(out)],
backend_kwargs={
"model_dir": str(artifacts_dir.relative_to(out)),
"processor_dir": str(artifacts_dir.relative_to(out)),
"max_new_tokens": max_new_tokens,
},
train_manifest=train_manifest,
eval_manifest=eval_manifest,
training_summary=training_summary,
metrics_report=metrics_report,
admission_validation_report=admission_validation_report,
artifact_base_path=out,
)
(out / "PACKAGE.md").write_text(
"# Packaged Hugging Face OCR Candidate\n\n"
f"Model card: `{card_path}`\n"
f"Artifacts: `{artifacts_dir}`\n\n"
"Audit before benchmarking:\n\n"
"```bash\n"
f"ocrtech audit-model {card_path}\n"
"```\n",
encoding="utf-8",
)
return card_path
def finalize_hf_recognizer_run(
run_dir: str | Path,
*,
output_dir: str | Path | None = None,
model_id: str,
base_model: str,
train_manifest: str | Path | None = None,
eval_manifest: str | Path | None = None,
training_summary: str | Path | None = None,
admission_validation_report: str | Path | None = None,
max_new_tokens: int = 128,
) -> HfRecognizerFinalization:
run_path = Path(run_dir)
if not run_path.exists() or not run_path.is_dir():
raise DataValidationError(f"run_dir does not exist or is not a directory: {run_path}")
final_model_dir = run_path / "final-model"
metrics_path = run_path / "training_metrics.json"
if not final_model_dir.exists() or not final_model_dir.is_dir():
raise DataValidationError(f"HF recognizer run is not finished; missing final model directory: {final_model_dir}")
if not metrics_path.exists() or not metrics_path.is_file():
raise DataValidationError(f"HF recognizer run is not finished; missing training metrics report: {metrics_path}")
try:
metrics_payload = json.loads(metrics_path.read_text(encoding="utf-8"))
except json.JSONDecodeError as exc:
raise DataValidationError(f"Invalid training metrics JSON {metrics_path}: {exc}") from exc
if not isinstance(metrics_payload, dict):
raise DataValidationError(f"training metrics report must be a JSON object: {metrics_path}")
out = Path(output_dir) if output_dir is not None else run_path / "finalized"
package_dir = out / "package"
audit_dir = out / "audit"
report_json = out / "hf-recognizer-finalization.json"
report_md = out / "hf-recognizer-finalization.md"
out.mkdir(parents=True, exist_ok=True)
card_path = package_hf_recognizer_model(
final_model_dir,
package_dir,
model_id=model_id,
base_model=base_model,
train_manifest=train_manifest,
eval_manifest=eval_manifest,
training_summary=training_summary,
metrics_report=metrics_path,
admission_validation_report=admission_validation_report,
max_new_tokens=max_new_tokens,
)
audit = audit_model_card(card_path, audit_dir)
result = HfRecognizerFinalization(
run_dir=str(run_path),
package_dir=str(package_dir),
model_card_path=str(card_path),
audit_path=str(audit_dir / "model-audit.json"),
report_json_path=str(report_json),
report_md_path=str(report_md),
audit_passed=audit.passed,
)
report_json.write_text(json.dumps(result.to_dict(), ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
report_md.write_text(_render_hf_recognizer_finalization(result), encoding="utf-8")
return result
def _render_hf_recognizer_finalization(result: HfRecognizerFinalization) -> str:
return "\n".join(
[
"# HF Recognizer Finalization",
"",
f"Run directory: `{result.run_dir}`",
f"Package directory: `{result.package_dir}`",
f"Model card: `{result.model_card_path}`",
f"Model audit: `{result.audit_path}`",
f"Audit passed: `{'yes' if result.audit_passed else 'no'}`",
"",
"## Next Claim-Bearing Steps",
"",
"1. Run `ocrtech audit-model` on the packaged model card if the generated audit did not pass.",
"2. Benchmark the packaged candidate against named baselines on the held-out manifest.",
"3. Summarize paired metrics and run `ocrtech validate-claim` before writing any SOTA claim.",
"",
"Example experiment command:",
"",
"```bash",
"ocrtech run-experiment \\",
" --eval-manifest data/splits-mixed-v1/eval-with-refs.jsonl \\",
" --out outputs/experiment-hf-recognizer-mixed-v2-full \\",
" --baselines candidate,tesseract,surya,stock-paddle,glm-ocr,paddleocr-vl \\",
" --candidate-model-config " + result.model_card_path + " \\",
" --train-manifest data/splits-mixed-v1/train-rebalanced-nep-eng.jsonl",
"```",
"",
]
)
def package_hf_text_corrector_model(
model_dir: str | Path,
output_dir: str | Path,
*,
model_id: str,
base_model: str,
base_engine: str = "tesseract",
base_engine_kwargs: dict[str, Any] | None = None,
train_manifest: str | Path | None = None,
eval_manifest: str | Path | None = None,
metrics_report: str | Path | None = None,
max_new_tokens: int = 256,
) -> Path:
source_dir = Path(model_dir)
if not source_dir.exists() or not source_dir.is_dir():
raise DataValidationError(f"model_dir does not exist or is not a directory: {source_dir}")
out = Path(output_dir)
artifacts_dir = out / "artifacts" / "model"
if artifacts_dir.exists():
shutil.rmtree(artifacts_dir)
artifacts_dir.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(source_dir, artifacts_dir)
card_path = out / "model-card.json"
write_model_card(
card_path,
model_id=model_id,
backend="text_correction_composite",
base_model=base_model,
artifact_paths=[artifacts_dir.relative_to(out)],
backend_kwargs={
"base_engine": base_engine,
"base_engine_kwargs": base_engine_kwargs or {},
"model_dir": str(artifacts_dir.relative_to(out)),
"tokenizer_dir": str(artifacts_dir.relative_to(out)),
"max_new_tokens": max_new_tokens,
},
train_manifest=train_manifest,
eval_manifest=eval_manifest,
metrics_report=metrics_report,
)
(out / "PACKAGE.md").write_text(
"# Packaged Text Correction Composite Candidate\n\n"
f"Model card: `{card_path}`\n"
f"Artifacts: `{artifacts_dir}`\n\n"
"Audit before benchmarking:\n\n"
"```bash\n"
f"ocrtech audit-model {card_path}\n"
"```\n",
encoding="utf-8",
)
return card_path
def _copy_pairs(source: str | Path, target: Path, *, limit: int | None) -> None:
source_path = Path(source)
count = 0
with source_path.open("r", encoding="utf-8") as src, target.open("w", encoding="utf-8") as dst:
for line_number, line in enumerate(src, start=1):
if limit is not None and count >= limit:
break
if not line.strip():
continue
data = json.loads(line)
if "noisy_text" not in data or "clean_text" not in data:
raise DataValidationError(f"Correction pair at {source_path}:{line_number} needs noisy_text and clean_text")
dst.write(json.dumps({"noisy_text": data["noisy_text"], "clean_text": data["clean_text"]}, ensure_ascii=False) + "\n")
count += 1
def _synthetic_pairs(entries: list[ManifestEntry], *, limit: int | None, seed: int) -> list[tuple[str, str]]:
rng = random.Random(seed)
pairs: list[tuple[str, str]] = []
for entry in entries:
clean = normalize_ocr_text(entry.text)
noisy = _corrupt_text(clean, rng)
if noisy != clean:
pairs.append((noisy, clean))
if limit is not None and len(pairs) >= limit:
break
if not pairs:
raise DataValidationError("No correction pairs generated")
return pairs
def _load_correction_pairs(path: Path) -> list[dict[str, str]]:
pairs: list[dict[str, str]] = []
with path.open("r", encoding="utf-8") as handle:
for line_number, line in enumerate(handle, start=1):
if not line.strip():
continue
data = json.loads(line)
noisy = data.get("noisy_text")
clean = data.get("clean_text")
if not isinstance(noisy, str) or not isinstance(clean, str):
raise DataValidationError(f"Correction pair at {path}:{line_number} needs string noisy_text and clean_text")
pairs.append({"noisy_text": normalize_ocr_text(noisy), "clean_text": normalize_ocr_text(clean)})
if not pairs:
raise DataValidationError(f"No correction pairs found in {path}")
return pairs
def _write_correction_pairs_jsonl(pairs: list[dict[str, str]], path: Path) -> None:
with path.open("w", encoding="utf-8") as handle:
for pair in pairs:
handle.write(json.dumps(pair, ensure_ascii=False) + "\n")
def _corrupt_text(text: str, rng: random.Random) -> str:
chars = list(text)
if len(chars) < 2:
return text
operations = max(1, len(chars) // 24)
for _ in range(operations):
index = rng.randrange(len(chars))
draw = rng.random()
if draw < 0.4:
chars.pop(index)
if not chars:
break
elif draw < 0.7:
chars[index] = " "
else:
chars.insert(index, chars[index])
return "".join(chars)
def _find_paddle_inference_files(source_dir: Path) -> list[Path]:
files = [path for path in source_dir.iterdir() if path.is_file()]
if not files:
raise DataValidationError(f"inference_dir contains no files: {source_dir}")
parameter_files = sorted(path for path in files if path.suffix == ".pdiparams" or path.name.endswith(".pdiparams"))
model_files = sorted(path for path in files if path.suffix in {".pdmodel", ".json"} or path.name in {"inference.pdmodel", "inference.json"})
config_files = sorted(path for path in files if path.name in {"inference.yml", "inference.yaml"})
info_files = sorted(path for path in files if path.name.endswith(".pdiparams.info"))
if not parameter_files:
raise DataValidationError(f"inference_dir is missing PaddleOCR parameter file (*.pdiparams): {source_dir}")
if not model_files:
raise DataValidationError(f"inference_dir is missing PaddleOCR model file (*.pdmodel or *.json): {source_dir}")
return [*model_files, *parameter_files, *config_files, *info_files]
class _VisionTextDataset:
def __init__(self, entries: list[ManifestEntry], processor: Any, image_module: Any, max_target_length: int) -> None:
self.entries = entries
self.processor = processor
self.image_module = image_module
self.max_target_length = max_target_length
def __len__(self) -> int:
return len(self.entries)
def __getitem__(self, index: int) -> dict[str, Any]:
entry = self.entries[index]
image = self.image_module.open(Path(entry.image_path)).convert("RGB")
pixel_values = self.processor(image, return_tensors="pt").pixel_values.squeeze(0)
labels = self.processor.tokenizer(
entry.text,
padding=False,
truncation=True,
max_length=self.max_target_length,
return_tensors="pt",
).input_ids.squeeze(0)
return {"pixel_values": pixel_values, "labels": labels}
class _VisionTextCollator:
def __init__(self, processor: Any, torch: Any) -> None:
self.processor = processor
self.torch = torch
def __call__(self, features: list[dict[str, Any]]) -> dict[str, Any]:
pixel_values = self.torch.stack([feature["pixel_values"] for feature in features])
label_features = [{"input_ids": feature["labels"]} for feature in features]
labels = self.processor.tokenizer.pad(label_features, padding=True, return_tensors="pt").input_ids
labels = labels.masked_fill(labels == self.processor.tokenizer.pad_token_id, -100)
return {"pixel_values": pixel_values, "labels": labels}
class _TextCorrectionDataset:
def __init__(self, pairs: list[dict[str, str]], tokenizer: Any, max_source_length: int, max_target_length: int) -> None:
self.pairs = pairs
self.tokenizer = tokenizer
self.max_source_length = max_source_length
self.max_target_length = max_target_length
def __len__(self) -> int:
return len(self.pairs)
def __getitem__(self, index: int) -> dict[str, Any]:
pair = self.pairs[index]
model_inputs = self.tokenizer(
pair["noisy_text"],
padding=False,
truncation=True,
max_length=self.max_source_length,
)
labels = self.tokenizer(
text_target=pair["clean_text"],
padding=False,
truncation=True,
max_length=self.max_target_length,
)
model_inputs["labels"] = labels["input_ids"]
return model_inputs
def _hf_compute_metrics_factory(processor: Any):
def compute_metrics(eval_pred: Any) -> dict[str, float]:
prediction_ids = eval_pred.predictions
label_ids = eval_pred.label_ids
if isinstance(prediction_ids, tuple):
prediction_ids = prediction_ids[0]
predicted_text = processor.batch_decode(
_sanitize_batch_token_ids(processor.tokenizer, prediction_ids),
skip_special_tokens=True,
)
reference_text = processor.batch_decode(
_sanitize_batch_token_ids(processor.tokenizer, label_ids),
skip_special_tokens=True,
)
return {
"cer": sum(cer(pred, ref) for pred, ref in zip(predicted_text, reference_text, strict=True)) / len(reference_text),
"wer": sum(wer(pred, ref) for pred, ref in zip(predicted_text, reference_text, strict=True)) / len(reference_text),
"exact_line_accuracy": exact_line_accuracy(predicted_text, reference_text),
}
return compute_metrics
def _hf_text_corrector_metrics_factory(tokenizer: Any):
def compute_metrics(eval_pred: Any) -> dict[str, float]:
prediction_ids = eval_pred.predictions
label_ids = eval_pred.label_ids
if isinstance(prediction_ids, tuple):
prediction_ids = prediction_ids[0]
predicted_text = [
normalize_ocr_text(text)
for text in tokenizer.batch_decode(_sanitize_batch_token_ids(tokenizer, prediction_ids), skip_special_tokens=True)
]
reference_text = [
normalize_ocr_text(text)
for text in tokenizer.batch_decode(_sanitize_batch_token_ids(tokenizer, label_ids), skip_special_tokens=True)
]
return {
"cer": sum(cer(pred, ref) for pred, ref in zip(predicted_text, reference_text, strict=True)) / len(reference_text),
"wer": sum(wer(pred, ref) for pred, ref in zip(predicted_text, reference_text, strict=True)) / len(reference_text),
"exact_line_accuracy": exact_line_accuracy(predicted_text, reference_text),
}
return compute_metrics
def _sanitize_batch_token_ids(tokenizer: Any, token_rows: Any) -> list[list[int]]:
rows = token_rows.tolist() if hasattr(token_rows, "tolist") else token_rows
pad_token_id = _fallback_token_id(tokenizer)
vocab_size = _tokenizer_vocab_size(tokenizer)
sanitized_rows: list[list[int]] = []
for row in rows:
values = row.tolist() if hasattr(row, "tolist") else row
sanitized_rows.append([_sanitize_token_id(token, pad_token_id=pad_token_id, vocab_size=vocab_size) for token in values])
return sanitized_rows
def _sanitize_token_id(token: Any, *, pad_token_id: int, vocab_size: int | None) -> int:
try:
value = int(token)
except (TypeError, ValueError, OverflowError):
return pad_token_id
if value == -100:
return pad_token_id
if value < 0:
return pad_token_id
if vocab_size is not None and value >= vocab_size:
return pad_token_id
return value
def _tokenizer_vocab_size(tokenizer: Any) -> int | None:
vocab_size = getattr(tokenizer, "vocab_size", None)
if vocab_size is not None:
return int(vocab_size)
try:
return len(tokenizer)
except TypeError:
return None
def _fallback_token_id(tokenizer: Any) -> int:
for attribute in ("pad_token_id", "eos_token_id", "bos_token_id"):
value = getattr(tokenizer, attribute, None)
if value is not None:
return int(value)
return 0
|