Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsRogge committed Aug 25, 2024
1 parent edbd7eb commit be0cf18
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_deta_config(model_name):
depths=(2, 2, 18, 2),
num_heads=(6, 12, 24, 48),
window_size=12,
out_features=["stage2", "stage3", "stage4"],
out_indices=[2, 3, 4],
)

config = DetaConfig(
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/models/dpt/convert_dpt_beit_to_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def get_dpt_config(model_name):
num_hidden_layers = 12
num_attention_heads = 12
intermediate_size = 3072
out_features = ["stage3", "stage6", "stage9", "stage12"] # beit-base-384 uses [2, 5, 8, 11]
out_indices = [3, 6, 9, 12] # beit-base-384 uses [2, 5, 8, 11] in original implementation

if "large" in model_name:
hidden_size = 1024
num_hidden_layers = 24
num_attention_heads = 16
intermediate_size = 4096
out_features = ["stage6", "stage12", "stage18", "stage24"] # beit-large-512 uses [5, 11, 17, 23]
out_indices = [6, 12, 18, 24] # beit-large-512 uses [5, 11, 17, 23] in original implementation

if "512" in model_name:
image_size = 512
Expand All @@ -58,7 +58,7 @@ def get_dpt_config(model_name):
num_attention_heads=num_attention_heads,
use_relative_position_bias=True,
reshape_hidden_states=False,
out_features=out_features,
out_indices=out_indices,
)

neck_hidden_sizes = [256, 512, 1024, 1024] if "large" in model_name else [96, 192, 384, 768]
Expand Down
6 changes: 3 additions & 3 deletions tests/models/depth_anything/test_modeling_depth_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
num_hidden_layers=2,
num_attention_heads=2,
intermediate_size=8,
out_features=["stage1", "stage2"],
out_indices=[1, 2],
apply_layernorm=False,
reshape_hidden_states=False,
neck_hidden_sizes=[2, 2],
Expand All @@ -68,7 +68,7 @@ def __init__(
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.intermediate_size = intermediate_size
self.out_features = out_features
self.out_indices = out_indices
self.apply_layernorm = apply_layernorm
self.reshape_hidden_states = reshape_hidden_states
self.use_labels = use_labels
Expand Down Expand Up @@ -111,7 +111,7 @@ def get_backbone_config(self):
num_attention_heads=self.num_attention_heads,
intermediate_size=self.intermediate_size,
is_training=self.is_training,
out_features=self.out_features,
out_indices=self.out_indices,
reshape_hidden_states=self.reshape_hidden_states,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/models/dinov2/test_modeling_dinov2.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify hidden states
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
expected_size = self.image_size // config.patch_size
self.parent.assertListEqual(
list(result.feature_maps[0].shape), [self.batch_size, model.channels[0], expected_size, expected_size]
)

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = Dinov2Backbone(config=config)
model.to(torch_device)
model.eval()
Expand Down
6 changes: 3 additions & 3 deletions tests/models/dpt/test_modeling_dpt_auto_backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
num_hidden_layers=2,
num_attention_heads=2,
intermediate_size=8,
out_features=["stage1", "stage2"],
out_indices=[1, 2],
apply_layernorm=False,
reshape_hidden_states=False,
neck_hidden_sizes=[2, 2],
Expand All @@ -68,7 +68,7 @@ def __init__(
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.intermediate_size = intermediate_size
self.out_features = out_features
self.out_indices = out_indices
self.apply_layernorm = apply_layernorm
self.reshape_hidden_states = reshape_hidden_states
self.use_labels = use_labels
Expand Down Expand Up @@ -108,7 +108,7 @@ def get_backbone_config(self):
num_attention_heads=self.num_attention_heads,
intermediate_size=self.intermediate_size,
is_training=self.is_training,
out_features=self.out_features,
out_indices=self.out_indices,
reshape_hidden_states=self.reshape_hidden_states,
)

Expand Down
11 changes: 4 additions & 7 deletions tests/models/focalnet/test_modeling_focalnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def __init__(
use_labels=True,
type_sequence_label_size=10,
encoder_stride=8,
out_features=["stage1", "stage2"],
out_indices=[1, 2],
):
self.parent = parent
Expand Down Expand Up @@ -100,7 +99,6 @@ def __init__(
self.use_labels = use_labels
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):
Expand Down Expand Up @@ -135,7 +133,6 @@ def get_config(self):
layer_norm_eps=self.layer_norm_eps,
initializer_range=self.initializer_range,
encoder_stride=self.encoder_stride,
out_features=self.out_features,
out_indices=self.out_indices,
)

