Rename processing_ernie_45t_vl.py to processing_ernie4_5_vl.py
Browse files
processing_ernie_45t_vl.py → processing_ernie4_5_vl.py
RENAMED
|
@@ -17,7 +17,6 @@
|
|
| 17 |
import copy
|
| 18 |
import io
|
| 19 |
import os
|
| 20 |
-
import re
|
| 21 |
import math
|
| 22 |
import random
|
| 23 |
import requests
|
|
@@ -28,14 +27,13 @@ import threading
|
|
| 28 |
import uuid
|
| 29 |
import decord
|
| 30 |
from shutil import copyfile
|
| 31 |
-
from typing import Dict, List, Optional, Tuple, Union
|
| 32 |
|
| 33 |
import numpy as np
|
| 34 |
import torch
|
| 35 |
from PIL import Image, ImageDraw, ImageFont
|
| 36 |
from PIL.ExifTags import TAGS
|
| 37 |
from collections import defaultdict
|
| 38 |
-
from typing import Any, Dict, List, Union
|
| 39 |
from pathlib import Path
|
| 40 |
from tempfile import NamedTemporaryFile as ntf
|
| 41 |
|
|
@@ -52,7 +50,6 @@ from transformers.tokenization_utils_base import (
|
|
| 52 |
PaddingStrategy,
|
| 53 |
TextInput,
|
| 54 |
)
|
| 55 |
-
from transformers.utils import logging
|
| 56 |
from transformers.utils import TensorType, logging
|
| 57 |
from transformers.video_utils import VideoInput
|
| 58 |
from transformers.processing_utils import ProcessorMixin
|
|
@@ -82,132 +79,419 @@ from transformers.image_utils import (
|
|
| 82 |
logger = logging.get_logger(__name__)
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
"""
|
| 87 |
-
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
min_pixels: int = 4 * 28 * 28,
|
| 105 |
-
max_pixels: int = 16384 * 28 * 28,
|
| 106 |
-
):
|
| 107 |
-
"""
|
| 108 |
-
Rescales the image so that the following conditions are met:
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
if height > width:
|
| 119 |
-
new_width = max(factor, round_by_factor(width, factor))
|
| 120 |
-
new_height = floor_by_factor(new_width * MAX_RATIO, factor)
|
| 121 |
-
else:
|
| 122 |
-
new_height = max(factor, round_by_factor(height, factor))
|
| 123 |
-
new_width = floor_by_factor(new_height * MAX_RATIO, factor)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
h_bar = floor_by_factor(height / beta, factor)
|
| 138 |
-
w_bar = floor_by_factor(width / beta, factor)
|
| 139 |
-
elif h_bar * w_bar < min_pixels:
|
| 140 |
-
beta = math.sqrt(min_pixels / (height * width))
|
| 141 |
-
h_bar = ceil_by_factor(height * beta, factor)
|
| 142 |
-
w_bar = ceil_by_factor(width * beta, factor)
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"""
|
| 154 |
-
if image.dtype == np.uint8:
|
| 155 |
-
return False
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
and isinstance(images[0], (list, tuple))
|
| 175 |
-
and is_valid_image(images[0][0])
|
| 176 |
-
):
|
| 177 |
-
return [img for img_list in images for img in img_list]
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
def make_batched_videos(videos) -> List[VideoInput]:
|
| 190 |
-
"""dummy"""
|
| 191 |
-
if (
|
| 192 |
-
isinstance(videos, (list, tuple))
|
| 193 |
-
and isinstance(videos[0], (list, tuple))
|
| 194 |
-
and is_valid_image(videos[0][0])
|
| 195 |
-
):
|
| 196 |
-
return videos
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
|
| 204 |
-
|
| 205 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 208 |
|
| 209 |
|
| 210 |
-
class
|
| 211 |
r"""
|
| 212 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 213 |
|
|
@@ -289,7 +573,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 289 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 290 |
), "min_pixels must be positive int"
|
| 291 |
logger.info(
|
| 292 |
-
f"{msg}
|
| 293 |
)
|
| 294 |
self.min_pixels = min_pixels
|
| 295 |
self.size["min_pixels"] = int(min_pixels)
|
|
@@ -298,7 +582,7 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 298 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 299 |
), "max_pixels must be positive int"
|
| 300 |
logger.info(
|
| 301 |
-
f"{msg}
|
| 302 |
)
|
| 303 |
self.max_pixels = max_pixels
|
| 304 |
self.size["max_pixels"] = int(max_pixels)
|
|
@@ -618,298 +902,15 @@ class Ernie_45T_VLImageProcessor(BaseImageProcessor):
|
|
| 618 |
return BatchFeature(data=data, tensor_type=return_tensors)
|
| 619 |
|
| 620 |
|
| 621 |
-
class Ernie4_5_VLTokenizer(PreTrainedTokenizer):
|
| 622 |
-
"""
|
| 623 |
-
Ernie4_5_VLTokenizer
|
| 624 |
-
"""
|
| 625 |
-
|
| 626 |
-
vocab_files_names = {
|
| 627 |
-
"vocab_file": "tokenizer.model",
|
| 628 |
-
}
|
| 629 |
-
# Model input names expected by the tokenizer
|
| 630 |
-
model_input_names = ["input_ids", "position_ids", "attention_mask", "labels"]
|
| 631 |
-
# Padding side (where to add padding tokens)
|
| 632 |
-
padding_side = "right"
|
| 633 |
-
|
| 634 |
-
def __init__(
|
| 635 |
-
self,
|
| 636 |
-
vocab_file,
|
| 637 |
-
bos_token="<s>",
|
| 638 |
-
cls_token="<cls>",
|
| 639 |
-
eos_token="</s>",
|
| 640 |
-
mask_token="<mask:0>",
|
| 641 |
-
pad_token="<pad>",
|
| 642 |
-
sep_token="<sep>",
|
| 643 |
-
unk_token="<unk>",
|
| 644 |
-
additional_special_tokens=None,
|
| 645 |
-
**kwargs,
|
| 646 |
-
):
|
| 647 |
-
"""
|
| 648 |
-
Initialize the Ernie4_5_VLTokenizer
|
| 649 |
-
|
| 650 |
-
Args:
|
| 651 |
-
vocab_file (str): Path to the tokenizer vocabulary model.
|
| 652 |
-
bos_token (str, optional): The beginning of sequence token. Defaults to `"<s>"`.
|
| 653 |
-
cls_token (str, optional): The classifier token. Defaults to `"<cls>"`.
|
| 654 |
-
eos_token (str, optional): The end of sequence token. Defaults to `"</s>"`.
|
| 655 |
-
mask_token (str, optional): The masking token. Defaults to `"<mask:0>"`.
|
| 656 |
-
pad_token (str, optional): The padding token. Defaults to `"<pad>"`.
|
| 657 |
-
sep_token (str, optional): The separation token. Defaults to `"<sep>"`.
|
| 658 |
-
unk_token (str, optional): The unknown tokens symbol. Defaults to `"<unk>"`.
|
| 659 |
-
additional_special_tokens (List[str], optional): Additional special tokens to use.
|
| 660 |
-
Defaults to `["<mask:1>", "<mask:7>"]`.
|
| 661 |
-
**kwargs (dict): Additional keyword arguments passed along to the superclass.
|
| 662 |
-
"""
|
| 663 |
-
|
| 664 |
-
# Store vocabulary file path
|
| 665 |
-
self.vocab_file = vocab_file
|
| 666 |
-
# Initialize SentencePiece processor
|
| 667 |
-
self.sp_model = spm.SentencePieceProcessor()
|
| 668 |
-
# Load the vocabulary model
|
| 669 |
-
self.sp_model.Load(vocab_file)
|
| 670 |
-
|
| 671 |
-
# Set default additional special tokens if none provided
|
| 672 |
-
if additional_special_tokens is None:
|
| 673 |
-
additional_special_tokens = ["<mask:1>", "<mask:7>"]
|
| 674 |
-
super().__init__(
|
| 675 |
-
bos_token=bos_token,
|
| 676 |
-
cls_token=cls_token,
|
| 677 |
-
eos_token=eos_token,
|
| 678 |
-
mask_token=mask_token,
|
| 679 |
-
pad_token=pad_token,
|
| 680 |
-
sep_token=sep_token,
|
| 681 |
-
unk_token=unk_token,
|
| 682 |
-
additional_special_tokens=additional_special_tokens,
|
| 683 |
-
**kwargs,
|
| 684 |
-
)
|
| 685 |
-
|
| 686 |
-
@property
|
| 687 |
-
def space_token(self):
|
| 688 |
-
"""Return the space token"""
|
| 689 |
-
return "<mask:1>"
|
| 690 |
-
|
| 691 |
-
@property
|
| 692 |
-
def space_token_id(self):
|
| 693 |
-
"""Return the ID of the space token"""
|
| 694 |
-
return self.sp_model.piece_to_id("<mask:1>")
|
| 695 |
-
|
| 696 |
-
@property
|
| 697 |
-
def gend_token(self):
|
| 698 |
-
"""Return the gender token"""
|
| 699 |
-
return "<mask:7>"
|
| 700 |
-
|
| 701 |
-
@property
|
| 702 |
-
def gend_token_id(self):
|
| 703 |
-
"""Return the ID of the gender token"""
|
| 704 |
-
return self.sp_model.piece_to_id("<mask:7>")
|
| 705 |
-
|
| 706 |
-
@property
|
| 707 |
-
def im_start_id(self):
|
| 708 |
-
"""Return the ID of the image start token"""
|
| 709 |
-
return self.sp_model.piece_to_id("<|im_start|>")
|
| 710 |
-
|
| 711 |
-
@property
|
| 712 |
-
def im_end_id(self):
|
| 713 |
-
"""Return the ID of the image end token"""
|
| 714 |
-
return self.sp_model.piece_to_id("<|im_end|>")
|
| 715 |
-
|
| 716 |
-
@property
|
| 717 |
-
def vocab_size(self):
|
| 718 |
-
"""Return the size of the vocabulary"""
|
| 719 |
-
return self.sp_model.vocab_size()
|
| 720 |
-
|
| 721 |
-
def get_vocab(self):
|
| 722 |
-
"""Return the vocabulary as a dictionary mapping tokens to IDs"""
|
| 723 |
-
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 724 |
-
vocab.update(self.added_tokens_encoder)
|
| 725 |
-
return vocab
|
| 726 |
-
|
| 727 |
-
def _tokenize(self, text):
|
| 728 |
-
"""Tokenize the input text into pieces"""
|
| 729 |
-
return self.sp_model.encode_as_pieces(text)
|
| 730 |
-
|
| 731 |
-
def _convert_token_to_id(self, token):
|
| 732 |
-
"""Convert a token to its corresponding ID"""
|
| 733 |
-
return self.sp_model.piece_to_id(token)
|
| 734 |
-
|
| 735 |
-
def _convert_id_to_token(self, id):
|
| 736 |
-
"""Convert an ID to its corresponding token"""
|
| 737 |
-
return self.sp_model.id_to_piece(id)
|
| 738 |
-
|
| 739 |
-
def convert_tokens_to_string(self, tokens):
|
| 740 |
-
"""Convert a sequence of tokens back to a string"""
|
| 741 |
-
current_sub_tokens = []
|
| 742 |
-
out_string = ""
|
| 743 |
-
|
| 744 |
-
for token in tokens:
|
| 745 |
-
# Handle special tokens differently
|
| 746 |
-
if token in self.all_special_tokens:
|
| 747 |
-
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 748 |
-
current_sub_tokens = []
|
| 749 |
-
else:
|
| 750 |
-
current_sub_tokens.append(token)
|
| 751 |
-
|
| 752 |
-
# Add any remaining sub-tokens
|
| 753 |
-
out_string += self.sp_model.decode(current_sub_tokens)
|
| 754 |
-
return out_string
|
| 755 |
-
|
| 756 |
-
def prepare_for_model(self, *args, **kwargs):
|
| 757 |
-
"""Prepare the tokenized inputs for the model"""
|
| 758 |
-
# Remove add_special_tokens if present (not supported)
|
| 759 |
-
if "add_special_tokens" in kwargs:
|
| 760 |
-
kwargs.pop("add_special_tokens")
|
| 761 |
-
return super().prepare_for_model(*args, **kwargs)
|
| 762 |
-
|
| 763 |
-
def save_vocabulary(
|
| 764 |
-
self, save_directory, filename_prefix: Optional[str] = None
|
| 765 |
-
) -> Tuple[str]:
|
| 766 |
-
"""
|
| 767 |
-
Save the vocabulary and special tokens file to a directory.
|
| 768 |
-
|
| 769 |
-
Args:
|
| 770 |
-
save_directory (`str`): The directory to save the vocabulary to
|
| 771 |
-
filename_prefix (`str`, optional): Prefix to add to the filename
|
| 772 |
-
|
| 773 |
-
Returns:
|
| 774 |
-
`Tuple(str)`: Paths to the saved files
|
| 775 |
-
"""
|
| 776 |
-
if not os.path.isdir(save_directory):
|
| 777 |
-
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 778 |
-
return
|
| 779 |
-
|
| 780 |
-
# Construct output vocabulary file path
|
| 781 |
-
out_vocab_file = os.path.join(
|
| 782 |
-
save_directory,
|
| 783 |
-
(filename_prefix + "-" if filename_prefix else "")
|
| 784 |
-
+ self.vocab_files_names["vocab_file"],
|
| 785 |
-
)
|
| 786 |
-
|
| 787 |
-
# Copy or create vocabulary file
|
| 788 |
-
if os.path.abspath(self.vocab_file) != os.path.abspath(
|
| 789 |
-
out_vocab_file
|
| 790 |
-
) and os.path.isfile(self.vocab_file):
|
| 791 |
-
copyfile(self.vocab_file, out_vocab_file)
|
| 792 |
-
elif not os.path.isfile(self.vocab_file):
|
| 793 |
-
with open(out_vocab_file, "wb") as fi:
|
| 794 |
-
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 795 |
-
fi.write(content_spiece_model)
|
| 796 |
-
|
| 797 |
-
return (out_vocab_file,)
|
| 798 |
-
|
| 799 |
-
def _decode(self, *args, **kwargs):
|
| 800 |
-
"""Decode token_id back to text"""
|
| 801 |
-
# Remove some parameters that aren't used
|
| 802 |
-
kwargs.pop("clean_up_tokenization_spaces", None)
|
| 803 |
-
kwargs.pop("spaces_between_special_tokens", None)
|
| 804 |
-
|
| 805 |
-
# Call parent decode method with specific parameters
|
| 806 |
-
return super()._decode(
|
| 807 |
-
*args,
|
| 808 |
-
**kwargs,
|
| 809 |
-
clean_up_tokenization_spaces=False,
|
| 810 |
-
spaces_between_special_tokens=False,
|
| 811 |
-
)
|
| 812 |
-
|
| 813 |
-
def _pad(
|
| 814 |
-
self,
|
| 815 |
-
encoded_inputs: Dict,
|
| 816 |
-
max_length: Optional[int] = None,
|
| 817 |
-
padding_strategy=PaddingStrategy.DO_NOT_PAD,
|
| 818 |
-
pad_to_multiple_of: Optional[int] = None,
|
| 819 |
-
return_attention_mask: Optional[bool] = None,
|
| 820 |
-
) -> dict:
|
| 821 |
-
"""Pad the encoded inputs to the specified length"""
|
| 822 |
-
if return_attention_mask is None:
|
| 823 |
-
return_attention_mask = "attention_mask" in self.model_input_names
|
| 824 |
-
if return_attention_mask:
|
| 825 |
-
required_input = encoded_inputs[self.model_input_names[0]]
|
| 826 |
-
if padding_strategy == PaddingStrategy.LONGEST:
|
| 827 |
-
max_length = len(required_input)
|
| 828 |
-
|
| 829 |
-
# Adjust max_length if needed for multiple of padding
|
| 830 |
-
if (
|
| 831 |
-
max_length is not None
|
| 832 |
-
and pad_to_multiple_of is not None
|
| 833 |
-
and (max_length % pad_to_multiple_of != 0)
|
| 834 |
-
):
|
| 835 |
-
max_length = (
|
| 836 |
-
(max_length // pad_to_multiple_of) + 1
|
| 837 |
-
) * pad_to_multiple_of
|
| 838 |
-
|
| 839 |
-
# Check if padding is needed
|
| 840 |
-
needs_to_be_padded = (
|
| 841 |
-
padding_strategy != PaddingStrategy.DO_NOT_PAD
|
| 842 |
-
and len(required_input) != max_length
|
| 843 |
-
)
|
| 844 |
-
|
| 845 |
-
# Handle attention mask if present
|
| 846 |
-
if (
|
| 847 |
-
"attention_mask" in encoded_inputs
|
| 848 |
-
and encoded_inputs["attention_mask"] is not None
|
| 849 |
-
):
|
| 850 |
-
attention_mask = encoded_inputs.pop("attention_mask")
|
| 851 |
-
if isinstance(attention_mask, torch.Tensor):
|
| 852 |
-
attention_mask = attention_mask.numpy()
|
| 853 |
-
elif isinstance(attention_mask, list):
|
| 854 |
-
attention_mask = np.array(attention_mask)
|
| 855 |
-
elif not isinstance(attention_mask, np.ndarray):
|
| 856 |
-
raise ValueError(
|
| 857 |
-
f"Unexpected type {type(attention_mask)} of attention_mask, "
|
| 858 |
-
)
|
| 859 |
-
else:
|
| 860 |
-
# Create default attention mask if none provided
|
| 861 |
-
attention_mask = np.tril(
|
| 862 |
-
np.ones((len(required_input), len(required_input)), dtype=np.int64)
|
| 863 |
-
)
|
| 864 |
-
attention_mask = np.expand_dims(attention_mask, axis=0)
|
| 865 |
-
|
| 866 |
-
# Perform padding if needed
|
| 867 |
-
if needs_to_be_padded:
|
| 868 |
-
difference = max_length - len(required_input)
|
| 869 |
-
if self.padding_side == "right":
|
| 870 |
-
if attention_mask.ndim == 1:
|
| 871 |
-
pad_width = [(0, difference)]
|
| 872 |
-
else:
|
| 873 |
-
pad_width = [(0, 0), (0, difference), (0, difference)]
|
| 874 |
-
elif self.padding_side == "left":
|
| 875 |
-
if attention_mask.ndim == 1:
|
| 876 |
-
pad_width = [(difference, 0)]
|
| 877 |
-
else:
|
| 878 |
-
pad_width = [(0, 0), (difference, 0), (difference, 0)]
|
| 879 |
-
else:
|
| 880 |
-
raise ValueError(
|
| 881 |
-
"Invalid padding strategy:" + str(self.padding_side)
|
| 882 |
-
)
|
| 883 |
-
|
| 884 |
-
attention_mask = np.pad(
|
| 885 |
-
attention_mask,
|
| 886 |
-
pad_width=pad_width,
|
| 887 |
-
mode="constant",
|
| 888 |
-
constant_values=0,
|
| 889 |
-
)
|
| 890 |
-
|
| 891 |
-
# Call parent padding method
|
| 892 |
-
encoded_inputs = super()._pad(
|
| 893 |
-
encoded_inputs,
|
| 894 |
-
max_length,
|
| 895 |
-
padding_strategy=padding_strategy,
|
| 896 |
-
pad_to_multiple_of=pad_to_multiple_of,
|
| 897 |
-
return_attention_mask=False,
|
| 898 |
-
)
|
| 899 |
-
|
| 900 |
-
# Add attention mask back if needed
|
| 901 |
-
if return_attention_mask:
|
| 902 |
-
encoded_inputs["attention_mask"] = attention_mask.tolist()
|
| 903 |
-
|
| 904 |
-
return encoded_inputs
|
| 905 |
-
|
| 906 |
-
|
| 907 |
RAW_VIDEO_DIR = "./download_tmp/raw_video/"
|
| 908 |
RAW_IMAGE_DIR = "./download_tmp/raw_images/"
|
| 909 |
EXTRACTED_FRAME_DIR = "./download_tmp/extracted_frames/"
|
| 910 |
TMP_DIR = "./download_tmp/upload_tmp/"
|
| 911 |
|
| 912 |
FONT_PATH = os.path.join(Path(__file__).parent.absolute(), "Roboto-Regular.ttf")
|
|
|
|
|
|
|
|
|
|
| 913 |
|
| 914 |
|
| 915 |
def is_gif(data: bytes) -> bool:
|
|
@@ -1380,7 +1381,7 @@ def render_frame_timestamp(frame, timestamp, font_rate=0.1):
|
|
| 1380 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1381 |
|
| 1382 |
|
| 1383 |
-
class
|
| 1384 |
"""
|
| 1385 |
Processes multimodal chat messages into model-ready inputs,
|
| 1386 |
handling text, images, and videos with 3D positional embeddings.
|
|
@@ -1527,11 +1528,11 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1527 |
|
| 1528 |
def __call__(
|
| 1529 |
self,
|
| 1530 |
-
text: List[str],
|
| 1531 |
-
images: List[Image.Image],
|
| 1532 |
-
videos: List[List[Image.Image]],
|
| 1533 |
**kwargs,
|
| 1534 |
-
) ->
|
| 1535 |
"""
|
| 1536 |
Convert chat messages into model inputs.
|
| 1537 |
Returns a dict with input_ids, token_type_ids, position_ids, images, grid_thw, image_type_ids, labels.
|
|
@@ -1547,6 +1548,9 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1547 |
"pic_cnt": 0,
|
| 1548 |
"video_cnt": 0,
|
| 1549 |
}
|
|
|
|
|
|
|
|
|
|
| 1550 |
texts = text[0]
|
| 1551 |
|
| 1552 |
new_video_seg = True
|
|
@@ -1811,4 +1815,4 @@ class Ernie_45T_VLProcessor(ProcessorMixin):
|
|
| 1811 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1812 |
|
| 1813 |
|
| 1814 |
-
__all__ = ["
|
|
|
|
| 17 |
import copy
|
| 18 |
import io
|
| 19 |
import os
|
|
|
|
| 20 |
import math
|
| 21 |
import random
|
| 22 |
import requests
|
|
|
|
| 27 |
import uuid
|
| 28 |
import decord
|
| 29 |
from shutil import copyfile
|
| 30 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 31 |
|
| 32 |
import numpy as np
|
| 33 |
import torch
|
| 34 |
from PIL import Image, ImageDraw, ImageFont
|
| 35 |
from PIL.ExifTags import TAGS
|
| 36 |
from collections import defaultdict
|
|
|
|
| 37 |
from pathlib import Path
|
| 38 |
from tempfile import NamedTemporaryFile as ntf
|
| 39 |
|
|
|
|
| 50 |
PaddingStrategy,
|
| 51 |
TextInput,
|
| 52 |
)
|
|
|
|
| 53 |
from transformers.utils import TensorType, logging
|
| 54 |
from transformers.video_utils import VideoInput
|
| 55 |
from transformers.processing_utils import ProcessorMixin
|
|
|
|
| 79 |
logger = logging.get_logger(__name__)
|
| 80 |
|
| 81 |
|
| 82 |
+
class Ernie4_5_VLTokenizer(PreTrainedTokenizer):
|
| 83 |
+
"""
|
| 84 |
+
Ernie4_5_VLTokenizer
|
| 85 |
+
"""
|
| 86 |
|
| 87 |
+
vocab_files_names = {
|
| 88 |
+
"vocab_file": "tokenizer.model",
|
| 89 |
+
}
|
| 90 |
+
# Model input names expected by the tokenizer
|
| 91 |
+
model_input_names = ["input_ids", "position_ids", "attention_mask", "labels"]
|
| 92 |
+
# Padding side (where to add padding tokens)
|
| 93 |
+
padding_side = "right"
|
| 94 |
|
| 95 |
+
def __init__(
|
| 96 |
+
self,
|
| 97 |
+
vocab_file,
|
| 98 |
+
bos_token="<s>",
|
| 99 |
+
cls_token="<cls>",
|
| 100 |
+
eos_token="</s>",
|
| 101 |
+
mask_token="<mask:0>",
|
| 102 |
+
pad_token="<pad>",
|
| 103 |
+
sep_token="<sep>",
|
| 104 |
+
unk_token="<unk>",
|
| 105 |
+
additional_special_tokens=None,
|
| 106 |
+
**kwargs,
|
| 107 |
+
):
|
| 108 |
+
"""
|
| 109 |
+
Initialize the Ernie4_5_VLTokenizer
|
| 110 |
|
| 111 |
+
Args:
|
| 112 |
+
vocab_file (str): Path to the tokenizer vocabulary model.
|
| 113 |
+
bos_token (str, optional): The beginning of sequence token. Defaults to `"<s>"`.
|
| 114 |
+
cls_token (str, optional): The classifier token. Defaults to `"<cls>"`.
|
| 115 |
+
eos_token (str, optional): The end of sequence token. Defaults to `"</s>"`.
|
| 116 |
+
mask_token (str, optional): The masking token. Defaults to `"<mask:0>"`.
|
| 117 |
+
pad_token (str, optional): The padding token. Defaults to `"<pad>"`.
|
| 118 |
+
sep_token (str, optional): The separation token. Defaults to `"<sep>"`.
|
| 119 |
+
unk_token (str, optional): The unknown tokens symbol. Defaults to `"<unk>"`.
|
| 120 |
+
additional_special_tokens (List[str], optional): Additional special tokens to use.
|
| 121 |
+
Defaults to `["<mask:1>", "<mask:7>"]`.
|
| 122 |
+
**kwargs (dict): Additional keyword arguments passed along to the superclass.
|
| 123 |
+
"""
|
| 124 |
|
| 125 |
+
# Store vocabulary file path
|
| 126 |
+
self.vocab_file = vocab_file
|
| 127 |
+
# Initialize SentencePiece processor
|
| 128 |
+
self.sp_model = spm.SentencePieceProcessor()
|
| 129 |
+
# Load the vocabulary model
|
| 130 |
+
self.sp_model.Load(vocab_file)
|
| 131 |
|
| 132 |
+
# Set default additional special tokens if none provided
|
| 133 |
+
if additional_special_tokens is None:
|
| 134 |
+
additional_special_tokens = ["<mask:1>", "<mask:7>"]
|
| 135 |
+
super().__init__(
|
| 136 |
+
bos_token=bos_token,
|
| 137 |
+
cls_token=cls_token,
|
| 138 |
+
eos_token=eos_token,
|
| 139 |
+
mask_token=mask_token,
|
| 140 |
+
pad_token=pad_token,
|
| 141 |
+
sep_token=sep_token,
|
| 142 |
+
unk_token=unk_token,
|
| 143 |
+
additional_special_tokens=additional_special_tokens,
|
| 144 |
+
**kwargs,
|
| 145 |
+
)
|
| 146 |
|
| 147 |
+
@property
|
| 148 |
+
def space_token(self):
|
| 149 |
+
"""Return the space token"""
|
| 150 |
+
return "<mask:1>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
@property
|
| 153 |
+
def space_token_id(self):
|
| 154 |
+
"""Return the ID of the space token"""
|
| 155 |
+
return self.sp_model.piece_to_id("<mask:1>")
|
| 156 |
|
| 157 |
+
@property
|
| 158 |
+
def gend_token(self):
|
| 159 |
+
"""Return the gender token"""
|
| 160 |
+
return "<mask:7>"
|
| 161 |
|
| 162 |
+
@property
|
| 163 |
+
def gend_token_id(self):
|
| 164 |
+
"""Return the ID of the gender token"""
|
| 165 |
+
return self.sp_model.piece_to_id("<mask:7>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
@property
|
| 168 |
+
def im_start_id(self):
|
| 169 |
+
"""Return the ID of the image start token"""
|
| 170 |
+
return self.sp_model.piece_to_id("<|im_start|>")
|
| 171 |
|
| 172 |
+
@property
|
| 173 |
+
def im_end_id(self):
|
| 174 |
+
"""Return the ID of the image end token"""
|
| 175 |
+
return self.sp_model.piece_to_id("<|im_end|>")
|
| 176 |
|
| 177 |
+
@property
|
| 178 |
+
def vocab_size(self):
|
| 179 |
+
"""Return the size of the vocabulary"""
|
| 180 |
+
return self.sp_model.vocab_size()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
def get_vocab(self):
|
| 183 |
+
"""Return the vocabulary as a dictionary mapping tokens to IDs"""
|
| 184 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
| 185 |
+
vocab.update(self.added_tokens_encoder)
|
| 186 |
+
return vocab
|
| 187 |
|
| 188 |
+
def _tokenize(self, text):
|
| 189 |
+
"""Tokenize the input text into pieces"""
|
| 190 |
+
return self.sp_model.encode_as_pieces(text)
|
| 191 |
|
| 192 |
+
def _convert_token_to_id(self, token):
|
| 193 |
+
"""Convert a token to its corresponding ID"""
|
| 194 |
+
return self.sp_model.piece_to_id(token)
|
| 195 |
|
| 196 |
+
def _convert_id_to_token(self, id):
|
| 197 |
+
"""Convert an ID to its corresponding token"""
|
| 198 |
+
return self.sp_model.id_to_piece(id)
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
def convert_tokens_to_string(self, tokens):
|
| 201 |
+
"""Convert a sequence of tokens back to a string"""
|
| 202 |
+
current_sub_tokens = []
|
| 203 |
+
out_string = ""
|
| 204 |
|
| 205 |
+
for token in tokens:
|
| 206 |
+
# Handle special tokens differently
|
| 207 |
+
if token in self.all_special_tokens:
|
| 208 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
| 209 |
+
current_sub_tokens = []
|
| 210 |
+
else:
|
| 211 |
+
current_sub_tokens.append(token)
|
| 212 |
|
| 213 |
+
# Add any remaining sub-tokens
|
| 214 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
| 215 |
+
return out_string
|
| 216 |
|
| 217 |
+
def prepare_for_model(self, *args, **kwargs):
|
| 218 |
+
"""Prepare the tokenized inputs for the model"""
|
| 219 |
+
# Remove add_special_tokens if present (not supported)
|
| 220 |
+
if "add_special_tokens" in kwargs:
|
| 221 |
+
kwargs.pop("add_special_tokens")
|
| 222 |
+
return super().prepare_for_model(*args, **kwargs)
|
| 223 |
|
| 224 |
+
def save_vocabulary(
|
| 225 |
+
self, save_directory, filename_prefix: Optional[str] = None
|
| 226 |
+
) -> Tuple[str]:
|
| 227 |
+
"""
|
| 228 |
+
Save the vocabulary and special tokens file to a directory.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
Args:
|
| 231 |
+
save_directory (`str`): The directory to save the vocabulary to
|
| 232 |
+
filename_prefix (`str`, optional): Prefix to add to the filename
|
| 233 |
|
| 234 |
+
Returns:
|
| 235 |
+
`Tuple(str)`: Paths to the saved files
|
| 236 |
+
"""
|
| 237 |
+
if not os.path.isdir(save_directory):
|
| 238 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
| 239 |
+
return
|
| 240 |
|
| 241 |
+
# Construct output vocabulary file path
|
| 242 |
+
out_vocab_file = os.path.join(
|
| 243 |
+
save_directory,
|
| 244 |
+
(filename_prefix + "-" if filename_prefix else "")
|
| 245 |
+
+ self.vocab_files_names["vocab_file"],
|
| 246 |
+
)
|
| 247 |
|
| 248 |
+
# Copy or create vocabulary file
|
| 249 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(
|
| 250 |
+
out_vocab_file
|
| 251 |
+
) and os.path.isfile(self.vocab_file):
|
| 252 |
+
copyfile(self.vocab_file, out_vocab_file)
|
| 253 |
+
elif not os.path.isfile(self.vocab_file):
|
| 254 |
+
with open(out_vocab_file, "wb") as fi:
|
| 255 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
| 256 |
+
fi.write(content_spiece_model)
|
| 257 |
|
| 258 |
+
return (out_vocab_file,)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
+
def _decode(self, *args, **kwargs):
|
| 261 |
+
"""Decode token_id back to text"""
|
| 262 |
+
# Remove some parameters that aren't used
|
| 263 |
+
kwargs.pop("clean_up_tokenization_spaces", None)
|
| 264 |
+
kwargs.pop("spaces_between_special_tokens", None)
|
| 265 |
|
| 266 |
+
# Call parent decode method with specific parameters
|
| 267 |
+
return super()._decode(
|
| 268 |
+
*args,
|
| 269 |
+
**kwargs,
|
| 270 |
+
clean_up_tokenization_spaces=False,
|
| 271 |
+
spaces_between_special_tokens=False,
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
def _pad(
|
| 275 |
+
self,
|
| 276 |
+
encoded_inputs: Dict,
|
| 277 |
+
max_length: Optional[int] = None,
|
| 278 |
+
padding_strategy=PaddingStrategy.DO_NOT_PAD,
|
| 279 |
+
pad_to_multiple_of: Optional[int] = None,
|
| 280 |
+
return_attention_mask: Optional[bool] = None,
|
| 281 |
+
**kwargs
|
| 282 |
+
) -> dict:
|
| 283 |
+
"""Pad the encoded inputs to the specified length"""
|
| 284 |
+
if return_attention_mask is None:
|
| 285 |
+
return_attention_mask = "attention_mask" in self.model_input_names
|
| 286 |
+
if return_attention_mask:
|
| 287 |
+
required_input = encoded_inputs[self.model_input_names[0]]
|
| 288 |
+
if padding_strategy == PaddingStrategy.LONGEST:
|
| 289 |
+
max_length = len(required_input)
|
| 290 |
+
|
| 291 |
+
# Adjust max_length if needed for multiple of padding
|
| 292 |
+
if (
|
| 293 |
+
max_length is not None
|
| 294 |
+
and pad_to_multiple_of is not None
|
| 295 |
+
and (max_length % pad_to_multiple_of != 0)
|
| 296 |
+
):
|
| 297 |
+
max_length = (
|
| 298 |
+
(max_length // pad_to_multiple_of) + 1
|
| 299 |
+
) * pad_to_multiple_of
|
| 300 |
+
|
| 301 |
+
# Check if padding is needed
|
| 302 |
+
needs_to_be_padded = (
|
| 303 |
+
padding_strategy != PaddingStrategy.DO_NOT_PAD
|
| 304 |
+
and len(required_input) != max_length
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
# Handle attention mask if present
|
| 308 |
+
if (
|
| 309 |
+
"attention_mask" in encoded_inputs
|
| 310 |
+
and encoded_inputs["attention_mask"] is not None
|
| 311 |
+
):
|
| 312 |
+
attention_mask = encoded_inputs.pop("attention_mask")
|
| 313 |
+
if isinstance(attention_mask, torch.Tensor):
|
| 314 |
+
attention_mask = attention_mask.numpy()
|
| 315 |
+
elif isinstance(attention_mask, list):
|
| 316 |
+
attention_mask = np.array(attention_mask)
|
| 317 |
+
elif not isinstance(attention_mask, np.ndarray):
|
| 318 |
+
raise ValueError(
|
| 319 |
+
f"Unexpected type {type(attention_mask)} of attention_mask, "
|
| 320 |
+
)
|
| 321 |
+
else:
|
| 322 |
+
# Create default attention mask if none provided
|
| 323 |
+
attention_mask = np.tril(
|
| 324 |
+
np.ones((len(required_input), len(required_input)), dtype=np.int64)
|
| 325 |
+
)
|
| 326 |
+
attention_mask = np.expand_dims(attention_mask, axis=0)
|
| 327 |
+
|
| 328 |
+
# Perform padding if needed
|
| 329 |
+
if needs_to_be_padded:
|
| 330 |
+
difference = max_length - len(required_input)
|
| 331 |
+
if self.padding_side == "right":
|
| 332 |
+
if attention_mask.ndim == 1:
|
| 333 |
+
pad_width = [(0, difference)]
|
| 334 |
+
else:
|
| 335 |
+
pad_width = [(0, 0), (0, difference), (0, difference)]
|
| 336 |
+
elif self.padding_side == "left":
|
| 337 |
+
if attention_mask.ndim == 1:
|
| 338 |
+
pad_width = [(difference, 0)]
|
| 339 |
+
else:
|
| 340 |
+
pad_width = [(0, 0), (difference, 0), (difference, 0)]
|
| 341 |
+
else:
|
| 342 |
+
raise ValueError(
|
| 343 |
+
"Invalid padding strategy:" + str(self.padding_side)
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
attention_mask = np.pad(
|
| 347 |
+
attention_mask,
|
| 348 |
+
pad_width=pad_width,
|
| 349 |
+
mode="constant",
|
| 350 |
+
constant_values=0,
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
# Call parent padding method
|
| 354 |
+
encoded_inputs = super()._pad(
|
| 355 |
+
encoded_inputs,
|
| 356 |
+
max_length,
|
| 357 |
+
padding_strategy=padding_strategy,
|
| 358 |
+
pad_to_multiple_of=pad_to_multiple_of,
|
| 359 |
+
return_attention_mask=False,
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
# Add attention mask back if needed
|
| 363 |
+
if return_attention_mask:
|
| 364 |
+
encoded_inputs["attention_mask"] = attention_mask.tolist()
|
| 365 |
+
|
| 366 |
+
return encoded_inputs
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def round_by_factor(number: int, factor: int) -> int:
|
| 370 |
+
"""Returns the closest integer to 'number' that is divisible by 'factor'."""
|
| 371 |
+
return round(number / factor) * factor
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def ceil_by_factor(number: int, factor: int) -> int:
|
| 375 |
+
"""Returns the smallest integer greater than or equal to 'number' that is divisible by 'factor'."""
|
| 376 |
+
return math.ceil(number / factor) * factor
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
def floor_by_factor(number: int, factor: int) -> int:
|
| 380 |
+
"""Returns the largest integer less than or equal to 'number' that is divisible by 'factor'."""
|
| 381 |
+
return math.floor(number / factor) * factor
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def smart_resize(
|
| 385 |
+
height: int,
|
| 386 |
+
width: int,
|
| 387 |
+
factor: int = 28,
|
| 388 |
+
min_pixels: int = 4 * 28 * 28,
|
| 389 |
+
max_pixels: int = 16384 * 28 * 28,
|
| 390 |
+
):
|
| 391 |
+
"""
|
| 392 |
+
Rescales the image so that the following conditions are met:
|
| 393 |
+
|
| 394 |
+
1. Both dimensions (height and width) are divisible by 'factor'.
|
| 395 |
+
|
| 396 |
+
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
|
| 397 |
+
|
| 398 |
+
3. The aspect ratio of the image is maintained as closely as possible.
|
| 399 |
+
"""
|
| 400 |
+
MAX_RATIO = 200
|
| 401 |
+
if max(height, width) / min(height, width) > MAX_RATIO:
|
| 402 |
+
if height > width:
|
| 403 |
+
new_width = max(factor, round_by_factor(width, factor))
|
| 404 |
+
new_height = floor_by_factor(new_width * MAX_RATIO, factor)
|
| 405 |
+
else:
|
| 406 |
+
new_height = max(factor, round_by_factor(height, factor))
|
| 407 |
+
new_width = floor_by_factor(new_height * MAX_RATIO, factor)
|
| 408 |
+
|
| 409 |
+
logger.info(
|
| 410 |
+
f"absolute aspect ratio must be smaller than {MAX_RATIO}, got {max(height, width) / min(height, width)},\
|
| 411 |
+
resize to {max(new_height, new_width) / min(new_height, new_width)}"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
height = new_height
|
| 415 |
+
width = new_width
|
| 416 |
+
|
| 417 |
+
h_bar = max(factor, round_by_factor(height, factor))
|
| 418 |
+
w_bar = max(factor, round_by_factor(width, factor))
|
| 419 |
+
if h_bar * w_bar > max_pixels:
|
| 420 |
+
beta = math.sqrt((height * width) / max_pixels)
|
| 421 |
+
h_bar = floor_by_factor(height / beta, factor)
|
| 422 |
+
w_bar = floor_by_factor(width / beta, factor)
|
| 423 |
+
elif h_bar * w_bar < min_pixels:
|
| 424 |
+
beta = math.sqrt(min_pixels / (height * width))
|
| 425 |
+
h_bar = ceil_by_factor(height * beta, factor)
|
| 426 |
+
w_bar = ceil_by_factor(width * beta, factor)
|
| 427 |
+
|
| 428 |
+
if min_pixels > h_bar * w_bar or h_bar * w_bar > max_pixels:
|
| 429 |
+
raise ValueError(f"encounter invalid h_bar: {h_bar}, w_bar: {w_bar}")
|
| 430 |
+
|
| 431 |
+
return h_bar, w_bar
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def is_scaled_image(image: np.ndarray) -> bool:
|
| 435 |
+
"""
|
| 436 |
+
Checks to see whether the pixel values have already been rescaled to [0, 1].
|
| 437 |
+
"""
|
| 438 |
+
if image.dtype == np.uint8:
|
| 439 |
+
return False
|
| 440 |
+
|
| 441 |
+
# It's possible the image has pixel values in [0, 255] but is of floating type
|
| 442 |
+
return np.min(image) >= 0 and np.max(image) <= 1
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def make_batched_images(images) -> List[List[ImageInput]]:
|
| 446 |
+
"""
|
| 447 |
+
Accepts images in list or nested list format, and makes a list of images for preprocessing.
|
| 448 |
+
|
| 449 |
+
Args:
|
| 450 |
+
images (`Union[List[List[ImageInput]], List[ImageInput], ImageInput]`):
|
| 451 |
+
The input image.
|
| 452 |
+
|
| 453 |
+
Returns:
|
| 454 |
+
list: A list of images.
|
| 455 |
+
"""
|
| 456 |
+
if (
|
| 457 |
+
isinstance(images, (list, tuple))
|
| 458 |
+
and isinstance(images[0], (list, tuple))
|
| 459 |
+
and is_valid_image(images[0][0])
|
| 460 |
+
):
|
| 461 |
+
return [img for img_list in images for img in img_list]
|
| 462 |
+
|
| 463 |
+
elif isinstance(images, (list, tuple)) and is_valid_image(images[0]):
|
| 464 |
+
return images
|
| 465 |
+
|
| 466 |
+
elif is_valid_image(images):
|
| 467 |
+
return [images]
|
| 468 |
+
|
| 469 |
+
raise ValueError(f"Could not make batched images from {images}")
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
# Copied from transformers.models.llava_next_video.image_processing_llava_next_video.make_batched_videos
|
| 473 |
+
def make_batched_videos(videos) -> List[VideoInput]:
|
| 474 |
+
"""dummy"""
|
| 475 |
+
if (
|
| 476 |
+
isinstance(videos, (list, tuple))
|
| 477 |
+
and isinstance(videos[0], (list, tuple))
|
| 478 |
+
and is_valid_image(videos[0][0])
|
| 479 |
+
):
|
| 480 |
+
return videos
|
| 481 |
+
|
| 482 |
+
elif isinstance(videos, (list, tuple)) and is_valid_image(videos[0]):
|
| 483 |
+
if isinstance(videos[0], Image.Image):
|
| 484 |
+
return [videos]
|
| 485 |
+
elif len(videos[0].shape) == 4:
|
| 486 |
+
return [list(video) for video in videos]
|
| 487 |
+
|
| 488 |
+
elif is_valid_image(videos) and len(videos.shape) == 4:
|
| 489 |
+
return [list(videos)]
|
| 490 |
|
| 491 |
raise ValueError(f"Could not make batched video from {videos}")
|
| 492 |
|
| 493 |
|
| 494 |
+
class Ernie4_5_VLImageProcessor(BaseImageProcessor):
|
| 495 |
r"""
|
| 496 |
Constructs a adaptive image processor that dynamically resizes images based on the original images.
|
| 497 |
|
|
|
|
| 573 |
isinstance(min_pixels, int) and min_pixels >= 0
|
| 574 |
), "min_pixels must be positive int"
|
| 575 |
logger.info(
|
| 576 |
+
f"{msg} Ernie4_5_VLImageProcessor set min_pixels = {min_pixels}"
|
| 577 |
)
|
| 578 |
self.min_pixels = min_pixels
|
| 579 |
self.size["min_pixels"] = int(min_pixels)
|
|
|
|
| 582 |
isinstance(max_pixels, int) and max_pixels > 0
|
| 583 |
), "max_pixels must be positive int"
|
| 584 |
logger.info(
|
| 585 |
+
f"{msg} Ernie4_5_VLImageProcessor set max_pixels = {max_pixels}"
|
| 586 |
)
|
| 587 |
self.max_pixels = max_pixels
|
| 588 |
self.size["max_pixels"] = int(max_pixels)
|
|
|
|
| 902 |
return BatchFeature(data=data, tensor_type=return_tensors)
|
| 903 |
|
| 904 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
RAW_VIDEO_DIR = "./download_tmp/raw_video/"
|
| 906 |
RAW_IMAGE_DIR = "./download_tmp/raw_images/"
|
| 907 |
EXTRACTED_FRAME_DIR = "./download_tmp/extracted_frames/"
|
| 908 |
TMP_DIR = "./download_tmp/upload_tmp/"
|
| 909 |
|
| 910 |
FONT_PATH = os.path.join(Path(__file__).parent.absolute(), "Roboto-Regular.ttf")
|
| 911 |
+
if not os.path.exists(FONT_PATH):
|
| 912 |
+
ttf = requests.get("https://paddlenlp.bj.bcebos.com/vision-language-models/materials/Roboto-Regular.ttf")
|
| 913 |
+
open(FONT_PATH, "wb").write(ttf.content)
|
| 914 |
|
| 915 |
|
| 916 |
def is_gif(data: bytes) -> bool:
|
|
|
|
| 1381 |
IDS_TYPE_FLAG = {"text": 0, "image": 1, "video": 2, "audio": 3}
|
| 1382 |
|
| 1383 |
|
| 1384 |
+
class Ernie4_5_VLProcessor(ProcessorMixin):
|
| 1385 |
"""
|
| 1386 |
Processes multimodal chat messages into model-ready inputs,
|
| 1387 |
handling text, images, and videos with 3D positional embeddings.
|
|
|
|
| 1528 |
|
| 1529 |
def __call__(
|
| 1530 |
self,
|
| 1531 |
+
text: Union[str, List[str]],
|
| 1532 |
+
images: List[Image.Image] = [],
|
| 1533 |
+
videos: List[List[Image.Image]] = [],
|
| 1534 |
**kwargs,
|
| 1535 |
+
) -> BatchFeature:
|
| 1536 |
"""
|
| 1537 |
Convert chat messages into model inputs.
|
| 1538 |
Returns a dict with input_ids, token_type_ids, position_ids, images, grid_thw, image_type_ids, labels.
|
|
|
|
| 1548 |
"pic_cnt": 0,
|
| 1549 |
"video_cnt": 0,
|
| 1550 |
}
|
| 1551 |
+
if not isinstance(text, list):
|
| 1552 |
+
text = [text]
|
| 1553 |
+
|
| 1554 |
texts = text[0]
|
| 1555 |
|
| 1556 |
new_video_seg = True
|
|
|
|
| 1815 |
return list(tokenizer_input_names) + list(image_processor_input_names)
|
| 1816 |
|
| 1817 |
|
| 1818 |
+
__all__ = ["Ernie4_5_VLTokenizer", "Ernie4_5_VLImageProcessor", "Ernie4_5_VLProcessor"]
|