diff --git a/gemma/vit_gemma4.cc b/gemma/vit_gemma4.cc index e59c8d36..02c2fd49 100644 --- a/gemma/vit_gemma4.cc +++ b/gemma/vit_gemma4.cc @@ -596,7 +596,7 @@ void PrefillVitGemma4(const ModelConfig& model_config, max_patches, pooling_kernel_size, target_height, target_width); - resized_image.ResizeBilinear(target_width, target_height); + resized_image.ResizeBicubic(target_width, target_height); // 2. Embed patches & add position embeddings EmbedImagePatchesGemma4(resized_image, model_config, weights, activations, diff --git a/paligemma/image.cc b/paligemma/image.cc index b1a959c3..65ed00bb 100644 --- a/paligemma/image.cc +++ b/paligemma/image.cc @@ -228,14 +228,16 @@ void Image::ResizeBilinear(int new_width, int new_height) { for (int y = 0; y < new_height; ++y) { const float src_y = (y + 0.5f) * scale_y - 0.5f; - const float src_y_clamped = std::clamp(src_y, 0.0f, static_cast(height_ - 1)); + const float src_y_clamped = + std::clamp(src_y, 0.0f, static_cast(height_ - 1)); const int y_l = static_cast(std::floor(src_y_clamped)); const int y_h = std::min(height_ - 1, y_l + 1); const float d_y = src_y_clamped - y_l; for (int x = 0; x < new_width; ++x) { const float src_x = (x + 0.5f) * scale_x - 0.5f; - const float src_x_clamped = std::clamp(src_x, 0.0f, static_cast(width_ - 1)); + const float src_x_clamped = + std::clamp(src_x, 0.0f, static_cast(width_ - 1)); const int x_l = static_cast(std::floor(src_x_clamped)); const int x_h = std::min(width_ - 1, x_l + 1); const float d_x = src_x_clamped - x_l; @@ -246,10 +248,14 @@ void Image::ResizeBilinear(int new_width, int new_height) { const float w11 = d_x * d_y; for (int k = 0; k < 3; ++k) { - const float v00 = data_[(static_cast(y_l) * width_ + x_l) * 3 + k]; - const float v10 = data_[(static_cast(y_l) * width_ + x_h) * 3 + k]; - const float v01 = data_[(static_cast(y_h) * width_ + x_l) * 3 + k]; - const float v11 = data_[(static_cast(y_h) * width_ + x_h) * 3 + k]; + const float v00 = + data_[(static_cast(y_l) * width_ + x_l) * 3 + k]; + const float v10 = + data_[(static_cast(y_l) * width_ + x_h) * 3 + k]; + const float v01 = + data_[(static_cast(y_h) * width_ + x_l) * 3 + k]; + const float v11 = + data_[(static_cast(y_h) * width_ + x_h) * 3 + k]; new_data[(static_cast(y) * new_width + x) * 3 + k] = w00 * v00 + w10 * v10 + w01 * v01 + w11 * v11; @@ -261,6 +267,157 @@ void Image::ResizeBilinear(int new_width, int new_height) { width_ = new_width; } +namespace { + +double BicubicFilter(double x) { + // Catmull-Rom (Keys cubic with a = -0.5), matching Pillow Image.BICUBIC. + x = std::abs(x); + if (x < 1.0) return ((1.5 * x - 2.5) * x) * x + 1.0; + if (x < 2.0) return ((-0.5 * x + 2.5) * x - 4.0) * x + 2.0; + return 0.0; +} + +struct ResampleCoeffs { + size_t ksize; + std::vector bounds; // [2 * out_size]: (xmin, count) per output idx + std::vector weights; // [out_size * ksize] +}; + +ResampleCoeffs ComputeBicubicCoeffs(int in_size, int out_size) { + const double scale = static_cast(in_size) / out_size; + const double filter_scale = scale > 1.0 ? scale : 1.0; + const double support = 2.0 * filter_scale; + // Cap ksize at in_size since count = xmax - xmin <= in_size; compute in + // uint64_t so extreme downscales (e.g. in_size > INT32_MAX / 4, out_size = 1) + // cannot overflow signed 32-bit int. + const uint64_t raw_ksize = static_cast(std::ceil(support)) * 2 + 1; + const size_t ksize = + static_cast(std::min(raw_ksize, in_size)); + + ResampleCoeffs c; + c.ksize = ksize; + c.bounds.resize(static_cast(out_size) * 2); + c.weights.assign(static_cast(out_size) * ksize, 0.0f); + + for (int i = 0; i < out_size; ++i) { + const double center = (i + 0.5) * scale; + const int xmin = + static_cast(std::max(0.0, std::floor(center - support + 0.5))); + const int xmax = static_cast(std::min( + static_cast(in_size), std::floor(center + support + 0.5))); + const int count = xmax - xmin; + HWY_ASSERT(count > 0); + HWY_ASSERT(static_cast(count) <= ksize); + c.bounds[i * 2] = xmin; + c.bounds[i * 2 + 1] = count; + + double wsum = 0.0; + float* w = &c.weights[static_cast(i) * ksize]; + for (int j = 0; j < count; ++j) { + const double d = (xmin + j + 0.5 - center) / filter_scale; + const double weight = BicubicFilter(d); + w[j] = static_cast(weight); + wsum += weight; + } + if (wsum != 0.0) { + const float inv = static_cast(1.0 / wsum); + for (int j = 0; j < count; ++j) { + w[j] *= inv; + } + } + } + return c; +} + +std::vector ResampleHorizontal(const float* src, int height, + int in_width, int out_width) { + const ResampleCoeffs hx = ComputeBicubicCoeffs(in_width, out_width); + std::vector out(static_cast(height) * out_width * 3); + for (int y = 0; y < height; ++y) { + const float* in_row = src + static_cast(y) * in_width * 3; + float* out_row = out.data() + static_cast(y) * out_width * 3; + for (int x = 0; x < out_width; ++x) { + const int xmin = hx.bounds[x * 2]; + const int count = hx.bounds[x * 2 + 1]; + const float* w = &hx.weights[static_cast(x) * hx.ksize]; + float s0 = 0.0f; + float s1 = 0.0f; + float s2 = 0.0f; + for (int j = 0; j < count; ++j) { + const float* px = in_row + static_cast(xmin + j) * 3; + s0 += w[j] * px[0]; + s1 += w[j] * px[1]; + s2 += w[j] * px[2]; + } + out_row[x * 3 + 0] = std::clamp(s0, -1.0f, 1.0f); + out_row[x * 3 + 1] = std::clamp(s1, -1.0f, 1.0f); + out_row[x * 3 + 2] = std::clamp(s2, -1.0f, 1.0f); + } + } + return out; +} + +std::vector ResampleVertical(const float* src, int in_height, + int out_height, int width) { + const ResampleCoeffs hy = ComputeBicubicCoeffs(in_height, out_height); + const size_t row_floats = static_cast(width) * 3; + std::vector out(static_cast(out_height) * row_floats); + for (int y = 0; y < out_height; ++y) { + const int ymin = hy.bounds[y * 2]; + const int count = hy.bounds[y * 2 + 1]; + const float* w = &hy.weights[static_cast(y) * hy.ksize]; + float* out_row = out.data() + static_cast(y) * row_floats; + for (int x = 0; x < width; ++x) { + float s0 = 0.0f; + float s1 = 0.0f; + float s2 = 0.0f; + for (int j = 0; j < count; ++j) { + const float* px = + src + static_cast(ymin + j) * row_floats + x * 3; + s0 += w[j] * px[0]; + s1 += w[j] * px[1]; + s2 += w[j] * px[2]; + } + out_row[x * 3 + 0] = std::clamp(s0, -1.0f, 1.0f); + out_row[x * 3 + 1] = std::clamp(s1, -1.0f, 1.0f); + out_row[x * 3 + 2] = std::clamp(s2, -1.0f, 1.0f); + } + } + return out; +} + +} // namespace + +void Image::ResizeBicubic(int new_width, int new_height) { + HWY_ASSERT(width_ > 0); + HWY_ASSERT(height_ > 0); + HWY_ASSERT(data_.size() == static_cast(width_) * height_ * 3); + HWY_ASSERT(new_width > 0); + HWY_ASSERT(new_height > 0); + HWY_ASSERT(new_width <= INT32_MAX / 3); + HWY_ASSERT(new_width * 3 <= INT32_MAX / new_height); + if (width_ == new_width && height_ == new_height) return; + + if (width_ == new_width) { + data_ = ResampleVertical(data_.data(), height_, new_height, width_); + } else if (height_ == new_height) { + data_ = ResampleHorizontal(data_.data(), height_, width_, new_width); + } else if (static_cast(height_) * new_width <= + static_cast(width_) * new_height) { + // Run the pass with the smaller intermediate buffer first so the + // intermediate size never exceeds max(input, output) <= INT32_MAX / 3. + const std::vector tmp = + ResampleHorizontal(data_.data(), height_, width_, new_width); + data_ = ResampleVertical(tmp.data(), height_, new_height, new_width); + } else { + const std::vector tmp = + ResampleVertical(data_.data(), height_, new_height, width_); + data_ = ResampleHorizontal(tmp.data(), new_height, width_, new_width); + } + height_ = new_height; + width_ = new_width; +} + bool Image::WriteBinary(const std::string& filename) const { // Writes the floating point values as float32 in binary format. std::ofstream file(filename); diff --git a/paligemma/image.h b/paligemma/image.h index 938acd21..5edc416d 100644 --- a/paligemma/image.h +++ b/paligemma/image.h @@ -43,6 +43,9 @@ class Image { void Resize(int width, int height); // Resizes to width x height using bilinear interpolation. void ResizeBilinear(int width, int height); + // Resizes to width x height using antialiased bicubic (Catmull-Rom, a=-0.5) + // interpolation, matching Pillow Image.BICUBIC. + void ResizeBicubic(int width, int height); // Writes the file as plain floats in binary. Useful to e.g. load in a colab. bool WriteBinary(const std::string& filename) const; // Stores the patch for the given patch number in `patch`. diff --git a/paligemma/image_test.cc b/paligemma/image_test.cc index d95a9de6..031c25e1 100644 --- a/paligemma/image_test.cc +++ b/paligemma/image_test.cc @@ -97,17 +97,17 @@ TEST(ImageTest, Non224) { EXPECT_NEAR(image.data()[5], Normalize(2.0f, max_value), 1e-6); EXPECT_NEAR(image.data()[6], Normalize(3.0f, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 9], - Normalize(data.size() - 6, max_value), 1e-6); + Normalize(data.size() - 6, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 8], - Normalize(data.size() - 5, max_value), 1e-6); + Normalize(data.size() - 5, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 7], - Normalize(data.size() - 4, max_value), 1e-6); + Normalize(data.size() - 4, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 3], - Normalize(data.size() - 3, max_value), 1e-6); + Normalize(data.size() - 3, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 2], - Normalize(data.size() - 2, max_value), 1e-6); + Normalize(data.size() - 2, max_value), 1e-6); EXPECT_NEAR(image.data()[image.size() - 1], - Normalize(data.size() - 1, max_value), 1e-6); + Normalize(data.size() - 1, max_value), 1e-6); // Extract two patches. const size_t kPatchValues = 14 * 14 * 3; // = 588 float patch[kPatchValues]; @@ -177,8 +177,7 @@ TEST(ImageTest, Resize1x1) { // Downsample a 2x2 image with distinct pixels to 1x2 and 1x1 (exercises // NearestNeighbor to_size==1 while from_size > 1). const float quad[2 * 2 * 3] = { - 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - 2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f, + 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f, }; image.Set(2, 2, quad); image.Resize(1, 2); @@ -197,6 +196,7 @@ TEST(ImageTest, PreconditionsAbortOnEmptyImage) { float patch[14 * 14 * 3]; EXPECT_DEATH_IF_SUPPORTED(empty.Resize(4, 4), "width_ > 0"); EXPECT_DEATH_IF_SUPPORTED(empty.ResizeBilinear(4, 4), "width_ > 0"); + EXPECT_DEATH_IF_SUPPORTED(empty.ResizeBicubic(4, 4), "width_ > 0"); EXPECT_DEATH_IF_SUPPORTED(empty.GetPatch(0, hwy::Divisor(14), patch), "width_ > 0"); @@ -234,5 +234,86 @@ TEST(ImageTest, ResizeBilinear1x1And2x2) { EXPECT_NEAR(image.data()[4 * 4 * 3 - 1], 1.0f, 1e-6f); } +TEST(ImageTest, ResizeBicubic) { + Image image; + ASSERT_TRUE(ReadPPMFromString(image, "P6\n1 1\n255\n\x20\x60\xa0")); + image.ResizeBicubic(5, 7); + EXPECT_EQ(image.width(), 5); + EXPECT_EQ(image.height(), 7); + for (int i = 0; i < 35; ++i) { + EXPECT_NEAR(image.data()[i * 3 + 0], Normalize(0x20), 1e-5f); + EXPECT_NEAR(image.data()[i * 3 + 1], Normalize(0x60), 1e-5f); + EXPECT_NEAR(image.data()[i * 3 + 2], Normalize(0xa0), 1e-5f); + } + + // Same-size ResizeBicubic is a no-op. + image.ResizeBicubic(5, 7); + EXPECT_EQ(image.width(), 5); + EXPECT_EQ(image.height(), 7); + EXPECT_NEAR(image.data()[0], Normalize(0x20), 1e-5f); + + // 1D horizontal-only and vertical-only resizes on a 4-pixel ramp, verifying + // exact Catmull-Rom bicubic interpolation and both single-pass branches. + const float ramp[4 * 3] = { + 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f, + }; + const float kExpectedRamp8[8] = { + -1.0f, -0.888078f, -0.526718f, -0.166667f, + 0.166667f, 0.526718f, 0.888078f, 1.0f, + }; + image.Set(4, 1, ramp); + image.ResizeBicubic(8, 1); + EXPECT_EQ(image.width(), 8); + EXPECT_EQ(image.height(), 1); + for (int i = 0; i < 8; ++i) { + EXPECT_NEAR(image.data()[i * 3], kExpectedRamp8[i], 1e-5f); + } + image.Set(1, 4, ramp); + image.ResizeBicubic(1, 8); + EXPECT_EQ(image.width(), 1); + EXPECT_EQ(image.height(), 8); + for (int i = 0; i < 8; ++i) { + EXPECT_NEAR(image.data()[i * 3], kExpectedRamp8[i], 1e-5f); + } + + // Asymmetric aspect-ratio swaps exercise both pass orders without allocating + // an O(kLong * kLong) intermediate buffer. Image::Set maps a constant input + // to -1.0f. + constexpr int kLong = 65535; + std::vector strip(kLong * 3, 1.0f); + image.Set(1, kLong, strip.data()); + image.ResizeBicubic(kLong, 1); + EXPECT_EQ(image.width(), kLong); + EXPECT_EQ(image.height(), 1); + EXPECT_NEAR(image.data()[0], -1.0f, 1e-4f); + image.ResizeBicubic(1, kLong); + EXPECT_EQ(image.width(), 1); + EXPECT_EQ(image.height(), kLong); + EXPECT_NEAR(image.data()[0], -1.0f, 1e-4f); + + // Downscaling a high-frequency checkerboard should antialias toward 0.0 + // rather than aliasing to +/-1.0, and stay clamped to [-1, 1]. + constexpr int kSrc = 64; + std::vector checker(kSrc * kSrc * 3); + for (int y = 0; y < kSrc; ++y) { + for (int x = 0; x < kSrc; ++x) { + const float v = ((x + y) & 1) ? 255.0f : 0.0f; + for (int c = 0; c < 3; ++c) { + checker[(y * kSrc + x) * 3 + c] = v; + } + } + } + image.Set(kSrc, kSrc, checker.data()); + image.ResizeBicubic(16, 16); + EXPECT_EQ(image.width(), 16); + EXPECT_EQ(image.height(), 16); + for (size_t i = 0; i < image.size(); ++i) { + EXPECT_GE(image.data()[i], -1.0f); + EXPECT_LE(image.data()[i], 1.0f); + } + // Interior pixel should average the alternating -1/+1 checkerboard near 0. + EXPECT_NEAR(image.data()[(8 * 16 + 8) * 3], 0.0f, 0.05f); +} + } // namespace } // namespace gcpp