chrisc36 commited on
Commit
3cc57c7
1 Parent(s): 450356a

Upload preprocessing_molmo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. preprocessing_molmo.py +19 -3
preprocessing_molmo.py CHANGED
@@ -4,6 +4,10 @@ Processor class for Molmo.
4
 
5
  from typing import Optional
6
 
 
 
 
 
7
  try:
8
  from typing import Unpack
9
  except ImportError:
@@ -23,7 +27,7 @@ from transformers.tokenization_utils_base import TextInput
23
  from transformers.utils import logging
24
 
25
  from transformers import AutoTokenizer
26
- from .image_preprocessing_molmo import MolmoImagesKwargs, make_batched_images, MolmoImageProcessor
27
 
28
 
29
  logger = logging.get_logger(__name__)
@@ -129,8 +133,20 @@ class MolmoProcessor(ProcessorMixin):
129
  image_token_id = self.special_token_ids[IMAGE_PROMPT]
130
 
131
  if images is not None:
132
- images = make_batched_images(images)
133
- images = [np.array(image).astype(np.uint8) for image in images]
 
 
 
 
 
 
 
 
 
 
 
 
134
  # For now only support inserting images at the start
135
  image_idx = [-1]*len(images)
136
  else:
 
4
 
5
  from typing import Optional
6
 
7
+ import PIL
8
+ from PIL import ImageOps
9
+ from PIL.Image import Image
10
+
11
  try:
12
  from typing import Unpack
13
  except ImportError:
 
27
  from transformers.utils import logging
28
 
29
  from transformers import AutoTokenizer
30
+ from .image_preprocessing_molmo import MolmoImagesKwargs, MolmoImageProcessor
31
 
32
 
33
  logger = logging.get_logger(__name__)
 
133
  image_token_id = self.special_token_ids[IMAGE_PROMPT]
134
 
135
  if images is not None:
136
+ if not isinstance(images, (list, tuple)):
137
+ images = [images]
138
+ image_arrays = []
139
+ for image in images:
140
+ if isinstance(image, Image):
141
+ image = image.convert("RGB")
142
+ # Handle images with EXIF orientation tags, which PIL will ignore by default
143
+ # https://github.com/python-pillow/Pillow/issues/4703
144
+ img = ImageOps.exif_transpose(image)
145
+ image_arrays.append(np.array(image))
146
+ else:
147
+ assert len(image.shape) == 3 and image.shape[-1] == 3
148
+ image_arrays.append(image.astype(np.uint8))
149
+ images = image_arrays
150
  # For now only support inserting images at the start
151
  image_idx = [-1]*len(images)
152
  else: