(self, caption, length, clip_model, device, idx=None)
| 118 | return xf_out |
| 119 | |
| 120 | def retrieve(self, caption, length, clip_model, device, idx=None): |
| 121 | value = hash(caption) |
| 122 | if value in self.results: |
| 123 | return self.results[value] |
| 124 | text_feature = self.extract_text_feature(caption, clip_model, device) |
| 125 | |
| 126 | rel_length = torch.LongTensor(self.m_lengths).to(device) |
| 127 | rel_length = torch.abs(rel_length - length) |
| 128 | rel_length = rel_length / torch.clamp(rel_length, min=length) |
| 129 | semantic_score = F.cosine_similarity(self.text_features.to(device), |
| 130 | text_feature) |
| 131 | kinematic_score = torch.exp(-rel_length * self.kinematic_coef) |
| 132 | score = semantic_score * kinematic_score |
| 133 | indexes = torch.argsort(score, descending=True) |
| 134 | data = [] |
| 135 | cnt = 0 |
| 136 | for idx in indexes: |
| 137 | caption, m_length = self.captions[idx], self.m_lengths[idx] |
| 138 | if not self.training or m_length != length: |
| 139 | cnt += 1 |
| 140 | data.append(idx.item()) |
| 141 | if cnt == self.num_retrieval: |
| 142 | self.results[value] = data |
| 143 | return data |
| 144 | assert False |
| 145 | |
| 146 | def generate_src_mask(self, T, length): |
| 147 | B = len(length) |
no test coverage detected