Adding SegGPT (#27735)
* First commit * Improvements * More improvements * Converted original checkpoint to HF checkpoint * Fix style * Fixed forward * More improvements * More improvements * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Remove asserts * Remove unnecessary attributes * Changed model name to camel case * Improve forward doc * Improve tests * More improvements * Fix copies * Fix doc * Make SegGptImageProcessor more flexible * Added few-shot test * Fix style * Update READMEs and docs * Update READMEs * Make inputs required * Add SegGptForImageSegmentation * Make tests pass * Rename to out_indicies * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Fixed naming convention * Copying SegGptMlp from modeling_sam.py * Some minor improvements * Remove mlp_ratio * Fix docstrings * Fixed docstring match * Objects defined before use * Storing only patch_size and beta for SegGptLoss * removed _prepare_inputs method * Removed modified from headers * Renamed to output_indicies * Removed unnecessary einsums * Update tests/models/seggpt/test_modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update tests/models/seggpt/test_modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update tests/models/seggpt/test_modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Fixing issues * Raise error as soon as possible * More fixes * Fix merge * Added palette to SegGptImageProcessor * Fixed typo * Fixed shape typo * Added permute before doing palette to class mapping * Fixed style * Fixed and added tests * Fixed docstrings * Matching SegFormer API for post_processing_semantic_segmentation * Fixed copies * Fixed SegGptImageProcessor to handle both binary and RGB masks * Updated docstrings of SegGptImageProcessor * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update docs/source/en/model_doc/seggpt.md Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/configuration_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/convert_seggpt_to_hf.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update tests/models/seggpt/test_image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update tests/models/seggpt/test_modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Object definitions above & fix style * Renamed output_indices to intermediate_feature_indices * Removed unnecessary check on bool_masked_pos * Loss first in the outputs * Added validation for do_normalize * Improved SegGptImageProcessor and added new tests * Added comment * Added docstrings to SegGptLoss * Reimplemented ensemble condition logic in SegGptEncoder * Update src/transformers/models/seggpt/__init__.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/seggpt/convert_seggpt_to_hf.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Update src/transformers/models/seggpt/configuration_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Updated docstrings to use post_process_semantic_segmentation * Fixed typo on docstrings * moved pixel values test to test_image_processing_seggpt * Addressed comments * Update src/transformers/models/seggpt/configuration_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/image_processing_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/configuration_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * Updated docstrings for SegGptLoss * Address comments * Added SegGpt example to model docs * Update src/transformers/models/seggpt/modeling_seggpt.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * moved patchify and unpatchify * Rename checkpoint * Renamed intermediate_features to intermediate_hidden_states for consistency * Update src/transformers/models/seggpt/configuration_seggpt.py Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> * Replaced post_process_masks for post_process_semantic_segmentation in the docs --------- Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> Co-authored-by: Niels <niels.rogge1@gmail.com> Co-authored-by: Eduardo Pacheco <eduardo.pacheco@limehome.com> Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
This commit is contained in:
0
tests/models/seggpt/__init__.py
Normal file
0
tests/models/seggpt/__init__.py
Normal file
231
tests/models/seggpt/test_image_processing_seggpt.py
Normal file
231
tests/models/seggpt/test_image_processing_seggpt.py
Normal file
@@ -0,0 +1,231 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2024 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
|
||||
|
||||
import numpy as np
|
||||
from datasets import load_dataset
|
||||
|
||||
from transformers.testing_utils import require_torch, require_vision, slow
|
||||
from transformers.utils import is_torch_available, is_vision_available
|
||||
|
||||
from ...test_image_processing_common import ImageProcessingTestMixin, prepare_image_inputs
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
|
||||
from transformers.models.seggpt.modeling_seggpt import SegGptImageSegmentationOutput
|
||||
|
||||
if is_vision_available():
|
||||
from transformers import SegGptImageProcessor
|
||||
|
||||
|
||||
class SegGptImageProcessingTester(unittest.TestCase):
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=7,
|
||||
num_channels=3,
|
||||
image_size=18,
|
||||
min_resolution=30,
|
||||
max_resolution=400,
|
||||
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 {"height": 18, "width": 18}
|
||||
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_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,
|
||||
}
|
||||
|
||||
def expected_output_image_shape(self, images):
|
||||
return self.num_channels, self.size["height"], self.size["width"]
|
||||
|
||||
def expected_post_processed_shape(self):
|
||||
return self.size["height"] // 2, self.size["width"]
|
||||
|
||||
def get_fake_image_segmentation_output(self):
|
||||
torch.manual_seed(42)
|
||||
return SegGptImageSegmentationOutput(
|
||||
pred_masks=torch.rand(self.batch_size, self.num_channels, self.size["height"], self.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,
|
||||
)
|
||||
|
||||
|
||||
def prepare_mask():
|
||||
ds = load_dataset("EduardoPacheco/seggpt-example-data")["train"]
|
||||
return ds[0]["mask"].convert("L")
|
||||
|
||||
|
||||
def prepare_img():
|
||||
ds = load_dataset("EduardoPacheco/seggpt-example-data")["train"]
|
||||
images = [image.convert("RGB") for image in ds["image"]]
|
||||
masks = [image.convert("RGB") for image in ds["mask"]]
|
||||
return images, masks
|
||||
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
class SegGptImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
|
||||
image_processing_class = SegGptImageProcessor if is_vision_available() else None
|
||||
|
||||
def setUp(self):
|
||||
self.image_processor_tester = SegGptImageProcessingTester(self)
|
||||
|
||||
@property
|
||||
def image_processor_dict(self):
|
||||
return self.image_processor_tester.prepare_image_processor_dict()
|
||||
|
||||
def test_image_processor_properties(self):
|
||||
image_processing = self.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"))
|
||||
|
||||
def test_image_processor_from_dict_with_kwargs(self):
|
||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
|
||||
self.assertEqual(image_processor.size, {"height": 18, "width": 18})
|
||||
|
||||
image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=42)
|
||||
self.assertEqual(image_processor.size, {"height": 42, "width": 42})
|
||||
|
||||
def test_image_processor_palette(self):
|
||||
num_labels = 3
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
palette = image_processing.get_palette(num_labels)
|
||||
self.assertEqual(len(palette), num_labels + 1)
|
||||
self.assertEqual(palette[0], (0, 0, 0))
|
||||
|
||||
def test_mask_equivalence(self):
|
||||
image_processor = SegGptImageProcessor()
|
||||
|
||||
mask_binary = prepare_mask()
|
||||
mask_rgb = mask_binary.convert("RGB")
|
||||
|
||||
inputs_binary = image_processor(images=None, prompt_masks=mask_binary, return_tensors="pt")
|
||||
inputs_rgb = image_processor(images=None, prompt_masks=mask_rgb, return_tensors="pt")
|
||||
|
||||
self.assertTrue((inputs_binary["prompt_masks"] == inputs_rgb["prompt_masks"]).all().item())
|
||||
|
||||
def test_mask_to_rgb(self):
|
||||
image_processing = self.image_processing_class(**self.image_processor_dict)
|
||||
mask = prepare_mask()
|
||||
mask = np.array(mask)
|
||||
mask = (mask > 0).astype(np.uint8)
|
||||
|
||||
def check_two_colors(image, color1=(0, 0, 0), color2=(255, 255, 255)):
|
||||
pixels = image.transpose(1, 2, 0).reshape(-1, 3)
|
||||
unique_colors = np.unique(pixels, axis=0)
|
||||
if len(unique_colors) == 2 and (color1 in unique_colors) and (color2 in unique_colors):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
num_labels = 1
|
||||
palette = image_processing.get_palette(num_labels)
|
||||
|
||||
# Should only duplicate repeat class indices map, hence only (0,0,0) and (1,1,1)
|
||||
mask_duplicated = image_processing.mask_to_rgb(mask)
|
||||
# Mask using palette, since only 1 class is present we have colors (0,0,0) and (255,255,255)
|
||||
mask_painted = image_processing.mask_to_rgb(mask, palette=palette)
|
||||
|
||||
self.assertTrue(check_two_colors(mask_duplicated, color2=(1, 1, 1)))
|
||||
self.assertTrue(check_two_colors(mask_painted, color2=(255, 255, 255)))
|
||||
|
||||
def test_post_processing_semantic_segmentation(self):
|
||||
image_processor = self.image_processing_class(**self.image_processor_dict)
|
||||
outputs = self.image_processor_tester.get_fake_image_segmentation_output()
|
||||
post_processed = image_processor.post_process_semantic_segmentation(outputs)
|
||||
|
||||
self.assertEqual(len(post_processed), self.image_processor_tester.batch_size)
|
||||
|
||||
expected_semantic_map_shape = self.image_processor_tester.expected_post_processed_shape()
|
||||
self.assertEqual(post_processed[0].shape, expected_semantic_map_shape)
|
||||
|
||||
@slow
|
||||
def test_pixel_values(self):
|
||||
images, masks = prepare_img()
|
||||
input_image = images[1]
|
||||
prompt_image = images[0]
|
||||
prompt_mask = masks[0]
|
||||
|
||||
image_processor = SegGptImageProcessor.from_pretrained("BAAI/seggpt-vit-large")
|
||||
|
||||
inputs = image_processor(
|
||||
images=input_image, prompt_images=prompt_image, prompt_masks=prompt_mask, return_tensors="pt"
|
||||
)
|
||||
|
||||
# Verify pixel values
|
||||
expected_prompt_pixel_values = torch.tensor(
|
||||
[
|
||||
[[-0.6965, -0.6965, -0.6965], [-0.6965, -0.6965, -0.6965], [-0.6965, -0.6965, -0.6965]],
|
||||
[[1.6583, 1.6583, 1.6583], [1.6583, 1.6583, 1.6583], [1.6583, 1.6583, 1.6583]],
|
||||
[[2.3088, 2.3088, 2.3088], [2.3088, 2.3088, 2.3088], [2.3088, 2.3088, 2.3088]],
|
||||
]
|
||||
)
|
||||
|
||||
expected_pixel_values = torch.tensor(
|
||||
[
|
||||
[[1.6324, 1.6153, 1.5810], [1.6153, 1.5982, 1.5810], [1.5810, 1.5639, 1.5639]],
|
||||
[[1.2731, 1.2556, 1.2206], [1.2556, 1.2381, 1.2031], [1.2206, 1.2031, 1.1681]],
|
||||
[[1.6465, 1.6465, 1.6465], [1.6465, 1.6465, 1.6465], [1.6291, 1.6291, 1.6291]],
|
||||
]
|
||||
)
|
||||
|
||||
expected_prompt_masks = torch.tensor(
|
||||
[
|
||||
[[-2.1179, -2.1179, -2.1179], [-2.1179, -2.1179, -2.1179], [-2.1179, -2.1179, -2.1179]],
|
||||
[[-2.0357, -2.0357, -2.0357], [-2.0357, -2.0357, -2.0357], [-2.0357, -2.0357, -2.0357]],
|
||||
[[-1.8044, -1.8044, -1.8044], [-1.8044, -1.8044, -1.8044], [-1.8044, -1.8044, -1.8044]],
|
||||
]
|
||||
)
|
||||
|
||||
self.assertTrue(torch.allclose(inputs.pixel_values[0, :, :3, :3], expected_pixel_values, atol=1e-4))
|
||||
self.assertTrue(
|
||||
torch.allclose(inputs.prompt_pixel_values[0, :, :3, :3], expected_prompt_pixel_values, atol=1e-4)
|
||||
)
|
||||
self.assertTrue(torch.allclose(inputs.prompt_masks[0, :, :3, :3], expected_prompt_masks, atol=1e-4))
|
||||
339
tests/models/seggpt/test_modeling_seggpt.py
Normal file
339
tests/models/seggpt/test_modeling_seggpt.py
Normal file
@@ -0,0 +1,339 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2024 The HuggingFace Inc. 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.
|
||||
""" Testing suite for the PyTorch SegGpt model. """
|
||||
|
||||
|
||||
import inspect
|
||||
import unittest
|
||||
|
||||
from datasets import load_dataset
|
||||
|
||||
from transformers import SegGptConfig
|
||||
from transformers.testing_utils import (
|
||||
require_torch,
|
||||
require_vision,
|
||||
slow,
|
||||
torch_device,
|
||||
)
|
||||
from transformers.utils import cached_property, is_torch_available, is_vision_available
|
||||
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import ModelTesterMixin, floats_tensor
|
||||
from ...test_pipeline_mixin import PipelineTesterMixin
|
||||
|
||||
|
||||
if is_torch_available():
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from transformers import SegGptForImageSegmentation, SegGptModel
|
||||
from transformers.models.seggpt.modeling_seggpt import SEGGPT_PRETRAINED_MODEL_ARCHIVE_LIST
|
||||
|
||||
|
||||
if is_vision_available():
|
||||
from transformers import SegGptImageProcessor
|
||||
|
||||
|
||||
class SegGptModelTester:
|
||||
def __init__(
|
||||
self,
|
||||
parent,
|
||||
batch_size=2,
|
||||
image_size=30,
|
||||
patch_size=2,
|
||||
num_channels=3,
|
||||
is_training=False,
|
||||
use_labels=True,
|
||||
hidden_size=32,
|
||||
num_hidden_layers=2,
|
||||
num_attention_heads=4,
|
||||
hidden_act="gelu",
|
||||
hidden_dropout_prob=0.1,
|
||||
attention_probs_dropout_prob=0.1,
|
||||
initializer_range=0.02,
|
||||
mlp_ratio=2.0,
|
||||
merge_index=0,
|
||||
intermediate_hidden_state_indices=[1],
|
||||
pretrain_image_size=10,
|
||||
decoder_hidden_size=10,
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
self.image_size = image_size
|
||||
self.patch_size = patch_size
|
||||
self.num_channels = num_channels
|
||||
self.is_training = is_training
|
||||
self.use_labels = use_labels
|
||||
self.hidden_size = hidden_size
|
||||
self.num_hidden_layers = num_hidden_layers
|
||||
self.num_attention_heads = num_attention_heads
|
||||
self.hidden_act = hidden_act
|
||||
self.hidden_dropout_prob = hidden_dropout_prob
|
||||
self.attention_probs_dropout_prob = attention_probs_dropout_prob
|
||||
self.initializer_range = initializer_range
|
||||
self.mlp_ratio = mlp_ratio
|
||||
self.merge_index = merge_index
|
||||
self.intermediate_hidden_state_indices = intermediate_hidden_state_indices
|
||||
self.pretrain_image_size = pretrain_image_size
|
||||
self.decoder_hidden_size = decoder_hidden_size
|
||||
|
||||
# in SegGpt, the seq length equals the number of patches (we don't use the [CLS] token)
|
||||
num_patches = (image_size // patch_size) ** 2
|
||||
self.seq_length = num_patches
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size // 2, self.image_size])
|
||||
prompt_pixel_values = floats_tensor(
|
||||
[self.batch_size, self.num_channels, self.image_size // 2, self.image_size]
|
||||
)
|
||||
prompt_masks = floats_tensor([self.batch_size, self.num_channels, self.image_size // 2, self.image_size])
|
||||
|
||||
labels = None
|
||||
if self.use_labels:
|
||||
labels = floats_tensor([self.batch_size, self.num_channels, self.image_size // 2, self.image_size])
|
||||
|
||||
config = self.get_config()
|
||||
|
||||
return config, pixel_values, prompt_pixel_values, prompt_masks, labels
|
||||
|
||||
def get_config(self):
|
||||
return SegGptConfig(
|
||||
image_size=self.image_size,
|
||||
patch_size=self.patch_size,
|
||||
num_channels=self.num_channels,
|
||||
hidden_size=self.hidden_size,
|
||||
num_hidden_layers=self.num_hidden_layers,
|
||||
num_attention_heads=self.num_attention_heads,
|
||||
hidden_act=self.hidden_act,
|
||||
hidden_dropout_prob=self.hidden_dropout_prob,
|
||||
initializer_range=self.initializer_range,
|
||||
mlp_ratio=self.mlp_ratio,
|
||||
merge_index=self.merge_index,
|
||||
intermediate_hidden_state_indices=self.intermediate_hidden_state_indices,
|
||||
pretrain_image_size=self.pretrain_image_size,
|
||||
decoder_hidden_size=self.decoder_hidden_size,
|
||||
)
|
||||
|
||||
def create_and_check_model(self, config, pixel_values, prompt_pixel_values, prompt_masks, labels):
|
||||
model = SegGptModel(config=config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
result = model(pixel_values, prompt_pixel_values, prompt_masks)
|
||||
self.parent.assertEqual(
|
||||
result.last_hidden_state.shape,
|
||||
(
|
||||
self.batch_size,
|
||||
self.image_size // self.patch_size,
|
||||
self.image_size // self.patch_size,
|
||||
self.hidden_size,
|
||||
),
|
||||
)
|
||||
|
||||
def prepare_config_and_inputs_for_common(self):
|
||||
config_and_inputs = self.prepare_config_and_inputs()
|
||||
(
|
||||
config,
|
||||
pixel_values,
|
||||
prompt_pixel_values,
|
||||
prompt_masks,
|
||||
labels,
|
||||
) = config_and_inputs
|
||||
inputs_dict = {
|
||||
"pixel_values": pixel_values,
|
||||
"prompt_pixel_values": prompt_pixel_values,
|
||||
"prompt_masks": prompt_masks,
|
||||
}
|
||||
return config, inputs_dict
|
||||
|
||||
|
||||
@require_torch
|
||||
class SegGptModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
|
||||
"""
|
||||
Here we also overwrite some of the tests of test_modeling_common.py, as SegGpt does not use input_ids, inputs_embeds,
|
||||
attention_mask and seq_length.
|
||||
"""
|
||||
|
||||
all_model_classes = (SegGptModel, SegGptForImageSegmentation) if is_torch_available() else ()
|
||||
fx_compatible = False
|
||||
|
||||
test_pruning = False
|
||||
test_resize_embeddings = False
|
||||
test_head_masking = False
|
||||
test_torchscript = False
|
||||
pipeline_model_mapping = (
|
||||
{"feature-extraction": SegGptModel, "mask-generation": SegGptModel} if is_torch_available() else {}
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = SegGptModelTester(self)
|
||||
self.config_tester = ConfigTester(self, config_class=SegGptConfig, has_text_modality=False)
|
||||
|
||||
def test_config(self):
|
||||
self.config_tester.run_common_tests()
|
||||
|
||||
@unittest.skip(reason="SegGpt does not use inputs_embeds")
|
||||
def test_inputs_embeds(self):
|
||||
pass
|
||||
|
||||
def test_model_common_attributes(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
self.assertIsInstance(model.get_input_embeddings(), (nn.Module))
|
||||
|
||||
def test_forward_signature(self):
|
||||
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
model = model_class(config)
|
||||
signature = inspect.signature(model.forward)
|
||||
# signature.parameters is an OrderedDict => so arg_names order is deterministic
|
||||
arg_names = [*signature.parameters.keys()]
|
||||
|
||||
expected_arg_names = ["pixel_values", "prompt_pixel_values", "prompt_masks"]
|
||||
self.assertListEqual(arg_names[:3], expected_arg_names)
|
||||
|
||||
def test_model(self):
|
||||
config_and_inputs = self.model_tester.prepare_config_and_inputs()
|
||||
self.model_tester.create_and_check_model(*config_and_inputs)
|
||||
|
||||
def test_hidden_states_output(self):
|
||||
def check_hidden_states_output(inputs_dict, config, model_class):
|
||||
model = model_class(config)
|
||||
model.to(torch_device)
|
||||
model.eval()
|
||||
|
||||
with torch.no_grad():
|
||||
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||
|
||||
hidden_states = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
|
||||
|
||||
expected_num_layers = getattr(
|
||||
self.model_tester, "expected_num_hidden_layers", self.model_tester.num_hidden_layers + 1
|
||||
)
|
||||
self.assertEqual(len(hidden_states), expected_num_layers)
|
||||
|
||||
patch_height = patch_width = config.image_size // config.patch_size
|
||||
|
||||
self.assertListEqual(
|
||||
list(hidden_states[0].shape[-3:]),
|
||||
[patch_height, patch_width, self.model_tester.hidden_size],
|
||||
)
|
||||
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
|
||||
for model_class in self.all_model_classes:
|
||||
inputs_dict["output_hidden_states"] = True
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
# check that output_hidden_states also work using config
|
||||
del inputs_dict["output_hidden_states"]
|
||||
config.output_hidden_states = True
|
||||
|
||||
check_hidden_states_output(inputs_dict, config, model_class)
|
||||
|
||||
@slow
|
||||
def test_model_from_pretrained(self):
|
||||
for model_name in SEGGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
|
||||
model = SegGptModel.from_pretrained(model_name)
|
||||
self.assertIsNotNone(model)
|
||||
|
||||
|
||||
def prepare_img():
|
||||
ds = load_dataset("EduardoPacheco/seggpt-example-data")["train"]
|
||||
images = [image.convert("RGB") for image in ds["image"]]
|
||||
masks = [image.convert("RGB") for image in ds["mask"]]
|
||||
return images, masks
|
||||
|
||||
|
||||
@require_torch
|
||||
@require_vision
|
||||
class SegGptModelIntegrationTest(unittest.TestCase):
|
||||
@cached_property
|
||||
def default_image_processor(self):
|
||||
return SegGptImageProcessor.from_pretrained("BAAI/seggpt-vit-large") if is_vision_available() else None
|
||||
|
||||
@slow
|
||||
def test_one_shot_inference(self):
|
||||
model = SegGptForImageSegmentation.from_pretrained("BAAI/seggpt-vit-large").to(torch_device)
|
||||
|
||||
image_processor = self.default_image_processor
|
||||
|
||||
images, masks = prepare_img()
|
||||
input_image = images[1]
|
||||
prompt_image = images[0]
|
||||
prompt_mask = masks[0]
|
||||
|
||||
inputs = image_processor(
|
||||
images=input_image, prompt_images=prompt_image, prompt_masks=prompt_mask, return_tensors="pt"
|
||||
)
|
||||
|
||||
inputs = inputs.to(torch_device)
|
||||
# forward pass
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs)
|
||||
|
||||
# verify the logits
|
||||
expected_shape = torch.Size((1, 3, 896, 448))
|
||||
self.assertEqual(outputs.pred_masks.shape, expected_shape)
|
||||
|
||||
expected_slice = torch.tensor(
|
||||
[
|
||||
[[-2.1208, -2.1190, -2.1198], [-2.1237, -2.1228, -2.1227], [-2.1232, -2.1226, -2.1228]],
|
||||
[[-2.0405, -2.0396, -2.0403], [-2.0434, -2.0434, -2.0433], [-2.0428, -2.0432, -2.0434]],
|
||||
[[-1.8102, -1.8088, -1.8099], [-1.8131, -1.8126, -1.8129], [-1.8130, -1.8128, -1.8131]],
|
||||
]
|
||||
).to(torch_device)
|
||||
|
||||
self.assertTrue(torch.allclose(outputs.pred_masks[0, :, :3, :3], expected_slice, atol=1e-4))
|
||||
|
||||
result = image_processor.post_process_semantic_segmentation(outputs, [input_image.size[::-1]])[0]
|
||||
|
||||
result_expected_shape = torch.Size((170, 297))
|
||||
expected_area = 1082
|
||||
area = (result > 0).sum().item()
|
||||
self.assertEqual(result.shape, result_expected_shape)
|
||||
self.assertEqual(area, expected_area)
|
||||
|
||||
@slow
|
||||
def test_few_shot_inference(self):
|
||||
model = SegGptForImageSegmentation.from_pretrained("BAAI/seggpt-vit-large").to(torch_device)
|
||||
image_processor = self.default_image_processor
|
||||
|
||||
images, masks = prepare_img()
|
||||
input_images = [images[1]] * 2
|
||||
prompt_images = [images[0], images[2]]
|
||||
prompt_masks = [masks[0], masks[2]]
|
||||
|
||||
inputs = image_processor(
|
||||
images=input_images, prompt_images=prompt_images, prompt_masks=prompt_masks, return_tensors="pt"
|
||||
)
|
||||
|
||||
inputs = {k: v.to(torch_device) for k, v in inputs.items()}
|
||||
with torch.no_grad():
|
||||
outputs = model(**inputs, feature_ensemble=True)
|
||||
|
||||
expected_shape = torch.Size((2, 3, 896, 448))
|
||||
expected_slice = torch.tensor(
|
||||
[
|
||||
[[-2.1201, -2.1192, -2.1189], [-2.1217, -2.1210, -2.1204], [-2.1216, -2.1202, -2.1194]],
|
||||
[[-2.0393, -2.0390, -2.0387], [-2.0402, -2.0402, -2.0397], [-2.0400, -2.0394, -2.0388]],
|
||||
[[-1.8083, -1.8076, -1.8077], [-1.8105, -1.8102, -1.8099], [-1.8105, -1.8095, -1.8090]],
|
||||
]
|
||||
).to(torch_device)
|
||||
|
||||
self.assertEqual(outputs.pred_masks.shape, expected_shape)
|
||||
self.assertTrue(torch.allclose(outputs.pred_masks[0, :, 448:451, :3], expected_slice, atol=4e-4))
|
||||
@@ -958,6 +958,16 @@ class ModelTesterMixin:
|
||||
traced_model = torch.jit.trace(
|
||||
model, (input_ids, bbox), check_trace=False
|
||||
) # when traced model is checked, an error is produced due to name mangling
|
||||
elif (
|
||||
"pixel_values" in inputs and "prompt_pixel_values" in inputs and "prompt_masks" in inputs
|
||||
): # SegGpt requires additional inputs
|
||||
pixel_values = inputs["pixel_values"]
|
||||
prompt_pixel_values = inputs["prompt_pixel_values"]
|
||||
prompt_masks = inputs["prompt_masks"]
|
||||
model(pixel_values, prompt_pixel_values, prompt_masks)
|
||||
traced_model = torch.jit.trace(
|
||||
model, (pixel_values, prompt_pixel_values, prompt_masks), check_trace=False
|
||||
) # when traced model is checked, an error is produced due to name mangling
|
||||
else:
|
||||
main_input = inputs[main_input_name]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user