class_docstr: BLIP2 OPT model.
  Supported model types:
      - pretrained_opt2.7b: pretrained model with OPT2.7b
      - pretrained_opt6.7b: pretrained model with OPT6.7b
      - caption_coco_opt2.7b: fintuned image captioning model with OPT2.7b
      - caption_coco_opt6.7b: fintuned image captioning model with OPT6.7b
  Usage:
      >>> from lavis.models import load_model
      >>> model = load_model("blip2_opt", "caption_coco_opt2.7b")
class_docstr_tok: ["BLIP2","OPT","model",".","Supported","model","types",":","-","pretrained_opt2.7b",":","pretrained","model","with","OPT2.7b","-","pretrained_opt6.7b",":","pretrained","model","with","OPT6.7b","-","caption_coco_opt2.7b",":","fintuned","image","captioning","model","with","OPT2.7b","-","caption_coco_opt6.7b",":","fintuned","image","captioning","model","with","OPT6.7b","Usage",":",">",">",">","from","lavis.models","import","load_model",">",">",">","model","=","load_model","(","``","blip2_opt","''",",","``","caption_coco_opt2.7b","''",")"]
code: def predict_answers(
      self,
      samples,
      num_beams=5,
      inference_method="generate",
      max_len=10,
      min_len=1,
      num_ans_candidates=128,
      answer_list=None,
      prompt="",
      length_penalty=0,
      **kwargs,
  ):
      image = samples["image"]
      with self.maybe_autocast():
          image_embeds = self.ln_vision(self.visual_encoder(image))
          image_atts = torch.ones(
              image_embeds.size()[:-1], dtype=torch.long
          ).to(image.device)
  
          query_tokens = self.query_tokens.expand(
              image_embeds.shape[0], -1, -1
          )
          query_output = self.Qformer.bert(
              query_embeds=query_tokens,
              encoder_hidden_states=image_embeds,
              encoder_attention_mask=image_atts,
              return_dict=True,
          )
  
          inputs_opt = self.opt_proj(query_output.last_hidden_state)
          atts_opt = torch.ones(
              inputs_opt.size()[:-1], dtype=torch.long
          ).to(image.device)
  
          if isinstance(samples["text_input"], str):
              samples["text_input"] = [samples["text_input"]]
          if prompt:
              text_input = [
                  prompt.format(question)
                  for question in samples["text_input"]
              ]
          else:
              text_input = samples["text_input"]
  
          self.opt_tokenizer.padding_side = "left"
          opt_tokens = self.opt_tokenizer(
              text_input,
              return_tensors="pt",
              padding="longest",
              truncation=True,
              max_length=self.max_txt_len,
          ).to(image.device)
  
          attention_mask = torch.cat(
              [atts_opt, opt_tokens.attention_mask], dim=1
          )
  
          # require transformers>=4.27
          inputs_embeds = self.opt_model.get_input_embeddings()(
              opt_tokens.input_ids
          )
          inputs_embeds = torch.cat([inputs_opt, inputs_embeds], dim=1)
  
          outputs = self.opt_model.generate(
              inputs_embeds=inputs_embeds,
              attention_mask=attention_mask,
              do_sample=False,
              num_beams=num_beams,
              max_new_tokens=max_len,
              min_length=min_len,
              eos_token_id=self.eos_token_id,
              length_penalty=length_penalty,
          )
          output_text = self.opt_tokenizer.batch_decode(
              outputs, skip_special_tokens=True
          )
          output_text = [text.strip() for text in output_text]
      if self._apply_lemmatizer or (
          "apply_lemmatizer" in samples.keys()
          and samples["apply_lemmatizer"]
      ):
          output_text = self._lemmatize(output_text)
  
      return output_text
  
file_path: lavis/lavis/models/blip2_models/blip2_opt.py
imports: import logging
  from packaging import version
  import torch
  from torch.cuda.amp import autocast
  import torch.nn
  from lavis.common.registry import registry
  from lavis.models.blip2_models.blip2 import Blip2Base, disabled_train
  from transformers import AutoTokenizer, OPTForCausalLM, OPTConfig
  import transformers
label_desc: Use image node_id 4 for calling the Blip2OPT 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, prompt, length_penalty) and returns: output_text