diff --git a/src/core/util.cpp b/src/core/util.cpp index 2a233e69..1653f241 100644 --- a/src/core/util.cpp +++ b/src/core/util.cpp @@ -755,6 +755,13 @@ sd::Tensor clip_preprocess(const sd::Tensor& image, int target_wid int64_t resized_width = static_cast(scale * static_cast(image.shape()[0])); int64_t resized_height = static_cast(scale * static_cast(image.shape()[1])); + // The resized image must cover the crop window. Floating-point rounding can + // leave a side one pixel short of the crop target (e.g. 730 -> 735.999... + // -> 735 after truncation), so clamp to keep the center crop in bounds. + // Truncation is otherwise preserved to avoid changing existing results. + resized_width = std::max(resized_width, target_width); + resized_height = std::max(resized_height, target_height); + sd::Tensor resized = sd::ops::interpolate( image, {resized_width, resized_height, image.shape()[2], image.shape()[3]});