MCPcopy Create free account
hub / github.com/OpenDCAI/DataFlow-MM / encode_text

Function encode_text

dataflow/utils/utils.py:126–152  ·  view source on GitHub ↗
(vid_caps, model, tokenizer, idf_dict, device)

Source from the content-addressed store, hash-verified

124
125
126def encode_text(vid_caps, model, tokenizer, idf_dict, device):
127 text_input = tokenizer(vid_caps).to(device=device)
128 with torch.no_grad():
129 text_features = model.encode_text(text_input, local=True).float()
130 text_features /= text_features.norm(dim=-1, keepdim=True)
131
132 # For special tokens, use [SOS] and [EOS]
133 txt_len = text_input.argmax(dim=-1)
134 mask = torch.zeros_like(text_input)
135 for i in range(len(mask)):
136 mask[i][0:txt_len[i] + 1] = 1
137
138 # For special tokens, only use [EOS]
139 # txt_len = text_input.argmax(dim=-1)
140 # mask = torch.zeros_like(text_input)
141 # for i in range(len(mask)):
142 # mask[i][1:txt_len[i]+1] = 1
143
144 # # For special tokens, don't use [SOS] and [EOS]
145 # txt_len = text_input.argmax(dim=-1)
146 # mask = torch.zeros_like(text_input)
147 # for i in range(len(mask)):
148 # mask[i][1:txt_len[i]] = 1
149
150 idf_weights = torch.tensor([[idf_dict[int(i)] for i in a] for a in text_input.cpu()])
151
152 return text_features, mask, idf_weights
153
154
155def process(a, tokenizer=None):

Callers 1

em_cos_scoreFunction · 0.85

Calls 1

encode_textMethod · 0.80

Tested by

no test coverage detected