Add ViTImageProcessorFast to tests (#31424)

* Add ViTImageProcessor to tests

* Correct data format

* Review comments
This commit is contained in:
amyeroberts
2024-06-25 13:36:58 +01:00
committed by GitHub
parent aab0829790
commit 0f67ba1d74
26 changed files with 230 additions and 87 deletions

View File

@@ -18,7 +18,9 @@ import json
import os
import pathlib
import tempfile
import time
import numpy as np
import requests
from transformers import AutoImageProcessor, BatchFeature
@@ -28,7 +30,6 @@ from transformers.utils import is_torch_available, is_vision_available
if is_torch_available():
import numpy as np
import torch
if is_vision_available():
@@ -72,6 +73,10 @@ def prepare_image_inputs(
if torchify:
image_inputs = [torch.from_numpy(image) for image in image_inputs]
if numpify:
# Numpy images are typically in channels last format
image_inputs = [image.transpose(1, 2, 0) for image in image_inputs]
return image_inputs
@@ -167,33 +172,28 @@ class ImageProcessingTestMixin:
encoding_slow = image_processor_slow(dummy_image, return_tensors="pt")
encoding_fast = image_processor_fast(dummy_image, return_tensors="pt")
self.assertTrue(torch.allclose(encoding_slow.pixel_values, encoding_fast.pixel_values, atol=1e-3))
self.assertTrue(torch.allclose(encoding_slow.pixel_values, encoding_fast.pixel_values, atol=1e-2))
@require_vision
@require_torch
def test_fast_is_faster_than_slow(self):
import time
def measure_time(self, image_processor, dummy_image):
start = time.time()
_ = image_processor(dummy_image, return_tensors="pt")
return time.time() - start
dummy_image = Image.open(
requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw
)
if not self.test_slow_image_processor or not self.test_fast_image_processor:
self.skipTest("Skipping speed test")
if self.image_processing_class is None or self.fast_image_processing_class is None:
self.skipTest("Skipping speed test as one of the image processors is not defined")
image_processor_slow = self.image_processing_class(**self.image_processor_dict)
image_processor_fast = self.fast_image_processing_class(**self.image_processor_dict)
def measure_time(image_processor, image):
start = time.time()
_ = image_processor(image, return_tensors="pt")
return time.time() - start
slow_time = self.measure_time(image_processor_slow, dummy_image)
fast_time = self.measure_time(image_processor_fast, dummy_image)
dummy_images = torch.randint(0, 255, (4, 3, 224, 224), dtype=torch.uint8)
image_processor_slow = self.image_processing_class(**self.image_processor_dict)
image_processor_fast = self.fast_image_processing_class()
fast_time = measure_time(image_processor_fast, dummy_images)
slow_time = measure_time(image_processor_slow, dummy_images)
self.assertLessEqual(fast_time, slow_time)
@@ -238,6 +238,52 @@ class ImageProcessingTestMixin:
self.assertEqual(image_processor_second.to_dict(), image_processor_first.to_dict())
def test_save_load_fast_slow(self):
"Test that we can load a fast image processor from a slow one and vice-versa."
if self.image_processing_class is None or self.fast_image_processing_class is None:
self.skipTest("Skipping slow/fast save/load test as one of the image processors is not defined")
image_processor_dict = self.image_processor_tester.prepare_image_processor_dict()
image_processor_slow_0 = self.image_processing_class(**image_processor_dict)
# Load fast image processor from slow one
with tempfile.TemporaryDirectory() as tmpdirname:
image_processor_slow_0.save_pretrained(tmpdirname)
image_processor_fast_0 = self.fast_image_processing_class.from_pretrained(tmpdirname)
image_processor_fast_1 = self.fast_image_processing_class(**image_processor_dict)
# Load slow image processor from fast one
with tempfile.TemporaryDirectory() as tmpdirname:
image_processor_fast_1.save_pretrained(tmpdirname)
image_processor_slow_1 = self.image_processing_class.from_pretrained(tmpdirname)
self.assertEqual(image_processor_slow_0.to_dict(), image_processor_slow_1.to_dict())
self.assertEqual(image_processor_fast_0.to_dict(), image_processor_fast_1.to_dict())
def test_save_load_fast_slow_auto(self):
"Test that we can load a fast image processor from a slow one and vice-versa using AutoImageProcessor."
if self.image_processing_class is None or self.fast_image_processing_class is None:
self.skipTest("Skipping slow/fast save/load test as one of the image processors is not defined")
image_processor_dict = self.image_processor_tester.prepare_image_processor_dict()
image_processor_slow_0 = self.image_processing_class(**image_processor_dict)
# Load fast image processor from slow one
with tempfile.TemporaryDirectory() as tmpdirname:
image_processor_slow_0.save_pretrained(tmpdirname)
image_processor_fast_0 = AutoImageProcessor.from_pretrained(tmpdirname, use_fast=True)
image_processor_fast_1 = self.fast_image_processing_class(**image_processor_dict)
# Load slow image processor from fast one
with tempfile.TemporaryDirectory() as tmpdirname:
image_processor_fast_1.save_pretrained(tmpdirname)
image_processor_slow_1 = AutoImageProcessor.from_pretrained(tmpdirname, use_fast=False)
self.assertEqual(image_processor_slow_0.to_dict(), image_processor_slow_1.to_dict())
self.assertEqual(image_processor_fast_0.to_dict(), image_processor_fast_1.to_dict())
def test_init_without_params(self):
for image_processing_class in self.image_processor_list:
image_processor = image_processing_class()
@@ -358,7 +404,7 @@ class ImageProcessingTestMixin:
encoded_images = image_processor(
image_inputs[0],
return_tensors="pt",
input_data_format="channels_first",
input_data_format="channels_last",
image_mean=0,
image_std=1,
).pixel_values
@@ -369,7 +415,7 @@ class ImageProcessingTestMixin:
encoded_images = image_processor(
image_inputs,
return_tensors="pt",
input_data_format="channels_first",
input_data_format="channels_last",
image_mean=0,
image_std=1,
).pixel_values