-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
added interpolation for owlvit & owlv2. #34268
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,8 +291,21 @@ def __init__(self, config: Owlv2VisionConfig): | |
self.position_embedding = nn.Embedding(self.num_positions, self.embed_dim) | ||
self.register_buffer("position_ids", torch.arange(self.num_positions).expand((1, -1)), persistent=False) | ||
|
||
def forward(self, pixel_values: torch.FloatTensor) -> torch.Tensor: | ||
def forward(self, pixel_values: torch.FloatTensor, interpolate_pos_encoding: bool = False) -> torch.Tensor: | ||
batch_size = pixel_values.shape[0] | ||
target_size = self.config.image_size | ||
|
||
if interpolate_pos_encoding: | ||
if pixel_values.shape[2] != target_size or pixel_values.shape[3] != target_size: | ||
pixel_values = torch.nn.functional.interpolate( | ||
pixel_values, size=(target_size, target_size), mode="bilinear", align_corners=False | ||
) | ||
else: | ||
if pixel_values.shape[2] != target_size or pixel_values.shape[3] != target_size: | ||
raise ValueError( | ||
f"Input image size ({pixel_values.shape[2]}*{pixel_values.shape[3]}) doesn't match model ({target_size}*{target_size})." | ||
) | ||
Comment on lines
+298
to
+307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused: here the patches are directly interpolated but the positional encodings are untouched. This will cause a misalignment between patches and positional encodings and degrade the results, unless there's something I'm missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @molbap ,thanks for taking out time to review this. I've gone through the |
||
|
||
patch_embeds = self.patch_embedding(pixel_values) # shape = [batch_size, num_channels, height, width] | ||
patch_embeds = patch_embeds.flatten(2).transpose(1, 2) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,8 +285,21 @@ def __init__(self, config: OwlViTVisionConfig): | |
self.position_embedding = nn.Embedding(self.num_positions, self.embed_dim) | ||
self.register_buffer("position_ids", torch.arange(self.num_positions).expand((1, -1)), persistent=False) | ||
|
||
def forward(self, pixel_values: torch.FloatTensor) -> torch.Tensor: | ||
def forward(self, pixel_values: torch.FloatTensor, interpolate_pos_encoding: bool = False) -> torch.Tensor: | ||
batch_size = pixel_values.shape[0] | ||
target_size = self.config.image_size | ||
|
||
if interpolate_pos_encoding: | ||
if pixel_values.shape[2] != target_size or pixel_values.shape[3] != target_size: | ||
pixel_values = torch.nn.functional.interpolate( | ||
pixel_values, size=(target_size, target_size), mode="bilinear", align_corners=False | ||
) | ||
else: | ||
if pixel_values.shape[2] != target_size or pixel_values.shape[3] != target_size: | ||
raise ValueError( | ||
f"Input image size ({pixel_values.shape[2]}*{pixel_values.shape[3]}) doesn't match model ({target_size}*{target_size})." | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment |
||
|
||
patch_embeds = self.patch_embedding(pixel_values) # shape = [batch_size, num_channels, height, width] | ||
patch_embeds = patch_embeds.flatten(2).transpose(1, 2) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A rebase on main should get rid of this!