Update tests: replace feature extractor tests with image processor (#20768)
* Update imports and test fetcher * Revert but keep test fetcher update * Fix imports * Fix all imports * Replace fe with ip names * Add generate kwargs to `AutomaticSpeechRecognitionPipeline` (#20952) * Add generate kwargs to AutomaticSpeechRecognitionPipeline * Add test for generation kwargs * Update image processor parameters if creating with kwargs (#20866) * Update parameters if creating with kwargs * Shallow copy to prevent mutating input * Pass all args in constructor dict - warnings in init * Fix typo * Rename tester class * Rebase and tidy up * Fixup * Use ImageProcessingSavingTestMixin * Update property ref in tests * Update property ref in tests * Update recently merged in models * Small fix Co-authored-by: bofeng huang <bofenghuang7@gmail.com>
This commit is contained in:
@@ -21,8 +21,7 @@ import numpy as np
|
||||
from transformers.testing_utils import require_torch, require_vision
|
||||
from transformers.utils import is_torch_available, is_vision_available
|
||||
|
||||
from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin
|
||||
from ...test_image_processing_common import prepare_image_inputs
|
||||
from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
@@ -31,10 +30,10 @@ if is_torch_available():
|
||||
if is_vision_available():
|
||||
from PIL import Image
|
||||
|
||||
from transformers import GLPNFeatureExtractor
|
||||
from transformers import GLPNImageProcessor
|
||||
|
||||
|
||||
class GLPNFeatureExtractionTester(unittest.TestCase):
|
||||
class GLPNImageProcessingTester(unittest.TestCase):
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
@@ -57,7 +56,7 @@ class GLPNFeatureExtractionTester(unittest.TestCase):
|
||||
self.size_divisor = size_divisor
|
||||
self.do_rescale = do_rescale
|
||||
|
||||
def prepare_feat_extract_dict(self):
|
||||
def prepare_image_processor_dict(self):
|
||||
return {
|
||||
"do_resize": self.do_resize,
|
||||
"size_divisor": self.size_divisor,
|
||||
@@ -67,62 +66,62 @@ class GLPNFeatureExtractionTester(unittest.TestCase):
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
class GLPNFeatureExtractionTest(FeatureExtractionSavingTestMixin, unittest.TestCase):
|
||||
class GLPNImageProcessingTest(ImageProcessingSavingTestMixin, unittest.TestCase):
|
||||
|
||||
feature_extraction_class = GLPNFeatureExtractor if is_vision_available() else None
|
||||
image_processing_class = GLPNImageProcessor if is_vision_available() else None
|
||||
|
||||
def setUp(self):
|
||||
self.feature_extract_tester = GLPNFeatureExtractionTester(self)
|
||||
self.image_processor_tester = GLPNImageProcessingTester(self)
|
||||
|
||||
@property
|
||||
def feat_extract_dict(self):
|
||||
return self.feature_extract_tester.prepare_feat_extract_dict()
|
||||
def image_processor_dict(self):
|
||||
return self.image_processor_tester.prepare_image_processor_dict()
|
||||
|
||||
def test_feat_extract_properties(self):
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
self.assertTrue(hasattr(feature_extractor, "do_resize"))
|
||||
self.assertTrue(hasattr(feature_extractor, "size_divisor"))
|
||||
self.assertTrue(hasattr(feature_extractor, "resample"))
|
||||
self.assertTrue(hasattr(feature_extractor, "do_rescale"))
|
||||
def test_image_processor_properties(self):
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
self.assertTrue(hasattr(image_processing, "do_resize"))
|
||||
self.assertTrue(hasattr(image_processing, "size_divisor"))
|
||||
self.assertTrue(hasattr(image_processing, "resample"))
|
||||
self.assertTrue(hasattr(image_processing, "do_rescale"))
|
||||
|
||||
def test_batch_feature(self):
|
||||
pass
|
||||
|
||||
def test_call_pil(self):
|
||||
# Initialize feature_extractor
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
# Initialize image_processing
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
# create random PIL images
|
||||
image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False)
|
||||
image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False)
|
||||
for image in image_inputs:
|
||||
self.assertIsInstance(image, Image.Image)
|
||||
|
||||
# Test not batched input (GLPNFeatureExtractor doesn't support batching)
|
||||
encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.feature_extract_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.feature_extract_tester.size_divisor == 0)
|
||||
# Test not batched input (GLPNImageProcessor doesn't support batching)
|
||||
encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0)
|
||||
|
||||
def test_call_numpy(self):
|
||||
# Initialize feature_extractor
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
# Initialize image_processing
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
# create random numpy tensors
|
||||
image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False, numpify=True)
|
||||
image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, numpify=True)
|
||||
for image in image_inputs:
|
||||
self.assertIsInstance(image, np.ndarray)
|
||||
|
||||
# Test not batched input (GLPNFeatureExtractor doesn't support batching)
|
||||
encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.feature_extract_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.feature_extract_tester.size_divisor == 0)
|
||||
# Test not batched input (GLPNImageProcessor doesn't support batching)
|
||||
encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0)
|
||||
|
||||
def test_call_pytorch(self):
|
||||
# Initialize feature_extractor
|
||||
feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
# Initialize image_processing
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
# create random PyTorch tensors
|
||||
image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False, torchify=True)
|
||||
image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, torchify=True)
|
||||
for image in image_inputs:
|
||||
self.assertIsInstance(image, torch.Tensor)
|
||||
|
||||
# Test not batched input (GLPNFeatureExtractor doesn't support batching)
|
||||
encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.feature_extract_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.feature_extract_tester.size_divisor == 0)
|
||||
# Test not batched input (GLPNImageProcessor doesn't support batching)
|
||||
encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
|
||||
self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0)
|
||||
self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0)
|
||||
|
||||
Reference in New Issue
Block a user