|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from pathlib import Path |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | import dlclivegui.utils.utils as u |
| 8 | +from dlclivegui.temp.engine import Engine # e.g. dlclivegui/utils/engine.py |
6 | 9 |
|
7 | 10 | pytestmark = pytest.mark.unit |
8 | 11 |
|
9 | 12 |
|
| 13 | +# NOTE @C-Achard: These tests are currently in test_utils.py for convenience, |
| 14 | +# but we may want to use dlclive.Engine directly |
| 15 | +# and possibly move these tests to dlclive's test suite |
10 | 16 | # ----------------------------- |
11 | | -# is_model_file |
| 17 | +# Engine.from_model_type |
12 | 18 | # ----------------------------- |
13 | | -@pytest.mark.unit |
14 | | -def test_is_model_file_true_for_supported_extensions(tmp_path: Path): |
15 | | - for ext in [".pt", ".pth", ".pb"]: |
16 | | - p = tmp_path / f"model{ext}" |
17 | | - p.write_text("x") |
18 | | - assert u.is_model_file(p) is True |
19 | | - assert u.is_model_file(str(p)) is True # also accepts str |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "inp, expected", |
| 21 | + [ |
| 22 | + ("pytorch", Engine.PYTORCH), |
| 23 | + ("PYTORCH", Engine.PYTORCH), |
| 24 | + ("tensorflow", Engine.TENSORFLOW), |
| 25 | + ("TensorFlow", Engine.TENSORFLOW), |
| 26 | + ("base", Engine.TENSORFLOW), |
| 27 | + ("tensorrt", Engine.TENSORFLOW), |
| 28 | + ("lite", Engine.TENSORFLOW), |
| 29 | + ], |
| 30 | +) |
| 31 | +def test_engine_from_model_type(inp: str, expected: Engine): |
| 32 | + assert Engine.from_model_type(inp) == expected |
| 33 | + |
| 34 | + |
| 35 | +def test_engine_from_model_type_unknown(): |
| 36 | + with pytest.raises(ValueError): |
| 37 | + Engine.from_model_type("onnx") |
20 | 38 |
|
21 | | - # case-insensitive |
22 | | - p2 = tmp_path / "MODEL.PT" |
23 | | - p2.write_text("x") |
24 | | - assert u.is_model_file(p2) is True |
25 | 39 |
|
| 40 | +# ----------------------------- |
| 41 | +# Engine.is_pytorch_model_path |
| 42 | +# ----------------------------- |
| 43 | +@pytest.mark.parametrize("ext", [".pt", ".pth"]) |
| 44 | +def test_engine_is_pytorch_model_path_true(tmp_path: Path, ext: str): |
| 45 | + p = tmp_path / f"model{ext}" |
| 46 | + p.write_text("x") |
| 47 | + assert Engine.is_pytorch_model_path(p) is True |
| 48 | + assert Engine.is_pytorch_model_path(str(p)) is True |
26 | 49 |
|
27 | | -@pytest.mark.unit |
28 | | -def test_is_model_file_false_for_missing_or_dir(tmp_path: Path): |
29 | | - missing = tmp_path / "missing.pt" |
30 | | - assert u.is_model_file(missing) is False |
31 | 50 |
|
| 51 | +def test_engine_is_pytorch_model_path_false_for_missing(tmp_path: Path): |
| 52 | + p = tmp_path / "missing.pt" |
| 53 | + assert Engine.is_pytorch_model_path(p) is False |
| 54 | + |
| 55 | + |
| 56 | +def test_engine_is_pytorch_model_path_false_for_dir(tmp_path: Path): |
32 | 57 | d = tmp_path / "model.pt" |
33 | 58 | d.mkdir() |
34 | | - assert u.is_model_file(d) is False |
| 59 | + assert Engine.is_pytorch_model_path(d) is False |
| 60 | + |
| 61 | + |
| 62 | +def test_engine_is_pytorch_model_path_case_insensitive(tmp_path: Path): |
| 63 | + # only include if you applied the .lower() patch |
| 64 | + p = tmp_path / "MODEL.PT" |
| 65 | + p.write_text("x") |
| 66 | + assert Engine.is_pytorch_model_path(p) is True |
| 67 | + |
| 68 | + |
| 69 | +# ----------------------------- |
| 70 | +# Engine.is_tensorflow_model_dir_path |
| 71 | +# ----------------------------- |
| 72 | +def _make_tf_dir(tmp_path: Path, *, with_cfg: bool = True, with_pb: bool = True, pb_name: str = "graph.pb") -> Path: |
| 73 | + d = tmp_path / "tf_model" |
| 74 | + d.mkdir() |
| 75 | + if with_cfg: |
| 76 | + (d / "pose_cfg.yaml").write_text("cfg: 1\n") |
| 77 | + if with_pb: |
| 78 | + (d / pb_name).write_text("pbdata") |
| 79 | + return d |
| 80 | + |
| 81 | + |
| 82 | +def test_engine_is_tensorflow_model_dir_path_true(tmp_path: Path): |
| 83 | + d = _make_tf_dir(tmp_path, with_cfg=True, with_pb=True) |
| 84 | + assert Engine.is_tensorflow_model_dir_path(d) is True |
| 85 | + assert Engine.is_tensorflow_model_dir_path(str(d)) is True |
| 86 | + |
| 87 | + |
| 88 | +def test_engine_is_tensorflow_model_dir_path_false_missing_cfg(tmp_path: Path): |
| 89 | + d = _make_tf_dir(tmp_path, with_cfg=False, with_pb=True) |
| 90 | + assert Engine.is_tensorflow_model_dir_path(d) is False |
| 91 | + |
| 92 | + |
| 93 | +def test_engine_is_tensorflow_model_dir_path_false_missing_pb(tmp_path: Path): |
| 94 | + d = _make_tf_dir(tmp_path, with_cfg=True, with_pb=False) |
| 95 | + assert Engine.is_tensorflow_model_dir_path(d) is False |
| 96 | + |
| 97 | + |
| 98 | +def test_engine_is_tensorflow_model_dir_path_case_insensitive_pb(tmp_path: Path): |
| 99 | + # only include if you applied the .lower() patch for pb suffix |
| 100 | + d = _make_tf_dir(tmp_path, with_cfg=True, with_pb=True, pb_name="GRAPH.PB") |
| 101 | + assert Engine.is_tensorflow_model_dir_path(d) is True |
| 102 | + |
| 103 | + |
| 104 | +# ----------------------------- |
| 105 | +# Engine.from_model_path |
| 106 | +# ----------------------------- |
| 107 | +def test_engine_from_model_path_missing_raises(tmp_path: Path): |
| 108 | + missing = tmp_path / "does_not_exist.pt" |
| 109 | + with pytest.raises(FileNotFoundError): |
| 110 | + Engine.from_model_path(missing) |
| 111 | + |
| 112 | + |
| 113 | +def test_engine_from_model_path_pytorch_file(tmp_path: Path): |
| 114 | + p = tmp_path / "net.pth" |
| 115 | + p.write_text("x") |
| 116 | + assert Engine.from_model_path(p) == Engine.PYTORCH |
| 117 | + |
| 118 | + |
| 119 | +def test_engine_from_model_path_tensorflow_dir(tmp_path: Path): |
| 120 | + d = _make_tf_dir(tmp_path, with_cfg=True, with_pb=True) |
| 121 | + assert Engine.from_model_path(d) == Engine.TENSORFLOW |
| 122 | + |
| 123 | + |
| 124 | +def test_engine_from_model_path_dir_not_tf_raises(tmp_path: Path): |
| 125 | + d = tmp_path / "some_dir" |
| 126 | + d.mkdir() |
| 127 | + with pytest.raises(ValueError): |
| 128 | + Engine.from_model_path(d) |
| 129 | + |
35 | 130 |
|
36 | | - bad = tmp_path / "model.onnx" |
37 | | - bad.write_text("x") |
38 | | - assert u.is_model_file(bad) is False |
| 131 | +def test_engine_from_model_path_file_not_pytorch_raises(tmp_path: Path): |
| 132 | + p = tmp_path / "model.pb" |
| 133 | + p.write_text("x") # PB file alone is not a TF dir |
| 134 | + with pytest.raises(ValueError): |
| 135 | + Engine.from_model_path(p) |
39 | 136 |
|
40 | 137 |
|
41 | 138 | # ----------------------------- |
|
0 commit comments