| #pragma once |
|
|
| #include "ggml.h" |
| #include "clip-model.h" |
|
|
| #include <vector> |
| #include <string> |
|
|
| #define MTMD_INTERNAL_HEADER |
|
|
| struct mtmd_image_preproc_out { |
| std::vector<clip_image_f32> entries; |
| |
|
|
| clip_image_f32 overview; |
| int grid_x = 0; |
| int grid_y = 0; |
|
|
| void append(const clip_hparams & hparams, const clip_image_u8 & img, bool normalized = true); |
| void append(const clip_hparams & hparams, const std::vector<clip_image_u8> & imgs, bool normalized = true); |
| void append(const clip_hparams & hparams, clip_image_f32 & img, bool normalized = true); |
|
|
| void append_overview(const clip_hparams & hparams, const clip_image_u8 & img, bool normalized = true); |
| bool has_overview() const { |
| return overview.nx() > 0 || overview.ny() > 0; |
| } |
| }; |
|
|
| |
| struct mtmd_image_preprocessor { |
| const clip_hparams & hparams; |
|
|
| mtmd_image_preprocessor(const clip_ctx * ctx): hparams(*clip_get_hparams(ctx)) {} |
|
|
| virtual ~mtmd_image_preprocessor() = default; |
| virtual mtmd_image_preproc_out preprocess(const clip_image_u8 & img) = 0; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| struct mtmd_image_preprocessor_llava_uhd : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_llava_uhd(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
|
|
| struct slice_coordinates { |
| int x; |
| int y; |
| clip_image_size size; |
| }; |
|
|
| struct slice_instructions { |
| clip_image_size overview_size; |
| clip_image_size refined_size; |
| clip_image_size grid_size; |
| std::vector<slice_coordinates> slices; |
| }; |
|
|
| virtual slice_instructions get_slice_instructions(const clip_image_size & original_size); |
|
|
| struct slice_output { |
| clip_image_u8 overview; |
| std::vector<clip_image_u8> slices; |
| }; |
| slice_output slice_image(const clip_image_u8 & img, const slice_instructions & inst); |
|
|
| protected: |
| clip_image_size get_best_resize(const clip_image_size & original_size, int scale_resolution, int patch_size, bool allow_upscale = false); |
|
|
| private: |
| clip_image_size resize_maintain_aspect_ratio(const clip_image_size & orig, const clip_image_size & target_max); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| clip_image_size select_best_resolution(const clip_image_size & original_size, const std::vector<clip_image_size> & possible_resolutions); |
| int ensure_divide(int length, int patch_size); |
| clip_image_size get_refine_size(const clip_image_size & original_size, const clip_image_size & grid, int scale_resolution, int patch_size, bool allow_upscale = false); |
| clip_image_size get_best_grid(const int max_slice_nums, const int multiple, const float log_ratio); |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_fixed_size : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_fixed_size(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| |
| |
| struct mtmd_image_preprocessor_dyn_size : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_dyn_size(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_longest_edge : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_longest_edge(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_minicpmv : mtmd_image_preprocessor_llava_uhd { |
| using mtmd_image_preprocessor_llava_uhd::mtmd_image_preprocessor_llava_uhd; |
| slice_instructions get_slice_instructions(const clip_image_size & original_size) override; |
| }; |
|
|
| |
| |
| struct mtmd_image_preprocessor_lfm2 : mtmd_image_preprocessor_llava_uhd { |
| |
| static constexpr int min_tiles = 2; |
| static constexpr int max_tiles = 10; |
| static constexpr float max_pixels_tolerance = 2.0f; |
| static constexpr int tile_size = 512; |
|
|
| using mtmd_image_preprocessor_llava_uhd::mtmd_image_preprocessor_llava_uhd; |
| slice_instructions get_slice_instructions(const clip_image_size & original_size) override; |
|
|
| private: |
| clip_image_size find_closest_aspect_ratio( |
| float aspect_ratio, |
| const std::vector<clip_image_size> & target_ratios, |
| int width, int height); |
| std::vector<clip_image_size> get_target_ratios(); |
| clip_image_size get_grid_layout(int height, int width); |
| }; |
|
|
| struct mtmd_image_preprocessor_idefics3 : mtmd_image_preprocessor_llava_uhd { |
| mtmd_image_preprocessor_idefics3(const clip_ctx * ctx) : mtmd_image_preprocessor_llava_uhd(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| struct mtmd_image_preprocessor_internvl : mtmd_image_preprocessor_llava_uhd { |
| mtmd_image_preprocessor_internvl(const clip_ctx * ctx) : mtmd_image_preprocessor_llava_uhd(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_deepseekocr : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_deepseekocr(const clip_ctx * ctx) |
| : mtmd_image_preprocessor(ctx), |
| fuse_row(clip_get_projector_type(ctx) == PROJECTOR_TYPE_DEEPSEEKOCR), |
| base_size(hparams.image_size), |
| tile_size(hparams.preproc_tile_size), |
| min_tiles(hparams.preproc_min_tiles), |
| max_tiles(hparams.preproc_max_tiles) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
|
|
| private: |
| bool fuse_row; |
| int base_size; |
| int tile_size; |
| int min_tiles; |
| int max_tiles; |
|
|
| std::vector<clip_image_size> get_target_ratios() const; |
| clip_image_size find_closest_aspect_ratio( |
| float aspect_ratio, |
| const std::vector<clip_image_size> & target_ratios, |
| int width, int height) const; |
| }; |
|
|
| |
| |
| struct mtmd_image_preprocessor_step3vl : mtmd_image_preprocessor_llava_uhd { |
| mtmd_image_preprocessor_step3vl(const clip_ctx * ctx) : mtmd_image_preprocessor_llava_uhd(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| static slice_instructions build_slice_instructions(const clip_hparams & params, const clip_image_size & prepared_size); |
|
|
| private: |
| static constexpr int default_image_longest_edge = 3024; |
| static constexpr int default_image_crop_size = 504; |
| static constexpr float small_aspect_ratio_limit = 1.5f; |
| static constexpr float wide_aspect_ratio_limit = 4.0f; |
| static constexpr float crop_rounding_threshold = 0.2f; |
|
|
| void img_u8_resize_bilinear_to_f32( |
| const clip_image_u8 & src, |
| clip_image_f32 & dst, |
| int target_width, |
| int target_height, |
| const float mean[3], |
| const float std[3]); |
| static int get_image_longest_edge(const clip_hparams & params); |
| static int determine_window_size(const clip_hparams & params, int longer, int shorter); |
| static int calc_crop_extent(int length, int window_size); |
| static std::vector<int> calc_grid(int length, int window_size); |
| static clip_image_u8 prepare_image(const clip_image_u8 & img, const clip_hparams & params); |
| static clip_image_u8 crop_with_black_padding(const clip_image_u8 & image, int x, int y, int w, int h); |
| }; |
|
|
| struct mtmd_image_preprocessor_youtuvl : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_youtuvl(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_granite : mtmd_image_preprocessor_llava_uhd { |
| mtmd_image_preprocessor_granite(const clip_ctx * ctx) : mtmd_image_preprocessor_llava_uhd(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|
| |
| struct mtmd_image_preprocessor_muse_glimmer : mtmd_image_preprocessor { |
| mtmd_image_preprocessor_muse_glimmer(const clip_ctx * ctx) : mtmd_image_preprocessor(ctx) {} |
| mtmd_image_preproc_out preprocess(const clip_image_u8 & img) override; |
| }; |
|
|