diff --git a/tests/models/align/test_processor_align.py b/tests/models/align/test_processor_align.py index cc5fdecb8df999..73e0d1df91f363 100644 --- a/tests/models/align/test_processor_align.py +++ b/tests/models/align/test_processor_align.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast @@ -30,8 +29,6 @@ if is_vision_available(): - from PIL import Image - from transformers import AlignProcessor, EfficientNetImageProcessor @@ -86,15 +83,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/blip/test_processor_blip.py b/tests/models/blip/test_processor_blip.py index b6d8b2e701759f..7b851c618a773d 100644 --- a/tests/models/blip/test_processor_blip.py +++ b/tests/models/blip/test_processor_blip.py @@ -15,21 +15,22 @@ import tempfile import unittest -import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast @require_vision -class BlipProcessorTest(unittest.TestCase): +class BlipProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = BlipProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -49,17 +50,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_additional_features(self): processor = BlipProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) diff --git a/tests/models/blip_2/test_processor_blip_2.py b/tests/models/blip_2/test_processor_blip_2.py index 5f13143c71cd99..8c7ca2ab698f48 100644 --- a/tests/models/blip_2/test_processor_blip_2.py +++ b/tests/models/blip_2/test_processor_blip_2.py @@ -15,21 +15,22 @@ import tempfile import unittest -import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import AutoProcessor, Blip2Processor, BlipImageProcessor, GPT2Tokenizer, PreTrainedTokenizerFast @require_vision -class Blip2ProcessorTest(unittest.TestCase): +class Blip2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = Blip2Processor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -49,17 +50,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_additional_features(self): processor = Blip2Processor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) diff --git a/tests/models/chinese_clip/test_processor_chinese_clip.py b/tests/models/chinese_clip/test_processor_chinese_clip.py index 969b4d8992c71c..e433c38f789104 100644 --- a/tests/models/chinese_clip/test_processor_chinese_clip.py +++ b/tests/models/chinese_clip/test_processor_chinese_clip.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast @@ -26,15 +25,17 @@ from transformers.testing_utils import require_vision from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor @require_vision -class ChineseCLIPProcessorTest(unittest.TestCase): +class ChineseCLIPProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = ChineseCLIPProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -76,6 +77,11 @@ def setUp(self): with open(self.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) + tokenizer = self.get_tokenizer() + image_processor = self.get_image_processor() + processor = ChineseCLIPProcessor(tokenizer=tokenizer, image_processor=image_processor) + processor.save_pretrained(self.tmpdirname) + def get_tokenizer(self, **kwargs): return BertTokenizer.from_pretrained(self.tmpdirname, **kwargs) @@ -88,17 +94,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/clip/test_processor_clip.py b/tests/models/clip/test_processor_clip.py index a76d3b33b829f4..7d7ea25b70cf0a 100644 --- a/tests/models/clip/test_processor_clip.py +++ b/tests/models/clip/test_processor_clip.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast @@ -30,8 +29,6 @@ if is_vision_available(): - from PIL import Image - from transformers import CLIPImageProcessor, CLIPProcessor @@ -79,17 +76,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/clipseg/test_processor_clipseg.py b/tests/models/clipseg/test_processor_clipseg.py index e33049b2768fe4..5147ed39753913 100644 --- a/tests/models/clipseg/test_processor_clipseg.py +++ b/tests/models/clipseg/test_processor_clipseg.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast @@ -26,15 +25,17 @@ from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import CLIPSegProcessor, ViTImageProcessor @require_vision -class CLIPSegProcessorTest(unittest.TestCase): +class CLIPSegProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = CLIPSegProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -75,16 +76,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True.""" - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/flava/test_processor_flava.py b/tests/models/flava/test_processor_flava.py index a83e459153d532..8489322efd6901 100644 --- a/tests/models/flava/test_processor_flava.py +++ b/tests/models/flava/test_processor_flava.py @@ -19,7 +19,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast @@ -27,10 +26,10 @@ from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import FlavaImageProcessor, FlavaProcessor from transformers.models.flava.image_processing_flava import ( FLAVA_CODEBOOK_MEAN, @@ -41,7 +40,9 @@ @require_vision -class FlavaProcessorTest(unittest.TestCase): +class FlavaProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = FlavaProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -91,17 +92,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/git/test_processor_git.py b/tests/models/git/test_processor_git.py index 95e436d8e4f526..bc62454cef6e25 100644 --- a/tests/models/git/test_processor_git.py +++ b/tests/models/git/test_processor_git.py @@ -15,21 +15,22 @@ import tempfile import unittest -import numpy as np import pytest from transformers.testing_utils import require_vision from transformers.utils import is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast @require_vision -class GitProcessorTest(unittest.TestCase): +class GitProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = GitProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -51,17 +52,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_additional_features(self): processor = GitProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) diff --git a/tests/models/grounding_dino/test_processor_grounding_dino.py b/tests/models/grounding_dino/test_processor_grounding_dino.py index 0c90938280916e..c2d8aee828dd52 100644 --- a/tests/models/grounding_dino/test_processor_grounding_dino.py +++ b/tests/models/grounding_dino/test_processor_grounding_dino.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import BertTokenizer, BertTokenizerFast, GroundingDinoProcessor @@ -35,8 +34,6 @@ from transformers.models.grounding_dino.modeling_grounding_dino import GroundingDinoObjectDetectionOutput if is_vision_available(): - from PIL import Image - from transformers import GroundingDinoImageProcessor @@ -96,18 +93,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - # Copied from tests.models.clip.test_processor_clip.CLIPProcessorTest.prepare_image_inputs - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def get_fake_grounding_dino_output(self): torch.manual_seed(42) return GroundingDinoObjectDetectionOutput( diff --git a/tests/models/instructblip/test_processor_instructblip.py b/tests/models/instructblip/test_processor_instructblip.py index 59198bcc5002c2..cc929e3575a6d8 100644 --- a/tests/models/instructblip/test_processor_instructblip.py +++ b/tests/models/instructblip/test_processor_instructblip.py @@ -15,7 +15,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers.testing_utils import require_torch, require_vision @@ -25,8 +24,6 @@ if is_vision_available(): - from PIL import Image - from transformers import ( AutoProcessor, BertTokenizerFast, @@ -64,17 +61,6 @@ def get_qformer_tokenizer(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_additional_features(self): processor = InstructBlipProcessor( tokenizer=self.get_tokenizer(), diff --git a/tests/models/kosmos2/test_processor_kosmos2.py b/tests/models/kosmos2/test_processor_kosmos2.py index d2223496c0c295..e07ba5fc106b6c 100644 --- a/tests/models/kosmos2/test_processor_kosmos2.py +++ b/tests/models/kosmos2/test_processor_kosmos2.py @@ -23,6 +23,7 @@ import pytest import requests +from transformers.models.auto.processing_auto import processor_class_from_name from transformers.testing_utils import ( get_tests_dir, require_sentencepiece, @@ -32,6 +33,8 @@ ) from transformers.utils import is_vision_available +from ...test_processing_common import ProcessorTesterMixin + if is_vision_available(): from PIL import Image @@ -52,7 +55,9 @@ @require_sentencepiece @require_tokenizers @require_vision -class Kosmos2ProcessorTest(unittest.TestCase): +class Kosmos2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = Kosmos2Processor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -65,6 +70,20 @@ def setUp(self): processor = Kosmos2Processor(image_processor, fast_tokenizer) processor.save_pretrained(self.tmpdirname) + # We override this method to take the fast tokenizer or image processor by default + def get_component(self, attribute, **kwargs): + assert attribute in self.processor_class.attributes + component_class_name = getattr(self.processor_class, f"{attribute}_class") + if isinstance(component_class_name, tuple): + component_class_name = component_class_name[-1] + + component_class = processor_class_from_name(component_class_name) + component = component_class.from_pretrained(self.tmpdirname, **kwargs) # noqa + if attribute == "tokenizer" and not component.pad_token: + component.pad_token = "[TEST_PAD]" + + return component + def get_tokenizer(self, **kwargs): return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).tokenizer @@ -74,17 +93,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_image_procesor_load_save_reload(self): # make sure load from Hub repo. -> save -> reload locally work image_processor = CLIPImageProcessor.from_pretrained("microsoft/kosmos-2-patch14-224") diff --git a/tests/models/layoutlmv2/test_processor_layoutlmv2.py b/tests/models/layoutlmv2/test_processor_layoutlmv2.py index 642eac6ba47e3e..a2676195ffd37c 100644 --- a/tests/models/layoutlmv2/test_processor_layoutlmv2.py +++ b/tests/models/layoutlmv2/test_processor_layoutlmv2.py @@ -19,26 +19,27 @@ import unittest from typing import List -import numpy as np - from transformers import PreTrainedTokenizer, PreTrainedTokenizerBase, PreTrainedTokenizerFast -from transformers.models.layoutlmv2 import LayoutLMv2Tokenizer, LayoutLMv2TokenizerFast +from transformers.models.layoutlmv2 import LayoutLMv2Processor, LayoutLMv2Tokenizer, LayoutLMv2TokenizerFast from transformers.models.layoutlmv2.tokenization_layoutlmv2 import VOCAB_FILES_NAMES from transformers.testing_utils import require_pytesseract, require_tokenizers, require_torch, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, cached_property, is_pytesseract_available +from ...test_processing_common import ProcessorTesterMixin + if is_pytesseract_available(): from PIL import Image - from transformers import LayoutLMv2ImageProcessor, LayoutLMv2Processor + from transformers import LayoutLMv2ImageProcessor @require_pytesseract @require_tokenizers -class LayoutLMv2ProcessorTest(unittest.TestCase): +class LayoutLMv2ProcessorTest(ProcessorTesterMixin, unittest.TestCase): tokenizer_class = LayoutLMv2Tokenizer rust_tokenizer_class = LayoutLMv2TokenizerFast + processor_class = LayoutLMv2Processor def setUp(self): vocab_tokens = [ @@ -88,17 +89,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): image_processor = self.get_image_processor() tokenizers = self.get_tokenizers() diff --git a/tests/models/layoutlmv3/test_processor_layoutlmv3.py b/tests/models/layoutlmv3/test_processor_layoutlmv3.py index 640eb92ea856e1..e55b19ea44b061 100644 --- a/tests/models/layoutlmv3/test_processor_layoutlmv3.py +++ b/tests/models/layoutlmv3/test_processor_layoutlmv3.py @@ -19,26 +19,27 @@ import unittest from typing import List -import numpy as np - from transformers import PreTrainedTokenizer, PreTrainedTokenizerBase, PreTrainedTokenizerFast -from transformers.models.layoutlmv3 import LayoutLMv3Tokenizer, LayoutLMv3TokenizerFast +from transformers.models.layoutlmv3 import LayoutLMv3Processor, LayoutLMv3Tokenizer, LayoutLMv3TokenizerFast from transformers.models.layoutlmv3.tokenization_layoutlmv3 import VOCAB_FILES_NAMES from transformers.testing_utils import require_pytesseract, require_tokenizers, require_torch, slow from transformers.utils import FEATURE_EXTRACTOR_NAME, cached_property, is_pytesseract_available +from ...test_processing_common import ProcessorTesterMixin + if is_pytesseract_available(): from PIL import Image - from transformers import LayoutLMv3ImageProcessor, LayoutLMv3Processor + from transformers import LayoutLMv3ImageProcessor @require_pytesseract @require_tokenizers -class LayoutLMv3ProcessorTest(unittest.TestCase): +class LayoutLMv3ProcessorTest(ProcessorTesterMixin, unittest.TestCase): tokenizer_class = LayoutLMv3Tokenizer rust_tokenizer_class = LayoutLMv3TokenizerFast + processor_class = LayoutLMv3Processor def setUp(self): # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt @@ -101,17 +102,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): image_processor = self.get_image_processor() tokenizers = self.get_tokenizers() diff --git a/tests/models/layoutxlm/test_processor_layoutxlm.py b/tests/models/layoutxlm/test_processor_layoutxlm.py index 98f6f07e38e6cf..b970a3e526830a 100644 --- a/tests/models/layoutxlm/test_processor_layoutxlm.py +++ b/tests/models/layoutxlm/test_processor_layoutxlm.py @@ -19,10 +19,8 @@ import unittest from typing import List -import numpy as np - from transformers import PreTrainedTokenizer, PreTrainedTokenizerBase, PreTrainedTokenizerFast -from transformers.models.layoutxlm import LayoutXLMTokenizer, LayoutXLMTokenizerFast +from transformers.models.layoutxlm import LayoutXLMProcessor, LayoutXLMTokenizer, LayoutXLMTokenizerFast from transformers.testing_utils import ( require_pytesseract, require_sentencepiece, @@ -32,19 +30,22 @@ ) from transformers.utils import FEATURE_EXTRACTOR_NAME, cached_property, is_pytesseract_available +from ...test_processing_common import ProcessorTesterMixin + if is_pytesseract_available(): from PIL import Image - from transformers import LayoutLMv2ImageProcessor, LayoutXLMProcessor + from transformers import LayoutLMv2ImageProcessor @require_pytesseract @require_sentencepiece @require_tokenizers -class LayoutXLMProcessorTest(unittest.TestCase): +class LayoutXLMProcessorTest(ProcessorTesterMixin, unittest.TestCase): tokenizer_class = LayoutXLMTokenizer rust_tokenizer_class = LayoutXLMTokenizerFast + processor_class = LayoutXLMProcessor def setUp(self): image_processor_map = { @@ -61,6 +62,11 @@ def setUp(self): # taken from `test_tokenization_layoutxlm.LayoutXLMTokenizationTest.test_save_pretrained` self.tokenizer_pretrained_name = "hf-internal-testing/tiny-random-layoutxlm" + tokenizer = self.get_tokenizer() + image_processor = self.get_image_processor() + processor = LayoutXLMProcessor(tokenizer=tokenizer, image_processor=image_processor) + processor.save_pretrained(self.tmpdirname) + def get_tokenizer(self, **kwargs) -> PreTrainedTokenizer: return self.tokenizer_class.from_pretrained(self.tokenizer_pretrained_name, **kwargs) @@ -76,17 +82,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): image_processor = self.get_image_processor() tokenizers = self.get_tokenizers() diff --git a/tests/models/mgp_str/test_processor_mgp_str.py b/tests/models/mgp_str/test_processor_mgp_str.py index 6a028a28424d61..783a61ebf144a9 100644 --- a/tests/models/mgp_str/test_processor_mgp_str.py +++ b/tests/models/mgp_str/test_processor_mgp_str.py @@ -70,6 +70,17 @@ def setUp(self): with open(self.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) + # We copy here rather than use the ProcessorTesterMixin as this processor has a `char_tokenizer` instad of a + # tokenizer attribute, which means all the tests would need to be overridden. + @require_vision + def prepare_image_inputs(self): + """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, + or a list of PyTorch tensors if one specifies torchify=True. + """ + image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] + image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] + return image_inputs + def get_tokenizer(self, **kwargs): return MgpstrTokenizer.from_pretrained(self.tmpdirname, **kwargs) @@ -79,15 +90,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images.""" - - image_input = np.random.randint(255, size=(3, 30, 400), dtype=np.uint8) - - image_input = Image.fromarray(np.moveaxis(image_input, 0, -1)) - - return image_input - def test_save_load_pretrained_default(self): tokenizer = self.get_tokenizer() image_processor = self.get_image_processor() diff --git a/tests/models/owlvit/test_processor_owlvit.py b/tests/models/owlvit/test_processor_owlvit.py index b271c8880bfddc..f31dbaf9fbccab 100644 --- a/tests/models/owlvit/test_processor_owlvit.py +++ b/tests/models/owlvit/test_processor_owlvit.py @@ -18,7 +18,6 @@ import tempfile import unittest -import numpy as np import pytest from transformers import CLIPTokenizer, CLIPTokenizerFast @@ -26,15 +25,17 @@ from transformers.testing_utils import require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import OwlViTImageProcessor, OwlViTProcessor @require_vision -class OwlViTProcessorTest(unittest.TestCase): +class OwlViTProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = OwlViTProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -75,17 +76,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer_slow = self.get_tokenizer() tokenizer_fast = self.get_rust_tokenizer() diff --git a/tests/models/pix2struct/test_processor_pix2struct.py b/tests/models/pix2struct/test_processor_pix2struct.py index 88335296f03590..17b3298145f823 100644 --- a/tests/models/pix2struct/test_processor_pix2struct.py +++ b/tests/models/pix2struct/test_processor_pix2struct.py @@ -15,16 +15,15 @@ import tempfile import unittest -import numpy as np import pytest from transformers.testing_utils import require_torch, require_vision from transformers.utils import is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import ( AutoProcessor, Pix2StructImageProcessor, @@ -36,7 +35,9 @@ @require_vision @require_torch -class Pix2StructProcessorTest(unittest.TestCase): +class Pix2StructProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = Pix2StructProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -56,17 +57,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """ - This function prepares a list of random PIL images of the same fixed size. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_additional_features(self): processor = Pix2StructProcessor(tokenizer=self.get_tokenizer(), image_processor=self.get_image_processor()) processor.save_pretrained(self.tmpdirname) diff --git a/tests/models/sam/test_processor_sam.py b/tests/models/sam/test_processor_sam.py index 377f5031e0e83a..22eb88d03d6b04 100644 --- a/tests/models/sam/test_processor_sam.py +++ b/tests/models/sam/test_processor_sam.py @@ -26,6 +26,8 @@ ) from transformers.utils import is_tf_available, is_torch_available, is_vision_available +from ...test_processing_common import prepare_image_inputs + if is_vision_available(): from PIL import Image @@ -54,13 +56,10 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) + # Processor tester class can't use ProcessorTesterMixin atm because the processor is atypical e.g. only contains an image processor def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - return image_inputs + """This function prepares a list of PIL images.""" + return prepare_image_inputs() def prepare_mask_inputs(self): """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, @@ -166,16 +165,10 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) + # Processor tester class can't use ProcessorTesterMixin as processor is atypical e.g. only contains an image processor and it assumes torch def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs + """This function prepares a list of PIL images.""" + return prepare_image_inputs() def test_save_load_pretrained_additional_features(self): processor = SamProcessor(image_processor=self.get_image_processor()) @@ -255,16 +248,10 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) + # Processor tester class can't use ProcessorTesterMixin atm because the processor is atypical e.g. only contains an image processor def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs + """This function prepares a list of PIL images.""" + return prepare_image_inputs() @is_pt_tf_cross_test def test_post_process_masks_equivalence(self): diff --git a/tests/models/udop/test_processor_udop.py b/tests/models/udop/test_processor_udop.py index eaa8ca7e5afc15..749ec7c3d6df78 100644 --- a/tests/models/udop/test_processor_udop.py +++ b/tests/models/udop/test_processor_udop.py @@ -19,12 +19,11 @@ import unittest from typing import List -import numpy as np - from transformers import ( PreTrainedTokenizer, PreTrainedTokenizerBase, PreTrainedTokenizerFast, + UdopProcessor, UdopTokenizer, UdopTokenizerFast, ) @@ -37,6 +36,8 @@ ) from transformers.utils import FEATURE_EXTRACTOR_NAME, cached_property, is_pytesseract_available, is_torch_available +from ...test_processing_common import ProcessorTesterMixin + if is_torch_available(): import torch @@ -45,16 +46,17 @@ if is_pytesseract_available(): from PIL import Image - from transformers import LayoutLMv3ImageProcessor, UdopProcessor + from transformers import LayoutLMv3ImageProcessor @require_pytesseract @require_sentencepiece @require_tokenizers -class UdopProcessorTest(unittest.TestCase): +class UdopProcessorTest(ProcessorTesterMixin, unittest.TestCase): tokenizer_class = UdopTokenizer rust_tokenizer_class = UdopTokenizerFast maxDiff = None + processor_class = UdopProcessor def setUp(self): image_processor_map = { @@ -70,6 +72,11 @@ def setUp(self): self.tokenizer_pretrained_name = "microsoft/udop-large" + image_processor = self.get_image_processor() + tokenizer = self.get_tokenizers()[0] + processor = UdopProcessor(image_processor=image_processor, tokenizer=tokenizer) + processor.save_pretrained(self.tmpdirname) + def get_tokenizer(self, **kwargs) -> PreTrainedTokenizer: return self.tokenizer_class.from_pretrained(self.tokenizer_pretrained_name, **kwargs) @@ -85,17 +92,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): image_processor = self.get_image_processor() tokenizers = self.get_tokenizers() diff --git a/tests/models/vision_text_dual_encoder/test_processor_vision_text_dual_encoder.py b/tests/models/vision_text_dual_encoder/test_processor_vision_text_dual_encoder.py index aebe723bd5fd7f..c9386a160f843d 100644 --- a/tests/models/vision_text_dual_encoder/test_processor_vision_text_dual_encoder.py +++ b/tests/models/vision_text_dual_encoder/test_processor_vision_text_dual_encoder.py @@ -18,23 +18,23 @@ import tempfile import unittest -import numpy as np - from transformers import BertTokenizerFast from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer from transformers.testing_utils import require_tokenizers, require_vision from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available +from ...test_processing_common import ProcessorTesterMixin -if is_vision_available(): - from PIL import Image +if is_vision_available(): from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor @require_tokenizers @require_vision -class VisionTextDualEncoderProcessorTest(unittest.TestCase): +class VisionTextDualEncoderProcessorTest(ProcessorTesterMixin, unittest.TestCase): + processor_class = VisionTextDualEncoderProcessor + def setUp(self): self.tmpdirname = tempfile.mkdtemp() @@ -54,6 +54,11 @@ def setUp(self): with open(self.image_processor_file, "w", encoding="utf-8") as fp: json.dump(image_processor_map, fp) + tokenizer = self.get_tokenizer() + image_processor = self.get_image_processor() + processor = VisionTextDualEncoderProcessor(tokenizer=tokenizer, image_processor=image_processor) + processor.save_pretrained(self.tmpdirname) + def get_tokenizer(self, **kwargs): return BertTokenizer.from_pretrained(self.tmpdirname, **kwargs) @@ -63,17 +68,6 @@ def get_image_processor(self, **kwargs): def tearDown(self): shutil.rmtree(self.tmpdirname) - def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - - return image_inputs - def test_save_load_pretrained_default(self): tokenizer = self.get_tokenizer() image_processor = self.get_image_processor() diff --git a/tests/test_processing_common.py b/tests/test_processing_common.py index a30c6363b9d7ff..1343f3bac703e4 100644 --- a/tests/test_processing_common.py +++ b/tests/test_processing_common.py @@ -44,6 +44,13 @@ from transformers import CLIPImageProcessor +def prepare_image_inputs(): + """This function prepares a list of PIL images""" + image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] + image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] + return image_inputs + + @require_torch @require_vision @require_torch @@ -81,12 +88,8 @@ def get_processor(self): @require_vision def prepare_image_inputs(self): - """This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True, - or a list of PyTorch tensors if one specifies torchify=True. - """ - image_inputs = [np.random.randint(255, size=(3, 30, 400), dtype=np.uint8)] - image_inputs = [Image.fromarray(np.moveaxis(x, 0, -1)) for x in image_inputs] - return image_inputs + """This function prepares a list of PIL images for testing""" + return prepare_image_inputs() def test_processor_to_json_string(self): processor = self.get_processor()