class_docstr: BLIP VQA models.
  
  Supported model types:
      - base: vqa model initialized with pre-trained BLIP base model on 115M image-text pairs after CapFilt; not fine-tuned.
      - vqav2: fine-tuned BLIP base model on VQA v2.0 dataset.
  
  Usage:
      >>> from lavis.models import load_model
      >>> model = load_model("blip_vqa", "vqav2")
      >>> model = load_model("blip_vqa", "okvqa")
      >>> model = load_model("blip_vqa", "aokvqa")
class_docstr_tok: ["BLIP","VQA","models",".","Supported","model","types",":","-","base",":","vqa","model","initialized","with","pre-trained","BLIP","base","model","on","115M","image-text","pairs","after","CapFilt",";","not","fine-tuned",".","-","vqav2",":","fine-tuned","BLIP","base","model","on","VQA","v2.0","dataset",".","Usage",":",">",">",">","from","lavis.models","import","load_model",">",">",">","model","=","load_model","(","``","blip_vqa","''",",","``","vqav2","''",")",">",">",">","model","=","load_model","(","``","blip_vqa","''",",","``","okvqa","''",")",">",">",">","model","=","load_model","(","``","blip_vqa","''",",","``","aokvqa","''",")"]
docstr: Args:
      samples (dict): A dictionary containing the following keys:
          - image (torch.Tensor): A tensor of shape (batch_size, 3, H, W). Default H=480, W=480.
          - text_input (str or [str]): String or a list of strings, each string is a question.
                                       The number of questions must be equal to the batch size. If a single string, will be converted to a list of string, with length 1 first.
      num_beams (int): Number of beams for beam search. 1 means no beam search.
      inference_method (str): Inference method. One of "rank", "generate".
          - If "rank", the model will return answers with the highest probability from the answer list.
          - If "generate", the model will generate answers.
      max_len (int): Maximum length of generated answers.
      min_len (int): Minimum length of generated answers.
      num_ans_candidates (int): Number of answer candidates, used to filter out answers with low probability.
      answer_list (list): A list of strings, each string is an answer.
  
  Returns:
      List: A list of strings, each string is an answer.
  
  Examples:
  ```python
      >>> from PIL import Image
      >>> from lavis.models import load_model_and_preprocess
      >>> model, vis_processors, txt_processors = load_model_and_preprocess("blip_vqa", "vqav2")
      >>> raw_image = Image.open("docs/data/merlion.png").convert("RGB")
      >>> question = "Which city is this photo taken?"
      >>> image = vis_processors["eval"](raw_image).unsqueeze(0)
      >>> question = txt_processors["eval"](question)
      >>> samples = {"image": image, "text_input": [question]}
      >>> answers = model.predict_answers(samples)
      >>> answers
      ['singapore']
      >>> answer_list = ["Singapore", "London", "Palo Alto", "Tokyo"]
      >>> answers = model.predict_answers(samples, answer_list=answer_list)
      >>> answers
      ['Singapore']
  ```
