MCPcopy Create free account
hub / github.com/Sense-GVT/DeCLIP / forward

Method forward

prototype/model/slip.py:245–286  ·  view source on GitHub ↗
(self, input, return_dict=False)

Source from the content-addressed store, hash-verified

243 return output
244
245 def forward(self, input, return_dict=False):
246 # input
247 images = input['images']
248 images_base, images_1, images_2 = torch.split(images, [3,3,3], dim=1)
249 texts = input['captions']
250 texts = self.sample_captions(texts)
251 text_features = self.encode_text(texts)
252
253 image_features = self.encode_image(images_base)
254 image_features_1, image_sim_1 = self.encode_image(images_1, return_sim=True)
255 image_features_2, image_sim_2 = self.encode_image(images_2, return_sim=True)
256
257 # normalized features
258 image_features = image_features / (image_features.norm(dim=-1, keepdim=True))
259 text_features = text_features / (text_features.norm(dim=-1, keepdim=True)+1e-10)
260
261 # image_sim_1 = image_sim_1 / (image_sim_1.norm(dim=-1, keepdim=True))
262 # image_sim_2 = image_sim_2 / (image_sim_2.norm(dim=-1, keepdim=True))
263
264 # cosine similarity as logits
265 logit_scale = self.logit_scale.exp()
266 if self.training and self.use_allgather:
267 link.barrier()
268 gathered_image_features = self.all_gather(image_features)
269 gathered_text_features = self.all_gather(text_features)
270
271 gathered_image_sim_1 = self.all_gather(image_sim_1)
272 gathered_image_sim_2 = self.all_gather(image_sim_2)
273
274 logits_per_image = logit_scale * image_features @ gathered_text_features.t()
275 logits_per_text = logit_scale * text_features @ gathered_image_features.t()
276 else:
277 raise NotImplementedError('2-View: Not Implemented')
278
279 if return_dict:
280 link.barrier()
281 ret_dict = {}
282 ret_dict['logits'] = logits_per_image, logits_per_text
283 ret_dict['sim_features'] = image_sim_1, gathered_image_sim_1, image_sim_2, gathered_image_sim_2
284 ret_dict['features'] = text_features, image_features
285 return ret_dict
286 raise NotImplementedError('Must Return A Dict')
287
288
289def slip_res50(**kwargs):

Callers

nothing calls this directly

Calls 4

encode_textMethod · 0.95
encode_imageMethod · 0.95
sample_captionsMethod · 0.45
all_gatherMethod · 0.45

Tested by

no test coverage detected