Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified Download ocr-tech/tests/test_audit_local_paddleocr_training_run.py from prosperious/ocr-projects-archive: direct link, hf CLI and curl.
- Browser
- Download file 3.9 kB
-
https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_local_paddleocr_training_run.py
- Command line
-
hf download hf://prosperious/ocr-projects-archive/ocr-tech/tests/test_audit_local_paddleocr_training_run.py
-
curl -L -o test_audit_local_paddleocr_training_run.py https://huggingface.co/prosperious/ocr-projects-archive/resolve/main/ocr-tech/tests/test_audit_local_paddleocr_training_run.py
3.9 kB
| from __future__ import annotations | |
| import importlib.util | |
| import json | |
| import sys | |
| from argparse import Namespace | |
| from pathlib import Path | |
| from ocrtech.manifest import ManifestEntry, write_manifest | |
| from ocrtech.training import write_recognizer_recipe | |
| def _load_tool(): | |
| path = Path(__file__).resolve().parents[1] / "tools" / "audit_local_paddleocr_training_run.py" | |
| spec = importlib.util.spec_from_file_location("audit_local_paddleocr_training_run", path) | |
| assert spec is not None | |
| assert spec.loader is not None | |
| module = importlib.util.module_from_spec(spec) | |
| sys.modules[spec.name] = module | |
| spec.loader.exec_module(module) | |
| return module | |
| def _fixture_run(tmp_path: Path) -> Path: | |
| train_image = tmp_path / "train.png" | |
| eval_image = tmp_path / "eval.png" | |
| train_image.write_bytes(b"train") | |
| eval_image.write_bytes(b"eval") | |
| train_manifest = tmp_path / "train.jsonl" | |
| eval_manifest = tmp_path / "eval.jsonl" | |
| write_manifest( | |
| [ | |
| ManifestEntry( | |
| sample_id="train-1", | |
| dataset="local", | |
| split="train", | |
| image_path=str(train_image), | |
| text="नेपाल", | |
| ) | |
| ], | |
| train_manifest, | |
| ) | |
| write_manifest( | |
| [ | |
| ManifestEntry( | |
| sample_id="eval-1", | |
| dataset="local", | |
| split="eval", | |
| image_path=str(eval_image), | |
| text="नेपाल", | |
| ) | |
| ], | |
| eval_manifest, | |
| ) | |
| run_dir = tmp_path / "run" | |
| write_recognizer_recipe(train_manifest, eval_manifest, run_dir, epochs=1) | |
| checkpoint_dir = run_dir / "checkpoints" | |
| checkpoint_dir.mkdir() | |
| for name in ["best_accuracy", "latest", "iter_epoch_1"]: | |
| (checkpoint_dir / f"{name}.pdparams").write_bytes(f"{name}-params".encode("utf-8")) | |
| (checkpoint_dir / "best_accuracy.pdopt").write_bytes(b"best-opt") | |
| (checkpoint_dir / "train.log").write_text( | |
| "train dataloader has 1 iters\n" | |
| "valid dataloader has 1 iters\n" | |
| "load pretrain successful from fixture\n" | |
| "cur metric, acc: 0.5, norm_edit_dis: 0.8, fps: 1.0\n" | |
| "save best model is to run/checkpoints/best_accuracy\n" | |
| "best metric, acc: 0.5, is_float16: False, norm_edit_dis: 0.8, fps: 1.0, best_epoch: 1\n" | |
| "save model in run/checkpoints/latest\n" | |
| "save model in run/checkpoints/iter_epoch_1\n", | |
| encoding="utf-8", | |
| ) | |
| return run_dir | |
| def test_audit_local_paddleocr_training_run_passes_complete_run(tmp_path: Path) -> None: | |
| tool = _load_tool() | |
| run_dir = _fixture_run(tmp_path) | |
| report = tool.audit_local_run( | |
| Namespace( | |
| run_dir=run_dir, | |
| root=tmp_path, | |
| out=tmp_path / "audit", | |
| checkpoint_name="best_accuracy", | |
| require_pass=True, | |
| ) | |
| ) | |
| assert report["audit_passed"] is True | |
| assert report["issue_count"] == 0 | |
| assert report["summary"]["pack_content_sha256"] == report["summary"]["recomputed_pack_content_sha256"] | |
| assert report["log_summary"]["best_metric"]["acc"] == 0.5 | |
| written = json.loads((tmp_path / "audit" / "local-paddleocr-training-audit.json").read_text(encoding="utf-8")) | |
| assert written["audit_passed"] is True | |
| def test_audit_local_paddleocr_training_run_rejects_missing_checkpoint(tmp_path: Path) -> None: | |
| tool = _load_tool() | |
| run_dir = _fixture_run(tmp_path) | |
| (run_dir / "checkpoints" / "latest.pdparams").unlink() | |
| report = tool.audit_local_run( | |
| Namespace( | |
| run_dir=run_dir, | |
| root=tmp_path, | |
| out=tmp_path / "audit", | |
| checkpoint_name="best_accuracy", | |
| require_pass=True, | |
| ) | |
| ) | |
| assert report["audit_passed"] is False | |
| assert any("required checkpoint missing" in issue for issue in report["issues"]) | |