Views
No views yet
transformers at https://github.com/RUCAIBox/LIVE.1import torch
2import torch.nn as nn
3from transformers import T5ForConditionalGeneration, AutoModel
4
5class LiveModel(nn.Module):
6 def __init__(self):
7 super().__init__()
8
9 self.model = T5ForConditionalGeneration.from_pretrained('RUCAIBox/live-t5-base', image_fusion_encoder=True)
10 self.vision_model = AutoModel.from_pretrained('openai/clip-vit-base-patch32').vision_model
11 hidden_size = self.model.config.hidden_size
12 self.trans = nn.Sequential(
13 nn.Linear(self.vision_model.config.hidden_size, hidden_size * 4),
14 nn.ReLU(),
15 nn.Linear(hidden_size * 4, hidden_size),
16 )
17
18model = LiveModel()
19trans = torch.load('trans.t5.pth')
20model.trans.load_state_dict(trans)
21
22# kwargs to model.forward() and model.generate()
23# input_ids [batch_size, seq_len], same to hugging face
24# attention_masks [batch_size, seq_len], same to hugging face
25# labels [batch_size, seq_len], same to hugging face
26# image_embeds [batch_size, image_num*patch_num, image_hidden_size], should be transfered using `trans`, image_num can be the sentence num of text, patch_num and image_hidden_size are 50 and 768 for openai/clip-vit-base-patch32, respectively
27# images_mask [batch_size, seq_len, image_num], this is the mask in Figure 1, 1 represents the i-th word should attend to the j-th image
28# images_mask_2d [batch_size, seq_len], 1 represents the i-th word should not be visually augmented, i.e., should not be attend to any image
29