|
| 1 | + |
| 2 | + |
| 3 | +import { |
| 4 | + ImageProcessor, |
| 5 | +} from "../../base/image_processors_utils.js"; |
| 6 | +import { cat, full, interpolate_4d } from "../../utils/tensor.js"; |
| 7 | + |
| 8 | +export class Idefics3ImageProcessor extends ImageProcessor { |
| 9 | + constructor(config) { |
| 10 | + super(config); |
| 11 | + |
| 12 | + this.do_image_splitting = config.do_image_splitting ?? true; |
| 13 | + this.max_image_size = config.max_image_size; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Calculate size to resize images to, to be multiples of `vision_encoder_max_size` while preserving the aspect ratio. |
| 18 | + * @param {import('../../utils/tensor.js').Tensor} pixel_values Tensor of the image to resize. |
| 19 | + * @param {number} vision_encoder_max_size Maximum size of the output image. If the image is larger than this size, |
| 20 | + * it will be split into patches of this size, and the original image will be concatenated with the patches, resized to max_size. |
| 21 | + */ |
| 22 | + get_resize_for_vision_encoder(pixel_values, vision_encoder_max_size) { |
| 23 | + let [height, width] = pixel_values.dims.slice(-2); |
| 24 | + |
| 25 | + const aspect_ratio = width / height; |
| 26 | + if (width >= height) { |
| 27 | + width = Math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size; |
| 28 | + height = Math.floor(width / aspect_ratio); |
| 29 | + height = Math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size; |
| 30 | + } else { |
| 31 | + height = Math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size; |
| 32 | + width = Math.floor(height * aspect_ratio); |
| 33 | + width = Math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size; |
| 34 | + } |
| 35 | + return { height, width }; |
| 36 | + } |
| 37 | + |
| 38 | + // /** @param {RawImage|RawImage[]|RawImage[][]} images */ |
| 39 | + async _call(images, { |
| 40 | + do_image_splitting = null, |
| 41 | + return_row_col_info = false, |
| 42 | + } = {}) { |
| 43 | + // TODO: support 2D RawImages |
| 44 | + if (!Array.isArray(images)) { |
| 45 | + images = [images]; |
| 46 | + } |
| 47 | + |
| 48 | + let images_list = await Promise.all(images.map(x => this.preprocess(x))); |
| 49 | + |
| 50 | + // Original sizes of images |
| 51 | + const original_sizes = images_list.map(x => x.original_size); |
| 52 | + |
| 53 | + // Reshaped sizes of images, before padding or cropping |
| 54 | + const reshaped_input_sizes = images_list.map(x => x.reshaped_input_size); |
| 55 | + |
| 56 | + // Convert images to 4D tensors for easier processing |
| 57 | + images_list.forEach(x => x.pixel_values.unsqueeze_(0)); |
| 58 | + |
| 59 | + let pixel_values; |
| 60 | + let images_list_rows = []; |
| 61 | + let images_list_cols = []; |
| 62 | + |
| 63 | + const { longest_edge } = this.max_image_size; |
| 64 | + |
| 65 | + if (do_image_splitting ?? this.do_image_splitting) { |
| 66 | + let image_rows = new Array(images_list.length); |
| 67 | + let image_cols = new Array(images_list.length); |
| 68 | + |
| 69 | + // We first resize both height and width of each image to the nearest max_image_size multiple, disregarding the aspect ratio |
| 70 | + images_list = await Promise.all(images_list.map(async (x, i) => { |
| 71 | + const new_size = this.get_resize_for_vision_encoder(x.pixel_values, longest_edge); |
| 72 | + |
| 73 | + const resized = await interpolate_4d(x.pixel_values, { |
| 74 | + size: [new_size.height, new_size.width], |
| 75 | + }); |
| 76 | + |
| 77 | + const { frames, num_splits_h, num_splits_w } = await this.split_image(resized, this.max_image_size); |
| 78 | + image_rows[i] = num_splits_h; |
| 79 | + image_cols[i] = num_splits_w; |
| 80 | + return cat(frames, 0); |
| 81 | + })); |
| 82 | + |
| 83 | + images_list_rows.push(image_rows); |
| 84 | + images_list_cols.push(image_cols); |
| 85 | + } else { |
| 86 | + /** @type {[number, number]} */ |
| 87 | + const size = [longest_edge, longest_edge]; |
| 88 | + images_list = await Promise.all( |
| 89 | + images_list.map(x => interpolate_4d(x.pixel_values, { size })) |
| 90 | + ); |
| 91 | + |
| 92 | + images_list_rows.push(new Array(images_list.length).fill(0)); |
| 93 | + images_list_cols.push(new Array(images_list.length).fill(0)); |
| 94 | + } |
| 95 | + |
| 96 | + // Stack pixel values |
| 97 | + // TODO: support 2D images inputs |
| 98 | + pixel_values = cat(images_list, 0); |
| 99 | + pixel_values.unsqueeze_(0); |
| 100 | + |
| 101 | + // TODO: Improve pixel_attention_mask |
| 102 | + const [b, n, c, h, w] = pixel_values.dims; |
| 103 | + const pixel_attention_mask = full([b, n, h, w], true); |
| 104 | + |
| 105 | + return { |
| 106 | + pixel_values, |
| 107 | + pixel_attention_mask, |
| 108 | + |
| 109 | + original_sizes, |
| 110 | + reshaped_input_sizes, |
| 111 | + ...( |
| 112 | + return_row_col_info |
| 113 | + ? { rows: images_list_rows, cols: images_list_cols } |
| 114 | + : {} |
| 115 | + ), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + async split_image(pixel_values, { longest_edge }) { |
| 120 | + const max_height = longest_edge; |
| 121 | + const max_width = longest_edge; |
| 122 | + |
| 123 | + const frames = []; |
| 124 | + |
| 125 | + const [height, width] = pixel_values.dims.slice(-2); |
| 126 | + |
| 127 | + let num_splits_h = 0, num_splits_w = 0; |
| 128 | + |
| 129 | + if (height > max_height || width > max_width) { |
| 130 | + // Calculate the number of splits |
| 131 | + num_splits_h = Math.ceil(height / max_height); |
| 132 | + num_splits_w = Math.ceil(width / max_width); |
| 133 | + |
| 134 | + // Calculate the optimal width and height for the sub-images |
| 135 | + const optimal_height = Math.ceil(height / num_splits_h); |
| 136 | + const optimal_width = Math.ceil(width / num_splits_w); |
| 137 | + |
| 138 | + // Iterate through each row and column |
| 139 | + for (let r = 0; r < num_splits_h; r++) { |
| 140 | + for (let c = 0; c < num_splits_w; c++) { |
| 141 | + // Calculate the starting point of the crop |
| 142 | + const start_x = c * optimal_width; |
| 143 | + const start_y = r * optimal_height; |
| 144 | + |
| 145 | + // Calculate the ending point of the crop |
| 146 | + const end_x = Math.min(start_x + optimal_width, width); |
| 147 | + const end_y = Math.min(start_y + optimal_height, height); |
| 148 | + |
| 149 | + // Crop the image |
| 150 | + frames.push(pixel_values.slice(null, null, [start_y, end_y], [start_x, end_x])); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Resize the global image to match max dimensions for memory efficiency |
| 155 | + const global_image_height = max_height; |
| 156 | + const global_image_width = max_width; |
| 157 | + |
| 158 | + if (height !== global_image_height || width !== global_image_width) { |
| 159 | + pixel_values = await interpolate_4d(pixel_values, { |
| 160 | + size: [global_image_height, global_image_width], |
| 161 | + }) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + frames.push(pixel_values); |
| 166 | + |
| 167 | + return { frames, num_splits_h, num_splits_w }; |
| 168 | + } |
| 169 | +} |
0 commit comments