docstr_tok: ["Args",":","samples","(","dict",")",":","A","dictionary","containing","the","following","keys",":","-","image","(","torch.Tensor",")",":","A","tensor","of","shape","(","batch_size",",","3",",","H",",","W",")",".","Default","H=480",",","W=480",".","-","text_input","(","str","or","[","str","]",")",":","String","or","a","list","of","strings",",","each","string","is","a","question",".","The","number","of","questions","must","be","equal","to","the","batch","size",".","If","a","single","string",",","will","be","converted","to","a","list","of","string",",","with","length","1","first",".","num_beams","(","int",")",":","Number","of","beams","for","beam","search",".","1","means","no","beam","search",".","inference_method","(","str",")",":","Inference","method",".","One","of","``","rank","''",",","``","generate","''",".","-","If","``","rank","''",",","the","model","will","return","answers","with","the","highest","probability","from","the","answer","list",".","-","If","``","generate","''",",","the","model","will","generate","answers",".","max_len","(","int",")",":","Maximum","length","of","generated","answers",".","min_len","(","int",")",":","Minimum","length","of","generated","answers",".","num_ans_candidates","(","int",")",":","Number","of","answer","candidates",",","used","to","filter","out","answers","with","low","probability",".","answer_list","(","list",")",":","A","list","of","strings",",","each","string","is","an","answer",".","Returns",":","List",":","A","list","of","strings",",","each","string","is","an","answer",".","Examples",":","``","`","python",">",">",">","from","PIL","import","Image",">",">",">","from","lavis.models","import","load_model_and_preprocess",">",">",">","model",",","vis_processors",",","txt_processors","=","load_model_and_preprocess","(","``","blip_vqa","''",",","``","vqav2","''",")",">",">",">","raw_image","=","Image.open","(","``","docs\/data\/merlion.png","''",")",".convert","(","``","RGB","''",")",">",">",">","question","=","``","Which","city","is","this","photo","taken","?","''",">",">",">","image","=","vis_processors","[","``","eval","''","]","(","raw_image",")",".unsqueeze","(","0",")",">",">",">","question","=","txt_processors","[","``","eval","''","]","(","question",")",">",">",">","samples","=","{","``","image","''",":","image",",","``","text_input","''",":","[","question","]","}",">",">",">","answers","=","model.predict_answers","(","samples",")",">",">",">","answers","[","'singapore","'","]",">",">",">","answer_list","=","[","``","Singapore","''",",","``","London","''",",","``","Palo","Alto","''",",","``","Tokyo","''","]",">",">",">","answers","=","model.predict_answers","(","samples",",","answer_list=answer_list",")",">",">",">","answers","[","'Singapore","'","]","``","`"]
code: def predict_answers(
      self,
      samples,
      num_beams=3,
      inference_method="rank",
      max_len=10,
      min_len=1,
      num_ans_candidates=128,
      answer_list=None,
      **kwargs,
  ):
      """
      Args:
          samples (dict): A dictionary containing the following keys:
              - image (torch.Tensor): A tensor of shape (batch_size, 3, H, W). Default H=480, W=480.
              - text_input (str or [str]): String or a list of strings, each string is a question.
                                           The number of questions must be equal to the batch size. If a single string, will be converted to a list of string, with length 1 first.
          num_beams (int): Number of beams for beam search. 1 means no beam search.
          inference_method (str): Inference method. One of "rank", "generate".
              - If "rank", the model will return answers with the highest probability from the answer list.
              - If "generate", the model will generate answers.
          max_len (int): Maximum length of generated answers.
          min_len (int): Minimum length of generated answers.
          num_ans_candidates (int): Number of answer candidates, used to filter out answers with low probability.
          answer_list (list): A list of strings, each string is an answer.
  
      Returns:
          List: A list of strings, each string is an answer.
  
      Examples:
      ```python
          >>> from PIL import Image
          >>> from lavis.models import load_model_and_preprocess
          >>> model, vis_processors, txt_processors = load_model_and_preprocess("blip_vqa", "vqav2")
          >>> raw_image = Image.open("docs/data/merlion.png").convert("RGB")
          >>> question = "Which city is this photo taken?"
          >>> image = vis_processors["eval"](raw_image).unsqueeze(0)
          >>> question = txt_processors["eval"](question)
          >>> samples = {"image": image, "text_input": [question]}
          >>> answers = model.predict_answers(samples)
          >>> answers
          ['singapore']
          >>> answer_list = ["Singapore", "London", "Palo Alto", "Tokyo"]
          >>> answers = model.predict_answers(samples, answer_list=answer_list)
          >>> answers
          ['Singapore']
      ```
      """
      assert inference_method in [
          "rank",
          "generate",
      ], "Inference method must be one of 'rank' or 'generate', got {}.".format(
          inference_method
      )
  
      if isinstance(samples["text_input"], str):
          samples["text_input"] = [samples["text_input"]]
  
      assert len(samples["text_input"]) == samples["image"].size(
          0
      ), "The number of questions must be equal to the batch size."
  
      if inference_method == "generate":
          return self._generate_answers(
              samples,
              num_beams=num_beams,
              max_length=max_len,
              min_length=min_len,
          )
      elif inference_method == "rank":
          assert (
              answer_list is not None
          ), "answer_list must be provided for ranking"
  
          num_ans_candidates = min(num_ans_candidates, len(answer_list))
  
          return self._rank_answers(
              samples,
              answer_list=answer_list,
              num_ans_candidates=num_ans_candidates,
          )
  
file_path: lavis/lavis/models/blip_models/blip_vqa.py
imports: import torch
  import torch.nn.functional
  from lavis.common.registry import registry
  from lavis.models.base_model import tile
  from lavis.models.blip_models.blip import BlipBase
  from lavis.models.blip_models.blip_outputs import BlipOutput, BlipIntermediateOutput
  from lavis.models.med import XBertEncoder, XBertLMHeadDecoder
  from lavis.models.vit import VisionTransformerEncoder
label_desc: Use image node_id 5 for calling the BlipVQA obj's underlying member method code with example usage: obj.predict_answers(samples, num_beams, inference_method, max_len, min_len, num_ans_candidates, answer_list) and returns: self, self