File size: 3,896 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
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"])