init
This commit is contained in:
0
transformers/tests/models/sam/__init__.py
Normal file
0
transformers/tests/models/sam/__init__.py
Normal file
301
transformers/tests/models/sam/test_image_processing_sam.py
Normal file
301
transformers/tests/models/sam/test_image_processing_sam.py
Normal file
@@ -0,0 +1,301 @@
|
||||
# Copyright 2025 HuggingFace Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from datasets import load_dataset
|
||||
|
||||
from transformers.file_utils import is_torch_available, is_vision_available
|
||||
from transformers.testing_utils import require_torch, require_vision
|
||||
from transformers.utils import is_torchvision_available
|
||||
|
||||
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
if is_vision_available():
|
||||
from transformers import SamImageProcessor
|
||||
|
||||
if is_torchvision_available():
|
||||
from transformers import SamImageProcessorFast
|
||||
|
||||
|
||||
class SamImageProcessingTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=7,
|
||||
num_channels=3,
|
||||
image_size=18,
|
||||
min_resolution=30,
|
||||
max_resolution=400,
|
||||
do_pad=True,
|
||||
pad_size=None,
|
||||
mask_size=None,
|
||||
mask_pad_size=None,
|
||||
do_resize=True,
|
||||
size=None,
|
||||
do_normalize=True,
|
||||
image_mean=[0.5, 0.5, 0.5],
|
||||
image_std=[0.5, 0.5, 0.5],
|
||||
):
|
||||
size = size if size is not None else {"longest_edge": 20}
|
||||
pad_size = pad_size if pad_size is not None else {"height": 20, "width": 20}
|
||||
mask_size = mask_size if mask_size is not None else {"longest_edge": 12}
|
||||
mask_pad_size = mask_pad_size if mask_pad_size is not None else {"height": 12, "width": 12}
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.num_channels = num_channels
|
||||
self.image_size = image_size
|
||||
self.min_resolution = min_resolution
|
||||
self.max_resolution = max_resolution
|
||||
self.do_pad = do_pad
|
||||
self.pad_size = pad_size
|
||||
self.mask_size = mask_size
|
||||
self.mask_pad_size = mask_pad_size
|
||||
self.do_resize = do_resize
|
||||
self.size = size
|
||||
self.do_normalize = do_normalize
|
||||
self.image_mean = image_mean
|
||||
self.image_std = image_std
|
||||
|
||||
def prepare_image_processor_dict(self):
|
||||
return {
|
||||
"image_mean": self.image_mean,
|
||||
"image_std": self.image_std,
|
||||
"do_normalize": self.do_normalize,
|
||||
"do_resize": self.do_resize,
|
||||
"size": self.size,
|
||||
"do_pad": self.do_pad,
|
||||
"pad_size": self.pad_size,
|
||||
"mask_size": self.mask_size,
|
||||
"mask_pad_size": self.mask_pad_size,
|
||||
}
|
||||
|
||||
def expected_output_image_shape(self, images):
|
||||
return self.num_channels, self.pad_size["height"], self.pad_size["width"]
|
||||
|
||||
def prepare_image_inputs(self, equal_resolution=False, numpify=False, torchify=False):
|
||||
return prepare_image_inputs(
|
||||
batch_size=self.batch_size,
|
||||
num_channels=self.num_channels,
|
||||
min_resolution=self.min_resolution,
|
||||
max_resolution=self.max_resolution,
|
||||
equal_resolution=equal_resolution,
|
||||
numpify=numpify,
|
||||
torchify=torchify,
|
||||
)
|
||||
|
||||
|
||||
# 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_vision
|
||||
class SamImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
||||
image_processing_class = SamImageProcessor if is_vision_available() else None
|
||||
fast_image_processing_class = SamImageProcessorFast if is_torchvision_available() else None
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.image_processor_tester = SamImageProcessingTester(self)
|
||||
|
||||
@property
|
||||
def image_processor_dict(self):
|
||||
return self.image_processor_tester.prepare_image_processor_dict()
|
||||
|
||||
def test_image_processor_properties(self):
|
||||
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_std"))
|
||||
self.assertTrue(hasattr(image_processing, "do_normalize"))
|
||||
self.assertTrue(hasattr(image_processing, "do_resize"))
|
||||
self.assertTrue(hasattr(image_processing, "size"))
|
||||
self.assertTrue(hasattr(image_processing, "do_rescale"))
|
||||
self.assertTrue(hasattr(image_processing, "rescale_factor"))
|
||||
self.assertTrue(hasattr(image_processing, "do_pad"))
|
||||
self.assertTrue(hasattr(image_processing, "pad_size"))
|
||||
self.assertTrue(hasattr(image_processing, "mask_size"))
|
||||
self.assertTrue(hasattr(image_processing, "mask_pad_size"))
|
||||
|
||||
def test_image_processor_from_dict_with_kwargs(self):
|
||||
for image_processing_class in self.image_processor_list:
|
||||
image_processing_class = image_processing_class(**self.image_processor_dict)
|
||||
image_processor = image_processing_class.from_dict(self.image_processor_dict)
|
||||
self.assertEqual(image_processor.size, {"longest_edge": 20})
|
||||
|
||||
image_processor = image_processing_class.from_dict(self.image_processor_dict, size={"longest_edge": 42})
|
||||
self.assertEqual(image_processor.size, {"longest_edge": 42})
|
||||
|
||||
def test_call_segmentation_maps(self):
|
||||
for image_processing_class in self.image_processor_list:
|
||||
# Initialize image_processor
|
||||
image_processor = image_processing_class(**self.image_processor_dict)
|
||||
# create random PyTorch tensors
|
||||
image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=False, torchify=True)
|
||||
maps = []
|
||||
for image in image_inputs:
|
||||
self.assertIsInstance(image, torch.Tensor)
|
||||
maps.append(torch.zeros(image.shape[-2:]).long())
|
||||
|
||||
# Test not batched input
|
||||
encoding = image_processor(image_inputs[0], maps[0], return_tensors="pt")
|
||||
self.assertEqual(
|
||||
encoding["pixel_values"].shape,
|
||||
(
|
||||
1,
|
||||
self.image_processor_tester.num_channels,
|
||||
self.image_processor_tester.pad_size["height"],
|
||||
self.image_processor_tester.pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
encoding["labels"].shape,
|
||||
(
|
||||
1,
|
||||
self.image_processor_tester.mask_pad_size["height"],
|
||||
self.image_processor_tester.mask_pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(encoding["labels"].dtype, torch.long)
|
||||
self.assertTrue(encoding["labels"].min().item() >= 0)
|
||||
self.assertTrue(encoding["labels"].max().item() <= 255)
|
||||
|
||||
# Test batched
|
||||
encoding = image_processor(image_inputs, maps, return_tensors="pt")
|
||||
self.assertEqual(
|
||||
encoding["pixel_values"].shape,
|
||||
(
|
||||
self.image_processor_tester.batch_size,
|
||||
self.image_processor_tester.num_channels,
|
||||
self.image_processor_tester.pad_size["height"],
|
||||
self.image_processor_tester.pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
encoding["labels"].shape,
|
||||
(
|
||||
self.image_processor_tester.batch_size,
|
||||
self.image_processor_tester.mask_pad_size["height"],
|
||||
self.image_processor_tester.mask_pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(encoding["labels"].dtype, torch.long)
|
||||
self.assertTrue(encoding["labels"].min().item() >= 0)
|
||||
self.assertTrue(encoding["labels"].max().item() <= 255)
|
||||
|
||||
# Test not batched input (PIL images)
|
||||
image, segmentation_map = prepare_semantic_single_inputs()
|
||||
|
||||
encoding = image_processor(image, segmentation_map, return_tensors="pt")
|
||||
self.assertEqual(
|
||||
encoding["pixel_values"].shape,
|
||||
(
|
||||
1,
|
||||
self.image_processor_tester.num_channels,
|
||||
self.image_processor_tester.pad_size["height"],
|
||||
self.image_processor_tester.pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
encoding["labels"].shape,
|
||||
(
|
||||
1,
|
||||
self.image_processor_tester.mask_pad_size["height"],
|
||||
self.image_processor_tester.mask_pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(encoding["labels"].dtype, torch.long)
|
||||
self.assertTrue(encoding["labels"].min().item() >= 0)
|
||||
self.assertTrue(encoding["labels"].max().item() <= 255)
|
||||
|
||||
# Test batched input (PIL images)
|
||||
images, segmentation_maps = prepare_semantic_batch_inputs()
|
||||
|
||||
encoding = image_processor(images, segmentation_maps, return_tensors="pt")
|
||||
self.assertEqual(
|
||||
encoding["pixel_values"].shape,
|
||||
(
|
||||
2,
|
||||
self.image_processor_tester.num_channels,
|
||||
self.image_processor_tester.pad_size["height"],
|
||||
self.image_processor_tester.pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(
|
||||
encoding["labels"].shape,
|
||||
(
|
||||
2,
|
||||
self.image_processor_tester.mask_pad_size["height"],
|
||||
self.image_processor_tester.mask_pad_size["width"],
|
||||
),
|
||||
)
|
||||
self.assertEqual(encoding["labels"].dtype, torch.long)
|
||||
self.assertTrue(encoding["labels"].min().item() >= 0)
|
||||
self.assertTrue(encoding["labels"].max().item() <= 255)
|
||||
|
||||
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.assertTrue(torch.allclose(image_encoding_slow.pixel_values, image_encoding_fast.pixel_values, atol=1e-1))
|
||||
self.assertLessEqual(
|
||||
torch.mean(torch.abs(image_encoding_slow.pixel_values - image_encoding_fast.pixel_values)).item(), 1e-3
|
||||
)
|
||||
self.assertTrue(torch.allclose(image_encoding_slow.labels, image_encoding_fast.labels, atol=1e-1))
|
||||
|
||||
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")
|
||||
|
||||
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, return_tensors="pt")
|
||||
encoding_fast = image_processor_fast(dummy_images, segmentation_maps=dummy_maps, return_tensors="pt")
|
||||
|
||||
self.assertTrue(torch.allclose(encoding_slow.pixel_values, encoding_fast.pixel_values, atol=1e-1))
|
||||
self.assertLessEqual(
|
||||
torch.mean(torch.abs(encoding_slow.pixel_values - encoding_fast.pixel_values)).item(), 1e-3
|
||||
)
|
||||
1044
transformers/tests/models/sam/test_modeling_sam.py
Normal file
1044
transformers/tests/models/sam/test_modeling_sam.py
Normal file
File diff suppressed because it is too large
Load Diff
194
transformers/tests/models/sam/test_processing_sam.py
Normal file
194
transformers/tests/models/sam/test_processing_sam.py
Normal file
@@ -0,0 +1,194 @@
|
||||
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
import numpy as np
|
||||
|
||||
from transformers.testing_utils import require_torch, require_torchvision, require_vision
|
||||
from transformers.utils import is_torch_available, is_vision_available
|
||||
|
||||
from ...test_processing_common import ProcessorTesterMixin
|
||||
|
||||
|
||||
if is_vision_available():
|
||||
from PIL import Image
|
||||
|
||||
from transformers import AutoProcessor, SamImageProcessor, SamProcessor
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers.models.sam.image_processing_sam import _mask_to_rle_pytorch
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_torchvision
|
||||
class SamProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
||||
processor_class = SamProcessor
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmpdirname = tempfile.mkdtemp()
|
||||
image_processor = SamImageProcessor()
|
||||
processor = SamProcessor(image_processor)
|
||||
processor.save_pretrained(cls.tmpdirname)
|
||||
|
||||
def get_image_processor(self, **kwargs):
|
||||
return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.tmpdirname, ignore_errors=True)
|
||||
|
||||
def prepare_mask_inputs(self):
|
||||
"""This function prepares a list of PIL images, or a list of numpy arrays if one specifies numpify=True,
|
||||
or a list of PyTorch tensors if one specifies torchify=True.
|
||||
"""
|
||||
mask_inputs = [np.random.randint(255, size=(30, 400), dtype=np.uint8)]
|
||||
mask_inputs = [Image.fromarray(x) for x in mask_inputs]
|
||||
return mask_inputs
|
||||
|
||||
def test_chat_template_save_loading(self):
|
||||
self.skipTest("SamProcessor does not have a tokenizer")
|
||||
|
||||
def test_image_processor_defaults_preserved_by_image_kwargs(self):
|
||||
self.skipTest("SamProcessor does not have a tokenizer")
|
||||
|
||||
def test_kwargs_overrides_default_image_processor_kwargs(self):
|
||||
self.skipTest("SamProcessor does not have a tokenizer")
|
||||
|
||||
def test_kwargs_overrides_default_tokenizer_kwargs(self):
|
||||
self.skipTest("SamProcessor does not have a tokenizer")
|
||||
|
||||
def test_tokenizer_defaults_preserved_by_kwargs(self):
|
||||
self.skipTest("SamProcessor does not have a tokenizer")
|
||||
|
||||
def test_save_load_pretrained_additional_features(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
processor = SamProcessor(image_processor=self.get_image_processor())
|
||||
processor.save_pretrained(tmpdir)
|
||||
|
||||
image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0)
|
||||
|
||||
processor = SamProcessor.from_pretrained(tmpdir, do_normalize=False, padding_value=1.0)
|
||||
|
||||
self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string())
|
||||
self.assertIsInstance(processor.image_processor, SamImageProcessor)
|
||||
|
||||
def test_image_processor_no_masks(self):
|
||||
image_processor = self.get_image_processor()
|
||||
|
||||
processor = SamProcessor(image_processor=image_processor)
|
||||
|
||||
image_input = self.prepare_image_inputs()
|
||||
|
||||
input_feat_extract = image_processor(image_input, return_tensors="np")
|
||||
input_processor = processor(images=image_input, return_tensors="np")
|
||||
|
||||
for key in input_feat_extract:
|
||||
self.assertAlmostEqual(input_feat_extract[key].sum(), input_processor[key].sum(), delta=1e-2)
|
||||
|
||||
for image in input_feat_extract.pixel_values:
|
||||
self.assertEqual(image.shape, (3, 1024, 1024))
|
||||
|
||||
for original_size in input_feat_extract.original_sizes:
|
||||
np.testing.assert_array_equal(original_size, np.array([30, 400]))
|
||||
|
||||
for reshaped_input_size in input_feat_extract.reshaped_input_sizes:
|
||||
np.testing.assert_array_equal(
|
||||
reshaped_input_size, np.array([77, 1024])
|
||||
) # reshaped_input_size value is before padding
|
||||
|
||||
def test_image_processor_with_masks(self):
|
||||
image_processor = self.get_image_processor()
|
||||
|
||||
processor = SamProcessor(image_processor=image_processor)
|
||||
|
||||
image_input = self.prepare_image_inputs()
|
||||
mask_input = self.prepare_mask_inputs()
|
||||
|
||||
input_feat_extract = image_processor(images=image_input, segmentation_maps=mask_input, return_tensors="np")
|
||||
input_processor = processor(images=image_input, segmentation_maps=mask_input, return_tensors="np")
|
||||
|
||||
for key in input_feat_extract:
|
||||
self.assertAlmostEqual(input_feat_extract[key].sum(), input_processor[key].sum(), delta=1e-2)
|
||||
|
||||
for label in input_feat_extract.labels:
|
||||
self.assertEqual(label.shape, (256, 256))
|
||||
|
||||
@require_torch
|
||||
def test_post_process_masks(self):
|
||||
image_processor = self.get_image_processor()
|
||||
|
||||
processor = SamProcessor(image_processor=image_processor)
|
||||
dummy_masks = [torch.ones((1, 3, 5, 5))]
|
||||
|
||||
original_sizes = [[1764, 2646]]
|
||||
|
||||
reshaped_input_size = [[683, 1024]]
|
||||
masks = processor.post_process_masks(dummy_masks, original_sizes, reshaped_input_size)
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
masks = processor.post_process_masks(
|
||||
dummy_masks, torch.tensor(original_sizes), torch.tensor(reshaped_input_size)
|
||||
)
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
# should also work with np
|
||||
dummy_masks = [np.ones((1, 3, 5, 5))]
|
||||
masks = processor.post_process_masks(dummy_masks, np.array(original_sizes), np.array(reshaped_input_size))
|
||||
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
dummy_masks = [[1, 0], [0, 1]]
|
||||
with self.assertRaises(TypeError):
|
||||
masks = processor.post_process_masks(dummy_masks, np.array(original_sizes), np.array(reshaped_input_size))
|
||||
|
||||
def test_rle_encoding(self):
|
||||
"""
|
||||
Test the run-length encoding function.
|
||||
"""
|
||||
# Test that a mask of all zeros returns a single run [height * width].
|
||||
input_mask = torch.zeros((1, 2, 2), dtype=torch.long) # shape: 1 x 2 x 2
|
||||
rle = _mask_to_rle_pytorch(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
# For a 2x2 all-zero mask, we expect a single run of length 4:
|
||||
self.assertEqual(rle[0]["counts"], [4])
|
||||
|
||||
# Test that a mask of all ones returns [0, height * width].
|
||||
input_mask = torch.ones((1, 2, 2), dtype=torch.long) # shape: 1 x 2 x 2
|
||||
rle = _mask_to_rle_pytorch(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
# For a 2x2 all-one mask, we expect two runs: [0, 4].
|
||||
self.assertEqual(rle[0]["counts"], [0, 4])
|
||||
|
||||
# Test a mask with mixed 0s and 1s to ensure the run-length encoding is correct.
|
||||
# Example mask:
|
||||
# Row 0: [0, 1]
|
||||
# Row 1: [1, 1]
|
||||
# This is shape (1, 2, 2).
|
||||
# Flattened in Fortran order -> [0, 1, 1, 1].
|
||||
# The RLE for [0,1,1,1] is [1, 3].
|
||||
input_mask = torch.tensor([[[0, 1], [1, 1]]], dtype=torch.long)
|
||||
rle = _mask_to_rle_pytorch(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
self.assertEqual(rle[0]["counts"], [1, 3]) # 1 zero, followed by 3 ones
|
||||
Reference in New Issue
Block a user