Backbone add mixin tests (#22542)
* Add out_indices to backbones, deprecate out_features * Update - can specify both out_features and out_indices but not both * Add backbone mixin tests * Test tidy up * Add test_backbone for convnext * Remove redefinition of method * Update for Dinat and Nat backbones * Update tests * Smarter indexing * Add checks on config creation for backbone * PR comments
This commit is contained in:
@@ -23,6 +23,7 @@ from transformers import MaskFormerSwinConfig
|
||||
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
|
||||
from transformers.utils import is_torch_available
|
||||
|
||||
from ...test_backbone_common import BackboneTesterMixin
|
||||
from ...test_configuration_common import ConfigTester
|
||||
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
|
||||
from ...test_pipeline_mixin import PipelineTesterMixin
|
||||
@@ -64,6 +65,7 @@ class MaskFormerSwinModelTester:
|
||||
type_sequence_label_size=10,
|
||||
encoder_stride=8,
|
||||
out_features=["stage1", "stage2", "stage3"],
|
||||
out_indices=[1, 2, 3],
|
||||
):
|
||||
self.parent = parent
|
||||
self.batch_size = batch_size
|
||||
@@ -90,6 +92,7 @@ class MaskFormerSwinModelTester:
|
||||
self.type_sequence_label_size = type_sequence_label_size
|
||||
self.encoder_stride = encoder_stride
|
||||
self.out_features = out_features
|
||||
self.out_indices = out_indices
|
||||
|
||||
def prepare_config_and_inputs(self):
|
||||
pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
|
||||
@@ -123,6 +126,7 @@ class MaskFormerSwinModelTester:
|
||||
initializer_range=self.initializer_range,
|
||||
encoder_stride=self.encoder_stride,
|
||||
out_features=self.out_features,
|
||||
out_indices=self.out_indices,
|
||||
)
|
||||
|
||||
def create_and_check_model(self, config, pixel_values, labels):
|
||||
@@ -395,3 +399,48 @@ class MaskFormerSwinModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.Te
|
||||
tuple_inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
|
||||
dict_inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
|
||||
check_equivalence(model, tuple_inputs, dict_inputs, {"output_hidden_states": True})
|
||||
|
||||
|
||||
@require_torch
|
||||
class MaskFormerSwinBackboneTest(unittest.TestCase, BackboneTesterMixin):
|
||||
all_model_classes = (MaskFormerSwinBackbone,) if is_torch_available() else ()
|
||||
config_class = MaskFormerSwinConfig
|
||||
|
||||
def setUp(self):
|
||||
self.model_tester = MaskFormerSwinModelTester(self)
|
||||
|
||||
# Overriding as returned hidden states are tuples of tensors instead of a single tensor
|
||||
def test_backbone_outputs(self):
|
||||
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||
batch_size = inputs_dict["pixel_values"].shape[0]
|
||||
|
||||
for backbone_class in self.all_model_classes:
|
||||
backbone = backbone_class(config)
|
||||
backbone.to(torch_device)
|
||||
backbone.eval()
|
||||
|
||||
outputs = backbone(**inputs_dict)
|
||||
|
||||
# Test default outputs and verify feature maps
|
||||
self.assertIsInstance(outputs.feature_maps, tuple)
|
||||
self.assertTrue(len(outputs.feature_maps) == len(backbone.channels))
|
||||
for feature_map, n_channels in zip(outputs.feature_maps, backbone.channels):
|
||||
self.assertTrue(feature_map.shape[:2], (batch_size, n_channels))
|
||||
self.assertIsNone(outputs.hidden_states)
|
||||
self.assertIsNone(outputs.attentions)
|
||||
|
||||
# Test output_hidden_states=True
|
||||
outputs = backbone(**inputs_dict, output_hidden_states=True)
|
||||
self.assertIsNotNone(outputs.hidden_states)
|
||||
self.assertTrue(len(outputs.hidden_states), len(backbone.stage_names))
|
||||
# We skip the stem layer
|
||||
for hidden_states, n_channels in zip(outputs.hidden_states[1:], backbone.channels):
|
||||
for hidden_state in hidden_states:
|
||||
# Hidden states are in the format (batch_size, (height * width), n_channels)
|
||||
h_batch_size, _, h_n_channels = hidden_state.shape
|
||||
self.assertTrue((h_batch_size, h_n_channels), (batch_size, n_channels))
|
||||
|
||||
# Test output_attentions=True
|
||||
if self.has_attentions:
|
||||
outputs = backbone(**inputs_dict, output_attentions=True)
|
||||
self.assertIsNotNone(outputs.attentions)
|
||||
|
||||
Reference in New Issue
Block a user