Unverified Commit f53fe35b authored by amyeroberts's avatar amyeroberts Committed by GitHub
Browse files

Fast image processor (#28847)



* Draft fast image processors

* Draft working fast version

* py3.8 compatible cache

* Enable loading fast image processors through auto

* Tidy up; rescale behaviour based on input type

* Enable tests for fast image processors

* Smarter rescaling

* Don't default to Fast

* Safer imports

* Add necessary Pillow requirement

* Woops

* Add AutoImageProcessor test

* Fix up

* Fix test for imagegpt

* Fix test

* Review comments

* Add warning for TF and JAX input types

* Rearrange

* Return transforms

* NumpyToTensor transformation

* Rebase - include changes from upstream in ImageProcessingMixin

* Safe typing

* Fix up

* convert mean/std to tesnor to rescale

* Don't store transforms in state

* Fix up

* Update src/transformers/image_processing_utils_fast.py
Co-authored-by: default avatarArthur <48595927+ArthurZucker@users.noreply.github.com>

* Update src/transformers/models/auto/image_processing_auto.py
Co-authored-by: default avatarArthur <48595927+ArthurZucker@users.noreply.github.com>

* Update src/transformers/models/auto/image_processing_auto.py
Co-authored-by: default avatarArthur <48595927+ArthurZucker@users.noreply.github.com>

* Update src/transformers/models/auto/image_processing_auto.py
Co-authored-by: default avatarArthur <48595927+ArthurZucker@users.noreply.github.com>

* Warn if fast image processor available

* Update src/transformers/models/vit/image_processing_vit_fast.py

* Transpose incoming numpy images to be in CHW format

* Update mapping names based on packages, auto set fast to None

* Fix up

* Fix

* Add AutoImageProcessor.from_pretrained(checkpoint, use_fast=True) test

* Update src/transformers/models/vit/image_processing_vit_fast.py
Co-authored-by: default avatarPavel Iakubovskii <qubvel@gmail.com>

* Add equivalence and speed tests

* Fix up

---------
Co-authored-by: default avatarArthur <48595927+ArthurZucker@users.noreply.github.com>
Co-authored-by: default avatarPavel Iakubovskii <qubvel@gmail.com>
parent edc1dffd
...@@ -131,6 +131,7 @@ class ConditionalDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcess ...@@ -131,6 +131,7 @@ class ConditionalDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcess
image_processing_class = ConditionalDetrImageProcessor if is_vision_available() else None image_processing_class = ConditionalDetrImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = ConditionalDetrImageProcessingTester(self) self.image_processor_tester = ConditionalDetrImageProcessingTester(self)
@property @property
......
...@@ -87,6 +87,7 @@ class ConvNextImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -87,6 +87,7 @@ class ConvNextImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = ConvNextImageProcessor if is_vision_available() else None image_processing_class = ConvNextImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = ConvNextImageProcessingTester(self) self.image_processor_tester = ConvNextImageProcessingTester(self)
@property @property
......
...@@ -131,6 +131,7 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi ...@@ -131,6 +131,7 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
image_processing_class = DeformableDetrImageProcessor if is_vision_available() else None image_processing_class = DeformableDetrImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = DeformableDetrImageProcessingTester(self) self.image_processor_tester = DeformableDetrImageProcessingTester(self)
@property @property
......
...@@ -93,6 +93,7 @@ class DeiTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -93,6 +93,7 @@ class DeiTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
test_cast_dtype = True test_cast_dtype = True
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = DeiTImageProcessingTester(self) self.image_processor_tester = DeiTImageProcessingTester(self)
@property @property
......
...@@ -130,6 +130,7 @@ class DetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMixi ...@@ -130,6 +130,7 @@ class DetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMixi
image_processing_class = DetrImageProcessor if is_vision_available() else None image_processing_class = DetrImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = DetrImageProcessingTester(self) self.image_processor_tester = DetrImageProcessingTester(self)
@property @property
......
...@@ -99,6 +99,7 @@ class DonutImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -99,6 +99,7 @@ class DonutImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = DonutImageProcessor if is_vision_available() else None image_processing_class = DonutImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = DonutImageProcessingTester(self) self.image_processor_tester = DonutImageProcessingTester(self)
@property @property
......
...@@ -86,6 +86,7 @@ class DPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -86,6 +86,7 @@ class DPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = DPTImageProcessor if is_vision_available() else None image_processing_class = DPTImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = DPTImageProcessingTester(self) self.image_processor_tester = DPTImageProcessingTester(self)
@property @property
......
...@@ -86,6 +86,7 @@ class EfficientNetImageProcessorTest(ImageProcessingTestMixin, unittest.TestCase ...@@ -86,6 +86,7 @@ class EfficientNetImageProcessorTest(ImageProcessingTestMixin, unittest.TestCase
image_processing_class = EfficientNetImageProcessor if is_vision_available() else None image_processing_class = EfficientNetImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = EfficientNetImageProcessorTester(self) self.image_processor_tester = EfficientNetImageProcessorTester(self)
@property @property
......
...@@ -175,6 +175,7 @@ class FlavaImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -175,6 +175,7 @@ class FlavaImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
maxDiff = None maxDiff = None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = FlavaImageProcessingTester(self) self.image_processor_tester = FlavaImageProcessingTester(self)
@property @property
......
...@@ -93,6 +93,7 @@ class GLPNImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -93,6 +93,7 @@ class GLPNImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = GLPNImageProcessor if is_vision_available() else None image_processing_class = GLPNImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = GLPNImageProcessingTester(self) self.image_processor_tester = GLPNImageProcessingTester(self)
@property @property
......
...@@ -146,6 +146,7 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin ...@@ -146,6 +146,7 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
image_processing_class = GroundingDinoImageProcessor if is_vision_available() else None image_processing_class = GroundingDinoImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = GroundingDinoImageProcessingTester(self) self.image_processor_tester = GroundingDinoImageProcessingTester(self)
@property @property
......
...@@ -127,6 +127,7 @@ class IdeficsImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -127,6 +127,7 @@ class IdeficsImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = IdeficsImageProcessor if is_vision_available() else None image_processing_class = IdeficsImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = IdeficsImageProcessingTester(self) self.image_processor_tester = IdeficsImageProcessingTester(self)
@property @property
......
...@@ -185,6 +185,7 @@ class Idefics2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -185,6 +185,7 @@ class Idefics2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = Idefics2ImageProcessor if is_vision_available() else None image_processing_class = Idefics2ImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = Idefics2ImageProcessingTester(self) self.image_processor_tester = Idefics2ImageProcessingTester(self)
@property @property
......
...@@ -22,7 +22,8 @@ import unittest ...@@ -22,7 +22,8 @@ import unittest
import numpy as np import numpy as np
from datasets import load_dataset from datasets import load_dataset
from transformers.testing_utils import require_torch, require_vision, slow from transformers import AutoImageProcessor
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available from transformers.utils import is_torch_available, is_vision_available
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
...@@ -96,6 +97,7 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -96,6 +97,7 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = ImageGPTImageProcessor if is_vision_available() else None image_processing_class = ImageGPTImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = ImageGPTImageProcessingTester(self) self.image_processor_tester = ImageGPTImageProcessingTester(self)
@property @property
...@@ -141,6 +143,7 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -141,6 +143,7 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
self.assertEqual(image_processor_first[key], value) self.assertEqual(image_processor_first[key], value)
def test_image_processor_from_and_save_pretrained(self): def test_image_processor_from_and_save_pretrained(self):
for image_processing_class in self.image_processor_list:
image_processor_first = self.image_processing_class(**self.image_processor_dict) image_processor_first = self.image_processing_class(**self.image_processor_dict)
with tempfile.TemporaryDirectory() as tmpdirname: with tempfile.TemporaryDirectory() as tmpdirname:
...@@ -154,6 +157,25 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -154,6 +157,25 @@ class ImageGPTImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
else: else:
self.assertEqual(image_processor_first[key], value) self.assertEqual(image_processor_first[key], value)
def test_image_processor_save_load_with_autoimageprocessor(self):
for image_processing_class in self.image_processor_list:
image_processor_first = image_processing_class(**self.image_processor_dict)
with tempfile.TemporaryDirectory() as tmpdirname:
saved_file = image_processor_first.save_pretrained(tmpdirname)[0]
check_json_file_has_correct_format(saved_file)
image_processor_second = AutoImageProcessor.from_pretrained(tmpdirname)
image_processor_first = image_processor_first.to_dict()
image_processor_second = image_processor_second.to_dict()
for key, value in image_processor_first.items():
if key == "clusters":
self.assertTrue(np.array_equal(value, image_processor_second[key]))
else:
self.assertEqual(image_processor_first[key], value)
@unittest.skip("ImageGPT requires clusters at initialization") @unittest.skip("ImageGPT requires clusters at initialization")
def test_init_without_params(self): def test_init_without_params(self):
pass pass
......
...@@ -76,6 +76,7 @@ class LayoutLMv2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase) ...@@ -76,6 +76,7 @@ class LayoutLMv2ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase)
image_processing_class = LayoutLMv2ImageProcessor if is_pytesseract_available() else None image_processing_class = LayoutLMv2ImageProcessor if is_pytesseract_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = LayoutLMv2ImageProcessingTester(self) self.image_processor_tester = LayoutLMv2ImageProcessingTester(self)
@property @property
......
...@@ -76,6 +76,7 @@ class LayoutLMv3ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase) ...@@ -76,6 +76,7 @@ class LayoutLMv3ImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase)
image_processing_class = LayoutLMv3ImageProcessor if is_pytesseract_available() else None image_processing_class = LayoutLMv3ImageProcessor if is_pytesseract_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = LayoutLMv3ImageProcessingTester(self) self.image_processor_tester = LayoutLMv3ImageProcessingTester(self)
@property @property
......
...@@ -91,6 +91,7 @@ class LevitImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -91,6 +91,7 @@ class LevitImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
image_processing_class = LevitImageProcessor if is_vision_available() else None image_processing_class = LevitImageProcessor if is_vision_available() else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = LevitImageProcessingTester(self) self.image_processor_tester = LevitImageProcessingTester(self)
@property @property
......
...@@ -105,6 +105,7 @@ class LlavaNextImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase): ...@@ -105,6 +105,7 @@ class LlavaNextImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
# Copied from tests.models.clip.test_image_processing_clip.CLIPImageProcessingTest.setUp with CLIP->LlavaNext # Copied from tests.models.clip.test_image_processing_clip.CLIPImageProcessingTest.setUp with CLIP->LlavaNext
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = LlavaNextImageProcessingTester(self) self.image_processor_tester = LlavaNextImageProcessingTester(self)
@property @property
......
...@@ -149,6 +149,7 @@ class Mask2FormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase ...@@ -149,6 +149,7 @@ class Mask2FormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase
image_processing_class = Mask2FormerImageProcessor if (is_vision_available() and is_torch_available()) else None image_processing_class = Mask2FormerImageProcessor if (is_vision_available() and is_torch_available()) else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = Mask2FormerImageProcessingTester(self) self.image_processor_tester = Mask2FormerImageProcessingTester(self)
@property @property
......
...@@ -149,6 +149,7 @@ class MaskFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase) ...@@ -149,6 +149,7 @@ class MaskFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase)
image_processing_class = MaskFormerImageProcessor if (is_vision_available() and is_torch_available()) else None image_processing_class = MaskFormerImageProcessor if (is_vision_available() and is_torch_available()) else None
def setUp(self): def setUp(self):
super().setUp()
self.image_processor_tester = MaskFormerImageProcessingTester(self) self.image_processor_tester = MaskFormerImageProcessingTester(self)
@property @property
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment