Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(controlnet): preserve lineart source dimensions

Josh Corbett92f5918bc768
diff --git a/invokeai/backend/image_util/lineart.py b/invokeai/backend/image_util/lineart.py
index bfef6f6da0..9db118265a 100644
--- a/invokeai/backend/image_util/lineart.py
+++ b/invokeai/backend/image_util/lineart.py
@@ -205,6 +205,7 @@ class LineartEdgeDetector:
device = get_effective_device(self.model)
np_image = pil_to_np(image)
+ height, width = np_image.shape[:2]
with torch.no_grad():
np_image = torch.from_numpy(np_image).float().to(device)
@@ -213,6 +214,8 @@ class LineartEdgeDetector:
line = self.model(np_image)[0][0]
line = line.cpu().numpy()
+ if line.shape != (height, width):
+ line = cv2.resize(line, (width, height), interpolation=cv2.INTER_LINEAR)
line = (line * 255.0).clip(0, 255).astype(np.uint8)
detected_map = 255 - line
diff --git a/tests/backend/image_util/test_lineart.py b/tests/backend/image_util/test_lineart.py
new file mode 100644
index 0000000000..57bde5ec64
--- /dev/null
+++ b/tests/backend/image_util/test_lineart.py
@@ -0,0 +1,27 @@
+import math
+
+import pytest
+import torch
+from PIL import Image
+
+from invokeai.backend.image_util.lineart import LineartEdgeDetector
+
+
+class _StrideRoundedLineartModel(torch.nn.Module):
+ """Models the lineart generator's two stride-2 down/up-sampling stages."""
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ height, width = image.shape[-2:]
+ output_height = math.ceil(height / 4) * 4
+ output_width = math.ceil(width / 4) * 4
+ return torch.zeros((1, 1, output_height, output_width), dtype=image.dtype, device=image.device)
+
+
[email protected]("size", [(64, 64), (65, 67), (66, 68), (67, 69)])
+def test_lineart_edge_detector_preserves_input_dimensions(size: tuple[int, int]) -> None:
+ source = Image.new("RGB", size, "white")
+ detector = LineartEdgeDetector(_StrideRoundedLineartModel()) # type: ignore[arg-type]
+
+ result = detector.run(source)
+
+ assert result.size == source.size