pixtreme-train 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pixtreme/train/common/__init__.py +18 -0
- pixtreme/train/common/checkpoint.py +31 -0
- pixtreme/train/common/configuration.py +61 -0
- pixtreme/train/common/metrics.py +128 -0
- pixtreme_train-0.1.0.dist-info/METADATA +50 -0
- pixtreme_train-0.1.0.dist-info/RECORD +8 -0
- pixtreme_train-0.1.0.dist-info/WHEEL +4 -0
- pixtreme_train-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Shared configuration, image metrics, and checkpoint saving for model trainers."""
|
|
2
|
+
|
|
3
|
+
from .checkpoint import save_checkpoint
|
|
4
|
+
from .configuration import Option, plain, write_yaml
|
|
5
|
+
from .metrics import EvaluationMetrics, FrameMetrics, MaterialMetrics, json_metric, measure, rgb_frame
|
|
6
|
+
|
|
7
|
+
__all__ = (
|
|
8
|
+
"Option",
|
|
9
|
+
"plain",
|
|
10
|
+
"write_yaml",
|
|
11
|
+
"FrameMetrics",
|
|
12
|
+
"MaterialMetrics",
|
|
13
|
+
"EvaluationMetrics",
|
|
14
|
+
"json_metric",
|
|
15
|
+
"rgb_frame",
|
|
16
|
+
"measure",
|
|
17
|
+
"save_checkpoint",
|
|
18
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Save checkpoint payloads without replacing a previous file on write failure."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from tempfile import NamedTemporaryFile
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import torch
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def save_checkpoint(path: str | Path, payload: Mapping[str, Any], *, prefix: str = ".checkpoint-") -> None:
|
|
15
|
+
"""Flush and sync a sibling temporary file, then atomically replace the destination.
|
|
16
|
+
|
|
17
|
+
The caller assembles and validates the payload. Its values are passed unchanged
|
|
18
|
+
to torch.save; callers needing weights_only loading must supply compatible values.
|
|
19
|
+
"""
|
|
20
|
+
path = Path(path)
|
|
21
|
+
temporary: Path | None = None
|
|
22
|
+
try:
|
|
23
|
+
with NamedTemporaryFile(prefix=prefix, suffix=".tmp", dir=path.parent, delete=False) as stream:
|
|
24
|
+
temporary = Path(stream.name)
|
|
25
|
+
torch.save(payload, stream)
|
|
26
|
+
stream.flush()
|
|
27
|
+
os.fsync(stream.fileno())
|
|
28
|
+
os.replace(temporary, path)
|
|
29
|
+
finally:
|
|
30
|
+
if temporary is not None:
|
|
31
|
+
temporary.unlink(missing_ok=True)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Validate option values and write reusable YAML configurations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
from collections.abc import Callable, Mapping
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import yaml
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True)
|
|
15
|
+
class Option:
|
|
16
|
+
"""Describe an option's accepted YAML types, parser, and optional choices."""
|
|
17
|
+
|
|
18
|
+
name: str
|
|
19
|
+
parse: Callable[[str], Any]
|
|
20
|
+
default: Any
|
|
21
|
+
kinds: tuple[type, ...]
|
|
22
|
+
description: str
|
|
23
|
+
choices: tuple[str, ...] = ()
|
|
24
|
+
repeat: bool = False
|
|
25
|
+
required: bool = False
|
|
26
|
+
|
|
27
|
+
def yaml_value(self, value: Any) -> Any:
|
|
28
|
+
if value is None and self.default is None:
|
|
29
|
+
return None
|
|
30
|
+
if self.repeat:
|
|
31
|
+
if not isinstance(value, list):
|
|
32
|
+
raise ValueError("must be a list")
|
|
33
|
+
return [self._scalar(item) for item in value]
|
|
34
|
+
return self._scalar(value)
|
|
35
|
+
|
|
36
|
+
def _scalar(self, value: Any) -> Any:
|
|
37
|
+
if type(value) not in self.kinds:
|
|
38
|
+
raise ValueError(f"must be {self.description}")
|
|
39
|
+
try:
|
|
40
|
+
result = self.parse(str(value))
|
|
41
|
+
except argparse.ArgumentTypeError as error:
|
|
42
|
+
raise ValueError(f"must be {self.description}: {error}") from error
|
|
43
|
+
if self.choices and result not in self.choices:
|
|
44
|
+
raise ValueError(f"must be one of {self.choices}")
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def plain(value: Any) -> Any:
|
|
49
|
+
"""Resolve paths and recursively turn mappings and sequences into YAML values."""
|
|
50
|
+
if isinstance(value, Path):
|
|
51
|
+
return str(value.resolve())
|
|
52
|
+
if isinstance(value, Mapping):
|
|
53
|
+
return {key: plain(item) for key, item in value.items()}
|
|
54
|
+
if isinstance(value, (list, tuple)):
|
|
55
|
+
return [plain(item) for item in value]
|
|
56
|
+
return value
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def write_yaml(path: Path, document: Mapping[str, Any]) -> None:
|
|
60
|
+
"""Write a configuration with sorted keys and readable Unicode values."""
|
|
61
|
+
path.write_text(yaml.safe_dump(plain(document), sort_keys=True, allow_unicode=True))
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Measure RGB frames with pixtreme and aggregate PSNR and SSIM by frame count."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
|
|
8
|
+
import torch
|
|
9
|
+
from pixtreme.core import Frame
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class FrameMetrics:
|
|
14
|
+
"""PSNR and SSIM for one indexed frame."""
|
|
15
|
+
|
|
16
|
+
index: int
|
|
17
|
+
psnr: float
|
|
18
|
+
ssim: float
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class MaterialMetrics:
|
|
23
|
+
"""Per-frame measurements and their sums and means for one material."""
|
|
24
|
+
|
|
25
|
+
path: str
|
|
26
|
+
frames: tuple[FrameMetrics, ...]
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def frame_count(self) -> int:
|
|
30
|
+
return len(self.frames)
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def psnr_sum(self) -> float:
|
|
34
|
+
return math.fsum(frame.psnr for frame in self.frames)
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def psnr_mean(self) -> float:
|
|
38
|
+
return self.psnr_sum / self.frame_count
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def ssim_sum(self) -> float:
|
|
42
|
+
return math.fsum(frame.ssim for frame in self.frames)
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def ssim_mean(self) -> float:
|
|
46
|
+
return self.ssim_sum / self.frame_count
|
|
47
|
+
|
|
48
|
+
def to_dict(self) -> dict[str, object]:
|
|
49
|
+
return {
|
|
50
|
+
"path": self.path,
|
|
51
|
+
"frames": [
|
|
52
|
+
{"index": frame.index, "psnr": json_metric(frame.psnr), "ssim": json_metric(frame.ssim)}
|
|
53
|
+
for frame in self.frames
|
|
54
|
+
],
|
|
55
|
+
"frame_count": self.frame_count,
|
|
56
|
+
"psnr_sum": json_metric(self.psnr_sum),
|
|
57
|
+
"psnr_mean": json_metric(self.psnr_mean),
|
|
58
|
+
"ssim_sum": json_metric(self.ssim_sum),
|
|
59
|
+
"ssim_mean": json_metric(self.ssim_mean),
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class EvaluationMetrics:
|
|
65
|
+
"""Frame-weighted pixtreme PSNR and SSIM, with optional interpolation results."""
|
|
66
|
+
|
|
67
|
+
materials: tuple[MaterialMetrics, ...]
|
|
68
|
+
baselines: dict[str, EvaluationMetrics] = field(default_factory=dict)
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def frame_count(self) -> int:
|
|
72
|
+
return sum(material.frame_count for material in self.materials)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def psnr_sum(self) -> float:
|
|
76
|
+
return math.fsum(material.psnr_sum for material in self.materials)
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def psnr_mean(self) -> float:
|
|
80
|
+
return self.psnr_sum / self.frame_count
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def ssim_sum(self) -> float:
|
|
84
|
+
return math.fsum(material.ssim_sum for material in self.materials)
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def ssim_mean(self) -> float:
|
|
88
|
+
return self.ssim_sum / self.frame_count
|
|
89
|
+
|
|
90
|
+
def to_dict(self) -> dict[str, object]:
|
|
91
|
+
result: dict[str, object] = {
|
|
92
|
+
"materials": [material.to_dict() for material in self.materials],
|
|
93
|
+
"frame_count": self.frame_count,
|
|
94
|
+
"psnr_sum": json_metric(self.psnr_sum),
|
|
95
|
+
"psnr_mean": json_metric(self.psnr_mean),
|
|
96
|
+
"ssim_sum": json_metric(self.ssim_sum),
|
|
97
|
+
"ssim_mean": json_metric(self.ssim_mean),
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if self.baselines:
|
|
101
|
+
result["baselines"] = {name: value.to_dict() for name, value in self.baselines.items()}
|
|
102
|
+
return result
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def json_metric(value: float) -> float | str:
|
|
106
|
+
"""Represent positive infinity as a string accepted by strict JSON encoders."""
|
|
107
|
+
return "Infinity" if value == math.inf else value
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def rgb_frame(image: torch.Tensor) -> Frame:
|
|
111
|
+
"""Convert a CHW RGB CPU tensor into a Rec.709 Frame stored on the CUDA device."""
|
|
112
|
+
import cupy as cp
|
|
113
|
+
from pixtreme.io import from_array
|
|
114
|
+
|
|
115
|
+
return from_array(
|
|
116
|
+
cp.asarray(image.permute(1, 2, 0).contiguous().numpy()), colorspace="Rec.709", gamma="Rec.709", channels="RGB"
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def measure(index: int, reference: Frame, candidate: Frame) -> FrameMetrics:
|
|
121
|
+
"""Measure RGB PSNR and SSIM with pixtreme using a value range of 1.0."""
|
|
122
|
+
from pixtreme.metrics import psnr, ssim
|
|
123
|
+
|
|
124
|
+
return FrameMetrics(
|
|
125
|
+
index,
|
|
126
|
+
float(psnr(reference, candidate, data_range=1.0)),
|
|
127
|
+
float(ssim(reference, candidate, data_range=1.0)),
|
|
128
|
+
)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pixtreme-train
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Shared training components for pixtreme model trainers
|
|
5
|
+
Project-URL: repository, https://github.com/sync-dev-org/pixtreme-train
|
|
6
|
+
Author-email: minamik <mia@sync.dev>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
10
|
+
Classifier: Environment :: GPU :: NVIDIA CUDA :: 13
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
17
|
+
Requires-Python: >=3.12
|
|
18
|
+
Requires-Dist: numpy
|
|
19
|
+
Requires-Dist: pixtreme>=2.0.0
|
|
20
|
+
Requires-Dist: pyyaml>=6
|
|
21
|
+
Requires-Dist: torch>=2.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# pixtreme.train.common
|
|
25
|
+
|
|
26
|
+
The pure Python distribution `pixtreme-train` provides the import package
|
|
27
|
+
`pixtreme.train.common`: training components that do not depend on a particular
|
|
28
|
+
model, shared by the pixtreme trainer modules.
|
|
29
|
+
|
|
30
|
+
The wheel owns only the `pixtreme/train/common/` subtree. The namespace root
|
|
31
|
+
`pixtreme/__init__.py` is owned by [pixtreme](https://github.com/sync-dev-org/pixtreme),
|
|
32
|
+
and each trainer module owns its own `pixtreme/train/<module>/` subtree.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Use Linux x86_64 with CPython 3.12 or later. Install the distribution from PyPI:
|
|
37
|
+
|
|
38
|
+
```console
|
|
39
|
+
pip install pixtreme-train
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
For a project managed with uv:
|
|
43
|
+
|
|
44
|
+
```console
|
|
45
|
+
uv add pixtreme-train
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pixtreme/train/common/__init__.py,sha256=Ot1tWhucFZij1E4ZkLrhRYCGcuT6Aj--EEMU71mTEDA,484
|
|
2
|
+
pixtreme/train/common/checkpoint.py,sha256=LWPBkHLXWR4XJKQoLllO6T2zUIfo0mt_Lp3EgDtZU-I,1092
|
|
3
|
+
pixtreme/train/common/configuration.py,sha256=NLPYr3mJu02WBlBY6Sm2knx2LKNwa89DfM_E7necbow,2017
|
|
4
|
+
pixtreme/train/common/metrics.py,sha256=0l7ntRk9C9Tl0xMVbJVRBpuJzMdz4-Qa6ba86OPeB9E,3867
|
|
5
|
+
pixtreme_train-0.1.0.dist-info/METADATA,sha256=8h8Q7T9P3pZqSlsl_vXjy_6ZJ6Oi4JzL5E1YeBo0CKA,1585
|
|
6
|
+
pixtreme_train-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
7
|
+
pixtreme_train-0.1.0.dist-info/licenses/LICENSE,sha256=C3kcuSn77D1YmBPwywbEXaYrjkyWWcMvubpMt-2bi3Y,1064
|
|
8
|
+
pixtreme_train-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 minamik
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|