mtmd : lfm2 tiles and layout match transformers (#30261)
* mtmd : lfm2 tiles stretch the image to the grid Lfm2VlImageProcessor.crop_image_to_patches resizes the image straight to (512 * rows, 512 * cols) before it cuts the tiles. llama.cpp kept the aspect ratio and added black bars for LFM2 (only d1-omni stretched), so the tiles of d1-3B and every LFM2-VL / LFM2.5-VL model differed from transformers unless the image had the aspect ratio of the grid. With PAD_NONE the tiles are the same as in transformers. * mtmd : lfm2 layout with Python rounding in double precision Lfm2VlImageProcessor.smart_resize rounds with Python's round (half to even) and computes beta and the tile grid aspect ratio in double. llama.cpp used std::round and float, so 31926 of the 16.7M sizes in [1,4096]^2 got another thumbnail size or grid than transformers (and d1-omni vision.py), e.g. 400x600 -> 416x608 instead of 384x608 and 720x1600 -> 2x5 tiles instead of 2x4. The lfm2 smart resize and grid choice now follow the Python code and match it on every size of that range. * fix --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
This commit is contained in:
1 parent
7e3eeee048
commit
4a45de4f2c
4 files changed
+81
-28
No files matched your search
@@ -91,6 +91,10 @@ MAKE_TEST(test_image_preprocessor_dyn_size) {
|
||||
}
|
||||
}
|
||||
|
||||
static std::string image_size_to_str(const clip_image_size & size) {
|
||||
return std::to_string(size.width) + "x" + std::to_string(size.height);
|
||||
}
|
||||
|
||||
MAKE_TEST(test_image_preprocessor_lfm2) {
|
||||
clip_hparams hparams;
|
||||
hparams.patch_size = 16;
|
||||
@@ -121,6 +125,32 @@ MAKE_TEST(test_image_preprocessor_lfm2) {
|
||||
std::string(actual ? "tiled" : "single"));
|
||||
}
|
||||
|
||||
// { image size, expected size from HF smart_resize }
|
||||
const std::vector<std::pair<clip_image_size, clip_image_size>> resize_cases = {
|
||||
{ { 400, 600 }, { 384, 608 } }, // 400 / 32 = 12.5 rounds half to even
|
||||
{ { 480, 336 }, { 480, 320 } },
|
||||
{ { 64, 169 }, { 160, 416 } }, // 169 * beta / 32 is exactly 13 in double
|
||||
{ { 1024, 768 }, { 576, 416 } },
|
||||
};
|
||||
|
||||
for (const auto & [size, expected] : resize_cases) {
|
||||
t.assert_equal("smart_resize for " + image_size_to_str(size), image_size_to_str(expected),
|
||||
image_size_to_str(mtmd_image_preprocessor_lfm2::smart_resize(hparams, size)));
|
||||
}
|
||||
|
||||
// { image size, expected grid from HF }
|
||||
const std::vector<std::pair<clip_image_size, clip_image_size>> grid_cases = {
|
||||
{ { 720, 1600 }, { 2, 4 } }, // 0.45 is an exact tie between 2/4 and 2/5 in double, not in float
|
||||
{ { 420, 1440 }, { 1, 3 } },
|
||||
{ { 255, 2160 }, { 1, 8 } },
|
||||
{ { 1024, 768 }, { 3, 2 } },
|
||||
};
|
||||
|
||||
for (const auto & [size, expected] : grid_cases) {
|
||||
t.assert_equal("grid for " + image_size_to_str(size), image_size_to_str(expected),
|
||||
image_size_to_str(mtmd_image_preprocessor_lfm2::get_grid_layout(hparams, size.height, size.width)));
|
||||
}
|
||||
|
||||
// do_image_splitting=false
|
||||
hparams.preproc_min_tiles = 1;
|
||||
hparams.preproc_max_tiles = 1;
|
||||
|
||||
Reference in new issue
Block a user