Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DETR] Add num_channels attribute #18714

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/transformers/models/detr/configuration_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class DetrConfig(PretrainedConfig):
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PretrainedConfig`] for more information.


Args:
num_channels (`int`, *optional*, defaults to 3):
The number of input channels.
num_queries (`int`, *optional*, defaults to 100):
Number of object queries, i.e. detection slots. This is the maximal number of objects [`DetrModel`] can
detect in a single image. For COCO, we recommend 100 queries.
Expand Down Expand Up @@ -132,6 +133,7 @@ class DetrConfig(PretrainedConfig):

def __init__(
self,
num_channels=3,
num_queries=100,
max_position_embeddings=1024,
encoder_layers=6,
Expand Down Expand Up @@ -167,6 +169,7 @@ def __init__(
eos_coefficient=0.1,
**kwargs
):
self.num_channels = num_channels
self.num_queries = num_queries
self.max_position_embeddings = max_position_embeddings
self.d_model = d_model
Expand Down
13 changes: 10 additions & 3 deletions src/transformers/models/detr/modeling_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class DetrTimmConvEncoder(nn.Module):

"""

def __init__(self, name: str, dilation: bool, use_pretrained_backbone: bool):
def __init__(self, name: str, dilation: bool, use_pretrained_backbone: bool, num_channels: int = 3):
super().__init__()

kwargs = {}
Expand All @@ -336,7 +336,12 @@ def __init__(self, name: str, dilation: bool, use_pretrained_backbone: bool):
requires_backends(self, ["timm"])

backbone = create_model(
name, pretrained=use_pretrained_backbone, features_only=True, out_indices=(1, 2, 3, 4), **kwargs
name,
pretrained=use_pretrained_backbone,
features_only=True,
out_indices=(1, 2, 3, 4),
in_chans=num_channels,
**kwargs,
)
# replace batch norm by frozen batch norm
with torch.no_grad():
Expand Down Expand Up @@ -1179,7 +1184,9 @@ def __init__(self, config: DetrConfig):
super().__init__(config)

# Create backbone + positional encoding
backbone = DetrTimmConvEncoder(config.backbone, config.dilation, config.use_pretrained_backbone)
backbone = DetrTimmConvEncoder(
config.backbone, config.dilation, config.use_pretrained_backbone, config.num_channels
)
position_embeddings = build_position_encoding(config)
self.backbone = DetrConvModel(backbone, position_embeddings)

Expand Down
20 changes: 20 additions & 0 deletions tests/models/detr/test_modeling_detr.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ def test_different_timm_backbone(self):

self.assertTrue(outputs)

def test_greyscale_images(self):
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

# use greyscale pixel values
inputs_dict["pixel_values"] = floats_tensor(
[self.model_tester.batch_size, 1, self.model_tester.min_size, self.model_tester.max_size]
)

# let's set num_channels to 1
config.num_channels = 1

for model_class in self.all_model_classes:
model = model_class(config)
model.to(torch_device)
model.eval()
with torch.no_grad():
outputs = model(**self._prepare_for_class(inputs_dict, model_class))

self.assertTrue(outputs)

def test_initialization(self):
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

Expand Down