Uniformize kwargs for Paligemma processor and update docs (#33571)
* Uniformize paligemma processor * nit
This commit is contained in:
@@ -337,7 +337,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png"
|
||||
)
|
||||
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
||||
inputs = self.processor(text=prompt, images=raw_image, return_tensors="pt")
|
||||
inputs = self.processor(images=raw_image, text=prompt, return_tensors="pt")
|
||||
EXPECTED_INPUT_IDS = torch.tensor([[257152] * 256 + [2, 108]])
|
||||
self.assertTrue(torch.equal(inputs["input_ids"], EXPECTED_INPUT_IDS))
|
||||
|
||||
@@ -360,7 +360,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png"
|
||||
)
|
||||
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
||||
inputs = self.processor(text=prompt, images=raw_image, return_tensors="pt").to(torch.float16)
|
||||
inputs = self.processor(images=raw_image, text=prompt, return_tensors="pt").to(torch.float16)
|
||||
|
||||
output = model.generate(**inputs, max_new_tokens=900, do_sample=False)
|
||||
EXPECTED_DECODED_TEXT = "answer en Where is the cow standing?\nbeach" # fmt: skip
|
||||
@@ -382,7 +382,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
"https://huggingface.co/datasets/hf-internal-testing/fixtures-captioning/resolve/main/cow_beach_1.png"
|
||||
)
|
||||
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
||||
inputs = self.processor(text=prompt, images=raw_image, return_tensors="pt").to(torch.float16)
|
||||
inputs = self.processor(images=raw_image, text=prompt, return_tensors="pt").to(torch.float16)
|
||||
|
||||
output = model.generate(**inputs, max_new_tokens=900, do_sample=False)
|
||||
EXPECTED_DECODED_TEXT = "\ncow on the beach" # fmt: skip
|
||||
@@ -412,7 +412,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
)
|
||||
image2 = image1
|
||||
|
||||
inputs = self.processor(text=prompts, images=[image1, image2], return_tensors="pt", padding=True)
|
||||
inputs = self.processor(images=[image1, image2], text=prompts, return_tensors="pt", padding=True)
|
||||
|
||||
output = model.generate(**inputs, max_new_tokens=20)
|
||||
|
||||
@@ -443,7 +443,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
image2 = image1
|
||||
|
||||
inputs = (
|
||||
self.processor(text=prompts, images=[image1, image2], return_tensors="pt", padding=True)
|
||||
self.processor(images=[image1, image2], text=prompts, return_tensors="pt", padding=True)
|
||||
.to(torch.bfloat16)
|
||||
.to(torch_device)
|
||||
)
|
||||
@@ -475,7 +475,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
image2 = image1
|
||||
|
||||
inputs = (
|
||||
self.processor(text=prompts, images=[image1, image2], return_tensors="pt", padding=True)
|
||||
self.processor(images=[image1, image2], text=prompts, return_tensors="pt", padding=True)
|
||||
.to(torch.float16)
|
||||
.to(torch_device)
|
||||
)
|
||||
@@ -504,7 +504,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
).raw
|
||||
)
|
||||
|
||||
inputs = self.processor(text=prompt, images=image, return_tensors="pt").to(torch.bfloat16).to(torch_device)
|
||||
inputs = self.processor(images=image, text=prompt, return_tensors="pt").to(torch.bfloat16).to(torch_device)
|
||||
|
||||
output = model.generate(**inputs, max_new_tokens=20)
|
||||
|
||||
@@ -528,8 +528,8 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
|
||||
raw_image = Image.open(requests.get(image_file, stream=True).raw)
|
||||
inputs = self.processor(
|
||||
text=prompt,
|
||||
images=raw_image,
|
||||
text=prompt,
|
||||
return_tensors="pt",
|
||||
).to(torch.float16)
|
||||
|
||||
@@ -561,7 +561,7 @@ class PaliGemmaForConditionalGenerationIntegrationTest(unittest.TestCase):
|
||||
image2 = image1
|
||||
|
||||
inputs = (
|
||||
self.processor(text=prompts, suffix=suffixes, images=[image1, image2], return_tensors="pt", padding=True)
|
||||
self.processor(images=[image1, image2], text=prompts, suffix=suffixes, return_tensors="pt", padding=True)
|
||||
.to(torch.bfloat16)
|
||||
.to(torch_device)
|
||||
)
|
||||
|
||||
89
tests/models/paligemma/test_processor_paligemma.py
Normal file
89
tests/models/paligemma/test_processor_paligemma.py
Normal file
@@ -0,0 +1,89 @@
|
||||
# Copyright 2024 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from transformers import GemmaTokenizer
|
||||
from transformers.testing_utils import get_tests_dir, require_torch, require_vision
|
||||
from transformers.utils import is_vision_available
|
||||
|
||||
from ...test_processing_common import ProcessorTesterMixin
|
||||
|
||||
|
||||
if is_vision_available():
|
||||
from transformers import (
|
||||
PaliGemmaProcessor,
|
||||
SiglipImageProcessor,
|
||||
is_vision_available,
|
||||
)
|
||||
|
||||
SAMPLE_VOCAB = get_tests_dir("fixtures/test_sentencepiece.model")
|
||||
|
||||
|
||||
@require_vision
|
||||
class PaliGemmaProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
||||
processor_class = PaliGemmaProcessor
|
||||
|
||||
def setUp(self):
|
||||
self.tmpdirname = tempfile.mkdtemp()
|
||||
image_processor = SiglipImageProcessor.from_pretrained("google/siglip-so400m-patch14-384")
|
||||
image_processor.image_seq_length = 0
|
||||
tokenizer = GemmaTokenizer(SAMPLE_VOCAB, keep_accents=True)
|
||||
processor = PaliGemmaProcessor(image_processor=image_processor, tokenizer=tokenizer)
|
||||
processor.save_pretrained(self.tmpdirname)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpdirname)
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
def test_image_seq_length(self):
|
||||
input_str = "lower newer"
|
||||
image_input = self.prepare_image_inputs()
|
||||
image_processor = self.get_component("image_processor")
|
||||
tokenizer = self.get_component("tokenizer", max_length=112, padding="max_length")
|
||||
image_processor.image_seq_length = 14
|
||||
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
|
||||
inputs = processor(
|
||||
text=input_str, images=image_input, return_tensors="pt", max_length=112, padding="max_length"
|
||||
)
|
||||
self.assertEqual(len(inputs["input_ids"][0]), 112 + 14)
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
def test_unstructured_kwargs_batched(self):
|
||||
if "image_processor" not in self.processor_class.attributes:
|
||||
self.skipTest(f"image_processor attribute not present in {self.processor_class}")
|
||||
image_processor = self.get_component("image_processor")
|
||||
tokenizer = self.get_component("tokenizer")
|
||||
|
||||
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor)
|
||||
self.skip_processor_without_typed_kwargs(processor)
|
||||
|
||||
input_str = ["lower newer", "upper older longer string"]
|
||||
image_input = self.prepare_image_inputs() * 2
|
||||
inputs = processor(
|
||||
text=input_str,
|
||||
images=image_input,
|
||||
return_tensors="pt",
|
||||
size={"height": 214, "width": 214},
|
||||
padding="longest",
|
||||
max_length=76,
|
||||
)
|
||||
|
||||
self.assertEqual(inputs["pixel_values"].shape[2], 214)
|
||||
|
||||
self.assertEqual(len(inputs["input_ids"][0]), 10)
|
||||
Reference in New Issue
Block a user