Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion tests/utils/test_image_processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from huggingface_hub import HfFolder
from requests.exceptions import HTTPError

from transformers import AutoImageProcessor, ViTImageProcessor
from transformers import AutoImageProcessor, ViTImageProcessor, ViTImageProcessorFast
from transformers.image_processing_utils import get_size_dict
from transformers.testing_utils import TOKEN, TemporaryHubRepo, get_tests_dir, is_staging_test

Expand All @@ -45,9 +45,12 @@ def test_cached_files_are_used_when_internet_is_down(self):

# Download this model to make sure it's in the cache.
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
_ = ViTImageProcessorFast.from_pretrained("hf-internal-testing/tiny-random-vit")

# Under the mock environment we get a 500 error when trying to reach the model.
with mock.patch("requests.Session.request", return_value=response_mock) as mock_head:
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
_ = ViTImageProcessorFast.from_pretrained("hf-internal-testing/tiny-random-vit")
# This check we did call the fake head request
mock_head.assert_called()

Expand All @@ -62,6 +65,16 @@ def test_image_processor_from_pretrained_subfolder(self):

self.assertIsNotNone(config)

def test_vit_processors_compatibility(self):
processor = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit")
processor_fast = ViTImageProcessorFast.from_pretrained("hf-internal-testing/tiny-random-vit")

# Check that both processors have similar configurations
self.assertEqual(processor.size, processor_fast.size)
self.assertEqual(processor.do_resize, processor_fast.do_resize)
self.assertEqual(processor.do_rescale, processor_fast.do_rescale)
self.assertEqual(processor.do_normalize, processor_fast.do_normalize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have extensive tests for this already, no need to add here



@is_staging_test
class ImageProcessorPushToHubTester(unittest.TestCase):
Expand All @@ -79,6 +92,15 @@ def test_push_to_hub(self):
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_fast(self):
with TemporaryHubRepo(token=self._token) as tmp_repo:
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)

new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_via_save_pretrained(self):
with TemporaryHubRepo(token=self._token) as tmp_repo:
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
Expand All @@ -90,6 +112,17 @@ def test_push_to_hub_via_save_pretrained(self):
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_via_save_pretrained_fast(self):
with TemporaryHubRepo(token=self._token) as tmp_repo:
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
# Push to hub via save_pretrained
with tempfile.TemporaryDirectory() as tmp_dir:
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)

new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_in_organization(self):
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
Expand All @@ -99,6 +132,15 @@ def test_push_to_hub_in_organization(self):
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_in_organization_fast(self):
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token)

new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_in_organization_via_save_pretrained(self):
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
Expand All @@ -110,6 +152,17 @@ def test_push_to_hub_in_organization_via_save_pretrained(self):
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_in_organization_via_save_pretrained_fast(self):
with TemporaryHubRepo(namespace="valid_org", token=self._token) as tmp_repo:
image_processor = ViTImageProcessorFast.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR)
# Push to hub via save_pretrained
with tempfile.TemporaryDirectory() as tmp_dir:
image_processor.save_pretrained(tmp_dir, repo_id=tmp_repo.repo_id, push_to_hub=True, token=self._token)

new_image_processor = ViTImageProcessorFast.from_pretrained(tmp_repo.repo_id)
for k, v in image_processor.__dict__.items():
self.assertEqual(v, getattr(new_image_processor, k))

def test_push_to_hub_dynamic_image_processor(self):
with TemporaryHubRepo(token=self._token) as tmp_repo:
CustomImageProcessor.register_for_auto_class()
Expand Down