Skip to content

Commit

Permalink
Remove repeated prepare_images in processor tests (huggingface#33163)
Browse files Browse the repository at this point in the history
* Remove repeated prepare_images

* Address comments - update docstring; explanatory comment
  • Loading branch information
amyeroberts authored Sep 9, 2024
1 parent 0574fa6 commit f745e7d
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 304 deletions.
12 changes: 0 additions & 12 deletions tests/models/align/test_processor_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
Expand All @@ -30,8 +29,6 @@


if is_vision_available():
from PIL import Image

from transformers import AlignProcessor, EfficientNetImageProcessor


Expand Down Expand Up @@ -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()
Expand Down
20 changes: 5 additions & 15 deletions tests/models/blip/test_processor_blip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
20 changes: 5 additions & 15 deletions tests/models/blip_2/test_processor_blip_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
25 changes: 10 additions & 15 deletions tests/models/chinese_clip/test_processor_chinese_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
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()

Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
14 changes: 0 additions & 14 deletions tests/models/clip/test_processor_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
Expand All @@ -30,8 +29,6 @@


if is_vision_available():
from PIL import Image

from transformers import CLIPImageProcessor, CLIPProcessor


Expand Down Expand Up @@ -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()
Expand Down
19 changes: 5 additions & 14 deletions tests/models/clipseg/test_processor_clipseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
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()

Expand Down Expand Up @@ -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()
Expand Down
20 changes: 5 additions & 15 deletions tests/models/flava/test_processor_flava.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
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,
Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
20 changes: 5 additions & 15 deletions tests/models/git/test_processor_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions tests/models/grounding_dino/test_processor_grounding_dino.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast, GroundingDinoProcessor
Expand All @@ -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


Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit f745e7d

Please sign in to comment.