Add Image Processor Fast Deformable DETR (#34353)
* add deformable detr image processor fast * add fast processor to doc * fix copies * nit docstring * Add tests gpu/cpu and fix docstrings * fix docstring * import changes from detr * fix imports * rebase and fix * fix input data format change in detr and rtdetr fast
This commit is contained in:
@@ -54,6 +54,12 @@ If you're interested in submitting a resource to be included here, please feel f
|
|||||||
- preprocess
|
- preprocess
|
||||||
- post_process_object_detection
|
- post_process_object_detection
|
||||||
|
|
||||||
|
## DeformableDetrImageProcessorFast
|
||||||
|
|
||||||
|
[[autodoc]] DeformableDetrImageProcessorFast
|
||||||
|
- preprocess
|
||||||
|
- post_process_object_detection
|
||||||
|
|
||||||
## DeformableDetrFeatureExtractor
|
## DeformableDetrFeatureExtractor
|
||||||
|
|
||||||
[[autodoc]] DeformableDetrFeatureExtractor
|
[[autodoc]] DeformableDetrFeatureExtractor
|
||||||
|
|||||||
@@ -1186,7 +1186,7 @@ else:
|
|||||||
)
|
)
|
||||||
_import_structure["models.convnext"].extend(["ConvNextFeatureExtractor", "ConvNextImageProcessor"])
|
_import_structure["models.convnext"].extend(["ConvNextFeatureExtractor", "ConvNextImageProcessor"])
|
||||||
_import_structure["models.deformable_detr"].extend(
|
_import_structure["models.deformable_detr"].extend(
|
||||||
["DeformableDetrFeatureExtractor", "DeformableDetrImageProcessor"]
|
["DeformableDetrFeatureExtractor", "DeformableDetrImageProcessor", "DeformableDetrImageProcessorFast"]
|
||||||
)
|
)
|
||||||
_import_structure["models.deit"].extend(["DeiTFeatureExtractor", "DeiTImageProcessor"])
|
_import_structure["models.deit"].extend(["DeiTFeatureExtractor", "DeiTImageProcessor"])
|
||||||
_import_structure["models.deprecated.deta"].append("DetaImageProcessor")
|
_import_structure["models.deprecated.deta"].append("DetaImageProcessor")
|
||||||
@@ -6100,6 +6100,7 @@ if TYPE_CHECKING:
|
|||||||
from .models.deformable_detr import (
|
from .models.deformable_detr import (
|
||||||
DeformableDetrFeatureExtractor,
|
DeformableDetrFeatureExtractor,
|
||||||
DeformableDetrImageProcessor,
|
DeformableDetrImageProcessor,
|
||||||
|
DeformableDetrImageProcessorFast,
|
||||||
)
|
)
|
||||||
from .models.deit import DeiTFeatureExtractor, DeiTImageProcessor
|
from .models.deit import DeiTFeatureExtractor, DeiTImageProcessor
|
||||||
from .models.deprecated.deta import DetaImageProcessor
|
from .models.deprecated.deta import DetaImageProcessor
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ else:
|
|||||||
("convnextv2", ("ConvNextImageProcessor",)),
|
("convnextv2", ("ConvNextImageProcessor",)),
|
||||||
("cvt", ("ConvNextImageProcessor",)),
|
("cvt", ("ConvNextImageProcessor",)),
|
||||||
("data2vec-vision", ("BeitImageProcessor",)),
|
("data2vec-vision", ("BeitImageProcessor",)),
|
||||||
("deformable_detr", ("DeformableDetrImageProcessor",)),
|
("deformable_detr", ("DeformableDetrImageProcessor", "DeformableDetrImageProcessorFast")),
|
||||||
("deit", ("DeiTImageProcessor",)),
|
("deit", ("DeiTImageProcessor",)),
|
||||||
("depth_anything", ("DPTImageProcessor",)),
|
("depth_anything", ("DPTImageProcessor",)),
|
||||||
("deta", ("DetaImageProcessor",)),
|
("deta", ("DetaImageProcessor",)),
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ except OptionalDependencyNotAvailable:
|
|||||||
else:
|
else:
|
||||||
_import_structure["feature_extraction_deformable_detr"] = ["DeformableDetrFeatureExtractor"]
|
_import_structure["feature_extraction_deformable_detr"] = ["DeformableDetrFeatureExtractor"]
|
||||||
_import_structure["image_processing_deformable_detr"] = ["DeformableDetrImageProcessor"]
|
_import_structure["image_processing_deformable_detr"] = ["DeformableDetrImageProcessor"]
|
||||||
|
_import_structure["image_processing_deformable_detr_fast"] = ["DeformableDetrImageProcessorFast"]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not is_torch_available():
|
if not is_torch_available():
|
||||||
@@ -54,6 +55,7 @@ if TYPE_CHECKING:
|
|||||||
else:
|
else:
|
||||||
from .feature_extraction_deformable_detr import DeformableDetrFeatureExtractor
|
from .feature_extraction_deformable_detr import DeformableDetrFeatureExtractor
|
||||||
from .image_processing_deformable_detr import DeformableDetrImageProcessor
|
from .image_processing_deformable_detr import DeformableDetrImageProcessor
|
||||||
|
from .image_processing_deformable_detr_fast import DeformableDetrImageProcessorFast
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not is_torch_available():
|
if not is_torch_available():
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -416,7 +416,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
|
def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
|
||||||
"""
|
"""
|
||||||
Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is
|
Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is
|
||||||
created using from_dict and kwargs e.g. `DetrImageProcessor.from_pretrained(checkpoint, size=600,
|
created using from_dict and kwargs e.g. `DetrImageProcessorFast.from_pretrained(checkpoint, size=600,
|
||||||
max_size=800)`
|
max_size=800)`
|
||||||
"""
|
"""
|
||||||
image_processor_dict = image_processor_dict.copy()
|
image_processor_dict = image_processor_dict.copy()
|
||||||
@@ -863,6 +863,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
input_data_format = infer_channel_dimension_format(images[0])
|
input_data_format = infer_channel_dimension_format(images[0])
|
||||||
if input_data_format == ChannelDimension.LAST:
|
if input_data_format == ChannelDimension.LAST:
|
||||||
images = [image.permute(2, 0, 1).contiguous() for image in images]
|
images = [image.permute(2, 0, 1).contiguous() for image in images]
|
||||||
|
input_data_format = ChannelDimension.FIRST
|
||||||
|
|
||||||
if do_rescale and do_normalize:
|
if do_rescale and do_normalize:
|
||||||
# fused rescale and normalize
|
# fused rescale and normalize
|
||||||
|
|||||||
@@ -639,6 +639,7 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast):
|
|||||||
input_data_format = infer_channel_dimension_format(images[0])
|
input_data_format = infer_channel_dimension_format(images[0])
|
||||||
if input_data_format == ChannelDimension.LAST:
|
if input_data_format == ChannelDimension.LAST:
|
||||||
images = [image.permute(2, 0, 1).contiguous() for image in images]
|
images = [image.permute(2, 0, 1).contiguous() for image in images]
|
||||||
|
input_data_format = ChannelDimension.FIRST
|
||||||
|
|
||||||
if do_rescale and do_normalize:
|
if do_rescale and do_normalize:
|
||||||
# fused rescale and normalize
|
# fused rescale and normalize
|
||||||
|
|||||||
@@ -135,6 +135,13 @@ class DeformableDetrImageProcessor(metaclass=DummyObject):
|
|||||||
requires_backends(self, ["vision"])
|
requires_backends(self, ["vision"])
|
||||||
|
|
||||||
|
|
||||||
|
class DeformableDetrImageProcessorFast(metaclass=DummyObject):
|
||||||
|
_backends = ["vision"]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["vision"])
|
||||||
|
|
||||||
|
|
||||||
class DeiTFeatureExtractor(metaclass=DummyObject):
|
class DeiTFeatureExtractor(metaclass=DummyObject):
|
||||||
_backends = ["vision"]
|
_backends = ["vision"]
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ import unittest
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from transformers.testing_utils import require_torch, require_vision, slow
|
from transformers.testing_utils import require_torch, require_torch_gpu, require_vision, slow
|
||||||
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 AnnotationFormatTestMixin, ImageProcessingTestMixin, prepare_image_inputs
|
from ...test_image_processing_common import AnnotationFormatTestMixin, ImageProcessingTestMixin, prepare_image_inputs
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ if is_torch_available():
|
|||||||
if is_vision_available():
|
if is_vision_available():
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from transformers import DeformableDetrImageProcessor
|
from transformers import DeformableDetrImageProcessor, DeformableDetrImageProcessorFast
|
||||||
|
|
||||||
|
|
||||||
class DeformableDetrImageProcessingTester(unittest.TestCase):
|
class DeformableDetrImageProcessingTester(unittest.TestCase):
|
||||||
@@ -52,6 +52,7 @@ class DeformableDetrImageProcessingTester(unittest.TestCase):
|
|||||||
rescale_factor=1 / 255,
|
rescale_factor=1 / 255,
|
||||||
do_pad=True,
|
do_pad=True,
|
||||||
):
|
):
|
||||||
|
super().__init__()
|
||||||
# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
|
# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
|
||||||
size = size if size is not None else {"shortest_edge": 18, "longest_edge": 1333}
|
size = size if size is not None else {"shortest_edge": 18, "longest_edge": 1333}
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
@@ -133,6 +134,7 @@ class DeformableDetrImageProcessingTester(unittest.TestCase):
|
|||||||
@require_vision
|
@require_vision
|
||||||
class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMixin, unittest.TestCase):
|
class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMixin, unittest.TestCase):
|
||||||
image_processing_class = DeformableDetrImageProcessor if is_vision_available() else None
|
image_processing_class = DeformableDetrImageProcessor if is_vision_available() else None
|
||||||
|
fast_image_processing_class = DeformableDetrImageProcessorFast if is_torchvision_available() else None
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
@@ -143,7 +145,8 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
return self.image_processor_tester.prepare_image_processor_dict()
|
return self.image_processor_tester.prepare_image_processor_dict()
|
||||||
|
|
||||||
def test_image_processor_properties(self):
|
def test_image_processor_properties(self):
|
||||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
for image_processing_class in self.image_processor_list:
|
||||||
|
image_processing = image_processing_class(**self.image_processor_dict)
|
||||||
self.assertTrue(hasattr(image_processing, "image_mean"))
|
self.assertTrue(hasattr(image_processing, "image_mean"))
|
||||||
self.assertTrue(hasattr(image_processing, "image_std"))
|
self.assertTrue(hasattr(image_processing, "image_std"))
|
||||||
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
||||||
@@ -153,11 +156,12 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
self.assertTrue(hasattr(image_processing, "size"))
|
self.assertTrue(hasattr(image_processing, "size"))
|
||||||
|
|
||||||
def test_image_processor_from_dict_with_kwargs(self):
|
def test_image_processor_from_dict_with_kwargs(self):
|
||||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
|
for image_processing_class in self.image_processor_list:
|
||||||
|
image_processor = image_processing_class.from_dict(self.image_processor_dict)
|
||||||
self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 1333})
|
self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 1333})
|
||||||
self.assertEqual(image_processor.do_pad, True)
|
self.assertEqual(image_processor.do_pad, True)
|
||||||
|
|
||||||
image_processor = self.image_processing_class.from_dict(
|
image_processor = image_processing_class.from_dict(
|
||||||
self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=False
|
self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=False
|
||||||
)
|
)
|
||||||
self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84})
|
self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84})
|
||||||
@@ -172,8 +176,9 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
|
|
||||||
target = {"image_id": 39769, "annotations": target}
|
target = {"image_id": 39769, "annotations": target}
|
||||||
|
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
# encode them
|
# encode them
|
||||||
image_processing = DeformableDetrImageProcessor()
|
image_processing = image_processing_class()
|
||||||
encoding = image_processing(images=image, annotations=target, return_tensors="pt")
|
encoding = image_processing(images=image, annotations=target, return_tensors="pt")
|
||||||
|
|
||||||
# verify pixel values
|
# verify pixel values
|
||||||
@@ -218,8 +223,9 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
|
|
||||||
masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")
|
masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")
|
||||||
|
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
# encode them
|
# encode them
|
||||||
image_processing = DeformableDetrImageProcessor(format="coco_panoptic")
|
image_processing = image_processing_class(format="coco_panoptic")
|
||||||
encoding = image_processing(images=image, annotations=target, masks_path=masks_path, return_tensors="pt")
|
encoding = image_processing(images=image, annotations=target, masks_path=masks_path, return_tensors="pt")
|
||||||
|
|
||||||
# verify pixel values
|
# verify pixel values
|
||||||
@@ -248,7 +254,8 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
|
self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
|
||||||
# verify masks
|
# verify masks
|
||||||
expected_masks_sum = 822873
|
expected_masks_sum = 822873
|
||||||
self.assertEqual(encoding["labels"][0]["masks"].sum().item(), expected_masks_sum)
|
relative_error = torch.abs(encoding["labels"][0]["masks"].sum() - expected_masks_sum) / expected_masks_sum
|
||||||
|
self.assertTrue(relative_error < 1e-3)
|
||||||
# verify orig_size
|
# verify orig_size
|
||||||
expected_orig_size = torch.tensor([480, 640])
|
expected_orig_size = torch.tensor([480, 640])
|
||||||
self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
|
self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
|
||||||
@@ -549,11 +556,12 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
self.assertEqual(inputs["pixel_values"].shape, torch.Size([2, 3, 150, 100]))
|
self.assertEqual(inputs["pixel_values"].shape, torch.Size([2, 3, 150, 100]))
|
||||||
|
|
||||||
def test_longest_edge_shortest_edge_resizing_strategy(self):
|
def test_longest_edge_shortest_edge_resizing_strategy(self):
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
image_1 = torch.ones([958, 653, 3], dtype=torch.uint8)
|
image_1 = torch.ones([958, 653, 3], dtype=torch.uint8)
|
||||||
|
|
||||||
# max size is set; width < height;
|
# max size is set; width < height;
|
||||||
# do_pad=False, longest_edge=640, shortest_edge=640, image=958x653 -> 640x436
|
# do_pad=False, longest_edge=640, shortest_edge=640, image=958x653 -> 640x436
|
||||||
image_processor = DeformableDetrImageProcessor(
|
image_processor = image_processing_class(
|
||||||
size={"longest_edge": 640, "shortest_edge": 640},
|
size={"longest_edge": 640, "shortest_edge": 640},
|
||||||
do_pad=False,
|
do_pad=False,
|
||||||
)
|
)
|
||||||
@@ -563,7 +571,7 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
image_2 = torch.ones([653, 958, 3], dtype=torch.uint8)
|
image_2 = torch.ones([653, 958, 3], dtype=torch.uint8)
|
||||||
# max size is set; height < width;
|
# max size is set; height < width;
|
||||||
# do_pad=False, longest_edge=640, shortest_edge=640, image=653x958 -> 436x640
|
# do_pad=False, longest_edge=640, shortest_edge=640, image=653x958 -> 436x640
|
||||||
image_processor = DeformableDetrImageProcessor(
|
image_processor = image_processing_class(
|
||||||
size={"longest_edge": 640, "shortest_edge": 640},
|
size={"longest_edge": 640, "shortest_edge": 640},
|
||||||
do_pad=False,
|
do_pad=False,
|
||||||
)
|
)
|
||||||
@@ -573,7 +581,7 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
image_3 = torch.ones([100, 120, 3], dtype=torch.uint8)
|
image_3 = torch.ones([100, 120, 3], dtype=torch.uint8)
|
||||||
# max size is set; width == size; height > max_size;
|
# max size is set; width == size; height > max_size;
|
||||||
# do_pad=False, longest_edge=118, shortest_edge=100, image=120x100 -> 118x98
|
# do_pad=False, longest_edge=118, shortest_edge=100, image=120x100 -> 118x98
|
||||||
image_processor = DeformableDetrImageProcessor(
|
image_processor = image_processing_class(
|
||||||
size={"longest_edge": 118, "shortest_edge": 100},
|
size={"longest_edge": 118, "shortest_edge": 100},
|
||||||
do_pad=False,
|
do_pad=False,
|
||||||
)
|
)
|
||||||
@@ -583,7 +591,7 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
image_4 = torch.ones([128, 50, 3], dtype=torch.uint8)
|
image_4 = torch.ones([128, 50, 3], dtype=torch.uint8)
|
||||||
# max size is set; height == size; width < max_size;
|
# max size is set; height == size; width < max_size;
|
||||||
# do_pad=False, longest_edge=256, shortest_edge=50, image=50x128 -> 50x128
|
# do_pad=False, longest_edge=256, shortest_edge=50, image=50x128 -> 50x128
|
||||||
image_processor = DeformableDetrImageProcessor(
|
image_processor = image_processing_class(
|
||||||
size={"longest_edge": 256, "shortest_edge": 50},
|
size={"longest_edge": 256, "shortest_edge": 50},
|
||||||
do_pad=False,
|
do_pad=False,
|
||||||
)
|
)
|
||||||
@@ -593,9 +601,136 @@ class DeformableDetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessi
|
|||||||
image_5 = torch.ones([50, 50, 3], dtype=torch.uint8)
|
image_5 = torch.ones([50, 50, 3], dtype=torch.uint8)
|
||||||
# max size is set; height == width; width < max_size;
|
# max size is set; height == width; width < max_size;
|
||||||
# do_pad=False, longest_edge=117, shortest_edge=50, image=50x50 -> 50x50
|
# do_pad=False, longest_edge=117, shortest_edge=50, image=50x50 -> 50x50
|
||||||
image_processor = DeformableDetrImageProcessor(
|
image_processor = image_processing_class(
|
||||||
size={"longest_edge": 117, "shortest_edge": 50},
|
size={"longest_edge": 117, "shortest_edge": 50},
|
||||||
do_pad=False,
|
do_pad=False,
|
||||||
)
|
)
|
||||||
inputs = image_processor(images=[image_5], return_tensors="pt")
|
inputs = image_processor(images=[image_5], return_tensors="pt")
|
||||||
self.assertEqual(inputs["pixel_values"].shape, torch.Size([1, 3, 50, 50]))
|
self.assertEqual(inputs["pixel_values"].shape, torch.Size([1, 3, 50, 50]))
|
||||||
|
|
||||||
|
@slow
|
||||||
|
@require_torch_gpu
|
||||||
|
# Copied from tests.models.detr.test_image_processing_detr.DetrImageProcessingTest.test_fast_processor_equivalence_cpu_gpu_coco_detection_annotations
|
||||||
|
def test_fast_processor_equivalence_cpu_gpu_coco_detection_annotations(self):
|
||||||
|
# prepare image and target
|
||||||
|
image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
|
||||||
|
with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt", "r") as f:
|
||||||
|
target = json.loads(f.read())
|
||||||
|
|
||||||
|
target = {"image_id": 39769, "annotations": target}
|
||||||
|
|
||||||
|
# Ignore copy
|
||||||
|
processor = self.image_processor_list[1]()
|
||||||
|
|
||||||
|
# 1. run processor on CPU
|
||||||
|
encoding_cpu = processor(images=image, annotations=target, return_tensors="pt", device="cpu")
|
||||||
|
# 2. run processor on GPU
|
||||||
|
encoding_gpu = processor(images=image, annotations=target, return_tensors="pt", device="cuda")
|
||||||
|
|
||||||
|
# verify pixel values
|
||||||
|
self.assertEqual(encoding_cpu["pixel_values"].shape, encoding_gpu["pixel_values"].shape)
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["pixel_values"][0, 0, 0, :3],
|
||||||
|
encoding_gpu["pixel_values"][0, 0, 0, :3].to("cpu"),
|
||||||
|
atol=1e-4,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify area
|
||||||
|
self.assertTrue(torch.allclose(encoding_cpu["labels"][0]["area"], encoding_gpu["labels"][0]["area"].to("cpu")))
|
||||||
|
# verify boxes
|
||||||
|
self.assertEqual(encoding_cpu["labels"][0]["boxes"].shape, encoding_gpu["labels"][0]["boxes"].shape)
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["labels"][0]["boxes"][0], encoding_gpu["labels"][0]["boxes"][0].to("cpu"), atol=1e-3
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify image_id
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["image_id"], encoding_gpu["labels"][0]["image_id"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify is_crowd
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["iscrowd"], encoding_gpu["labels"][0]["iscrowd"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify class_labels
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["labels"][0]["class_labels"], encoding_gpu["labels"][0]["class_labels"].to("cpu")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify orig_size
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["orig_size"], encoding_gpu["labels"][0]["orig_size"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify size
|
||||||
|
self.assertTrue(torch.allclose(encoding_cpu["labels"][0]["size"], encoding_gpu["labels"][0]["size"].to("cpu")))
|
||||||
|
|
||||||
|
@slow
|
||||||
|
@require_torch_gpu
|
||||||
|
# Copied from tests.models.detr.test_image_processing_detr.DetrImageProcessingTest.test_fast_processor_equivalence_cpu_gpu_coco_panoptic_annotations
|
||||||
|
def test_fast_processor_equivalence_cpu_gpu_coco_panoptic_annotations(self):
|
||||||
|
# prepare image, target and masks_path
|
||||||
|
image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
|
||||||
|
with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt", "r") as f:
|
||||||
|
target = json.loads(f.read())
|
||||||
|
|
||||||
|
target = {"file_name": "000000039769.png", "image_id": 39769, "segments_info": target}
|
||||||
|
|
||||||
|
masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")
|
||||||
|
|
||||||
|
# Ignore copy
|
||||||
|
processor = self.image_processor_list[1](format="coco_panoptic")
|
||||||
|
|
||||||
|
# 1. run processor on CPU
|
||||||
|
encoding_cpu = processor(
|
||||||
|
images=image, annotations=target, masks_path=masks_path, return_tensors="pt", device="cpu"
|
||||||
|
)
|
||||||
|
# 2. run processor on GPU
|
||||||
|
encoding_gpu = processor(
|
||||||
|
images=image, annotations=target, masks_path=masks_path, return_tensors="pt", device="cuda"
|
||||||
|
)
|
||||||
|
|
||||||
|
# verify pixel values
|
||||||
|
self.assertEqual(encoding_cpu["pixel_values"].shape, encoding_gpu["pixel_values"].shape)
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["pixel_values"][0, 0, 0, :3],
|
||||||
|
encoding_gpu["pixel_values"][0, 0, 0, :3].to("cpu"),
|
||||||
|
atol=1e-4,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify area
|
||||||
|
self.assertTrue(torch.allclose(encoding_cpu["labels"][0]["area"], encoding_gpu["labels"][0]["area"].to("cpu")))
|
||||||
|
# verify boxes
|
||||||
|
self.assertEqual(encoding_cpu["labels"][0]["boxes"].shape, encoding_gpu["labels"][0]["boxes"].shape)
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["labels"][0]["boxes"][0], encoding_gpu["labels"][0]["boxes"][0].to("cpu"), atol=1e-3
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify image_id
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["image_id"], encoding_gpu["labels"][0]["image_id"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify is_crowd
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["iscrowd"], encoding_gpu["labels"][0]["iscrowd"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify class_labels
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(
|
||||||
|
encoding_cpu["labels"][0]["class_labels"], encoding_gpu["labels"][0]["class_labels"].to("cpu")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# verify masks
|
||||||
|
masks_sum_cpu = encoding_cpu["labels"][0]["masks"].sum()
|
||||||
|
masks_sum_gpu = encoding_gpu["labels"][0]["masks"].sum()
|
||||||
|
relative_error = torch.abs(masks_sum_cpu - masks_sum_gpu) / masks_sum_cpu
|
||||||
|
self.assertTrue(relative_error < 1e-3)
|
||||||
|
# verify orig_size
|
||||||
|
self.assertTrue(
|
||||||
|
torch.allclose(encoding_cpu["labels"][0]["orig_size"], encoding_gpu["labels"][0]["orig_size"].to("cpu"))
|
||||||
|
)
|
||||||
|
# verify size
|
||||||
|
self.assertTrue(torch.allclose(encoding_cpu["labels"][0]["size"], encoding_gpu["labels"][0]["size"].to("cpu")))
|
||||||
|
|||||||
@@ -159,7 +159,8 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
|
|||||||
|
|
||||||
# Copied from tests.models.deformable_detr.test_image_processing_deformable_detr.DeformableDetrImageProcessingTest.test_image_processor_properties with DeformableDetr->GroundingDino
|
# Copied from tests.models.deformable_detr.test_image_processing_deformable_detr.DeformableDetrImageProcessingTest.test_image_processor_properties with DeformableDetr->GroundingDino
|
||||||
def test_image_processor_properties(self):
|
def test_image_processor_properties(self):
|
||||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
for image_processing_class in self.image_processor_list:
|
||||||
|
image_processing = image_processing_class(**self.image_processor_dict)
|
||||||
self.assertTrue(hasattr(image_processing, "image_mean"))
|
self.assertTrue(hasattr(image_processing, "image_mean"))
|
||||||
self.assertTrue(hasattr(image_processing, "image_std"))
|
self.assertTrue(hasattr(image_processing, "image_std"))
|
||||||
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
||||||
@@ -170,11 +171,12 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
|
|||||||
|
|
||||||
# Copied from tests.models.deformable_detr.test_image_processing_deformable_detr.DeformableDetrImageProcessingTest.test_image_processor_from_dict_with_kwargs with DeformableDetr->GroundingDino
|
# Copied from tests.models.deformable_detr.test_image_processing_deformable_detr.DeformableDetrImageProcessingTest.test_image_processor_from_dict_with_kwargs with DeformableDetr->GroundingDino
|
||||||
def test_image_processor_from_dict_with_kwargs(self):
|
def test_image_processor_from_dict_with_kwargs(self):
|
||||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
|
for image_processing_class in self.image_processor_list:
|
||||||
|
image_processor = image_processing_class.from_dict(self.image_processor_dict)
|
||||||
self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 1333})
|
self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 1333})
|
||||||
self.assertEqual(image_processor.do_pad, True)
|
self.assertEqual(image_processor.do_pad, True)
|
||||||
|
|
||||||
image_processor = self.image_processing_class.from_dict(
|
image_processor = image_processing_class.from_dict(
|
||||||
self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=False
|
self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=False
|
||||||
)
|
)
|
||||||
self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84})
|
self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84})
|
||||||
@@ -206,8 +208,9 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
|
|||||||
|
|
||||||
target = {"image_id": 39769, "annotations": target}
|
target = {"image_id": 39769, "annotations": target}
|
||||||
|
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
# encode them
|
# encode them
|
||||||
image_processing = GroundingDinoImageProcessor()
|
image_processing = image_processing_class()
|
||||||
encoding = image_processing(images=image, annotations=target, return_tensors="pt")
|
encoding = image_processing(images=image, annotations=target, return_tensors="pt")
|
||||||
|
|
||||||
# verify pixel values
|
# verify pixel values
|
||||||
@@ -373,8 +376,9 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
|
|||||||
|
|
||||||
masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")
|
masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")
|
||||||
|
|
||||||
|
for image_processing_class in self.image_processor_list:
|
||||||
# encode them
|
# encode them
|
||||||
image_processing = GroundingDinoImageProcessor(format="coco_panoptic")
|
image_processing = image_processing_class(format="coco_panoptic")
|
||||||
encoding = image_processing(images=image, annotations=target, masks_path=masks_path, return_tensors="pt")
|
encoding = image_processing(images=image, annotations=target, masks_path=masks_path, return_tensors="pt")
|
||||||
|
|
||||||
# verify pixel values
|
# verify pixel values
|
||||||
@@ -403,7 +407,8 @@ class GroundingDinoImageProcessingTest(AnnotationFormatTestMixin, ImageProcessin
|
|||||||
self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
|
self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
|
||||||
# verify masks
|
# verify masks
|
||||||
expected_masks_sum = 822873
|
expected_masks_sum = 822873
|
||||||
self.assertEqual(encoding["labels"][0]["masks"].sum().item(), expected_masks_sum)
|
relative_error = torch.abs(encoding["labels"][0]["masks"].sum() - expected_masks_sum) / expected_masks_sum
|
||||||
|
self.assertTrue(relative_error < 1e-3)
|
||||||
# verify orig_size
|
# verify orig_size
|
||||||
expected_orig_size = torch.tensor([480, 640])
|
expected_orig_size = torch.tensor([480, 640])
|
||||||
self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
|
self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
|
||||||
|
|||||||
Reference in New Issue
Block a user