Expand All @@ -157,15 +154,15 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify feature maps
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
self.parent.assertListEqual(list(result.feature_maps[0].shape), [self.batch_size, self.image_size, 8, 8])

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))
self.parent.assertListEqual(model.channels, config.hidden_sizes[:-1])

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = FocalNetBackbone(config=config)
model.to(torch_device)
model.eval()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def get_config(self):
depths=[1, 1, 1, 1],
num_heads=[1, 1, 1, 1],
image_size=self.image_size,
out_features=["stage2", "stage3", "stage4"],
out_indices=[2, 3, 4],
)
text_backbone = {
Expand Down
8 changes: 4 additions & 4 deletions tests/models/hiera/test_modeling_hiera.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify hidden states
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
num_patches = config.image_size[0] // config.patch_stride[0] // config.masked_unit_size[0]
self.parent.assertListEqual(
list(result.feature_maps[0].shape), [self.batch_size, model.channels[0], num_patches, num_patches]
)

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = HieraBackbone(config=config)
model.to(torch_device)
model.eval()
Expand Down
12 changes: 2 additions & 10 deletions tests/models/maskformer/test_modeling_maskformer_swin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __init__(
use_labels=True,
type_sequence_label_size=10,
encoder_stride=8,
out_features=["stage1", "stage2", "stage3"],
out_indices=[1, 2, 3],
):
self.parent = parent
Expand All @@ -90,7 +89,6 @@ def __init__(
self.use_labels = use_labels
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):
Expand Down Expand Up @@ -124,7 +122,6 @@ def get_config(self):
layer_norm_eps=self.layer_norm_eps,
initializer_range=self.initializer_range,
encoder_stride=self.encoder_stride,
out_features=self.out_features,
out_indices=self.out_indices,
)

Expand All @@ -146,18 +143,13 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify feature maps
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
self.parent.assertListEqual(list(result.feature_maps[0].shape), [13, 16, 16, 16])

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))
self.parent.assertListEqual(model.channels, [16, 32, 64])

# verify ValueError
with self.parent.assertRaises(ValueError):
config.out_features = ["stem"]
model = MaskFormerSwinBackbone(config=config)

def prepare_config_and_inputs_for_common(self):
config_and_inputs = self.prepare_config_and_inputs()
config, pixel_values, labels = config_and_inputs
Expand Down
31 changes: 5 additions & 26 deletions tests/models/pvt_v2/test_modeling_pvt_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def create_and_check_backbone(self, config, pixel_values, labels):
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertListEqual(model.channels, config.hidden_sizes[1:])

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = PvtV2Backbone(config=config)
model.to(torch_device)
model.eval()
Expand Down Expand Up @@ -397,33 +397,12 @@ def test_config(self):
num_stages = len(config.depths) if hasattr(config, "depths") else config.num_hidden_layers
expected_stage_names = [f"stage{idx}" for idx in range(1, num_stages + 1)]
self.assertEqual(config.stage_names, expected_stage_names)
self.assertTrue(set(config.out_features).issubset(set(config.stage_names)))

# Test out_features and out_indices are correctly set
# out_features and out_indices both None
config = config_class(out_features=None, out_indices=None)
self.assertEqual(config.out_features, [config.stage_names[-1]])
# Test out_indices are correctly set
# out_indices set to None
config = config_class(out_indices=None)
self.assertEqual(config.out_indices, [len(config.stage_names) - 1])

# out_features and out_indices both set
config = config_class(out_features=["stage1", "stage2"], out_indices=[0, 1])
self.assertEqual(config.out_features, ["stage1", "stage2"])
self.assertEqual(config.out_indices, [0, 1])

