Feature Extraction
Transformers
Safetensors
English
prismatic
remyx
custom_code
salma-remyx commited on
Commit
ccb7017
1 Parent(s): 723ad90

Upload processor

Browse files
Files changed (2) hide show
  1. preprocessor_config.json +111 -0
  2. processing_prismatic.py +252 -0
preprocessor_config.json ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "processing_prismatic.PrismaticImageProcessor"
4
+ },
5
+ "image_processor_type": "PrismaticImageProcessor",
6
+ "image_resize_strategy": "letterbox",
7
+ "input_sizes": [
8
+ [
9
+ 3,
10
+ 224,
11
+ 224
12
+ ],
13
+ [
14
+ 3,
15
+ 224,
16
+ 224
17
+ ]
18
+ ],
19
+ "interpolations": [
20
+ "bicubic",
21
+ "bicubic"
22
+ ],
23
+ "means": [
24
+ [
25
+ 0.485,
26
+ 0.456,
27
+ 0.406
28
+ ],
29
+ [
30
+ 0.5,
31
+ 0.5,
32
+ 0.5
33
+ ]
34
+ ],
35
+ "processor_class": "PrismaticProcessor",
36
+ "stds": [
37
+ [
38
+ 0.229,
39
+ 0.224,
40
+ 0.225
41
+ ],
42
+ [
43
+ 0.5,
44
+ 0.5,
45
+ 0.5
46
+ ]
47
+ ],
48
+ "tvf_crop_params": [
49
+ {
50
+ "output_size": [
51
+ 224,
52
+ 224
53
+ ]
54
+ },
55
+ {
56
+ "output_size": [
57
+ 224,
58
+ 224
59
+ ]
60
+ }
61
+ ],
62
+ "tvf_do_letterbox": true,
63
+ "tvf_letterbox_fill": [
64
+ 127,
65
+ 127,
66
+ 127
67
+ ],
68
+ "tvf_normalize_params": [
69
+ {
70
+ "inplace": false,
71
+ "mean": [
72
+ 0.484375,
73
+ 0.455078125,
74
+ 0.40625
75
+ ],
76
+ "std": [
77
+ 0.228515625,
78
+ 0.2236328125,
79
+ 0.224609375
80
+ ]
81
+ },
82
+ {
83
+ "inplace": false,
84
+ "mean": [
85
+ 0.5,
86
+ 0.5,
87
+ 0.5
88
+ ],
89
+ "std": [
90
+ 0.5,
91
+ 0.5,
92
+ 0.5
93
+ ]
94
+ }
95
+ ],
96
+ "tvf_resize_params": [
97
+ {
98
+ "antialias": true,
99
+ "interpolation": 3,
100
+ "max_size": null,
101
+ "size": 224
102
+ },
103
+ {
104
+ "antialias": true,
105
+ "interpolation": 3,
106
+ "max_size": null,
107
+ "size": 224
108
+ }
109
+ ],
110
+ "use_fused_vision_backbone": true
111
+ }
processing_prismatic.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ processing_prismatic.py
3
+
4
+ HuggingFace-style preprocessor definitions for Prismatic VLMs, inheriting from `ProcessorMixin`. Default configuration
5
+ specifies `siglip-224px+7b`.
6
+ """
7
+
8
+ from typing import Any, ClassVar, List, Optional, Tuple, Union
9
+
10
+ import timm.data
11
+ import torch
12
+ import torchvision.transforms.functional as TVF
13
+ from PIL import Image
14
+ from torchvision.transforms import CenterCrop, Compose, Normalize, Resize, ToTensor
15
+ from transformers import PreTrainedTokenizerBase
16
+ from transformers.image_processing_utils import BatchFeature, ImageProcessingMixin
17
+ from transformers.processing_utils import ProcessorMixin
18
+ from transformers.tokenization_utils import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
19
+ from transformers.utils import TensorType
20
+
21
+
22
+ # === Image Processing ===
23
+ def letterbox_pad_transform(image: Image.Image, padding_fill_value: Tuple[int, int, int]) -> Image.Image:
24
+ """Given a PIL.Image, pad to square by adding a symmetric border around the height/width."""
25
+ (w, h), max_wh = image.size, max(image.size)
26
+ horizontal_pad, vertical_pad = int((max_wh - w) / 2), int((max_wh - h) / 2)
27
+ padding = (horizontal_pad, vertical_pad, horizontal_pad, vertical_pad)
28
+
29
+ return TVF.pad(image, padding, fill=padding_fill_value, padding_mode="constant")
30
+
31
+
32
+ class PrismaticImageProcessor(ImageProcessingMixin):
33
+ model_input_names: ClassVar[List[str]] = ["pixel_values"]
34
+
35
+ def __init__(
36
+ self,
37
+ use_fused_vision_backbone: bool = False,
38
+ image_resize_strategy: str = "letterbox",
39
+ input_sizes: Optional[List[Tuple[int, int, int]]] = None,
40
+ interpolations: Optional[List[str]] = None,
41
+ means: Optional[List[Tuple[float, float, float]]] = None,
42
+ stds: Optional[List[Tuple[float, float, float]]] = None,
43
+ **kwargs: str,
44
+ ) -> None:
45
+ """
46
+ Initialize a PrismaticImageProcessor as a wrapper around a torchvision transform; this transform will be
47
+ created by TIMM, and edited to follow our custom `image_resize_strategy` logic.
48
+ @param use_fused_vision_backbone: Boolean indicating single or fused (dual) vision backbone
49
+ @param image_resize_strategy: Prismatic image resize strategy in < resize-naive | resize-crop | letterbox >
50
+ @param input_size: [TIMM :: `data_cfg`] Input image size as tuple (channels, width, height)
51
+ @param interpolation: [TIMM :: `data_cfg`] Interpolation as string (default: "bicubic")
52
+ @param mean: [TIMM :: `data_cfg`] Normalization mean as float tuple (or two-tuple if `fused_backbone`)
53
+ @param std: [TIMM :: `data_cfg`] Normalization std as float tuple (or two-tuple if `fused_backbone`)
54
+ """
55
+ self.use_fused_vision_backbone = use_fused_vision_backbone
56
+ self.image_resize_strategy = image_resize_strategy
57
+
58
+ # Handle `None` default values
59
+ input_sizes = [(3, 224, 224)] if input_sizes is None else input_sizes
60
+ means = [(0.5, 0.5, 0.5)] if means is None else means
61
+ stds = [(0.5, 0.5, 0.5)] if stds is None else stds
62
+
63
+ # TIMM `data_cfg` Parameters
64
+ self.input_sizes, self.interpolations, self.means, self.stds = input_sizes, interpolations, means, stds
65
+
66
+ # Grab torchvision transforms via TIMM =>> need to parse for specific "functional" transform values!
67
+ self.tvf_resize_params, self.tvf_crop_params, self.tvf_normalize_params = [], [], []
68
+ self.tvf_do_letterbox, self.tvf_letterbox_fill = False, None
69
+
70
+ for idx in range(len(input_sizes)):
71
+ transform = timm.data.create_transform(
72
+ input_size=self.input_sizes[idx],
73
+ interpolation=self.interpolations[idx],
74
+ mean=self.means[idx],
75
+ std=self.stds[idx],
76
+ crop_pct=1.0, # Set to 1.0 to ignore cropping (initial Resize sets `input_size`)
77
+ crop_mode="center", # Default crop mode -- no-op when `crop_pct == 1.0`
78
+ is_training=False, # No image augmentations when loading the transform!
79
+ )
80
+
81
+ # [Validation] Ensure appropriate transform structure, expected sizes
82
+ if not (
83
+ isinstance(transform, Compose)
84
+ and (len(transform.transforms) == 4)
85
+ and isinstance(transform.transforms[0], Resize)
86
+ and isinstance(transform.transforms[1], CenterCrop)
87
+ and isinstance(transform.transforms[2], ToTensor)
88
+ and isinstance(transform.transforms[3], Normalize)
89
+ and (transform.transforms[0].size == self.input_sizes[idx][-1])
90
+ and (transform.transforms[1].size == self.input_sizes[idx][-2:])
91
+ ):
92
+ raise ValueError(f"Unexpected TIMM image transformation structure/sizes: `{transform}`")
93
+
94
+ # HF Image Processors *must* be JSON-serializable; as such, cannot have torchvision. as an attribute.
95
+ # => Instead, we're going to parse the transform and call "torchvision.transforms.functional" (`tvf`)
96
+ resize_t, crop_t, norm_t = transform.transforms[0], transform.transforms[1], transform.transforms[3]
97
+ self.tvf_resize_params.append(
98
+ {
99
+ "size": resize_t.size,
100
+ "interpolation": TVF.pil_modes_mapping[resize_t.interpolation],
101
+ "max_size": None,
102
+ "antialias": True,
103
+ }
104
+ )
105
+ self.tvf_crop_params.append({"output_size": crop_t.size})
106
+ self.tvf_normalize_params.append(
107
+ {
108
+ "mean": norm_t.mean.float().numpy().tolist(),
109
+ "std": norm_t.std.float().numpy().tolist(),
110
+ "inplace": False,
111
+ }
112
+ )
113
+ self.tvf_do_letterbox, self.tvf_letterbox_fill = False, None
114
+
115
+ # Handle Prismatic `image_resize_strategy`
116
+ if self.image_resize_strategy == "resize-naive":
117
+ self.tvf_resize_params[idx]["size"] = (resize_t.size, resize_t.size)
118
+ elif self.image_resize_strategy == "letterbox":
119
+ self.tvf_do_letterbox, self.tvf_letterbox_fill = True, tuple([int(x * 255) for x in self.means[idx]])
120
+ elif self.image_resize_strategy == "resize-crop":
121
+ pass
122
+ else:
123
+ raise ValueError(f"Image resize strategy `{self.image_resize_strategy}` is not supported!")
124
+
125
+ # Dispatch **kwargs to super()
126
+ super().__init__(**kwargs)
127
+
128
+ def apply_transform(self, img: Image.Image) -> torch.Tensor:
129
+ """Apply `functional` variant of TIMM's Transform = Compose([Resize -> CenterCrop -> ToTensor -> Normalize])"""
130
+ if self.tvf_do_letterbox:
131
+ img = letterbox_pad_transform(img, self.tvf_letterbox_fill)
132
+
133
+ # [Contract] Fused Backbones expect "channel-stacked" inputs; we'll unpack on the model side!
134
+ imgs_t = []
135
+ for idx in range(len(self.input_sizes)):
136
+ img_idx = TVF.resize(img, **self.tvf_resize_params[idx])
137
+ img_idx = TVF.center_crop(img_idx, **self.tvf_crop_params[idx])
138
+ img_idx_t = TVF.to_tensor(img_idx)
139
+ img_idx_t = TVF.normalize(img_idx_t, **self.tvf_normalize_params[idx])
140
+ imgs_t.append(img_idx_t)
141
+
142
+ # [Contract] `imgs_t` is a list of Tensors of shape [3, input_size, input_size]; stack along dim = 0
143
+ img_t = torch.vstack(imgs_t)
144
+
145
+ return img_t
146
+
147
+ def preprocess(
148
+ self,
149
+ images: Union[Image.Image, List[Image.Image]],
150
+ return_tensors: Optional[Union[str, TensorType]] = None,
151
+ **_: str,
152
+ ) -> BatchFeature:
153
+ """
154
+ Preprocess an image (or batch of images); note that unlike the `transformers :: BaseImageProcessor` we
155
+ explicitly only handle PIL.Image.Image instances for simplicity.
156
+ @param images: A (batch of) PIL.Image.Image instance(s) to preprocess.
157
+ @param return_tensors: BatchFeature default Tensor format (e.g., "pt" for torch); if None, returns np.ndarray
158
+ @return: Instance of `transformers :: BatchFeature` with a single key "pixel_values"
159
+ """
160
+ if not isinstance(images, list):
161
+ images = [images]
162
+
163
+ # Apply `self.img_transform` to each image (will return list of torch.Tensors); stack into "batched" Tensor
164
+ pixel_values = torch.stack([self.apply_transform(img.convert("RGB")) for img in images])
165
+
166
+ # Return BatchFeature =>> note that for compatibility, constructor expects Dict[str, np.ndarray], so we convert
167
+ return BatchFeature(data={"pixel_values": pixel_values.float().numpy()}, tensor_type=return_tensors)
168
+
169
+ def __call__(self, images: Union[Image.Image, List[Image.Image]], **kwargs) -> BatchFeature:
170
+ return self.preprocess(images, **kwargs)
171
+
172
+
173
+ # === PrismaticProcessor =>> Wraps both ImageProcessor and Tokenizer ===
174
+ # =>> https://github.com/huggingface/transformers/blob/main/src/transformers/models/llava/processing_llava.py
175
+ class PrismaticProcessor(ProcessorMixin):
176
+ attributes: ClassVar[List[str]] = ["image_processor", "tokenizer"]
177
+ image_processor_class: str = "AutoImageProcessor"
178
+ tokenizer_class: str = "AutoTokenizer"
179
+
180
+ def __init__(
181
+ self,
182
+ image_processor: Optional[ImageProcessingMixin] = None,
183
+ tokenizer: Optional[PreTrainedTokenizerBase] = None,
184
+ ) -> None:
185
+ super().__init__(image_processor, tokenizer)
186
+
187
+ def __call__(
188
+ self,
189
+ text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]],
190
+ images: Union[Image.Image, List[Image.Image]],
191
+ padding: Union[bool, str, PaddingStrategy] = False,
192
+ truncation: Optional[Union[bool, str, TruncationStrategy]] = None,
193
+ max_length: Optional[int] = None,
194
+ return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH,
195
+ ) -> BatchFeature:
196
+ """
197
+ Preprocess a given (batch) of text/images for a Prismatic VLM; forwards text to the underlying LLM's tokenizer,
198
+ forwards images to PrismaticImageProcessor.
199
+ @param text: The (batch) of text to encode; must be a string or list of strings.
200
+ @param images: A (batch of) PIL.Image.Image instance(s) to preprocess.
201
+ @param padding: Sequence padding strategy (if multiple specified) in < True = "longest" | "max_length" | False >
202
+ @param truncation: Truncation strategy for the output sequences; requires `max_length` to be specified
203
+ @param max_length: Maximum length (in tokens) to truncate
204
+ @param return_tensors: Type of return tensors (usually "pt" or TensorType.PYTORCH)
205
+ @return: BatchFeature with keys for `input_ids`, `attention_mask` and `pixel_values`.
206
+ """
207
+ pixel_values = self.image_processor(images, return_tensors=return_tensors)["pixel_values"]
208
+ text_inputs = self.tokenizer(
209
+ text, return_tensors=return_tensors, padding=padding, truncation=truncation, max_length=max_length
210
+ )
211
+
212
+ # [Validate] Need same number of images and text inputs!
213
+ if pixel_values.shape[0] != text_inputs.input_ids.shape[0]:
214
+ raise ValueError("Batch is malformed; expected same number of images and text inputs!")
215
+
216
+ return BatchFeature(data={**text_inputs, "pixel_values": pixel_values})
217
+
218
+ # === Tokenizer Dispatch Utilities =>> check `PreTrainedTokenizerBase` for documentation ===
219
+ def batch_decode(
220
+ self,
221
+ sequences: Union[List[int], List[List[int]], torch.Tensor, Any], # `Any` = np.ndarray | tf.Tensor
222
+ skip_special_tokens: bool = False,
223
+ clean_up_tokenization_spaces: Optional[bool] = None,
224
+ **kwargs: str,
225
+ ) -> List[str]:
226
+ return self.tokenizer.batch_decode(
227
+ sequences=sequences,
228
+ skip_special_tokens=skip_special_tokens,
229
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
230
+ **kwargs,
231
+ )
232
+
233
+ def decode(
234
+ self,
235
+ token_ids: Union[int, List[int], torch.Tensor, Any], # `Any` = np.ndarray | tf.Tensor
236
+ skip_special_tokens: bool = False,
237
+ clean_up_tokenization_spaces: Optional[bool] = None,
238
+ **kwargs: str,
239
+ ) -> str:
240
+ return self.tokenizer.decode(
241
+ token_ids=token_ids,
242
+ skip_special_tokens=skip_special_tokens,
243
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
244
+ **kwargs,
245
+ )
246
+
247
+ @property
248
+ def model_input_names(self) -> List[str]:
249
+ tokenizer_input_names = self.tokenizer.model_input_names
250
+ image_processor_input_names = self.image_processor.model_input_names
251
+
252
+ return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))