| 413 | |
| 414 | |
| 415 | class TextImageProjection(nn.Module): |
| 416 | def __init__( |
| 417 | self, |
| 418 | text_embed_dim: int = 1024, |
| 419 | image_embed_dim: int = 768, |
| 420 | cross_attention_dim: int = 768, |
| 421 | num_image_text_embeds: int = 10, |
| 422 | ): |
| 423 | super().__init__() |
| 424 | |
| 425 | self.num_image_text_embeds = num_image_text_embeds |
| 426 | self.image_embeds = nn.Linear(image_embed_dim, self.num_image_text_embeds * cross_attention_dim) |
| 427 | self.text_proj = nn.Linear(text_embed_dim, cross_attention_dim) |
| 428 | |
| 429 | def forward(self, text_embeds: torch.FloatTensor, image_embeds: torch.FloatTensor): |
| 430 | batch_size = text_embeds.shape[0] |
| 431 | |
| 432 | # image |
| 433 | image_text_embeds = self.image_embeds(image_embeds) |
| 434 | image_text_embeds = image_text_embeds.reshape(batch_size, self.num_image_text_embeds, -1) |
| 435 | |
| 436 | # text |
| 437 | text_embeds = self.text_proj(text_embeds) |
| 438 | |
| 439 | return torch.cat([image_text_embeds, text_embeds], dim=1) |
| 440 | |
| 441 | |
| 442 | class ImageProjection(nn.Module): |
no outgoing calls
no test coverage detected