upscale 0.5.1__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.
- upscale/__init__.py +30 -0
- upscale/__main__.py +3 -0
- upscale/about.py +168 -0
- upscale/algorithms/__init__.py +166 -0
- upscale/algorithms/classical.py +82 -0
- upscale/algorithms/plugins/__init__.py +52 -0
- upscale/algorithms/plugins/texture_residual.py +117 -0
- upscale/algorithms/plugins/vectorize_scale.py +450 -0
- upscale/api/__init__.py +17 -0
- upscale/api/compare_routes.py +243 -0
- upscale/api/deps.py +18 -0
- upscale/api/jobs.py +516 -0
- upscale/api/models_routes.py +107 -0
- upscale/api/system_routes.py +200 -0
- upscale/backends/__init__.py +27 -0
- upscale/backends/onnx_rt.py +265 -0
- upscale/benchmark.py +96 -0
- upscale/checkpoint.py +258 -0
- upscale/cli.py +608 -0
- upscale/compare.py +202 -0
- upscale/config.py +56 -0
- upscale/engine.py +348 -0
- upscale/estimates.py +338 -0
- upscale/filters.py +180 -0
- upscale/help_content.py +94 -0
- upscale/infer_util.py +22 -0
- upscale/io_utils.py +616 -0
- upscale/job_prep.py +318 -0
- upscale/mcp_server.py +287 -0
- upscale/models.py +628 -0
- upscale/naming.py +63 -0
- upscale/pipeline.py +485 -0
- upscale/presets.py +127 -0
- upscale/quality.py +36 -0
- upscale/schema.py +315 -0
- upscale/server.py +1056 -0
- upscale/speed_stats.py +273 -0
- upscale/suggest.py +93 -0
- upscale/system.py +102 -0
- upscale/tiling.py +392 -0
- upscale/web.py +118 -0
- upscale-0.5.1.dist-info/METADATA +149 -0
- upscale-0.5.1.dist-info/RECORD +48 -0
- upscale-0.5.1.dist-info/WHEEL +5 -0
- upscale-0.5.1.dist-info/entry_points.txt +2 -0
- upscale-0.5.1.dist-info/licenses/LICENSE +21 -0
- upscale-0.5.1.dist-info/licenses/NOTICE +6 -0
- upscale-0.5.1.dist-info/top_level.txt +1 -0
upscale/checkpoint.py
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"""Strip checkpoints for resumable long jobs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from dataclasses import asdict, dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class StripCheckpoint:
|
|
16
|
+
"""Flush-row progress for strip-based tiled_upscale."""
|
|
17
|
+
|
|
18
|
+
pass_index: int
|
|
19
|
+
flush_row: int
|
|
20
|
+
out_h: int
|
|
21
|
+
out_w: int
|
|
22
|
+
channels: int
|
|
23
|
+
model_scale: int
|
|
24
|
+
tile: int
|
|
25
|
+
overlap: int
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def checkpoint_dir(job_dir: Path | str) -> Path:
|
|
29
|
+
d = Path(job_dir) / "checkpoints"
|
|
30
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
31
|
+
return d
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def list_checkpoints(job_dir: Path | str) -> list[dict[str, Any]]:
|
|
35
|
+
root = checkpoint_dir(job_dir)
|
|
36
|
+
items: list[dict[str, Any]] = []
|
|
37
|
+
strip = root / "strip.json"
|
|
38
|
+
if strip.is_file():
|
|
39
|
+
items.append(json.loads(strip.read_text(encoding="utf-8")))
|
|
40
|
+
return items
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _fsync_path(path: Path) -> None:
|
|
44
|
+
try:
|
|
45
|
+
with open(path, "rb") as f:
|
|
46
|
+
os.fsync(f.fileno())
|
|
47
|
+
except OSError:
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def save_strip_checkpoint(
|
|
52
|
+
job_dir: Path | str,
|
|
53
|
+
meta: StripCheckpoint,
|
|
54
|
+
*,
|
|
55
|
+
result_mmap_path: Path | None = None,
|
|
56
|
+
carry_canvas: np.ndarray | None = None,
|
|
57
|
+
carry_weight: np.ndarray | None = None,
|
|
58
|
+
carry_top: int = 0,
|
|
59
|
+
) -> Path:
|
|
60
|
+
"""Save flush_row + optional carry buffers after a strip band.
|
|
61
|
+
|
|
62
|
+
Carry is written first (temp → replace), then strip.json is replaced
|
|
63
|
+
atomically so a crash cannot leave metadata ahead of the carry file.
|
|
64
|
+
"""
|
|
65
|
+
root = checkpoint_dir(job_dir)
|
|
66
|
+
meta_path = root / "strip.json"
|
|
67
|
+
carry_path = root / "strip_carry.npz"
|
|
68
|
+
payload = asdict(meta)
|
|
69
|
+
payload["carry_top"] = carry_top
|
|
70
|
+
payload["result_mmap"] = str(result_mmap_path) if result_mmap_path else None
|
|
71
|
+
payload["has_carry"] = carry_canvas is not None and carry_weight is not None
|
|
72
|
+
|
|
73
|
+
if carry_canvas is not None and carry_weight is not None:
|
|
74
|
+
tmp_carry = root / "strip_carry.npz.tmp"
|
|
75
|
+
# Uncompressed .npy archive — faster than zip on the hot path.
|
|
76
|
+
np.savez(
|
|
77
|
+
tmp_carry,
|
|
78
|
+
canvas=carry_canvas,
|
|
79
|
+
weight=carry_weight,
|
|
80
|
+
carry_top=np.array([carry_top], dtype=np.int64),
|
|
81
|
+
)
|
|
82
|
+
# np.savez may append .npz
|
|
83
|
+
if not tmp_carry.is_file() and (root / "strip_carry.npz.tmp.npz").is_file():
|
|
84
|
+
(root / "strip_carry.npz.tmp.npz").replace(tmp_carry)
|
|
85
|
+
_fsync_path(tmp_carry)
|
|
86
|
+
os.replace(tmp_carry, carry_path)
|
|
87
|
+
_fsync_path(carry_path)
|
|
88
|
+
elif carry_path.is_file():
|
|
89
|
+
carry_path.unlink(missing_ok=True)
|
|
90
|
+
|
|
91
|
+
tmp_meta = root / "strip.json.tmp"
|
|
92
|
+
tmp_meta.write_text(json.dumps(payload, indent=2), encoding="utf-8")
|
|
93
|
+
_fsync_path(tmp_meta)
|
|
94
|
+
os.replace(tmp_meta, meta_path)
|
|
95
|
+
return meta_path
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def load_strip_checkpoint(
|
|
99
|
+
job_dir: Path | str,
|
|
100
|
+
*,
|
|
101
|
+
expect_tile: int | None = None,
|
|
102
|
+
expect_overlap: int | None = None,
|
|
103
|
+
expect_pass: int | None = None,
|
|
104
|
+
expect_out_h: int | None = None,
|
|
105
|
+
expect_out_w: int | None = None,
|
|
106
|
+
expect_channels: int | None = None,
|
|
107
|
+
expect_model_scale: int | None = None,
|
|
108
|
+
) -> tuple[StripCheckpoint, dict[str, Any]] | None:
|
|
109
|
+
"""Load strip checkpoint; return None if missing or inconsistent."""
|
|
110
|
+
root = checkpoint_dir(job_dir)
|
|
111
|
+
meta_path = root / "strip.json"
|
|
112
|
+
if not meta_path.is_file():
|
|
113
|
+
return None
|
|
114
|
+
try:
|
|
115
|
+
raw = json.loads(meta_path.read_text(encoding="utf-8"))
|
|
116
|
+
meta = StripCheckpoint(
|
|
117
|
+
pass_index=int(raw["pass_index"]),
|
|
118
|
+
flush_row=int(raw["flush_row"]),
|
|
119
|
+
out_h=int(raw["out_h"]),
|
|
120
|
+
out_w=int(raw["out_w"]),
|
|
121
|
+
channels=int(raw["channels"]),
|
|
122
|
+
model_scale=int(raw["model_scale"]),
|
|
123
|
+
tile=int(raw["tile"]),
|
|
124
|
+
overlap=int(raw["overlap"]),
|
|
125
|
+
)
|
|
126
|
+
except (KeyError, TypeError, ValueError, json.JSONDecodeError):
|
|
127
|
+
return None
|
|
128
|
+
|
|
129
|
+
if expect_tile is not None and meta.tile != expect_tile:
|
|
130
|
+
return None
|
|
131
|
+
if expect_overlap is not None and meta.overlap != expect_overlap:
|
|
132
|
+
return None
|
|
133
|
+
if expect_pass is not None and meta.pass_index != expect_pass:
|
|
134
|
+
return None
|
|
135
|
+
if expect_out_h is not None and meta.out_h != expect_out_h:
|
|
136
|
+
return None
|
|
137
|
+
if expect_out_w is not None and meta.out_w != expect_out_w:
|
|
138
|
+
return None
|
|
139
|
+
if expect_channels is not None and meta.channels != expect_channels:
|
|
140
|
+
return None
|
|
141
|
+
if expect_model_scale is not None and meta.model_scale != expect_model_scale:
|
|
142
|
+
return None
|
|
143
|
+
|
|
144
|
+
extras: dict[str, Any] = {
|
|
145
|
+
"carry_top": int(raw.get("carry_top") or 0),
|
|
146
|
+
"result_mmap": raw.get("result_mmap"),
|
|
147
|
+
"carry_canvas": None,
|
|
148
|
+
"carry_weight": None,
|
|
149
|
+
"has_carry": bool(raw.get("has_carry")),
|
|
150
|
+
}
|
|
151
|
+
carry_path = root / "strip_carry.npz"
|
|
152
|
+
needs_carry = bool(raw.get("has_carry")) or meta.flush_row > 0
|
|
153
|
+
if carry_path.is_file():
|
|
154
|
+
try:
|
|
155
|
+
with np.load(carry_path) as z:
|
|
156
|
+
extras["carry_canvas"] = np.array(z["canvas"])
|
|
157
|
+
extras["carry_weight"] = np.array(z["weight"])
|
|
158
|
+
if "carry_top" in z.files:
|
|
159
|
+
extras["carry_top"] = int(z["carry_top"][0])
|
|
160
|
+
except (OSError, ValueError, KeyError):
|
|
161
|
+
return None
|
|
162
|
+
elif needs_carry and meta.flush_row > 0 and meta.flush_row < meta.out_h:
|
|
163
|
+
# Metadata claims progress past row 0 but carry is missing — unsafe.
|
|
164
|
+
if bool(raw.get("has_carry", True)):
|
|
165
|
+
return None
|
|
166
|
+
return meta, extras
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def clear_strip_checkpoint(job_dir: Path | str) -> None:
|
|
170
|
+
root = checkpoint_dir(job_dir)
|
|
171
|
+
for name in ("strip.json", "strip_carry.npz", "strip.json.tmp", "strip_carry.npz.tmp"):
|
|
172
|
+
p = root / name
|
|
173
|
+
if p.is_file():
|
|
174
|
+
p.unlink(missing_ok=True)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def pass_complete_meta_path(job_dir: Path | str, pass_index: int) -> Path:
|
|
178
|
+
return Path(job_dir) / f"pass_{pass_index}_complete.json"
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def pass_complete_mmap_path(job_dir: Path | str, pass_index: int) -> Path:
|
|
182
|
+
return Path(job_dir) / f"pass_{pass_index}_out.uint8.mmap"
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def save_pass_complete(
|
|
186
|
+
job_dir: Path | str,
|
|
187
|
+
pass_index: int,
|
|
188
|
+
arr: np.ndarray,
|
|
189
|
+
) -> Path:
|
|
190
|
+
"""Persist a finished pass output under the job dir for multi-pass resume."""
|
|
191
|
+
jd = Path(job_dir)
|
|
192
|
+
jd.mkdir(parents=True, exist_ok=True)
|
|
193
|
+
shape = tuple(int(x) for x in arr.shape)
|
|
194
|
+
mmap_path = pass_complete_mmap_path(jd, pass_index)
|
|
195
|
+
meta_path = pass_complete_meta_path(jd, pass_index)
|
|
196
|
+
# tiled_upscale already file-backs into this path when job_dir is set —
|
|
197
|
+
# flush + metadata only. Rewriting via os.replace fails on Windows while
|
|
198
|
+
# the caller's memmap still holds the destination open.
|
|
199
|
+
existing = getattr(arr, "filename", None)
|
|
200
|
+
already = False
|
|
201
|
+
if existing is not None:
|
|
202
|
+
try:
|
|
203
|
+
already = Path(existing).resolve() == mmap_path.resolve()
|
|
204
|
+
except OSError:
|
|
205
|
+
already = False
|
|
206
|
+
if already:
|
|
207
|
+
try:
|
|
208
|
+
arr.flush()
|
|
209
|
+
except Exception: # noqa: BLE001
|
|
210
|
+
pass
|
|
211
|
+
else:
|
|
212
|
+
tmp_mmap = jd / f"pass_{pass_index}_out.uint8.mmap.tmp"
|
|
213
|
+
if tmp_mmap.is_file():
|
|
214
|
+
tmp_mmap.unlink(missing_ok=True)
|
|
215
|
+
# Prefer plain file I/O so Windows can replace without an open memmap handle.
|
|
216
|
+
src = np.ascontiguousarray(arr, dtype=np.uint8)
|
|
217
|
+
with open(tmp_mmap, "wb") as f:
|
|
218
|
+
step = max(1, 64)
|
|
219
|
+
for y0 in range(0, shape[0], step):
|
|
220
|
+
y1 = min(shape[0], y0 + step)
|
|
221
|
+
f.write(src[y0:y1].tobytes())
|
|
222
|
+
if mmap_path.is_file():
|
|
223
|
+
mmap_path.unlink(missing_ok=True)
|
|
224
|
+
os.replace(tmp_mmap, mmap_path)
|
|
225
|
+
tmp_meta = jd / f"pass_{pass_index}_complete.json.tmp"
|
|
226
|
+
tmp_meta.write_text(
|
|
227
|
+
json.dumps({"pass_index": pass_index, "shape": list(shape)}, indent=2),
|
|
228
|
+
encoding="utf-8",
|
|
229
|
+
)
|
|
230
|
+
os.replace(tmp_meta, meta_path)
|
|
231
|
+
return mmap_path
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def load_pass_complete(
|
|
235
|
+
job_dir: Path | str, pass_index: int
|
|
236
|
+
) -> np.ndarray | None:
|
|
237
|
+
jd = Path(job_dir)
|
|
238
|
+
meta_path = pass_complete_meta_path(jd, pass_index)
|
|
239
|
+
mmap_path = pass_complete_mmap_path(jd, pass_index)
|
|
240
|
+
if not meta_path.is_file() or not mmap_path.is_file():
|
|
241
|
+
return None
|
|
242
|
+
try:
|
|
243
|
+
raw = json.loads(meta_path.read_text(encoding="utf-8"))
|
|
244
|
+
shape = tuple(int(x) for x in raw["shape"])
|
|
245
|
+
except (KeyError, TypeError, ValueError, json.JSONDecodeError):
|
|
246
|
+
return None
|
|
247
|
+
need = int(np.prod(shape))
|
|
248
|
+
if mmap_path.stat().st_size < need:
|
|
249
|
+
return None
|
|
250
|
+
return np.memmap(mmap_path, dtype=np.uint8, mode="r", shape=shape)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def first_incomplete_pass(job_dir: Path | str, passes: int) -> int:
|
|
254
|
+
"""Return 0-based index of the first pass without a complete output."""
|
|
255
|
+
for p in range(1, passes + 1):
|
|
256
|
+
if load_pass_complete(job_dir, p) is None:
|
|
257
|
+
return p - 1
|
|
258
|
+
return passes
|