-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #12 from ddlBoJack/dev-mzy
update q-former
Showing
8 changed files
with
134 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class EncoderProjectorConcat(nn.Module): | ||
def __init__(self, config): | ||
super().__init__() | ||
self.k = config.encoder_projector_ds_rate | ||
self.linear1 = nn.Linear(1280 * self.k, 2048) | ||
self.relu = nn.ReLU() | ||
self.linear2 = nn.Linear(2048, 4096) | ||
|
||
def forward(self, x): | ||
batch_size, seq_len, dim = x.size() | ||
num_frames_to_discard = seq_len % self.k | ||
if num_frames_to_discard > 0: | ||
x = x[:, :-num_frames_to_discard, :] | ||
seq_len = x.size(1) | ||
|
||
x = x.view(batch_size, seq_len // self.k, dim * self.k) | ||
x = self.linear1(x) | ||
x = self.relu(x) | ||
x = self.linear2(x) | ||
return x | ||
|
||
class EncoderProjectorCov1d(nn.Module): | ||
def __init__(self, config): | ||
super().__init__() | ||
self.conv1d = nn.Conv1d(in_channels=1280, out_channels=1280, kernel_size=config.encoder_projector_ds_rate, stride=config.encoder_projector_ds_rate, padding=0) | ||
self.linear1 = nn.Linear(1280, 2048) | ||
self.relu1 = nn.ReLU() | ||
self.linear2 = nn.Linear(2048, 4096) | ||
self.relu2 = nn.ReLU() | ||
|
||
def forward(self, x): | ||
x = x.transpose(1, 2) | ||
x = self.conv1d(x) | ||
x = x.transpose(1, 2) | ||
x = self.relu1(x) | ||
x = self.linear1(x) | ||
x = self.relu2(x) | ||
x = self.linear2(x) | ||
return x | ||
|
||
class EncoderProjectorQFormer(nn.Module): | ||
def __init__(self, config): | ||
super().__init__() | ||
from transformers import Blip2QFormerConfig, Blip2QFormerModel | ||
configuration = Blip2QFormerConfig() | ||
configuration.encoder_hidden_size = 1280 | ||
configuration.num_hidden_layers = 2 | ||
|
||
self.query_len = 64 | ||
self.query = nn.Parameter(torch.zeros(1, self.query_len, configuration.hidden_size)) | ||
self.query.data.normal_(mean=0.0, std=1.0) | ||
self.qformer = Blip2QFormerModel(configuration) | ||
|
||
self.linear = nn.Linear(configuration.hidden_size, 4096) | ||
self.norm = nn.LayerNorm(4096, eps=1e-5) | ||
|
||
def forward(self, x, atts): | ||
query = self.query.expand(x.shape[0], -1, -1) | ||
|
||
query_output = self.qformer( | ||
query_embeds=query, | ||
encoder_hidden_states=x, | ||
encoder_attention_mask=atts, | ||
return_dict=True, | ||
) | ||
|
||
query_proj = self.norm(self.linear(query_output.last_hidden_state)) | ||
|
||
return query_proj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters