Upload processor
Browse files- .gitattributes +1 -0
- added_tokens.json +3 -0
- preprocessor_config.json +30 -0
- processing_divedoc.py +372 -0
- processor_config.json +6 -0
- special_tokens_map.json +39 -0
- tokenizer.json +3 -0
- tokenizer.model +3 -0
- tokenizer_config.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
added_tokens.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"<image>": 257152
|
| 3 |
+
}
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoProcessor": "processing_divedoc.DIVEdocProcessor"
|
| 4 |
+
},
|
| 5 |
+
"do_align_long_axis": false,
|
| 6 |
+
"do_normalize": true,
|
| 7 |
+
"do_pad": true,
|
| 8 |
+
"do_rescale": true,
|
| 9 |
+
"do_resize": true,
|
| 10 |
+
"do_thumbnail": true,
|
| 11 |
+
"image_mean": [
|
| 12 |
+
0.5,
|
| 13 |
+
0.5,
|
| 14 |
+
0.5
|
| 15 |
+
],
|
| 16 |
+
"image_processor_type": "DonutImageProcessor",
|
| 17 |
+
"image_seq_length": 4096,
|
| 18 |
+
"image_std": [
|
| 19 |
+
0.5,
|
| 20 |
+
0.5,
|
| 21 |
+
0.5
|
| 22 |
+
],
|
| 23 |
+
"processor_class": "DIVEdocProcessor",
|
| 24 |
+
"resample": 2,
|
| 25 |
+
"rescale_factor": 0.00392156862745098,
|
| 26 |
+
"size": {
|
| 27 |
+
"height": 2048,
|
| 28 |
+
"width": 2048
|
| 29 |
+
}
|
| 30 |
+
}
|
processing_divedoc.py
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional, Union
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from transformers import AutoTokenizer, DonutImageProcessor
|
| 6 |
+
from transformers.feature_extraction_utils import BatchFeature
|
| 7 |
+
from transformers.image_utils import ImageInput, is_valid_image
|
| 8 |
+
from transformers.processing_utils import (
|
| 9 |
+
MultiModalData,
|
| 10 |
+
ProcessingKwargs,
|
| 11 |
+
ProcessorMixin,
|
| 12 |
+
TextKwargs,
|
| 13 |
+
Unpack,
|
| 14 |
+
)
|
| 15 |
+
from transformers.tokenization_utils_base import (
|
| 16 |
+
AddedToken,
|
| 17 |
+
PreTokenizedInput,
|
| 18 |
+
TextInput,
|
| 19 |
+
)
|
| 20 |
+
from transformers.utils import logging
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
logger = logging.get_logger(__name__)
|
| 24 |
+
|
| 25 |
+
IMAGE_TOKEN = "<image>"
|
| 26 |
+
EXTRA_TOKENS = [f"<loc{i:0>4}>" for i in range(1024)] + [
|
| 27 |
+
f"<seg{i:0>3}>" for i in range(128)
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Copied from https://github.com/huggingface/transformers/blob/main/src/transformers/processing_utils.py
|
| 32 |
+
class PaliGemmaTextKwargs(TextKwargs):
|
| 33 |
+
suffix: Optional[
|
| 34 |
+
Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class PaliGemmaProcessorKwargs(ProcessingKwargs, total=False):
|
| 39 |
+
text_kwargs: PaliGemmaTextKwargs
|
| 40 |
+
_defaults = {
|
| 41 |
+
"text_kwargs": {
|
| 42 |
+
"padding": False,
|
| 43 |
+
"return_mm_token_type_ids": False,
|
| 44 |
+
},
|
| 45 |
+
"images_kwargs": {
|
| 46 |
+
"data_format": "channels_first",
|
| 47 |
+
},
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Copied from transformers.models.idefics2.processing_idefics2.is_url
|
| 52 |
+
def is_url(val) -> bool:
|
| 53 |
+
return isinstance(val, str) and val.startswith("http")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Copied from transformers.models.idefics2.processing_idefics2.is_image_or_image_url
|
| 57 |
+
def is_image_or_image_url(elem):
|
| 58 |
+
return is_url(elem) or is_valid_image(elem)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _is_str_or_image(elem):
|
| 62 |
+
return isinstance(elem, (str)) or is_image_or_image_url(elem)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def build_string_from_input(prompt, bos_token, image_seq_len, image_token, num_images):
|
| 66 |
+
"""
|
| 67 |
+
Builds a string from the input prompt and image tokens.
|
| 68 |
+
For example, for the call:
|
| 69 |
+
build_string_from_input(
|
| 70 |
+
prompt="Prefix str"
|
| 71 |
+
bos_token="<s>",
|
| 72 |
+
image_seq_len=3,
|
| 73 |
+
image_token="<im>",
|
| 74 |
+
)
|
| 75 |
+
The output will be:
|
| 76 |
+
"<im><im><im><s>Initial str"
|
| 77 |
+
Args:
|
| 78 |
+
prompt (`list[Union[str, ImageInput]]`): The input prompt.
|
| 79 |
+
bos_token (`str`): The beginning of sentence token.
|
| 80 |
+
image_seq_len (`int`): The length of the image sequence.
|
| 81 |
+
image_token (`str`): The image token.
|
| 82 |
+
num_images (`int`): Number of images in the prompt.
|
| 83 |
+
"""
|
| 84 |
+
return f"{image_token * image_seq_len * num_images}{bos_token}{prompt}\n"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Copied and adapted from https://github.com/huggingface/transformers/blob/main/src/transformers/models/paligemma/processing_paligemma.py
|
| 88 |
+
class DIVEdocProcessor(ProcessorMixin):
|
| 89 |
+
attributes = ["image_processor", "tokenizer"]
|
| 90 |
+
image_processor_class = "DonutImageProcessor" # change from the original SigLipImageProcessor to DonutImageProcessor
|
| 91 |
+
tokenizer_class = "GemmaTokenizerFast"
|
| 92 |
+
r"""
|
| 93 |
+
Constructs a PaliGemma processor which wraps a PaliGemma image processor and a PaliGemma tokenizer into a single processor.
|
| 94 |
+
|
| 95 |
+
[`PaliGemmaProcessor`] offers all the functionalities of [`SiglipImageProcessor`] and [`GemmaTokenizerFast`]. See the
|
| 96 |
+
[`~PaliGemmaProcessor.__call__`] and [`~PaliGemmaProcessor.decode`] for more information.
|
| 97 |
+
|
| 98 |
+
Args:
|
| 99 |
+
image_processor ([`SiglipImageProcessor`], *optional*):
|
| 100 |
+
The image processor is a required input.
|
| 101 |
+
tokenizer ([`GemmaTokenizerFast`], *optional*):
|
| 102 |
+
The tokenizer is a required input.
|
| 103 |
+
chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
|
| 104 |
+
in a chat into a tokenizable string.
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
def __init__(
|
| 108 |
+
self,
|
| 109 |
+
image_processor=None,
|
| 110 |
+
tokenizer=None,
|
| 111 |
+
chat_template=None,
|
| 112 |
+
**kwargs,
|
| 113 |
+
):
|
| 114 |
+
if not hasattr(image_processor, "image_seq_length"):
|
| 115 |
+
raise ValueError(
|
| 116 |
+
"Image processor is missing an `image_seq_length` attribute."
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
self.image_seq_length = image_processor.image_seq_length
|
| 120 |
+
|
| 121 |
+
if not hasattr(tokenizer, "image_token"):
|
| 122 |
+
image_token = AddedToken(IMAGE_TOKEN, normalized=False, special=True)
|
| 123 |
+
tokens_to_add = {"additional_special_tokens": [image_token]}
|
| 124 |
+
tokenizer.add_special_tokens(tokens_to_add)
|
| 125 |
+
self.image_token_id = tokenizer.convert_tokens_to_ids(IMAGE_TOKEN)
|
| 126 |
+
self.image_token = IMAGE_TOKEN
|
| 127 |
+
else:
|
| 128 |
+
self.image_token_id = tokenizer.image_token_id
|
| 129 |
+
self.image_token = tokenizer.image_token
|
| 130 |
+
|
| 131 |
+
tokenizer.add_tokens(EXTRA_TOKENS)
|
| 132 |
+
tokenizer.add_bos_token = False
|
| 133 |
+
tokenizer.add_eos_token = False
|
| 134 |
+
|
| 135 |
+
super().__init__(image_processor, tokenizer, chat_template=chat_template)
|
| 136 |
+
|
| 137 |
+
def __call__(
|
| 138 |
+
self,
|
| 139 |
+
images: Optional[ImageInput] = None,
|
| 140 |
+
text: Union[
|
| 141 |
+
TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]
|
| 142 |
+
] = None,
|
| 143 |
+
**kwargs: Unpack[PaliGemmaProcessorKwargs],
|
| 144 |
+
) -> BatchFeature:
|
| 145 |
+
"""
|
| 146 |
+
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
|
| 147 |
+
and `kwargs` arguments to GemmaTokenizerFast's [`~GemmaTokenizerFast.__call__`] if `text` is not `None` to encode
|
| 148 |
+
the text. To prepare the image(s), this method forwards the `images` and `kwargs` arguments to
|
| 149 |
+
SiglipImageProcessor's [`~SiglipImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
|
| 150 |
+
of the above two methods for more information.
|
| 151 |
+
|
| 152 |
+
The usage for PaliGemma fine-tuning preparation is slightly different than usual. suffix passed are suffixes to
|
| 153 |
+
the prompt in `text`, and will be placed after the prompt. This is because attention is handled differently for
|
| 154 |
+
the prefix and the suffix. For instance,
|
| 155 |
+
```python
|
| 156 |
+
image = PIL_cow_image
|
| 157 |
+
prompt = "answer en Where is the cow standing?"
|
| 158 |
+
suffix = "on the beach"
|
| 159 |
+
inputs = processor(text=prompt, images=image, suffix=suffix)
|
| 160 |
+
```
|
| 161 |
+
Here `inputs` will contain the `input_ids` and `token_type_ids` that follow
|
| 162 |
+
```python
|
| 163 |
+
inputs["input_ids"][:, 256:]
|
| 164 |
+
# tensor([[ 2, 6006, 603, 573, 13910, 9980, 235336, 108, 477, 573, 8318]])
|
| 165 |
+
inputs["token_type_ids"][:, 256:]
|
| 166 |
+
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]])
|
| 167 |
+
```
|
| 168 |
+
Meaning the last three tokens are of "label" ("suffix") type while the other ones are of "prefix" type.
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
Args:
|
| 172 |
+
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`):
|
| 173 |
+
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
|
| 174 |
+
tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a
|
| 175 |
+
number of channels, H and W are image height and width.
|
| 176 |
+
text (`str`, `list[str]`, `list[list[str]]`):
|
| 177 |
+
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
|
| 178 |
+
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
|
| 179 |
+
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
|
| 180 |
+
return_tensors (`str` or [`~utils.TensorType`], *optional*):
|
| 181 |
+
If set, will return tensors of a particular framework. Acceptable values are:
|
| 182 |
+
|
| 183 |
+
- `'pt'`: Return PyTorch `torch.Tensor` objects.
|
| 184 |
+
- `'np'`: Return NumPy `np.ndarray` objects.
|
| 185 |
+
suffix (`str`, `list[str]`, `list[list[str]]`):
|
| 186 |
+
The suffixes or batch of suffixes to be encoded. Only necessary for finetuning. See https://github.com/google-research/big_vision/blob/main/big_vision/configs/proj/paligemma/README.md
|
| 187 |
+
for more information. If your prompt is "<image> What is on the image", the suffix corresponds to the expected prediction "a cow sitting on a bench".
|
| 188 |
+
|
| 189 |
+
Returns:
|
| 190 |
+
[`BatchFeature`]: A [`BatchFeature`] with the following fields:
|
| 191 |
+
|
| 192 |
+
- **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`. If `suffix`
|
| 193 |
+
is provided, the `input_ids` will also contain the suffix input ids.
|
| 194 |
+
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
|
| 195 |
+
`return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
|
| 196 |
+
`None`).
|
| 197 |
+
- **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
|
| 198 |
+
- **labels** -- Labels compatible with training if `suffix` is not None
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
output_kwargs = self._merge_kwargs(
|
| 202 |
+
PaliGemmaProcessorKwargs,
|
| 203 |
+
tokenizer_init_kwargs=self.tokenizer.init_kwargs,
|
| 204 |
+
**kwargs,
|
| 205 |
+
)
|
| 206 |
+
suffix = output_kwargs["text_kwargs"].pop("suffix", None)
|
| 207 |
+
|
| 208 |
+
return_token_type_ids = True
|
| 209 |
+
|
| 210 |
+
if images is None:
|
| 211 |
+
raise ValueError(
|
| 212 |
+
"`images` are expected as arguments to a `PaliGemmaProcessor` instance."
|
| 213 |
+
)
|
| 214 |
+
if text is None:
|
| 215 |
+
logger.warning_once(
|
| 216 |
+
"You are using PaliGemma without a text prefix. It will perform as a picture-captioning model."
|
| 217 |
+
)
|
| 218 |
+
text = ""
|
| 219 |
+
|
| 220 |
+
if _is_str_or_image(text):
|
| 221 |
+
text = [text]
|
| 222 |
+
elif isinstance(text, list) and _is_str_or_image(text[0]):
|
| 223 |
+
pass
|
| 224 |
+
|
| 225 |
+
if text is not None and images is not None:
|
| 226 |
+
if not any(IMAGE_TOKEN in sample for sample in text):
|
| 227 |
+
logger.warning(
|
| 228 |
+
"You are passing both `text` and `images` to `PaliGemmaProcessor`. The processor expects special "
|
| 229 |
+
"image tokens in the text, as many tokens as there are images per each text. It is recommended to "
|
| 230 |
+
"add `<image>` tokens in the very beginning of your text. For this call, we will infer how many images "
|
| 231 |
+
"each text has and add special tokens."
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
if isinstance(text, list) and isinstance(images, list):
|
| 235 |
+
if len(images) != len(text):
|
| 236 |
+
raise ValueError(
|
| 237 |
+
f"Received {len(images)} images for {len(text)} prompts. Each prompt should be associated with an image or list of images."
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# make a nested list of lists to be able to iterate over the images and text below
|
| 241 |
+
|
| 242 |
+
if is_valid_image(images):
|
| 243 |
+
images = [images]
|
| 244 |
+
elif isinstance(images, (list, tuple)) and is_valid_image(images[0]):
|
| 245 |
+
images = [image for image in images]
|
| 246 |
+
elif not (
|
| 247 |
+
isinstance(images, (list, tuple))
|
| 248 |
+
# and isinstance(images[0], (list, tuple))
|
| 249 |
+
and is_valid_image(images[0])
|
| 250 |
+
):
|
| 251 |
+
raise ValueError(
|
| 252 |
+
"images must be an image, list of images or list of list of images"
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
input_strings = [
|
| 256 |
+
build_string_from_input(
|
| 257 |
+
prompt=prompt,
|
| 258 |
+
bos_token=self.tokenizer.bos_token,
|
| 259 |
+
image_seq_len=self.image_seq_length,
|
| 260 |
+
image_token=IMAGE_TOKEN,
|
| 261 |
+
num_images=len(image_list)
|
| 262 |
+
if isinstance(image_list, list)
|
| 263 |
+
else 1,
|
| 264 |
+
)
|
| 265 |
+
for prompt, image_list in zip(text, images)
|
| 266 |
+
]
|
| 267 |
+
else:
|
| 268 |
+
expanded_samples = []
|
| 269 |
+
for sample in text:
|
| 270 |
+
expanded_sample = sample.replace(
|
| 271 |
+
IMAGE_TOKEN, IMAGE_TOKEN * self.image_seq_length
|
| 272 |
+
)
|
| 273 |
+
bos_rfind_index = expanded_sample.rfind(IMAGE_TOKEN)
|
| 274 |
+
bos_index = (
|
| 275 |
+
bos_rfind_index + len(IMAGE_TOKEN)
|
| 276 |
+
if bos_rfind_index != -1
|
| 277 |
+
else 0
|
| 278 |
+
)
|
| 279 |
+
expanded_sample = (
|
| 280 |
+
expanded_sample[:bos_index]
|
| 281 |
+
+ self.tokenizer.bos_token
|
| 282 |
+
+ expanded_sample[bos_index:]
|
| 283 |
+
)
|
| 284 |
+
expanded_samples.append(expanded_sample)
|
| 285 |
+
input_strings = [f"{sample}\n" for sample in expanded_samples]
|
| 286 |
+
|
| 287 |
+
if suffix is not None and _is_str_or_image(suffix):
|
| 288 |
+
suffix = [suffix]
|
| 289 |
+
if suffix is not None:
|
| 290 |
+
suffix = [sfx + self.tokenizer.eos_token for sfx in suffix]
|
| 291 |
+
pixel_values = self.image_processor(images, **output_kwargs["images_kwargs"])[
|
| 292 |
+
"pixel_values"
|
| 293 |
+
]
|
| 294 |
+
|
| 295 |
+
return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)
|
| 296 |
+
return_mm_token_type_ids = output_kwargs["text_kwargs"].pop(
|
| 297 |
+
"return_mm_token_type_ids", None
|
| 298 |
+
)
|
| 299 |
+
inputs = self.tokenizer(
|
| 300 |
+
input_strings,
|
| 301 |
+
text_pair=suffix,
|
| 302 |
+
return_token_type_ids=return_token_type_ids,
|
| 303 |
+
**output_kwargs["text_kwargs"],
|
| 304 |
+
)
|
| 305 |
+
# self._check_special_mm_tokens(input_strings, inputs, modalities=["image"])
|
| 306 |
+
|
| 307 |
+
return_data = {**inputs, "pixel_values": pixel_values}
|
| 308 |
+
|
| 309 |
+
# TODO: ideally we would control label generation separately, now that we always return token_type_ids.
|
| 310 |
+
if return_token_type_ids:
|
| 311 |
+
labels = np.array(inputs["input_ids"])
|
| 312 |
+
labels[np.array(inputs["token_type_ids"]) == 0] = -100
|
| 313 |
+
return_data.update({"labels": labels})
|
| 314 |
+
|
| 315 |
+
if return_mm_token_type_ids:
|
| 316 |
+
array_ids = np.array(return_data["input_ids"])
|
| 317 |
+
mm_token_type_ids = np.zeros_like(return_data["input_ids"])
|
| 318 |
+
mm_token_type_ids[array_ids == self.image_token_id] = 1
|
| 319 |
+
return_data["mm_token_type_ids"] = mm_token_type_ids.tolist()
|
| 320 |
+
|
| 321 |
+
return BatchFeature(data=return_data, tensor_type=return_tensors)
|
| 322 |
+
|
| 323 |
+
def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
|
| 324 |
+
"""
|
| 325 |
+
Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.
|
| 326 |
+
|
| 327 |
+
Args:
|
| 328 |
+
image_sizes (list[list[str]], *optional*):
|
| 329 |
+
The input sizes formatted as (height, width) per each image.
|
| 330 |
+
Returns:
|
| 331 |
+
`MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
|
| 332 |
+
input modalities, along with other useful data.
|
| 333 |
+
"""
|
| 334 |
+
vision_data = {}
|
| 335 |
+
if image_sizes is not None:
|
| 336 |
+
num_image_tokens = [self.image_seq_length] * len(image_sizes)
|
| 337 |
+
num_image_patches = [1] * len(image_sizes)
|
| 338 |
+
vision_data.update(
|
| 339 |
+
{
|
| 340 |
+
"num_image_tokens": num_image_tokens,
|
| 341 |
+
"num_image_patches": num_image_patches,
|
| 342 |
+
}
|
| 343 |
+
)
|
| 344 |
+
return MultiModalData(**vision_data)
|
| 345 |
+
|
| 346 |
+
@property
|
| 347 |
+
def model_input_names(self):
|
| 348 |
+
tokenizer_input_names = self.tokenizer.model_input_names + [
|
| 349 |
+
"token_type_ids",
|
| 350 |
+
"labels",
|
| 351 |
+
]
|
| 352 |
+
image_processor_input_names = self.image_processor.model_input_names
|
| 353 |
+
return list(tokenizer_input_names + image_processor_input_names)
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
def get_processor(hf_token, img_height, img_width, img_lm_input_seq_length):
|
| 357 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 358 |
+
"google/paligemma-3b-ft-docvqa-896",
|
| 359 |
+
token=hf_token,
|
| 360 |
+
revision="acbe61b1b8507f7c7af03a0d42e9908e7b6d4d5d",
|
| 361 |
+
)
|
| 362 |
+
image_processor = DonutImageProcessor.from_pretrained(
|
| 363 |
+
"naver-clova-ix/donut-base-finetuned-docvqa",
|
| 364 |
+
revision="b19d2e332684b0e2d35d9144ce34047767335cf8",
|
| 365 |
+
)
|
| 366 |
+
image_processor.image_seq_length = img_lm_input_seq_length
|
| 367 |
+
image_processor.size["height"], image_processor.size["width"] = (
|
| 368 |
+
img_height,
|
| 369 |
+
img_width,
|
| 370 |
+
)
|
| 371 |
+
processor = DIVEdocProcessor(tokenizer=tokenizer, image_processor=image_processor)
|
| 372 |
+
return processor
|
processor_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"auto_map": {
|
| 3 |
+
"AutoProcessor": "processing_divedoc.DIVEdocProcessor"
|
| 4 |
+
},
|
| 5 |
+
"processor_class": "DIVEdocProcessor"
|
| 6 |
+
}
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
{
|
| 4 |
+
"content": "<image>",
|
| 5 |
+
"lstrip": false,
|
| 6 |
+
"normalized": false,
|
| 7 |
+
"rstrip": false,
|
| 8 |
+
"single_word": false
|
| 9 |
+
}
|
| 10 |
+
],
|
| 11 |
+
"bos_token": {
|
| 12 |
+
"content": "<bos>",
|
| 13 |
+
"lstrip": false,
|
| 14 |
+
"normalized": false,
|
| 15 |
+
"rstrip": false,
|
| 16 |
+
"single_word": false
|
| 17 |
+
},
|
| 18 |
+
"eos_token": {
|
| 19 |
+
"content": "<eos>",
|
| 20 |
+
"lstrip": false,
|
| 21 |
+
"normalized": false,
|
| 22 |
+
"rstrip": false,
|
| 23 |
+
"single_word": false
|
| 24 |
+
},
|
| 25 |
+
"pad_token": {
|
| 26 |
+
"content": "<pad>",
|
| 27 |
+
"lstrip": false,
|
| 28 |
+
"normalized": false,
|
| 29 |
+
"rstrip": false,
|
| 30 |
+
"single_word": false
|
| 31 |
+
},
|
| 32 |
+
"unk_token": {
|
| 33 |
+
"content": "<unk>",
|
| 34 |
+
"lstrip": false,
|
| 35 |
+
"normalized": false,
|
| 36 |
+
"rstrip": false,
|
| 37 |
+
"single_word": false
|
| 38 |
+
}
|
| 39 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:172fab587d68c56b63eb3620057c62dfd15e503079ff7fce584692e3fd5bf4da
|
| 3 |
+
size 34600820
|
tokenizer.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8986bb4f423f07f8c7f70d0dbe3526fb2316056c17bae71b1ea975e77a168fc6
|
| 3 |
+
size 4264023
|
tokenizer_config.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|