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:
@@ -25,7 +25,7 @@ from datasets import load_dataset
|
||||
from transformers.testing_utils import require_torch, require_vision, slow
|
||||
from transformers.utils import is_torch_available, is_vision_available
|
||||
|
||||
from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin
|
||||
from ...test_image_processing_common import ImageProcessingSavingTestMixin
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
@@ -34,10 +34,10 @@ if is_torch_available():
|
||||
if is_vision_available():
|
||||
from PIL import Image
|
||||
|
||||
from transformers import ImageGPTFeatureExtractor
|
||||
from transformers import ImageGPTImageProcessor
|
||||
|
||||
|
||||
class ImageGPTFeatureExtractionTester(unittest.TestCase):
|
||||
class ImageGPTImageProcessingTester(unittest.TestCase):
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
@@ -61,7 +61,7 @@ class ImageGPTFeatureExtractionTester(unittest.TestCase):
|
||||
self.size = size
|
||||
self.do_normalize = do_normalize
|
||||
|
||||
def prepare_feat_extract_dict(self):
|
||||
def prepare_image_processor_dict(self):
|
||||
return {
|
||||
# here we create 2 clusters for the sake of simplicity
|
||||
"clusters": np.asarray(
|
||||
@@ -78,68 +78,68 @@ class ImageGPTFeatureExtractionTester(unittest.TestCase):
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
class ImageGPTFeatureExtractionTest(FeatureExtractionSavingTestMixin, unittest.TestCase):
|
||||
class ImageGPTImageProcessingTest(ImageProcessingSavingTestMixin, unittest.TestCase):
|
||||
|
||||
feature_extraction_class = ImageGPTFeatureExtractor if is_vision_available() else None
|
||||
image_processing_class = ImageGPTImageProcessor if is_vision_available() else None
|
||||
|
||||
def setUp(self):
|
||||
self.feature_extract_tester = ImageGPTFeatureExtractionTester(self)
|
||||
self.image_processor_tester = ImageGPTImageProcessingTester(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, "clusters"))
|
||||
self.assertTrue(hasattr(feature_extractor, "do_resize"))
|
||||
self.assertTrue(hasattr(feature_extractor, "size"))
|
||||
self.assertTrue(hasattr(feature_extractor, "do_normalize"))
|
||||
def test_image_processor_properties(self):
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
self.assertTrue(hasattr(image_processing, "clusters"))
|
||||
self.assertTrue(hasattr(image_processing, "do_resize"))
|
||||
self.assertTrue(hasattr(image_processing, "size"))
|
||||
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
||||
|
||||
def test_feat_extract_from_dict_with_kwargs(self):
|
||||
feature_extractor = self.feature_extraction_class.from_dict(self.feat_extract_dict)
|
||||
self.assertEqual(feature_extractor.size, {"height": 18, "width": 18})
|
||||
def test_image_processor_from_dict_with_kwargs(self):
|
||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
|
||||
self.assertEqual(image_processor.size, {"height": 18, "width": 18})
|
||||
|
||||
feature_extractor = self.feature_extraction_class.from_dict(self.feat_extract_dict, size=42)
|
||||
self.assertEqual(feature_extractor.size, {"height": 42, "width": 42})
|
||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=42)
|
||||
self.assertEqual(image_processor.size, {"height": 42, "width": 42})
|
||||
|
||||
def test_feat_extract_to_json_string(self):
|
||||
feat_extract = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
obj = json.loads(feat_extract.to_json_string())
|
||||
for key, value in self.feat_extract_dict.items():
|
||||
def test_image_processor_to_json_string(self):
|
||||
image_processor = self.image_processing_class(**self.image_processor_dict)
|
||||
obj = json.loads(image_processor.to_json_string())
|
||||
for key, value in self.image_processor_dict.items():
|
||||
if key == "clusters":
|
||||
self.assertTrue(np.array_equal(value, obj[key]))
|
||||
else:
|
||||
self.assertEqual(obj[key], value)
|
||||
|
||||
def test_feat_extract_to_json_file(self):
|
||||
feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
def test_image_processor_to_json_file(self):
|
||||
image_processor_first = self.image_processing_class(**self.image_processor_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
json_file_path = os.path.join(tmpdirname, "feat_extract.json")
|
||||
feat_extract_first.to_json_file(json_file_path)
|
||||
feat_extract_second = self.feature_extraction_class.from_json_file(json_file_path).to_dict()
|
||||
json_file_path = os.path.join(tmpdirname, "image_processor.json")
|
||||
image_processor_first.to_json_file(json_file_path)
|
||||
image_processor_second = self.image_processing_class.from_json_file(json_file_path).to_dict()
|
||||
|
||||
feat_extract_first = feat_extract_first.to_dict()
|
||||
for key, value in feat_extract_first.items():
|
||||
image_processor_first = image_processor_first.to_dict()
|
||||
for key, value in image_processor_first.items():
|
||||
if key == "clusters":
|
||||
self.assertTrue(np.array_equal(value, feat_extract_second[key]))
|
||||
self.assertTrue(np.array_equal(value, image_processor_second[key]))
|
||||
else:
|
||||
self.assertEqual(feat_extract_first[key], value)
|
||||
self.assertEqual(image_processor_first[key], value)
|
||||
|
||||
def test_feat_extract_from_and_save_pretrained(self):
|
||||
feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)
|
||||
def test_image_processor_from_and_save_pretrained(self):
|
||||
image_processor_first = self.image_processing_class(**self.image_processor_dict)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
feat_extract_first.save_pretrained(tmpdirname)
|
||||
feat_extract_second = self.feature_extraction_class.from_pretrained(tmpdirname).to_dict()
|
||||
image_processor_first.save_pretrained(tmpdirname)
|
||||
image_processor_second = self.image_processing_class.from_pretrained(tmpdirname).to_dict()
|
||||
|
||||
feat_extract_first = feat_extract_first.to_dict()
|
||||
for key, value in feat_extract_first.items():
|
||||
image_processor_first = image_processor_first.to_dict()
|
||||
for key, value in image_processor_first.items():
|
||||
if key == "clusters":
|
||||
self.assertTrue(np.array_equal(value, feat_extract_second[key]))
|
||||
self.assertTrue(np.array_equal(value, image_processor_second[key]))
|
||||
else:
|
||||
self.assertEqual(feat_extract_first[key], value)
|
||||
self.assertEqual(image_processor_first[key], value)
|
||||
|
||||
@unittest.skip("ImageGPT requires clusters at initialization")
|
||||
def test_init_without_params(self):
|
||||
@@ -159,15 +159,15 @@ def prepare_images():
|
||||
|
||||
@require_vision
|
||||
@require_torch
|
||||
class ImageGPTFeatureExtractorIntegrationTest(unittest.TestCase):
|
||||
class ImageGPTImageProcessorIntegrationTest(unittest.TestCase):
|
||||
@slow
|
||||
def test_image(self):
|
||||
feature_extractor = ImageGPTFeatureExtractor.from_pretrained("openai/imagegpt-small")
|
||||
image_processing = ImageGPTImageProcessor.from_pretrained("openai/imagegpt-small")
|
||||
|
||||
images = prepare_images()
|
||||
|
||||
# test non-batched
|
||||
encoding = feature_extractor(images[0], return_tensors="pt")
|
||||
encoding = image_processing(images[0], return_tensors="pt")
|
||||
|
||||
self.assertIsInstance(encoding.input_ids, torch.LongTensor)
|
||||
self.assertEqual(encoding.input_ids.shape, (1, 1024))
|
||||
@@ -176,7 +176,7 @@ class ImageGPTFeatureExtractorIntegrationTest(unittest.TestCase):
|
||||
self.assertEqual(encoding.input_ids[0, :3].tolist(), expected_slice)
|
||||
|
||||
# test batched
|
||||
encoding = feature_extractor(images, return_tensors="pt")
|
||||
encoding = image_processing(images, return_tensors="pt")
|
||||
|
||||
self.assertIsInstance(encoding.input_ids, torch.LongTensor)
|
||||
self.assertEqual(encoding.input_ids.shape, (2, 1024))
|
||||
|
||||
Reference in New Issue
Block a user