|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
import tempfile |
|
import unittest |
|
import unittest.mock as mock |
|
from pathlib import Path |
|
|
|
from huggingface_hub import HfFolder |
|
from requests.exceptions import HTTPError |
|
|
|
from transformers import AutoImageProcessor, ViTImageProcessor |
|
from transformers.image_processing_utils import get_size_dict |
|
from transformers.testing_utils import TOKEN, TemporaryHubRepo, get_tests_dir, is_staging_test |
|
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent.parent / "utils")) |
|
|
|
from test_module.custom_image_processing import CustomImageProcessor |
|
|
|
|
|
SAMPLE_IMAGE_PROCESSING_CONFIG_DIR = get_tests_dir("fixtures") |
|
|
|
|
|
class ImageProcessorUtilTester(unittest.TestCase): |
|
def test_cached_files_are_used_when_internet_is_down(self): |
|
|
|
response_mock = mock.Mock() |
|
response_mock.status_code = 500 |
|
response_mock.headers = {} |
|
response_mock.raise_for_status.side_effect = HTTPError |
|
response_mock.json.return_value = {} |
|
|
|
|
|
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit") |
|
|
|
with mock.patch("requests.Session.request", return_value=response_mock) as mock_head: |
|
_ = ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit") |
|
|
|
mock_head.assert_called() |
|
|
|
def test_image_processor_from_pretrained_subfolder(self): |
|
with self.assertRaises(OSError): |
|
|
|
_ = AutoImageProcessor.from_pretrained("hf-internal-testing/stable-diffusion-all-variants") |
|
|
|
config = AutoImageProcessor.from_pretrained( |
|
"hf-internal-testing/stable-diffusion-all-variants", subfolder="feature_extractor" |
|
) |
|
|
|
self.assertIsNotNone(config) |
|
|
|
|
|
@is_staging_test |
|
class ImageProcessorPushToHubTester(unittest.TestCase): |
|
@classmethod |
|
def setUpClass(cls): |
|
cls._token = TOKEN |
|
HfFolder.save_token(TOKEN) |
|
|
|
def test_push_to_hub(self): |
|
with TemporaryHubRepo(token=self._token) as tmp_repo: |
|
image_processor = ViTImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR) |
|
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token) |
|
|
|
new_image_processor = ViTImageProcessor.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) |
|
|
|
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 = ViTImageProcessor.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) |
|
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token) |
|
|
|
new_image_processor = ViTImageProcessor.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) |
|
|
|
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 = ViTImageProcessor.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() |
|
image_processor = CustomImageProcessor.from_pretrained(SAMPLE_IMAGE_PROCESSING_CONFIG_DIR) |
|
|
|
image_processor.push_to_hub(tmp_repo.repo_id, token=self._token) |
|
|
|
|
|
self.assertDictEqual( |
|
image_processor.auto_map, |
|
{"AutoImageProcessor": "custom_image_processing.CustomImageProcessor"}, |
|
) |
|
|
|
new_image_processor = AutoImageProcessor.from_pretrained(tmp_repo.repo_id, trust_remote_code=True) |
|
|
|
self.assertEqual(new_image_processor.__class__.__name__, "CustomImageProcessor") |
|
|
|
|
|
class ImageProcessingUtilsTester(unittest.TestCase): |
|
def test_get_size_dict(self): |
|
|
|
inputs = {"wrong_key": 224} |
|
with self.assertRaises(ValueError): |
|
get_size_dict(inputs) |
|
|
|
inputs = {"height": 224} |
|
with self.assertRaises(ValueError): |
|
get_size_dict(inputs) |
|
|
|
inputs = {"width": 224, "shortest_edge": 224} |
|
with self.assertRaises(ValueError): |
|
get_size_dict(inputs) |
|
|
|
|
|
inputs = {"height": 224, "width": 224} |
|
outputs = get_size_dict(inputs) |
|
self.assertEqual(outputs, inputs) |
|
|
|
inputs = {"shortest_edge": 224} |
|
outputs = get_size_dict(inputs) |
|
self.assertEqual(outputs, {"shortest_edge": 224}) |
|
|
|
inputs = {"longest_edge": 224, "shortest_edge": 224} |
|
outputs = get_size_dict(inputs) |
|
self.assertEqual(outputs, {"longest_edge": 224, "shortest_edge": 224}) |
|
|
|
|
|
outputs = get_size_dict(224) |
|
self.assertEqual(outputs, {"height": 224, "width": 224}) |
|
|
|
|
|
outputs = get_size_dict(224, default_to_square=False) |
|
self.assertEqual(outputs, {"shortest_edge": 224}) |
|
|
|
|
|
outputs = get_size_dict((150, 200)) |
|
self.assertEqual(outputs, {"height": 150, "width": 200}) |
|
|
|
|
|
outputs = get_size_dict((150, 200), height_width_order=False) |
|
self.assertEqual(outputs, {"height": 200, "width": 150}) |
|
|
|
|
|
outputs = get_size_dict(224, max_size=256, default_to_square=False) |
|
self.assertEqual(outputs, {"shortest_edge": 224, "longest_edge": 256}) |
|
|
|
|
|
with self.assertRaises(ValueError): |
|
get_size_dict(224, max_size=256, default_to_square=True) |
|
|