[WIP] Add OneformerFastImageProcessor (#38343)
* [WIP] OneformerFastImageProcessor * update init * Fully working oneformer image processor fast * change Nearest to Neares exact interpolation where needed * fix doc --------- Co-authored-by: yonigozlan <yoni.gozlan@huggingface.co> Co-authored-by: Yoni Gozlan <74535834+yonigozlan@users.noreply.github.com>
This commit is contained in:
@@ -69,7 +69,14 @@ The resource should ideally demonstrate something new instead of duplicating an
|
|||||||
|
|
||||||
[[autodoc]] OneFormerImageProcessor
|
[[autodoc]] OneFormerImageProcessor
|
||||||
- preprocess
|
- preprocess
|
||||||
- encode_inputs
|
- post_process_semantic_segmentation
|
||||||
|
- post_process_instance_segmentation
|
||||||
|
- post_process_panoptic_segmentation
|
||||||
|
|
||||||
|
## OneFormerImageProcessorFast
|
||||||
|
|
||||||
|
[[autodoc]] OneFormerImageProcessorFast
|
||||||
|
- preprocess
|
||||||
- post_process_semantic_segmentation
|
- post_process_semantic_segmentation
|
||||||
- post_process_instance_segmentation
|
- post_process_instance_segmentation
|
||||||
- post_process_panoptic_segmentation
|
- post_process_panoptic_segmentation
|
||||||
@@ -87,4 +94,3 @@ The resource should ideally demonstrate something new instead of duplicating an
|
|||||||
|
|
||||||
[[autodoc]] OneFormerForUniversalSegmentation
|
[[autodoc]] OneFormerForUniversalSegmentation
|
||||||
- forward
|
- forward
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ else:
|
|||||||
("mobilevitv2", ("MobileViTImageProcessor", "MobileViTImageProcessorFast")),
|
("mobilevitv2", ("MobileViTImageProcessor", "MobileViTImageProcessorFast")),
|
||||||
("nat", ("ViTImageProcessor", "ViTImageProcessorFast")),
|
("nat", ("ViTImageProcessor", "ViTImageProcessorFast")),
|
||||||
("nougat", ("NougatImageProcessor", "NougatImageProcessorFast")),
|
("nougat", ("NougatImageProcessor", "NougatImageProcessorFast")),
|
||||||
("oneformer", ("OneFormerImageProcessor",)),
|
("oneformer", ("OneFormerImageProcessor", "OneFormerImageProcessorFast")),
|
||||||
("owlv2", ("Owlv2ImageProcessor",)),
|
("owlv2", ("Owlv2ImageProcessor",)),
|
||||||
("owlvit", ("OwlViTImageProcessor", "OwlViTImageProcessorFast")),
|
("owlvit", ("OwlViTImageProcessor", "OwlViTImageProcessorFast")),
|
||||||
("paligemma", ("SiglipImageProcessor", "SiglipImageProcessorFast")),
|
("paligemma", ("SiglipImageProcessor", "SiglipImageProcessorFast")),
|
||||||
|
|||||||
@@ -454,10 +454,10 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -445,10 +445,10 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -466,10 +466,10 @@ class DetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -476,10 +476,10 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ from ...utils.import_utils import define_import_structure
|
|||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .configuration_oneformer import *
|
from .configuration_oneformer import *
|
||||||
from .image_processing_oneformer import *
|
from .image_processing_oneformer import *
|
||||||
|
from .image_processing_oneformer_fast import *
|
||||||
from .modeling_oneformer import *
|
from .modeling_oneformer import *
|
||||||
from .processing_oneformer import *
|
from .processing_oneformer import *
|
||||||
else:
|
else:
|
||||||
|
|||||||
1002
src/transformers/models/oneformer/image_processing_oneformer_fast.py
Normal file
1002
src/transformers/models/oneformer/image_processing_oneformer_fast.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -264,10 +264,10 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -493,10 +493,10 @@ class YolosImageProcessorFast(BaseImageProcessorFast):
|
|||||||
The target size of the image, as returned by the preprocessing `resize` step.
|
The target size of the image, as returned by the preprocessing `resize` step.
|
||||||
threshold (`float`, *optional*, defaults to 0.5):
|
threshold (`float`, *optional*, defaults to 0.5):
|
||||||
The threshold used to binarize the segmentation masks.
|
The threshold used to binarize the segmentation masks.
|
||||||
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST`):
|
resample (`InterpolationMode`, defaults to `InterpolationMode.NEAREST_EXACT`):
|
||||||
The resampling filter to use when resizing the masks.
|
The resampling filter to use when resizing the masks.
|
||||||
"""
|
"""
|
||||||
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST
|
interpolation = interpolation if interpolation is not None else F.InterpolationMode.NEAREST_EXACT
|
||||||
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
ratio_height, ratio_width = [target / orig for target, orig in zip(target_size, orig_size)]
|
||||||
|
|
||||||
new_annotation = {}
|
new_annotation = {}
|
||||||
|
|||||||
@@ -19,9 +19,10 @@ import tempfile
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from datasets import load_dataset
|
||||||
|
|
||||||
from transformers.testing_utils import require_torch, require_vision
|
from transformers.testing_utils import require_torch, require_vision
|
||||||
from transformers.utils import is_torch_available, is_vision_available
|
from transformers.utils import is_torch_available, is_torchvision_available, is_vision_available
|
||||||
|
|
||||||
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
|
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
|
||||||
|
|
||||||
@@ -31,6 +32,9 @@ if is_torch_available():
|
|||||||
|
|
||||||
if is_vision_available():
|
if is_vision_available():
|
||||||
from transformers import OneFormerImageProcessor
|
from transformers import OneFormerImageProcessor
|
||||||
|
|
||||||
|
if is_torchvision_available():
|
||||||
|
from transformers import OneFormerImageProcessorFast
|
||||||
from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle, prepare_metadata
|
from transformers.models.oneformer.image_processing_oneformer import binary_mask_to_rle, prepare_metadata
|
||||||
from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput
|
from transformers.models.oneformer.modeling_oneformer import OneFormerForUniversalSegmentationOutput
|
||||||
|
|
||||||
@@ -152,12 +156,24 @@ class OneFormerImageProcessorTester:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Copied from transformers.tests.models.beit.test_image_processing_beit.prepare_semantic_single_inputs
|
||||||
|
def prepare_semantic_single_inputs():
|
||||||
|
ds = load_dataset("hf-internal-testing/fixtures_ade20k", split="test")
|
||||||
|
example = ds[0]
|
||||||
|
return example["image"], example["map"]
|
||||||
|
|
||||||
|
|
||||||
|
# Copied from transformers.tests.models.beit.test_image_processing_beit.prepare_semantic_batch_inputs
|
||||||
|
def prepare_semantic_batch_inputs():
|
||||||
|
ds = load_dataset("hf-internal-testing/fixtures_ade20k", split="test")
|
||||||
|
return list(ds["image"][:2]), list(ds["map"][:2])
|
||||||
|
|
||||||
|
|
||||||
@require_torch
|
@require_torch
|
||||||
@require_vision
|
@require_vision
|
||||||
class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
||||||
image_processing_class = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None
|
image_processing_class = OneFormerImageProcessor if (is_vision_available() and is_torch_available()) else None
|
||||||
# only for test_image_processing_common.test_image_proc_to_json_string
|
fast_image_processing_class = OneFormerImageProcessorFast if is_torchvision_available() else None
|
||||||
image_processing_class = image_processing_class
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
@@ -168,23 +184,24 @@ class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
|||||||
return self.image_processor_tester.prepare_image_processor_dict()
|
return self.image_processor_tester.prepare_image_processor_dict()
|
||||||
|
|
||||||
def test_image_proc_properties(self):
|
def test_image_proc_properties(self):
|
||||||
image_processor = self.image_processing_class(**self.image_processor_dict)
|
for image_processing_class in self.image_processor_list:
|
||||||
self.assertTrue(hasattr(image_processor, "image_mean"))
|
image_processor = image_processing_class(**self.image_processor_dict)
|
||||||
self.assertTrue(hasattr(image_processor, "image_std"))
|
self.assertTrue(hasattr(image_processor, "image_mean"))
|
||||||
self.assertTrue(hasattr(image_processor, "do_normalize"))
|
self.assertTrue(hasattr(image_processor, "image_std"))
|
||||||
self.assertTrue(hasattr(image_processor, "do_resize"))
|
self.assertTrue(hasattr(image_processor, "do_normalize"))
|
||||||
self.assertTrue(hasattr(image_processor, "size"))
|
self.assertTrue(hasattr(image_processor, "do_resize"))
|
||||||
self.assertTrue(hasattr(image_processor, "ignore_index"))
|
self.assertTrue(hasattr(image_processor, "size"))
|
||||||
self.assertTrue(hasattr(image_processor, "class_info_file"))
|
self.assertTrue(hasattr(image_processor, "ignore_index"))
|
||||||
self.assertTrue(hasattr(image_processor, "num_text"))
|
self.assertTrue(hasattr(image_processor, "class_info_file"))
|
||||||
self.assertTrue(hasattr(image_processor, "repo_path"))
|
self.assertTrue(hasattr(image_processor, "num_text"))
|
||||||
self.assertTrue(hasattr(image_processor, "metadata"))
|
self.assertTrue(hasattr(image_processor, "repo_path"))
|
||||||
self.assertTrue(hasattr(image_processor, "do_reduce_labels"))
|
self.assertTrue(hasattr(image_processor, "metadata"))
|
||||||
|
self.assertTrue(hasattr(image_processor, "do_reduce_labels"))
|
||||||
|
|
||||||
def comm_get_image_processor_inputs(
|
def comm_get_image_processor_inputs(
|
||||||
self, with_segmentation_maps=False, is_instance_map=False, segmentation_type="np"
|
self, with_segmentation_maps=False, is_instance_map=False, segmentation_type="np", image_processing_class=None
|
||||||
):
|
):
|
||||||
image_processor = self.image_processing_class(**self.image_processor_dict)
|
image_processor = image_processing_class(**self.image_processor_dict)
|
||||||
# prepare image and target
|
# prepare image and target
|
||||||
num_labels = self.image_processor_tester.num_labels
|
num_labels = self.image_processor_tester.num_labels
|
||||||
annotations = None
|
annotations = None
|
||||||
@@ -218,21 +235,25 @@ class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
|||||||
|
|
||||||
def test_call_with_segmentation_maps(self):
|
def test_call_with_segmentation_maps(self):
|
||||||
def common(is_instance_map=False, segmentation_type=None):
|
def common(is_instance_map=False, segmentation_type=None):
|
||||||
inputs = self.comm_get_image_processor_inputs(
|
for image_processing_class in self.image_processor_list:
|
||||||
with_segmentation_maps=True, is_instance_map=is_instance_map, segmentation_type=segmentation_type
|
inputs = self.comm_get_image_processor_inputs(
|
||||||
)
|
with_segmentation_maps=True,
|
||||||
|
is_instance_map=is_instance_map,
|
||||||
|
segmentation_type=segmentation_type,
|
||||||
|
image_processing_class=image_processing_class,
|
||||||
|
)
|
||||||
|
|
||||||
mask_labels = inputs["mask_labels"]
|
mask_labels = inputs["mask_labels"]
|
||||||
class_labels = inputs["class_labels"]
|
class_labels = inputs["class_labels"]
|
||||||
pixel_values = inputs["pixel_values"]
|
pixel_values = inputs["pixel_values"]
|
||||||
text_inputs = inputs["text_inputs"]
|
text_inputs = inputs["text_inputs"]
|
||||||
|
|
||||||
# check the batch_size
|
# check the batch_size
|
||||||
for mask_label, class_label, text_input in zip(mask_labels, class_labels, text_inputs):
|
for mask_label, class_label, text_input in zip(mask_labels, class_labels, text_inputs):
|
||||||
self.assertEqual(mask_label.shape[0], class_label.shape[0])
|
self.assertEqual(mask_label.shape[0], class_label.shape[0])
|
||||||
# this ensure padding has happened
|
# this ensure padding has happened
|
||||||
self.assertEqual(mask_label.shape[1:], pixel_values.shape[2:])
|
self.assertEqual(mask_label.shape[1:], pixel_values.shape[2:])
|
||||||
self.assertEqual(len(text_input), self.image_processor_tester.num_text)
|
self.assertEqual(len(text_input), self.image_processor_tester.num_text)
|
||||||
|
|
||||||
common()
|
common()
|
||||||
common(is_instance_map=True)
|
common(is_instance_map=True)
|
||||||
@@ -251,86 +272,89 @@ class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
|||||||
self.assertEqual(rle[1], 45)
|
self.assertEqual(rle[1], 45)
|
||||||
|
|
||||||
def test_post_process_semantic_segmentation(self):
|
def test_post_process_semantic_segmentation(self):
|
||||||
fature_extractor = self.image_processing_class(
|
for image_processing_class in self.image_processor_list:
|
||||||
num_labels=self.image_processor_tester.num_classes,
|
fature_extractor = image_processing_class(
|
||||||
max_seq_length=77,
|
num_labels=self.image_processor_tester.num_classes,
|
||||||
task_seq_length=77,
|
max_seq_length=77,
|
||||||
class_info_file="ade20k_panoptic.json",
|
task_seq_length=77,
|
||||||
num_text=self.image_processor_tester.num_text,
|
class_info_file="ade20k_panoptic.json",
|
||||||
repo_path="shi-labs/oneformer_demo",
|
num_text=self.image_processor_tester.num_text,
|
||||||
)
|
repo_path="shi-labs/oneformer_demo",
|
||||||
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
)
|
||||||
|
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
||||||
|
|
||||||
segmentation = fature_extractor.post_process_semantic_segmentation(outputs)
|
segmentation = fature_extractor.post_process_semantic_segmentation(outputs)
|
||||||
|
|
||||||
self.assertEqual(len(segmentation), self.image_processor_tester.batch_size)
|
self.assertEqual(len(segmentation), self.image_processor_tester.batch_size)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
segmentation[0].shape,
|
segmentation[0].shape,
|
||||||
(
|
(
|
||||||
self.image_processor_tester.height,
|
self.image_processor_tester.height,
|
||||||
self.image_processor_tester.width,
|
self.image_processor_tester.width,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
target_sizes = [(1, 4) for i in range(self.image_processor_tester.batch_size)]
|
target_sizes = [(1, 4) for i in range(self.image_processor_tester.batch_size)]
|
||||||
segmentation = fature_extractor.post_process_semantic_segmentation(outputs, target_sizes=target_sizes)
|
segmentation = fature_extractor.post_process_semantic_segmentation(outputs, target_sizes=target_sizes)
|
||||||
|
|
||||||
self.assertEqual(segmentation[0].shape, target_sizes[0])
|
self.assertEqual(segmentation[0].shape, target_sizes[0])
|
||||||
|
|
||||||
def test_post_process_instance_segmentation(self):
|
def test_post_process_instance_segmentation(self):
|
||||||
image_processor = self.image_processing_class(
|
for image_processing_class in self.image_processor_list:
|
||||||
num_labels=self.image_processor_tester.num_classes,
|
image_processor = image_processing_class(
|
||||||
max_seq_length=77,
|
num_labels=self.image_processor_tester.num_classes,
|
||||||
task_seq_length=77,
|
max_seq_length=77,
|
||||||
class_info_file="ade20k_panoptic.json",
|
task_seq_length=77,
|
||||||
num_text=self.image_processor_tester.num_text,
|
class_info_file="ade20k_panoptic.json",
|
||||||
repo_path="shi-labs/oneformer_demo",
|
num_text=self.image_processor_tester.num_text,
|
||||||
)
|
repo_path="shi-labs/oneformer_demo",
|
||||||
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
|
||||||
segmentation = image_processor.post_process_instance_segmentation(outputs, threshold=0)
|
|
||||||
|
|
||||||
self.assertTrue(len(segmentation) == self.image_processor_tester.batch_size)
|
|
||||||
for el in segmentation:
|
|
||||||
self.assertTrue("segmentation" in el)
|
|
||||||
self.assertTrue("segments_info" in el)
|
|
||||||
self.assertEqual(type(el["segments_info"]), list)
|
|
||||||
self.assertEqual(
|
|
||||||
el["segmentation"].shape, (self.image_processor_tester.height, self.image_processor_tester.width)
|
|
||||||
)
|
)
|
||||||
|
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
||||||
|
segmentation = image_processor.post_process_instance_segmentation(outputs, threshold=0)
|
||||||
|
|
||||||
segmentation_with_opts = image_processor.post_process_instance_segmentation(
|
self.assertTrue(len(segmentation) == self.image_processor_tester.batch_size)
|
||||||
outputs,
|
for el in segmentation:
|
||||||
threshold=0,
|
self.assertTrue("segmentation" in el)
|
||||||
target_sizes=[(1, 4) for _ in range(self.image_processor_tester.batch_size)],
|
self.assertTrue("segments_info" in el)
|
||||||
task_type="panoptic",
|
self.assertEqual(type(el["segments_info"]), list)
|
||||||
)
|
self.assertEqual(
|
||||||
self.assertTrue(len(segmentation_with_opts) == self.image_processor_tester.batch_size)
|
el["segmentation"].shape, (self.image_processor_tester.height, self.image_processor_tester.width)
|
||||||
for el in segmentation_with_opts:
|
)
|
||||||
self.assertTrue("segmentation" in el)
|
|
||||||
self.assertTrue("segments_info" in el)
|
segmentation_with_opts = image_processor.post_process_instance_segmentation(
|
||||||
self.assertEqual(type(el["segments_info"]), list)
|
outputs,
|
||||||
self.assertEqual(el["segmentation"].shape, (1, 4))
|
threshold=0,
|
||||||
|
target_sizes=[(1, 4) for _ in range(self.image_processor_tester.batch_size)],
|
||||||
|
task_type="panoptic",
|
||||||
|
)
|
||||||
|
self.assertTrue(len(segmentation_with_opts) == self.image_processor_tester.batch_size)
|
||||||
|
for el in segmentation_with_opts:
|
||||||
|
self.assertTrue("segmentation" in el)
|
||||||
|
self.assertTrue("segments_info" in el)
|
||||||
|
self.assertEqual(type(el["segments_info"]), list)
|
||||||
|
self.assertEqual(el["segmentation"].shape, (1, 4))
|
||||||
|
|
||||||
def test_post_process_panoptic_segmentation(self):
|
def test_post_process_panoptic_segmentation(self):
|
||||||
image_processor = self.image_processing_class(
|
for image_processing_class in self.image_processor_list:
|
||||||
num_labels=self.image_processor_tester.num_classes,
|
image_processor = image_processing_class(
|
||||||
max_seq_length=77,
|
num_labels=self.image_processor_tester.num_classes,
|
||||||
task_seq_length=77,
|
max_seq_length=77,
|
||||||
class_info_file="ade20k_panoptic.json",
|
task_seq_length=77,
|
||||||
num_text=self.image_processor_tester.num_text,
|
class_info_file="ade20k_panoptic.json",
|
||||||
repo_path="shi-labs/oneformer_demo",
|
num_text=self.image_processor_tester.num_text,
|
||||||
)
|
repo_path="shi-labs/oneformer_demo",
|
||||||
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
|
||||||
segmentation = image_processor.post_process_panoptic_segmentation(outputs, threshold=0)
|
|
||||||
|
|
||||||
self.assertTrue(len(segmentation) == self.image_processor_tester.batch_size)
|
|
||||||
for el in segmentation:
|
|
||||||
self.assertTrue("segmentation" in el)
|
|
||||||
self.assertTrue("segments_info" in el)
|
|
||||||
self.assertEqual(type(el["segments_info"]), list)
|
|
||||||
self.assertEqual(
|
|
||||||
el["segmentation"].shape, (self.image_processor_tester.height, self.image_processor_tester.width)
|
|
||||||
)
|
)
|
||||||
|
outputs = self.image_processor_tester.get_fake_oneformer_outputs()
|
||||||
|
segmentation = image_processor.post_process_panoptic_segmentation(outputs, threshold=0)
|
||||||
|
|
||||||
|
self.assertTrue(len(segmentation) == self.image_processor_tester.batch_size)
|
||||||
|
for el in segmentation:
|
||||||
|
self.assertTrue("segmentation" in el)
|
||||||
|
self.assertTrue("segments_info" in el)
|
||||||
|
self.assertEqual(type(el["segments_info"]), list)
|
||||||
|
self.assertEqual(
|
||||||
|
el["segmentation"].shape, (self.image_processor_tester.height, self.image_processor_tester.width)
|
||||||
|
)
|
||||||
|
|
||||||
def test_can_load_with_local_metadata(self):
|
def test_can_load_with_local_metadata(self):
|
||||||
# Create a temporary json file
|
# Create a temporary json file
|
||||||
@@ -340,28 +364,91 @@ class OneFormerImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
|||||||
"2": {"isthing": 1, "name": "baz"},
|
"2": {"isthing": 1, "name": "baz"},
|
||||||
}
|
}
|
||||||
metadata = prepare_metadata(class_info)
|
metadata = prepare_metadata(class_info)
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
metadata_path = os.path.join(tmpdirname, "metadata.json")
|
||||||
|
with open(metadata_path, "w") as f:
|
||||||
|
json.dump(class_info, f)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
config_dict = self.image_processor_dict
|
||||||
metadata_path = os.path.join(tmpdirname, "metadata.json")
|
config_dict["class_info_file"] = metadata_path
|
||||||
with open(metadata_path, "w") as f:
|
config_dict["repo_path"] = tmpdirname
|
||||||
json.dump(class_info, f)
|
image_processor = image_processing_class(**config_dict)
|
||||||
|
|
||||||
config_dict = self.image_processor_dict
|
self.assertEqual(image_processor.metadata, metadata)
|
||||||
config_dict["class_info_file"] = metadata_path
|
|
||||||
config_dict["repo_path"] = tmpdirname
|
|
||||||
image_processor = self.image_processing_class(**config_dict)
|
|
||||||
|
|
||||||
self.assertEqual(image_processor.metadata, metadata)
|
|
||||||
|
|
||||||
def test_removed_deprecated_kwargs(self):
|
def test_removed_deprecated_kwargs(self):
|
||||||
image_processor_dict = dict(self.image_processor_dict)
|
image_processor_dict = dict(self.image_processor_dict)
|
||||||
image_processor_dict.pop("do_reduce_labels", None)
|
image_processor_dict.pop("do_reduce_labels", None)
|
||||||
image_processor_dict["reduce_labels"] = True
|
image_processor_dict["reduce_labels"] = True
|
||||||
|
# Only test for OneFormerImageProcessor
|
||||||
|
image_processing_class = self.image_processing_class
|
||||||
# test we are able to create the image processor with the deprecated kwargs
|
# test we are able to create the image processor with the deprecated kwargs
|
||||||
image_processor = self.image_processing_class(**image_processor_dict)
|
image_processor = image_processing_class(**image_processor_dict)
|
||||||
self.assertEqual(image_processor.do_reduce_labels, True)
|
self.assertEqual(image_processor.do_reduce_labels, True)
|
||||||
|
|
||||||
# test we still support reduce_labels with config
|
# test we still support reduce_labels with config
|
||||||
image_processor = self.image_processing_class.from_dict(image_processor_dict)
|
image_processor = image_processing_class.from_dict(image_processor_dict)
|
||||||
self.assertEqual(image_processor.do_reduce_labels, True)
|
self.assertEqual(image_processor.do_reduce_labels, True)
|
||||||
|
|
||||||
|
def test_slow_fast_equivalence(self):
|
||||||
|
if not self.test_slow_image_processor or not self.test_fast_image_processor:
|
||||||
|
self.skipTest(reason="Skipping slow/fast equivalence test")
|
||||||
|
|
||||||
|
if self.image_processing_class is None or self.fast_image_processing_class is None:
|
||||||
|
self.skipTest(reason="Skipping slow/fast equivalence test as one of the image processors is not defined")
|
||||||
|
|
||||||
|
dummy_image, dummy_map = prepare_semantic_single_inputs()
|
||||||
|
|
||||||
|
image_processor_slow = self.image_processing_class(**self.image_processor_dict)
|
||||||
|
image_processor_fast = self.fast_image_processing_class(**self.image_processor_dict)
|
||||||
|
|
||||||
|
image_encoding_slow = image_processor_slow(dummy_image, segmentation_maps=dummy_map, return_tensors="pt")
|
||||||
|
image_encoding_fast = image_processor_fast(dummy_image, segmentation_maps=dummy_map, return_tensors="pt")
|
||||||
|
self._assert_slow_fast_tensors_equivalence(image_encoding_slow.pixel_values, image_encoding_fast.pixel_values)
|
||||||
|
for mask_label_slow, mask_label_fast in zip(image_encoding_slow.mask_labels, image_encoding_fast.mask_labels):
|
||||||
|
self._assert_slow_fast_tensors_equivalence(mask_label_slow, mask_label_fast)
|
||||||
|
for class_label_slow, class_label_fast in zip(
|
||||||
|
image_encoding_slow.class_labels, image_encoding_fast.class_labels
|
||||||
|
):
|
||||||
|
self._assert_slow_fast_tensors_equivalence(class_label_slow.float(), class_label_fast.float())
|
||||||
|
self.assertEqual(image_encoding_slow.text_inputs, image_encoding_fast.text_inputs)
|
||||||
|
self.assertEqual(image_encoding_slow.task_inputs, image_encoding_fast.task_inputs)
|
||||||
|
|
||||||
|
def test_slow_fast_equivalence_batched(self):
|
||||||
|
if not self.test_slow_image_processor or not self.test_fast_image_processor:
|
||||||
|
self.skipTest(reason="Skipping slow/fast equivalence test")
|
||||||
|
|
||||||
|
if self.image_processing_class is None or self.fast_image_processing_class is None:
|
||||||
|
self.skipTest(reason="Skipping slow/fast equivalence test as one of the image processors is not defined")
|
||||||
|
|
||||||
|
if hasattr(self.image_processor_tester, "do_center_crop") and self.image_processor_tester.do_center_crop:
|
||||||
|
self.skipTest(
|
||||||
|
reason="Skipping as do_center_crop is True and center_crop functions are not equivalent for fast and slow processors"
|
||||||
|
)
|
||||||
|
|
||||||
|
dummy_images, dummy_maps = prepare_semantic_batch_inputs()
|
||||||
|
|
||||||
|
image_processor_slow = self.image_processing_class(**self.image_processor_dict)
|
||||||
|
image_processor_fast = self.fast_image_processing_class(**self.image_processor_dict)
|
||||||
|
|
||||||
|
encoding_slow = image_processor_slow(
|
||||||
|
dummy_images,
|
||||||
|
segmentation_maps=dummy_maps,
|
||||||
|
task_inputs=["instance"] + ["semantic"] * (len(dummy_images) - 1),
|
||||||
|
return_tensors="pt",
|
||||||
|
)
|
||||||
|
encoding_fast = image_processor_fast(
|
||||||
|
dummy_images,
|
||||||
|
segmentation_maps=dummy_maps,
|
||||||
|
task_inputs=["instance"] + ["semantic"] * (len(dummy_images) - 1),
|
||||||
|
return_tensors="pt",
|
||||||
|
)
|
||||||
|
|
||||||
|
self._assert_slow_fast_tensors_equivalence(encoding_slow.pixel_values, encoding_fast.pixel_values)
|
||||||
|
for mask_label_slow, mask_label_fast in zip(encoding_slow.mask_labels, encoding_fast.mask_labels):
|
||||||
|
self._assert_slow_fast_tensors_equivalence(mask_label_slow, mask_label_fast)
|
||||||
|
for class_label_slow, class_label_fast in zip(encoding_slow.class_labels, encoding_fast.class_labels):
|
||||||
|
self._assert_slow_fast_tensors_equivalence(class_label_slow.float(), class_label_fast.float())
|
||||||
|
self.assertEqual(encoding_slow.text_inputs, encoding_fast.text_inputs)
|
||||||
|
self.assertEqual(encoding_slow.task_inputs, encoding_fast.task_inputs)
|
||||||
|
|||||||
Reference in New Issue
Block a user