| 104 | |
| 105 | @SUBMODULES.register_module() |
| 106 | class T2MTextEncoder(nn.Module): |
| 107 | |
| 108 | def __init__(self, |
| 109 | word_size, |
| 110 | pos_size, |
| 111 | hidden_size, |
| 112 | output_size, |
| 113 | max_text_len): |
| 114 | super().__init__() |
| 115 | self.text_encoder = TextEncoderBiGRUCo( |
| 116 | word_size=word_size, |
| 117 | pos_size=pos_size, |
| 118 | hidden_size=hidden_size, |
| 119 | output_size=output_size, |
| 120 | ) |
| 121 | self.w_vectorizer = WordVectorizer('./data/glove', 'our_vab') |
| 122 | self.max_text_len = max_text_len |
| 123 | |
| 124 | def load_pretrained(self, ckpt_path): |
| 125 | checkpoint = torch.load(ckpt_path, map_location='cpu') |
| 126 | self.text_encoder.load_state_dict(checkpoint['text_encoder']) |
| 127 | |
| 128 | def forward(self, text, token, device): |
| 129 | B = len(text) |
| 130 | pos_one_hot = [] |
| 131 | word_emb = [] |
| 132 | sent_len = [] |
| 133 | for i in range(B): |
| 134 | tokens = token[i].split(" ") |
| 135 | if len(tokens) < self.max_text_len: |
| 136 | tokens = ['sos/OTHER'] + tokens + ['eos/OTHER'] |
| 137 | batch_sent_len = len(tokens) |
| 138 | tokens = tokens + ['unk/OTHER'] * (self.max_text_len + 2 - batch_sent_len) |
| 139 | else: |
| 140 | tokens = tokens[: self.max_text_len] |
| 141 | tokens = ['sos/OTHER'] + tokens + ['eos/OTHER'] |
| 142 | batch_sent_len = len(tokens) |
| 143 | sent_len.append(batch_sent_len) |
| 144 | batch_word_emb = [] |
| 145 | batch_pos_one_hot = [] |
| 146 | for cur_token in tokens: |
| 147 | cur_word_emb, cur_pos_one_hot = self.w_vectorizer[cur_token] |
| 148 | cur_word_emb = torch.from_numpy(cur_word_emb).float() |
| 149 | cur_pos_one_hot = torch.from_numpy(cur_pos_one_hot).float() |
| 150 | batch_word_emb.append(cur_word_emb) |
| 151 | batch_pos_one_hot.append(cur_pos_one_hot) |
| 152 | |
| 153 | batch_word_emb = torch.stack(batch_word_emb, dim=0) |
| 154 | batch_pos_one_hot = torch.stack(batch_pos_one_hot, dim=0) |
| 155 | word_emb.append(batch_word_emb) |
| 156 | pos_one_hot.append(batch_pos_one_hot) |
| 157 | word_emb = torch.stack(word_emb, dim=0).to(device) |
| 158 | pos_one_hot = torch.stack(pos_one_hot, dim=0).to(device) |
| 159 | sent_len = torch.tensor(sent_len, dtype=torch.long).to(device) |
| 160 | text_embedding = self.text_encoder(word_emb, pos_one_hot, sent_len) |
| 161 | return text_embedding |
| 162 | |
| 163 |
nothing calls this directly
no outgoing calls
no test coverage detected