class_docstr: BLIP Image-Text Matching (ITM) model.
  
  Supported model types:
      - base: fine-tuned BLIP retrieval weights on COCO dataset (Karpathy split).
      - large: fine-tuned BLIP retrieval weights on COCO dataset (Karpathy split).
  
  Usage:
      >>> from lavis.models import load_model
      >>> model = load_model("blip_image_text_matching", "base")
      >>> model = load_model("blip_image_text_matching", "large")
class_docstr_tok: ["BLIP","Image-Text","Matching","(","ITM",")","model",".","Supported","model","types",":","-","base",":","fine-tuned","BLIP","retrieval","weights","on","COCO","dataset","(","Karpathy","split",")",".","-","large",":","fine-tuned","BLIP","retrieval","weights","on","COCO","dataset","(","Karpathy","split",")",".","Usage",":",">",">",">","from","lavis.models","import","load_model",">",">",">","model","=","load_model","(","``","blip_image_text_matching","''",",","``","base","''",")",">",">",">","model","=","load_model","(","``","blip_image_text_matching","''",",","``","large","''",")"]
code: def itm_rank(
      self,
      image_embeds,
      image_atts,
      encoder_input_ids,
      match_head="itm",
  ):
      # breakpoint()
      encoder_input_ids = encoder_input_ids.clone()
      encoder_input_ids = encoder_input_ids[:, 3:]
      text_attention_mask = (
          encoder_input_ids != self.tokenizer.pad_token_id
      ).long()
  
      if match_head == "itm":
          # encoder_input_ids = encoder_input_ids.clone()
          encoder_input_ids[:, 0] = self.tokenizer.enc_token_id
          output = self.text_encoder(
              encoder_input_ids,
              attention_mask=text_attention_mask,
              encoder_hidden_states=image_embeds,
              encoder_attention_mask=image_atts,
              return_dict=True,
          )
          # print(output.last_hidden_state.shape)
          itm_output = self.itm_head(output.last_hidden_state[:, 0, :])
          itm_output = F.softmax(itm_output, dim=1)[:, 1]
          return itm_output  # , mask, token_length
  
      elif match_head == "itc":
          encoder_input_ids[:, 0] = self.tokenizer.cls_token_id
          text_output = self.text_encoder(
              encoder_input_ids,
              attention_mask=text_attention_mask,
              return_dict=True,
              mode="text",
          )
          image_feat = F.normalize(
              self.vision_proj(image_embeds[:, 0, :]), dim=-1
          )
          text_feat = F.normalize(
              self.text_proj(text_output.last_hidden_state[:, 0, :]),
              dim=-1,
          )
  
          sim = image_feat @ text_feat.t()
          return sim
  
file_path: lavis/lavis/models/blip_models/blip_image_text_matching.py
imports: import torch
  import torch.nn.functional
  from lavis.common.registry import registry
  from lavis.models.blip_models.blip import BlipBase
  from torch import nn
  from lavis.models.med import XBertEncoder
  from lavis.models.vit import VisionTransformerEncoder
label_desc: Use image node_id 3 for calling the BlipITM obj's underlying member method code with example usage: obj.itm_rank(image_embeds, image_atts, encoder_input_ids, match_head) and returns: itm_output, sim