code: def test_itm(self):
      from PIL import Image
      from lavis.models import load_model_and_preprocess
  
      def compute_itm():
          img = (
              vis_processors["eval"](raw_image).unsqueeze(0).to(device)
          )
          txt = txt_processors["eval"](caption)
  
          itm_output = model(
              {"image": img, "text_input": [txt]}, match_head="itm"
          )
          itm_scores = torch.nn.functional.softmax(itm_output, dim=1)
  
          return itm_scores
  
      def compute_itc():
          img = (
              vis_processors["eval"](raw_image).unsqueeze(0).to(device)
          )
          txt = txt_processors["eval"](caption)
  
          itc_score = model(
              {"image": img, "text_input": [txt]}, match_head="itc"
          )
  
          return itc_score
  
      raw_image = Image.open("docs/_static/merlion.png").convert("RGB")
  
      model, vis_processors, txt_processors = load_model_and_preprocess(
          "blip_image_text_matching",
          model_type="base",
          is_eval=True,
          device=device,
      )
  
      caption = "merlion in Singapore"
      itm_scores = compute_itm()
      itc_score = compute_itc()
  
      assert itm_scores[:, 1].item() == pytest.approx(0.98613, abs=1e-5)
      assert itc_score.item() == pytest.approx(0.4633, abs=1e-4)
  
      caption = "a random irrelevant caption"
      itm_scores = compute_itm()
      itc_score = compute_itc()
  
      assert itm_scores[:, 1].item() == pytest.approx(0.05704, abs=1e-5)
      assert itc_score.item() == pytest.approx(0.23282, abs=1e-5)
  
      # test BLIP ITM large
      model, vis_processors, txt_processors = load_model_and_preprocess(
          "blip_image_text_matching",
          model_type="large",
          is_eval=True,
          device=device,
      )
  
      caption = "merlion in Singapore"
      itm_scores = compute_itm()
      itc_score = compute_itc()
  
      assert itm_scores[:, 1].item() == pytest.approx(0.99466, abs=1e-5)
      assert itc_score.item() == pytest.approx(0.4474, abs=1e-4)
  
      caption = "a random irrelevant caption"
      itm_scores = compute_itm()
      itc_score = compute_itc()
  
      assert itm_scores[:, 1].item() == pytest.approx(0.04744, abs=1e-5)
      assert itc_score.item() == pytest.approx(0.12821, abs=1e-5)
  
file_path: lavis/tests/models/test_blip.py
imports: import pytest
  import torch
  from lavis.models import load_model, load_model_and_preprocess
  from PIL import Image
label_desc: Use image node_id 8 for calling the TestBlip obj's underlying member method code with example usage: obj.test_itm() and returns: itm_scores, itc_score