fix: prevent clip_preprocess center crop from exceeding the resized image (#1995)

This commit is contained in:
Fabrice Aneche 2026-09-19 06:08:34 -04:00 committed by GitHub
parent 9982c9caae
commit d32b4e893b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -755,6 +755,13 @@ sd::Tensor<float> clip_preprocess(const sd::Tensor<float>& image, int target_wid
int64_t resized_width = static_cast<int64_t>(scale * static_cast<float>(image.shape()[0])); int64_t resized_width = static_cast<int64_t>(scale * static_cast<float>(image.shape()[0]));
int64_t resized_height = static_cast<int64_t>(scale * static_cast<float>(image.shape()[1])); int64_t resized_height = static_cast<int64_t>(scale * static_cast<float>(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<int64_t>(resized_width, target_width);
resized_height = std::max<int64_t>(resized_height, target_height);
sd::Tensor<float> resized = sd::ops::interpolate( sd::Tensor<float> resized = sd::ops::interpolate(
image, image,
{resized_width, resized_height, image.shape()[2], image.shape()[3]}); {resized_width, resized_height, image.shape()[2], image.shape()[3]});