euler-inference 2.4.0__tar.gz → 2.6.0__tar.gz
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.
- {euler_inference-2.4.0 → euler_inference-2.6.0}/PKG-INFO +1 -1
- {euler_inference-2.4.0 → euler_inference-2.6.0}/README.md +8 -3
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/_outputs.py +205 -33
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/config.py +2 -2
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/inference.py +107 -32
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/models/external_model.py +131 -11
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/PKG-INFO +1 -1
- {euler_inference-2.4.0 → euler_inference-2.6.0}/pyproject.toml +1 -1
- {euler_inference-2.4.0 → euler_inference-2.6.0}/tests/test_model_metadata.py +77 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/tests/test_writer_integration.py +345 -20
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/__init__.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/__main__.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/api.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/model_card.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference/models/__init__.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/SOURCES.txt +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/dependency_links.txt +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/entry_points.txt +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/requires.txt +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/euler_inference.egg-info/top_level.txt +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/setup.cfg +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/tests/test_intrinsics_resolution.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/tests/test_model_card.py +0 -0
- {euler_inference-2.4.0 → euler_inference-2.6.0}/tests/test_save_path.py +0 -0
|
@@ -104,6 +104,8 @@ Models can declare class-level attributes so pipelines don't need to know model
|
|
|
104
104
|
class Model:
|
|
105
105
|
OUTPUTS = [
|
|
106
106
|
{"key": "depth", "type": "npy"},
|
|
107
|
+
# Dense 3D point map in (3, H, W) layout:
|
|
108
|
+
# {"key": "points_3d", "type": "npy"},
|
|
107
109
|
{"key": "confidence", "type": "png"},
|
|
108
110
|
]
|
|
109
111
|
|
|
@@ -281,7 +283,7 @@ For simpler setups without placeholders, use a monolithic JSON config:
|
|
|
281
283
|
| `dataset.hierarchical_modalities` | object | No | Map of hierarchical modality names to paths or structured entries |
|
|
282
284
|
| `outputs` | list | No | What to save from the model's output dict. Defaults to the model's `OUTPUTS`, else `[{"key": "depth", "type": "npy"}]` with the default `_depth` suffix. |
|
|
283
285
|
| `outputs[].key` | string | Yes | Key in the dict returned by `Model.predict()` |
|
|
284
|
-
| `outputs[].type` | string | Yes | Legacy file format: `npy`, `png`, `jpg`, `jpeg`, `exr`. Used when no source-backed writer is available. |
|
|
286
|
+
| `outputs[].type` | string | Yes | Legacy file format: `npy`, `npz`, `png`, `jpg`, `jpeg`, `exr`. Used when no source-backed writer is available. |
|
|
285
287
|
| `outputs[].suffix` | string | No | Legacy filename suffix before extension. Defaults to `_<key>`. Ignored for source-backed outputs. |
|
|
286
288
|
| `outputs[].source_modality` | string | No | Regular input modality whose `euler-loading` writer/path should be mirrored under `output_base_path/<key>/`. Defaults to `key` when it matches a regular input modality. |
|
|
287
289
|
| `outputs[].writer` | object | No | Override ds-crawler writer metadata for legacy outputs (see [Writer metadata](#writer-metadata)) |
|
|
@@ -445,6 +447,8 @@ output_base_path/
|
|
|
445
447
|
|
|
446
448
|
If `outputs[].source_modality` is set explicitly but the named modality is missing, or no `euler-loading` writer is available for it, inference fails instead of silently falling back.
|
|
447
449
|
|
|
450
|
+
`points_3d` is a built-in legacy output modality. Configure it as `{"key": "points_3d", "type": "npy"}` or `{"key": "points_3d", "type": "npz"}` and return a float-compatible array with shape `(3, H, W)`. The output dataset is indexed with `euler_loading.loader = "generic"` and `euler_loading.function = "points_3d"`, so recent `euler-loading` versions can read it without an explicit loader.
|
|
451
|
+
|
|
448
452
|
### Pipeline Manifest
|
|
449
453
|
|
|
450
454
|
When `pipeline.outputs_manifest_path` is set and at least one configured output has a matching pipeline target, finalization writes a manifest like this:
|
|
@@ -477,7 +481,7 @@ Legacy outputs are written via ds-crawler's `DatasetWriter`/`ZipDatasetWriter`,
|
|
|
477
481
|
| `name` | Input dataset's name (from its `output.json`), or the output key if unavailable | `outputs[].writer.name` |
|
|
478
482
|
| `type` | Output key (e.g. `"depth"`) | `outputs[].writer.type` |
|
|
479
483
|
| `euler_train` | `{"used_as": "target", "modality_type": "<key>"}` | `outputs[].writer.euler_train` |
|
|
480
|
-
| `euler_loading` | *(omitted)* | `outputs[].writer.euler_loading` |
|
|
484
|
+
| `euler_loading` | *(omitted; `points_3d` defaults to `{"loader": "generic", "function": "points_3d"}`)* | `outputs[].writer.euler_loading` |
|
|
481
485
|
| `separator` | `null` | — |
|
|
482
486
|
| `meta` | *(omitted)* | `outputs[].writer.meta` |
|
|
483
487
|
|
|
@@ -485,7 +489,7 @@ In `--no-strict` mode, `modality_type` defaults to `"other"` instead of the outp
|
|
|
485
489
|
|
|
486
490
|
#### `euler_loading`
|
|
487
491
|
|
|
488
|
-
When the output dataset should be loadable by [euler-loading](https://github.com/d-rothen/euler-loading) without an explicit loader, set `euler_loading` with the `loader` (module name) and `function` (callable name) that euler-loading should use to auto-resolve a loader from the output's `output.json`. Available loaders
|
|
492
|
+
When the output dataset should be loadable by [euler-loading](https://github.com/d-rothen/euler-loading) without an explicit loader, set `euler_loading` with the `loader` (module name) and `function` (callable name) that euler-loading should use to auto-resolve a loader from the output's `output.json`. Available loaders include `vkitti2`, `real_drive_sim`, `generic`, and `generic_dense_depth`.
|
|
489
493
|
|
|
490
494
|
You can also include `used_as`, `modality_type`, `slot`, and `task` — these are consulted by euler-loading's `describe_for_runlog()` for experiment metadata.
|
|
491
495
|
|
|
@@ -513,6 +517,7 @@ Example with explicit writer overrides:
|
|
|
513
517
|
| Format | Extension | Notes |
|
|
514
518
|
|--------|-----------|-------|
|
|
515
519
|
| NumPy | `npy` | Preserves full float precision |
|
|
520
|
+
| NumPy archive | `npz` | Compressed NumPy archive with array key `data` |
|
|
516
521
|
| PNG | `png` | Float arrays clipped to [0,1] and scaled to uint8. Supports grayscale, RGB, RGBA. |
|
|
517
522
|
| JPEG | `jpg`/`jpeg` | Same as PNG but lossy. |
|
|
518
523
|
| OpenEXR | `exr` | Full float precision. Requires `pip install OpenEXR`. Not supported in zip mode. |
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from dataclasses import dataclass
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
4
|
import json
|
|
5
5
|
import logging
|
|
6
6
|
from pathlib import Path
|
|
@@ -18,6 +18,14 @@ from euler_inference.config import (
|
|
|
18
18
|
|
|
19
19
|
PIPELINE_OUTPUTS_MANIFEST_VERSION = 1
|
|
20
20
|
|
|
21
|
+
_NUMPY_OUTPUT_TYPES = {"npy", "npz"}
|
|
22
|
+
_LOADABLE_LEGACY_OUTPUTS = {
|
|
23
|
+
"points_3d": {
|
|
24
|
+
"euler_loading": {"loader": "generic", "function": "points_3d"},
|
|
25
|
+
"output_types": _NUMPY_OUTPUT_TYPES,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
|
|
22
30
|
def get_save_path(output_base: Path, full_id: str, suffix: str, extension: str) -> Path:
|
|
23
31
|
"""
|
|
@@ -100,7 +108,7 @@ def save_output(data: np.ndarray, target: Path | IO[bytes], output_type: str) ->
|
|
|
100
108
|
Args:
|
|
101
109
|
data: numpy array to save
|
|
102
110
|
target: Path to save to, or a writable file-like object (for zip streaming)
|
|
103
|
-
output_type: File format ("npy", "png", "jpg", "jpeg", "exr")
|
|
111
|
+
output_type: File format ("npy", "npz", "png", "jpg", "jpeg", "exr")
|
|
104
112
|
|
|
105
113
|
Raises:
|
|
106
114
|
ValueError: If output_type is not supported
|
|
@@ -112,6 +120,10 @@ def save_output(data: np.ndarray, target: Path | IO[bytes], output_type: str) ->
|
|
|
112
120
|
np.save(target, data)
|
|
113
121
|
return
|
|
114
122
|
|
|
123
|
+
if output_type == "npz":
|
|
124
|
+
np.savez_compressed(target, data=data)
|
|
125
|
+
return
|
|
126
|
+
|
|
115
127
|
if output_type in ("png", "jpg", "jpeg"):
|
|
116
128
|
_save_image_output(data, target, output_type)
|
|
117
129
|
return
|
|
@@ -125,6 +137,89 @@ def save_output(data: np.ndarray, target: Path | IO[bytes], output_type: str) ->
|
|
|
125
137
|
raise ValueError(f"Unsupported output type: {output_type}")
|
|
126
138
|
|
|
127
139
|
|
|
140
|
+
def _get_writer_modality_type(
|
|
141
|
+
output_config: OutputConfig,
|
|
142
|
+
writer_meta: dict[str, Any],
|
|
143
|
+
*,
|
|
144
|
+
strict: bool,
|
|
145
|
+
) -> str:
|
|
146
|
+
euler_train = writer_meta.get("euler_train")
|
|
147
|
+
if isinstance(euler_train, dict):
|
|
148
|
+
modality_type = euler_train.get("modality_type")
|
|
149
|
+
if modality_type:
|
|
150
|
+
return modality_type
|
|
151
|
+
|
|
152
|
+
if output_config.key in _LOADABLE_LEGACY_OUTPUTS:
|
|
153
|
+
return output_config.key
|
|
154
|
+
|
|
155
|
+
if strict:
|
|
156
|
+
return output_config.key
|
|
157
|
+
return "other"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _get_loadable_legacy_output(modality_type: str) -> dict[str, Any] | None:
|
|
161
|
+
return _LOADABLE_LEGACY_OUTPUTS.get(modality_type)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _get_writer_type_default(
|
|
165
|
+
output_config: OutputConfig,
|
|
166
|
+
writer_meta: dict[str, Any],
|
|
167
|
+
modality_type: str,
|
|
168
|
+
) -> str:
|
|
169
|
+
euler_train = writer_meta.get("euler_train")
|
|
170
|
+
if isinstance(euler_train, dict):
|
|
171
|
+
euler_train_modality_type = euler_train.get("modality_type")
|
|
172
|
+
if euler_train_modality_type:
|
|
173
|
+
return euler_train_modality_type
|
|
174
|
+
return modality_type or output_config.key
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _validate_loadable_legacy_output(
|
|
178
|
+
output_config: OutputConfig,
|
|
179
|
+
modality_type: str,
|
|
180
|
+
) -> None:
|
|
181
|
+
loadable = _get_loadable_legacy_output(modality_type)
|
|
182
|
+
if loadable is None:
|
|
183
|
+
return
|
|
184
|
+
|
|
185
|
+
output_types = loadable.get("output_types")
|
|
186
|
+
if output_types and output_config.type not in output_types:
|
|
187
|
+
supported = ", ".join(sorted(output_types))
|
|
188
|
+
raise ValueError(
|
|
189
|
+
f"{modality_type} outputs must use one of: {supported}. "
|
|
190
|
+
f"Got: {output_config.type}"
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _to_numpy(data: Any) -> np.ndarray:
|
|
195
|
+
if hasattr(data, "detach"):
|
|
196
|
+
data = data.detach()
|
|
197
|
+
if hasattr(data, "cpu"):
|
|
198
|
+
data = data.cpu()
|
|
199
|
+
if hasattr(data, "numpy"):
|
|
200
|
+
data = data.numpy()
|
|
201
|
+
return np.asarray(data)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _prepare_legacy_output_data(data: Any, output_config: OutputConfig) -> Any:
|
|
205
|
+
arr = _to_numpy(data)
|
|
206
|
+
writer_meta = output_config.writer or {}
|
|
207
|
+
modality_type = _get_writer_modality_type(
|
|
208
|
+
output_config,
|
|
209
|
+
writer_meta,
|
|
210
|
+
strict=True,
|
|
211
|
+
)
|
|
212
|
+
if modality_type != "points_3d":
|
|
213
|
+
return arr
|
|
214
|
+
|
|
215
|
+
if arr.ndim != 3 or arr.shape[0] != 3:
|
|
216
|
+
raise ValueError(
|
|
217
|
+
"points_3d output must have shape (3, H, W), "
|
|
218
|
+
f"got {arr.shape}"
|
|
219
|
+
)
|
|
220
|
+
return arr.astype(np.float32, copy=False)
|
|
221
|
+
|
|
222
|
+
|
|
128
223
|
def _get_writer_kwargs(
|
|
129
224
|
output_config: OutputConfig,
|
|
130
225
|
*,
|
|
@@ -134,14 +229,20 @@ def _get_writer_kwargs(
|
|
|
134
229
|
"""Derive DatasetWriter/ZipDatasetWriter kwargs from an OutputConfig."""
|
|
135
230
|
writer_meta = output_config.writer or {}
|
|
136
231
|
name = writer_meta.get("name", dataset_name or output_config.key)
|
|
232
|
+
modality_type = _get_writer_modality_type(
|
|
233
|
+
output_config,
|
|
234
|
+
writer_meta,
|
|
235
|
+
strict=strict,
|
|
236
|
+
)
|
|
237
|
+
_validate_loadable_legacy_output(output_config, modality_type)
|
|
238
|
+
|
|
137
239
|
type_ = writer_meta.get(
|
|
138
240
|
"type",
|
|
139
|
-
|
|
241
|
+
_get_writer_type_default(output_config, writer_meta, modality_type),
|
|
140
242
|
)
|
|
141
|
-
default_modality_type = output_config.key if strict else "other"
|
|
142
243
|
euler_train = writer_meta.get("euler_train", {
|
|
143
244
|
"used_as": "target",
|
|
144
|
-
"modality_type":
|
|
245
|
+
"modality_type": type_,
|
|
145
246
|
})
|
|
146
247
|
kwargs = dict(name=name, type=type_, euler_train=euler_train, separator=None)
|
|
147
248
|
|
|
@@ -149,7 +250,15 @@ def _get_writer_kwargs(
|
|
|
149
250
|
if meta is not None:
|
|
150
251
|
kwargs["meta"] = meta
|
|
151
252
|
|
|
152
|
-
euler_loading
|
|
253
|
+
if "euler_loading" in writer_meta:
|
|
254
|
+
euler_loading = writer_meta.get("euler_loading")
|
|
255
|
+
else:
|
|
256
|
+
loadable = _get_loadable_legacy_output(modality_type)
|
|
257
|
+
euler_loading = (
|
|
258
|
+
loadable.get("euler_loading")
|
|
259
|
+
if loadable is not None
|
|
260
|
+
else None
|
|
261
|
+
)
|
|
153
262
|
if euler_loading is not None:
|
|
154
263
|
merged = {
|
|
155
264
|
key: value for key, value in euler_train.items()
|
|
@@ -171,17 +280,21 @@ def create_writers(
|
|
|
171
280
|
_validate_pipeline_targets(config)
|
|
172
281
|
writers: dict[str, Any] = {}
|
|
173
282
|
selected_outputs = outputs if outputs is not None else config.outputs
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
283
|
+
try:
|
|
284
|
+
for output_config in selected_outputs:
|
|
285
|
+
kwargs = _get_writer_kwargs(
|
|
286
|
+
output_config,
|
|
287
|
+
strict=config.strict,
|
|
288
|
+
dataset_name=dataset_name,
|
|
289
|
+
)
|
|
290
|
+
root, zip_mode = _resolve_output_root(config, output_config)
|
|
291
|
+
if zip_mode:
|
|
292
|
+
writers[output_config.key] = ZipDatasetWriter(root, **kwargs)
|
|
293
|
+
else:
|
|
294
|
+
writers[output_config.key] = DatasetWriter(root, **kwargs)
|
|
295
|
+
except BaseException:
|
|
296
|
+
_close_writers(writers.values())
|
|
297
|
+
raise
|
|
185
298
|
return writers
|
|
186
299
|
|
|
187
300
|
|
|
@@ -379,12 +492,16 @@ def _create_source_backed_writers(
|
|
|
379
492
|
dataset: MultiModalDataset,
|
|
380
493
|
) -> dict[str, Any]:
|
|
381
494
|
writers: dict[str, Any] = {}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
plan.
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
495
|
+
try:
|
|
496
|
+
for plan in plans:
|
|
497
|
+
writers[plan.output_config.key] = dataset.create_output_writer(
|
|
498
|
+
plan.source_modality,
|
|
499
|
+
plan.output_root,
|
|
500
|
+
zip=plan.output_root.suffix.lower() == ".zip",
|
|
501
|
+
)
|
|
502
|
+
except BaseException:
|
|
503
|
+
_close_writers(writers.values())
|
|
504
|
+
raise
|
|
388
505
|
return writers
|
|
389
506
|
|
|
390
507
|
|
|
@@ -403,6 +520,7 @@ class PreparedOutputs:
|
|
|
403
520
|
legacy_writers: dict[str, Any]
|
|
404
521
|
pipeline_manifest_path: Path | None
|
|
405
522
|
pipeline_manifest_targets: list[PipelineOutputTargetConfig]
|
|
523
|
+
_finalized: bool = field(default=False, init=False, repr=False)
|
|
406
524
|
|
|
407
525
|
@classmethod
|
|
408
526
|
def prepare(
|
|
@@ -472,7 +590,10 @@ class PreparedOutputs:
|
|
|
472
590
|
|
|
473
591
|
file_id = _full_id_leaf(full_id)
|
|
474
592
|
for output_config in self.legacy_outputs:
|
|
475
|
-
output_data =
|
|
593
|
+
output_data = _prepare_legacy_output_data(
|
|
594
|
+
outputs[output_config.key],
|
|
595
|
+
output_config,
|
|
596
|
+
)
|
|
476
597
|
basename = (
|
|
477
598
|
f"{file_id}{output_config.effective_suffix}.{output_config.type}"
|
|
478
599
|
)
|
|
@@ -490,14 +611,65 @@ class PreparedOutputs:
|
|
|
490
611
|
save_output(output_data, path, output_config.type)
|
|
491
612
|
|
|
492
613
|
def finalize(self) -> None:
|
|
493
|
-
|
|
494
|
-
|
|
614
|
+
if self._finalized:
|
|
615
|
+
return
|
|
495
616
|
|
|
496
|
-
|
|
497
|
-
|
|
617
|
+
first_error: tuple[type[BaseException], BaseException, Any] | None = None
|
|
618
|
+
try:
|
|
619
|
+
for writers in (
|
|
620
|
+
self.legacy_writers.values(),
|
|
621
|
+
self.source_backed_writers.values(),
|
|
622
|
+
):
|
|
623
|
+
try:
|
|
624
|
+
_finalize_writers(writers)
|
|
625
|
+
except BaseException as exc:
|
|
626
|
+
if first_error is None:
|
|
627
|
+
first_error = (type(exc), exc, exc.__traceback__)
|
|
628
|
+
|
|
629
|
+
if (
|
|
630
|
+
first_error is None
|
|
631
|
+
and self.pipeline_manifest_path
|
|
632
|
+
and self.pipeline_manifest_targets
|
|
633
|
+
):
|
|
634
|
+
_write_pipeline_outputs_manifest(
|
|
635
|
+
self.pipeline_manifest_path,
|
|
636
|
+
self.pipeline_manifest_targets,
|
|
637
|
+
)
|
|
498
638
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
639
|
+
if first_error is not None:
|
|
640
|
+
_, exc, traceback = first_error
|
|
641
|
+
raise exc.with_traceback(traceback)
|
|
642
|
+
finally:
|
|
643
|
+
self._finalized = True
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def _finalize_writers(writers) -> None:
|
|
647
|
+
first_error: tuple[type[BaseException], BaseException, Any] | None = None
|
|
648
|
+
for writer in writers:
|
|
649
|
+
try:
|
|
650
|
+
writer.save_index()
|
|
651
|
+
except BaseException as exc:
|
|
652
|
+
if first_error is None:
|
|
653
|
+
first_error = (type(exc), exc, exc.__traceback__)
|
|
654
|
+
finally:
|
|
655
|
+
close = getattr(writer, "close", None)
|
|
656
|
+
if callable(close):
|
|
657
|
+
try:
|
|
658
|
+
close()
|
|
659
|
+
except BaseException as exc:
|
|
660
|
+
if first_error is None:
|
|
661
|
+
first_error = (type(exc), exc, exc.__traceback__)
|
|
662
|
+
|
|
663
|
+
if first_error is not None:
|
|
664
|
+
_, exc, traceback = first_error
|
|
665
|
+
raise exc.with_traceback(traceback)
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
def _close_writers(writers) -> None:
|
|
669
|
+
for writer in writers:
|
|
670
|
+
close = getattr(writer, "close", None)
|
|
671
|
+
if callable(close):
|
|
672
|
+
try:
|
|
673
|
+
close()
|
|
674
|
+
except Exception:
|
|
675
|
+
pass
|
|
@@ -13,7 +13,7 @@ class OutputConfig:
|
|
|
13
13
|
|
|
14
14
|
Attributes:
|
|
15
15
|
key: Key in the model's output dictionary (e.g., "depth", "dehazed")
|
|
16
|
-
type: File format to save as ("npy", "png", "jpg", "exr")
|
|
16
|
+
type: File format to save as ("npy", "npz", "png", "jpg", "exr")
|
|
17
17
|
suffix: Filename suffix before extension. Defaults to "_{key}".
|
|
18
18
|
Use empty string "" for no suffix (filename will be {id}.{type}).
|
|
19
19
|
source_modality: Optional regular input modality whose euler-loading
|
|
@@ -35,7 +35,7 @@ class OutputConfig:
|
|
|
35
35
|
if not self.type:
|
|
36
36
|
raise ValueError("output.type is required")
|
|
37
37
|
|
|
38
|
-
supported_types = {"npy", "png", "jpg", "jpeg", "exr"}
|
|
38
|
+
supported_types = {"npy", "npz", "png", "jpg", "jpeg", "exr"}
|
|
39
39
|
if self.type not in supported_types:
|
|
40
40
|
raise ValueError(
|
|
41
41
|
f"Unsupported output type '{self.type}'. Supported: {supported_types}"
|
|
@@ -16,7 +16,7 @@ Usage:
|
|
|
16
16
|
import argparse
|
|
17
17
|
import logging
|
|
18
18
|
from pathlib import Path
|
|
19
|
-
from typing import NamedTuple
|
|
19
|
+
from typing import Any, NamedTuple
|
|
20
20
|
|
|
21
21
|
from euler_loading import Modality, MultiModalDataset
|
|
22
22
|
from tqdm import tqdm
|
|
@@ -253,6 +253,67 @@ def _build_model_inputs(sample: dict[str, object]) -> dict[str, object]:
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
|
|
256
|
+
def _close_model(model: Any, logger: logging.Logger) -> None:
|
|
257
|
+
close = getattr(model, "close", None)
|
|
258
|
+
try:
|
|
259
|
+
if callable(close):
|
|
260
|
+
close()
|
|
261
|
+
finally:
|
|
262
|
+
_release_accelerator_cache(logger)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _release_accelerator_cache(logger: logging.Logger) -> None:
|
|
266
|
+
try:
|
|
267
|
+
import torch
|
|
268
|
+
except ImportError:
|
|
269
|
+
return
|
|
270
|
+
|
|
271
|
+
try:
|
|
272
|
+
if torch.cuda.is_available():
|
|
273
|
+
torch.cuda.empty_cache()
|
|
274
|
+
mps = getattr(torch, "mps", None)
|
|
275
|
+
empty_mps_cache = getattr(mps, "empty_cache", None)
|
|
276
|
+
if callable(empty_mps_cache):
|
|
277
|
+
empty_mps_cache()
|
|
278
|
+
except Exception as exc:
|
|
279
|
+
logger.debug("Failed to release accelerator cache: %s", exc)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _cleanup_after_inference(
|
|
283
|
+
*,
|
|
284
|
+
prepared_outputs: PreparedOutputs | None,
|
|
285
|
+
model: Any | None,
|
|
286
|
+
logger: logging.Logger,
|
|
287
|
+
active_error: BaseException | None,
|
|
288
|
+
) -> None:
|
|
289
|
+
cleanup_error: BaseException | None = None
|
|
290
|
+
|
|
291
|
+
if prepared_outputs is not None:
|
|
292
|
+
try:
|
|
293
|
+
prepared_outputs.finalize()
|
|
294
|
+
except BaseException as exc:
|
|
295
|
+
cleanup_error = exc
|
|
296
|
+
|
|
297
|
+
if model is not None:
|
|
298
|
+
try:
|
|
299
|
+
_close_model(model, logger)
|
|
300
|
+
except BaseException as exc:
|
|
301
|
+
if cleanup_error is None:
|
|
302
|
+
cleanup_error = exc
|
|
303
|
+
|
|
304
|
+
if cleanup_error is None:
|
|
305
|
+
return
|
|
306
|
+
|
|
307
|
+
if active_error is not None:
|
|
308
|
+
logger.warning(
|
|
309
|
+
"Inference failed, and cleanup also failed: %s",
|
|
310
|
+
cleanup_error,
|
|
311
|
+
)
|
|
312
|
+
return
|
|
313
|
+
|
|
314
|
+
raise cleanup_error
|
|
315
|
+
|
|
316
|
+
|
|
256
317
|
def _build_arg_parser() -> argparse.ArgumentParser:
|
|
257
318
|
parser = argparse.ArgumentParser(
|
|
258
319
|
description="Run model inference using euler-loading",
|
|
@@ -420,46 +481,60 @@ def run_inference(config: InferenceConfig, verbose: bool = False) -> None:
|
|
|
420
481
|
# Prepare writers (one per output key)
|
|
421
482
|
output_base = Path(config.output_base_path)
|
|
422
483
|
output_base.mkdir(parents=True, exist_ok=True)
|
|
423
|
-
prepared_outputs =
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
484
|
+
prepared_outputs: PreparedOutputs | None = None
|
|
485
|
+
model: Any | None = None
|
|
486
|
+
active_error: BaseException | None = None
|
|
487
|
+
try:
|
|
488
|
+
prepared_outputs = PreparedOutputs.prepare(
|
|
489
|
+
config=config,
|
|
490
|
+
dataset=dataset,
|
|
491
|
+
logger=logger,
|
|
492
|
+
)
|
|
428
493
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
494
|
+
# Load model
|
|
495
|
+
logger.info("Loading model: %s", config.model_name)
|
|
496
|
+
model = get_model_from_config(config)
|
|
497
|
+
device_info = getattr(model, "device", "unknown")
|
|
498
|
+
logger.info("Model loaded on device: %s", device_info)
|
|
434
499
|
|
|
435
|
-
|
|
436
|
-
|
|
500
|
+
# Run inference loop
|
|
501
|
+
logger.info("Starting inference...")
|
|
437
502
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
503
|
+
for i, sample in enumerate(tqdm(dataset, total=total_samples, desc="Inference")):
|
|
504
|
+
if config.max_samples and i >= config.max_samples:
|
|
505
|
+
break
|
|
441
506
|
|
|
442
|
-
|
|
507
|
+
full_id = sample["full_id"]
|
|
443
508
|
|
|
444
|
-
|
|
445
|
-
|
|
509
|
+
# Build inputs: pass all loaded modalities (skip euler-loading metadata)
|
|
510
|
+
inputs = _build_model_inputs(sample)
|
|
446
511
|
|
|
447
|
-
|
|
448
|
-
|
|
512
|
+
# Run model inference
|
|
513
|
+
outputs = model.predict(inputs)
|
|
449
514
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
515
|
+
prepared_outputs.validate_prediction_keys(outputs)
|
|
516
|
+
prepared_outputs.write_predictions(
|
|
517
|
+
sample_index=i,
|
|
518
|
+
full_id=full_id,
|
|
519
|
+
outputs=outputs,
|
|
520
|
+
)
|
|
521
|
+
del outputs, inputs, sample
|
|
522
|
+
|
|
523
|
+
if verbose and (i + 1) % 100 == 0:
|
|
524
|
+
logger.debug("Processed %d/%d", i + 1, total_samples)
|
|
525
|
+
except BaseException as exc:
|
|
526
|
+
active_error = exc
|
|
527
|
+
raise
|
|
528
|
+
finally:
|
|
529
|
+
# Finalize writers (writes metadata / closes zip archives), release
|
|
530
|
+
# the model, and clear unused accelerator cache for repeated API calls.
|
|
531
|
+
_cleanup_after_inference(
|
|
532
|
+
prepared_outputs=prepared_outputs,
|
|
533
|
+
model=model,
|
|
534
|
+
logger=logger,
|
|
535
|
+
active_error=active_error,
|
|
455
536
|
)
|
|
456
537
|
|
|
457
|
-
if verbose and (i + 1) % 100 == 0:
|
|
458
|
-
logger.debug("Processed %d/%d", i + 1, total_samples)
|
|
459
|
-
|
|
460
|
-
# Finalize writers (writes output.json / closes zip archives)
|
|
461
|
-
prepared_outputs.finalize()
|
|
462
|
-
|
|
463
538
|
logger.info("Inference complete!")
|
|
464
539
|
logger.info("Predictions saved to: %s", output_base)
|
|
465
540
|
|