okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
2.9 KiB58 linesraw
1// tools/vision/own/image_prep.h - a picture to the tower's input: decode, size, resize, normalise (own-stack phase 3).
2//
3// Follows what llama.cpp's mtmd does for a Qwen-VL tower, step for step, so the pixels that reach the tower are the same ones (the resize
4// is a port of mtmd's Pillow-compatible fixed-point bicubic; its maths and its rounding are MIT, from llama.cpp tools/mtmd/mtmd-image.cpp):
5//   1. stb_image decodes to 8-bit RGB (JPEG, PNG, BMP, GIF first frame, ...);
6//   2. the target size is "smart_resize": both sides rounded to a multiple of patch * merge, then scaled down to the largest picture of at
7//      most max_pixels, or up to at least min_pixels (the limits are token counts times patch^2 * merge^2);
8//   3. the picture is resized with the aspect ratio kept (bicubic, a = -0.5; ceil rounding of the resized side) into the target and centred on
9//      a black canvas (mtmd's PAD_CEIL; nothing is padded when the picture already has the target's shape);
10//   4. (v / 255 - mean) / std per channel, planar CHW float32.
11#pragma once
12
13#include "vit_geometry.h"
14
15#include <cstddef>
16#include <cstdint>
17#include <string>
18#include <vector>
19
20namespace strata_vit {
21
22struct ImageU8 {
23    int w = 0, h = 0;
24    std::vector<uint8_t> rgb;   // h rows of w * 3 bytes
25};
26
27struct PreparedImage {
28    int w = 0, h = 0;               // the size the tower sees (multiples of patch * merge)
29    std::vector<float> chw;         // 3 * h * w, normalised, channel planes
30};
31
32struct Size2 {
33    int w = 0, h = 0;
34    bool operator==(const Size2& o) const { return w == o.w && h == o.h; }
35};
36
37// stb_image to RGB8.  False and `err` when the bytes are not a picture stb_image reads.
38bool image_decode(const uint8_t* data, size_t n, ImageU8& out, std::string& err);
39bool image_load_file(const std::string& path, ImageU8& out, std::string& err);
40
41// mtmd's calc_size_preserved_ratio (HF smart_resize): sides rounded to `align`, then bounded by max_pixels / min_pixels (0 = no bound)
42Size2 smart_resize(int w, int h, int align, int min_pixels, int max_pixels);
43
44// Pillow-compatible separable bicubic (a = -0.5), 22-bit fixed point on uint8, as mtmd's resize_pillow
45void resize_bicubic(const ImageU8& src, ImageU8& dst, int target_w, int target_h);
46
47// mtmd's img_tool::resize with PAD_CEIL and a black canvas: aspect kept, centred, padded
48void resize_pad_ceil(const ImageU8& src, ImageU8& dst, Size2 target);
49
50// The whole step 2-4 for a geometry.  min_tokens / max_tokens > 0 replace the geometry's limits (the helper's --min-tokens / --max-tokens).
51// The resized picture (before the normalisation) is returned in `resized` when it is not null (for tests).
52bool prepare_image(const ImageU8& src, const VitGeometry& g, int min_tokens, int max_tokens, PreparedImage& out, std::string& err,
53                   ImageU8* resized = nullptr);
54
55// the size prepare_image would give (no pixels touched)
56Size2 prepared_size(int w, int h, const VitGeometry& g, int min_tokens, int max_tokens);
57
58}  // namespace strata_vit