Skip to content

Image Pipeline Playground

fix(backend/engine): run full image batch per iteration

Mohammad Rajaeia4d4bf2ddacd
diff --git a/backend/app/engine/executor.py b/backend/app/engine/executor.py
index af2bf16..3d208d4 100644
--- a/backend/app/engine/executor.py
+++ b/backend/app/engine/executor.py
@@ -26,6 +26,7 @@ from app.models.graph import (
NodeInstance,
PortDirection,
)
+from app.nodes.io import list_image_files
class DagValidationError(ValueError):
@@ -189,7 +190,7 @@ def _collect_inputs(
node_id: str,
edges: list[Edge],
outputs: dict[str, dict[str, Any]],
- sample_index: int = 0,
+ batch_index: int = 0,
) -> dict[str, Any]:
incoming = [edge for edge in edges if edge.target == node_id]
inputs: dict[str, Any] = {}
@@ -202,13 +203,13 @@ def _collect_inputs(
value = next(iter(source_outputs.values()))
if value is None:
continue
- # Fan-out batch image inputs one image per sample so unary nodes keep working.
+ # Fan-out batch image lists one image per batch index so unary nodes keep working.
if (
isinstance(value, list)
and value
and all(isinstance(item, np.ndarray) for item in value)
):
- grouped[edge.target_port].append(value[sample_index % len(value)])
+ grouped[edge.target_port].append(value[batch_index % len(value)])
else:
grouped[edge.target_port].append(value)
@@ -218,6 +219,24 @@ def _collect_inputs(
return inputs
+def _batch_size_for_graph(graph: Graph) -> int:
+ """Largest Load Images batch in the graph (1 when there is no multi-image source)."""
+ size = 1
+ for node in graph.nodes:
+ if node.type != "load_image":
+ continue
+ path_value = str(node.params.get("path", "")).strip()
+ if not path_value:
+ continue
+ try:
+ files = list_image_files(Path(path_value))
+ except (OSError, ValueError, FileNotFoundError):
+ continue
+ if files:
+ size = max(size, len(files))
+ return size
+
+
def _input_hashes(
inputs: dict[str, Any],
cache: CacheManager,
@@ -231,6 +250,7 @@ def _emit_port_previews(
node_id: str,
produced: dict[str, Any],
sample_index: int,
+ batch_index: int,
cache_hit: bool,
sample_previews: dict[str, Any],
) -> None:
@@ -242,22 +262,25 @@ def _emit_port_previews(
for port_id, value in produced.items():
if is_image_value(value):
if isinstance(value, list):
- encoded_list: list[str] = []
- for index, image in enumerate(value):
- preview = _encode_preview(image)
- encoded_list.append(preview)
- emit(
- ExecutionEvent(
- type=ExecutionEventType.PREVIEW,
- node_id=node_id,
- port_id=port_id,
- image_b64=preview,
- sample_index=index,
- cache_hit=cache_hit,
- )
+ if not value:
+ continue
+ image = value[batch_index % len(value)]
+ if not isinstance(image, np.ndarray):
+ continue
+ preview = _encode_preview(image)
+ port_previews[port_id] = preview
+ # Key source batch previews by batch index so iterations do not
+ # duplicate the Load Images thumbnails in the UI.
+ emit(
+ ExecutionEvent(
+ type=ExecutionEventType.PREVIEW,
+ node_id=node_id,
+ port_id=port_id,
+ image_b64=preview,
+ sample_index=batch_index,
+ cache_hit=cache_hit,
)
- if encoded_list:
- port_previews[port_id] = encoded_list
+ )
continue
image = _as_image(value)
@@ -324,7 +347,10 @@ class DagExecutor:
if request.target_node_id:
keep = ancestors_through_target(request.graph, request.target_node_id)
order = [node_id for node_id in order if node_id in keep]
- sample_count = max(1, request.sample_count)
+ # sample_count == UI "Iterations": how many times to run the full image set.
+ iteration_count = max(1, request.sample_count)
+ batch_size = _batch_size_for_graph(request.graph)
+ total_passes = iteration_count * batch_size
results: dict[str, Any] = {"order": order, "samples": []}
save_bundle = SaveBundle()
bundle_token = current_save_bundle.set(save_bundle)
@@ -334,96 +360,107 @@ class DagExecutor:
on_event(event)
try:
- for sample_index in range(sample_count):
- cancel.check()
- sample_seed = request.seed + sample_index
- sample_token = current_sample_index.set(sample_index)
- outputs: dict[str, dict[str, Any]] = {}
- sample_previews: dict[str, Any] = {}
-
- try:
- for index, node_id in enumerate(order):
- cancel.check()
- instance = nodes[node_id]
- node_impl = self.registry.get(instance.type)
- params = node_impl.validate_params(instance.params)
- node_impl.prepare_run(params)
- inputs = _collect_inputs(
- node_id,
- request.graph.edges,
- outputs,
- sample_index=sample_index,
- )
- input_hash_map = _input_hashes(inputs, self.cache)
- cache_key = self.cache.make_key(
- instance.type, params, input_hash_map, sample_seed
- )
+ for iteration in range(iteration_count):
+ # Stochastic seed is per iteration, shared across the image batch.
+ iteration_seed = request.seed + iteration
+ for batch_index in range(batch_size):
+ cancel.check()
+ flat_index = iteration * batch_size + batch_index
+ sample_token = current_sample_index.set(flat_index)
+ outputs: dict[str, dict[str, Any]] = {}
+ sample_previews: dict[str, Any] = {}
+
+ try:
+ for index, node_id in enumerate(order):
+ cancel.check()
+ instance = nodes[node_id]
+ node_impl = self.registry.get(instance.type)
+ params = node_impl.validate_params(instance.params)
+ node_impl.prepare_run(params)
+ inputs = _collect_inputs(
+ node_id,
+ request.graph.edges,
+ outputs,
+ batch_index=batch_index,
+ )
+ input_hash_map = _input_hashes(inputs, self.cache)
+ cache_key = self.cache.make_key(
+ instance.type, params, input_hash_map, iteration_seed
+ )
- emit(
- ExecutionEvent(
- type=ExecutionEventType.PROGRESS,
- node_id=node_id,
- progress=(index + 1) / len(order),
- sample_index=sample_index,
- message=f"Executing {instance.type}",
+ progress = (flat_index * len(order) + index + 1) / (
+ total_passes * len(order)
+ )
+ emit(
+ ExecutionEvent(
+ type=ExecutionEventType.PROGRESS,
+ node_id=node_id,
+ progress=progress,
+ sample_index=flat_index,
+ message=f"Executing {instance.type}",
+ )
)
- )
- cache_hit = False
- produced: dict[str, Any]
- started = time.perf_counter()
- use_cache = request.cache and node_impl.cacheable
- if use_cache and self.cache.has_outputs(cache_key):
- cached = self.cache.get_outputs(cache_key)
- if cached is not None:
- produced = cached
+ cache_hit = False
+ produced: dict[str, Any]
+ started = time.perf_counter()
+ use_cache = request.cache and node_impl.cacheable
+ if use_cache and self.cache.has_outputs(cache_key):
+ cached = self.cache.get_outputs(cache_key)
+ if cached is not None:
+ produced = cached
+ outputs[node_id] = produced
+ cache_hit = True
+
+ if not cache_hit:
+ produced = node_impl.execute(
+ inputs, params, seed=iteration_seed
+ )
outputs[node_id] = produced
- cache_hit = True
-
- if not cache_hit:
- produced = node_impl.execute(inputs, params, seed=sample_seed)
- outputs[node_id] = produced
- if use_cache:
- self.cache.put_outputs(
- cache_key,
- produced,
- meta={"node_id": node_id, "type": instance.type},
+ if use_cache:
+ self.cache.put_outputs(
+ cache_key,
+ produced,
+ meta={"node_id": node_id, "type": instance.type},
+ )
+
+ duration_ms = (time.perf_counter() - started) * 1000.0
+ emit(
+ ExecutionEvent(
+ type=ExecutionEventType.PROGRESS,
+ node_id=node_id,
+ progress=progress,
+ sample_index=flat_index,
+ cache_hit=cache_hit,
+ duration_ms=duration_ms,
+ message=(
+ f"Finished {instance.type} in {duration_ms:.1f}ms"
+ + (" (cache)" if cache_hit else "")
+ ),
)
+ )
- duration_ms = (time.perf_counter() - started) * 1000.0
- emit(
- ExecutionEvent(
- type=ExecutionEventType.PROGRESS,
+ _emit_port_previews(
+ emit=emit,
node_id=node_id,
- progress=(index + 1) / len(order),
- sample_index=sample_index,
+ produced=produced,
+ sample_index=flat_index,
+ batch_index=batch_index,
cache_hit=cache_hit,
- duration_ms=duration_ms,
- message=(
- f"Finished {instance.type} in {duration_ms:.1f}ms"
- + (" (cache)" if cache_hit else "")
- ),
+ sample_previews=sample_previews,
)
- )
- _emit_port_previews(
- emit=emit,
- node_id=node_id,
- produced=produced,
- sample_index=sample_index,
- cache_hit=cache_hit,
- sample_previews=sample_previews,
+ results["samples"].append(
+ {
+ "sample_index": flat_index,
+ "iteration": iteration,
+ "batch_index": batch_index,
+ "seed": iteration_seed,
+ "previews": sample_previews,
+ }
)
-
- results["samples"].append(
- {
- "sample_index": sample_index,
- "seed": sample_seed,
- "previews": sample_previews,
- }
- )
- finally:
- current_sample_index.reset(sample_token)
+ finally:
+ current_sample_index.reset(sample_token)
if save_bundle.files:
zip_path = save_bundle.write_zip()
diff --git a/backend/tests/test_executor_batch.py b/backend/tests/test_executor_batch.py
new file mode 100644
index 0000000..68d996d
--- /dev/null
+++ b/backend/tests/test_executor_batch.py
@@ -0,0 +1,120 @@
+"""Batch image sets run fully under a single iteration."""
+
+from __future__ import annotations
+
+import cv2
+import numpy as np
+
+from app.engine.executor import DagExecutor, _batch_size_for_graph
+from app.engine.registry import registry
+from app.models.graph import Edge, ExecuteRequest, Graph, NodeInstance
+from app.nodes import register_builtin_nodes
+
+
+def test_batch_size_from_load_image_folder(tmp_path) -> None:
+ for index, value in enumerate((10, 20, 30)):
+ image = np.full((4, 4, 3), value, dtype=np.uint8)
+ cv2.imwrite(str(tmp_path / f"img{index}.png"), image)
+
+ graph = Graph(
+ nodes=[
+ NodeInstance(
+ id="load",
+ type="load_image",
+ params={"path": str(tmp_path)},
+ )
+ ],
+ edges=[],
+ )
+ assert _batch_size_for_graph(graph) == 3
+
+
+def test_one_iteration_processes_entire_image_set(tmp_path) -> None:
+ register_builtin_nodes()
+ values = (11, 22, 33)
+ for index, value in enumerate(values):
+ image = np.full((4, 4, 3), value, dtype=np.uint8)
+ cv2.imwrite(str(tmp_path / f"img{index}.png"), image)
+
+ request = ExecuteRequest(
+ graph=Graph(
+ nodes=[
+ NodeInstance(
+ id="load",
+ type="load_image",
+ params={"path": str(tmp_path)},
+ ),
+ NodeInstance(
+ id="blur",
+ type="gaussian_blur",
+ params={"ksize": 3, "sigma": 0},
+ ),
+ ],
+ edges=[
+ Edge(
+ id="e1",
+ source="load",
+ source_port="image",
+ target="blur",
+ target_port="image",
+ )
+ ],
+ ),
+ seed=0,
+ sample_count=1,
+ cache=False,
+ )
+
+ preview_indices: list[int] = []
+ result = DagExecutor(tmp_path / "cache", node_registry=registry).execute(
+ request,
+ on_event=lambda event: preview_indices.append(event.sample_index)
+ if event.type.value == "preview" and event.node_id == "blur"
+ else None,
+ )
+
+ assert len(result["samples"]) == 3
+ assert [sample["batch_index"] for sample in result["samples"]] == [0, 1, 2]
+ assert [sample["iteration"] for sample in result["samples"]] == [0, 0, 0]
+ assert [sample["seed"] for sample in result["samples"]] == [0, 0, 0]
+ assert sorted(set(preview_indices)) == [0, 1, 2]
+
+
+def test_two_iterations_repeat_full_set_with_seed_offset(tmp_path) -> None:
+ register_builtin_nodes()
+ for index, value in enumerate((5, 9)):
+ image = np.full((4, 4, 3), value, dtype=np.uint8)
+ cv2.imwrite(str(tmp_path / f"img{index}.png"), image)
+
+ request = ExecuteRequest(
+ graph=Graph(
+ nodes=[
+ NodeInstance(
+ id="load",
+ type="load_image",
+ params={"path": str(tmp_path)},
+ )
+ ],
+ edges=[],
+ ),
+ seed=7,
+ sample_count=2,
+ cache=False,
+ )
+ load_preview_indices: list[int] = []
+ result = DagExecutor(tmp_path / "cache", node_registry=registry).execute(
+ request,
+ on_event=lambda event: load_preview_indices.append(event.sample_index)
+ if event.type.value == "preview" and event.node_id == "load"
+ else None,
+ )
+
+ assert len(result["samples"]) == 4
+ assert [(s["iteration"], s["batch_index"], s["seed"]) for s in result["samples"]] == [
+ (0, 0, 7),
+ (0, 1, 7),
+ (1, 0, 8),
+ (1, 1, 8),
+ ]
+ # Load Images previews stay keyed by batch index (not flat iteration×batch).
+ assert sorted(set(load_preview_indices)) == [0, 1]
diff --git a/backend/tests/test_io_save_image.py b/backend/tests/test_io_save_image.py
index 16a4c49..02cf51d 100644
--- a/backend/tests/test_io_save_image.py
+++ b/backend/tests/test_io_save_image.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import zipfile
from datetime import datetime
import cv2
@@ -100,7 +101,7 @@ def test_executor_emits_download_zip(tmp_path, monkeypatch) -> None:
],
),
seed=0,
- sample_count=2,
+ sample_count=1,
cache=False,
)
executor = DagExecutor(tmp_path / "cache", node_registry=registry)
@@ -111,10 +112,14 @@ def test_executor_emits_download_zip(tmp_path, monkeypatch) -> None:
assert "download" in events
assert result["download_url"].startswith("/api/downloads/")
+ assert len(result["samples"]) == 2
zip_name = result["download_filename"]
zip_path = tmp_path / "downloads" / zip_name
assert zip_path.is_file()
+ with zipfile.ZipFile(zip_path) as archive:
+ assert sorted(archive.namelist()) == ["a_0.png", "b_1.png"]
+
client = TestClient(app)
# Point API download dir to the same temp folder used by the executor.
monkeypatch.setattr("app.engine.save_bundle.DOWNLOAD_DIR", tmp_path / "downloads")