# Only out_features set
config = config_class(out_features=["stage2", "stage4"])
self.assertEqual(config.out_features, ["stage2", "stage4"])
self.assertEqual(config.out_indices, [1, 3])

# Only out_indices set
config = config_class(out_indices=[0, 2])
self.assertEqual(config.out_features, [config.stage_names[0], config.stage_names[2]])
self.assertEqual(config.out_indices, [0, 2])

# Error raised when out_indices do not correspond to out_features
with self.assertRaises(ValueError):
config = config_class(out_features=["stage1", "stage2"], out_indices=[0, 2])

def test_config_save_pretrained(self):
config_class = self.config_class
config_first = config_class(out_indices=[0, 1, 2, 3])
Expand Down
11 changes: 4 additions & 7 deletions tests/models/resnet/test_modeling_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __init__(
hidden_act="relu",
num_labels=3,
scope=None,
out_features=["stage2", "stage3", "stage4"],
out_indices=[2, 3, 4],
):
self.parent = parent
Expand All @@ -70,7 +69,6 @@ def __init__(
self.num_labels = num_labels
self.scope = scope
self.num_stages = len(hidden_sizes)
self.out_features = out_features
self.out_indices = out_indices

def prepare_config_and_inputs(self):
Expand All @@ -92,7 +90,6 @@ def get_config(self):
depths=self.depths,
hidden_act=self.hidden_act,
num_labels=self.num_labels,
out_features=self.out_features,
out_indices=self.out_indices,
)

Expand Down Expand Up @@ -122,15 +119,15 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify feature maps
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
self.parent.assertListEqual(list(result.feature_maps[0].shape), [self.batch_size, self.hidden_sizes[1], 4, 4])

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))
self.parent.assertListEqual(model.channels, config.hidden_sizes[1:])

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = ResNetBackbone(config=config)
model.to(torch_device)
model.eval()
Expand Down
1 change: 0 additions & 1 deletion tests/models/rt_detr/test_modeling_rt_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def get_config(self):
embeddings_size=10,
hidden_sizes=hidden_sizes,
depths=[1, 1, 2, 1],
out_features=["stage2", "stage3", "stage4"],
out_indices=[2, 3, 4],
)
return RTDetrConfig.from_backbone_configs(
Expand Down
11 changes: 4 additions & 7 deletions tests/models/rt_detr/test_modeling_rt_detr_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def __init__(
hidden_act="relu",
num_labels=3,
scope=None,
out_features=["stage2", "stage3", "stage4"],
out_indices=[2, 3, 4],
):
self.parent = parent
Expand All @@ -58,7 +57,6 @@ def __init__(
self.num_labels = num_labels
self.scope = scope
self.num_stages = len(hidden_sizes)
self.out_features = out_features
self.out_indices = out_indices

def prepare_config_and_inputs(self):
Expand All @@ -80,7 +78,6 @@ def get_config(self):
depths=self.depths,
hidden_act=self.hidden_act,
num_labels=self.num_labels,
out_features=self.out_features,
out_indices=self.out_indices,
)

Expand All @@ -91,15 +88,15 @@ def create_and_check_backbone(self, config, pixel_values, labels):
result = model(pixel_values)

# verify feature maps
self.parent.assertEqual(len(result.feature_maps), len(config.out_features))
self.parent.assertEqual(len(result.feature_maps), len(config.out_indices))
self.parent.assertListEqual(list(result.feature_maps[0].shape), [self.batch_size, self.hidden_sizes[1], 4, 4])

# verify channels
self.parent.assertEqual(len(model.channels), len(config.out_features))
self.parent.assertEqual(len(model.channels), len(config.out_indices))
self.parent.assertListEqual(model.channels, config.hidden_sizes[1:])

# verify backbone works with out_features=None
config.out_features = None
# verify backbone works with out_indices=None
config.out_indices = None
model = RTDetrResNetBackbone(config=config)
model.to(torch_device)
model.eval()
Expand Down
Loading

0 comments on commit be0cf18

Please sign in to comment.