class_docstr: Interface for MMBT Grid for Hateful Memes.
class_docstr_tok: ["Interface","for","MMBT","Grid","for","Hateful","Memes","."]
docstr: Classifies a given image and text in it into Hateful/Non-Hateful.
  Image can be a url or a local path or you can directly pass a PIL.Image.Image
  object. Text needs to be a sentence containing all text in the image.
  
      >>> from mmf.models.mmbt import MMBT
      >>> model = MMBT.from_pretrained("mmbt.hateful_memes.images")
      >>> model.classify("some_url", "some_text")
      {"label": 0, "confidence": 0.56}
  
  Args:
      image (ImageType): Image to be classified
      text (str): Text in the image
  
  Returns:
      bool: Whether image is hateful (1) or non hateful (0)
docstr_tok: ["Classifies","a","given","image","and","text","in","it","into","Hateful\/Non-Hateful",".","Image","can","be","a","url","or","a","local","path","or","you","can","directly","pass","a","PIL.Image.Image","object",".","Text","needs","to","be","a","sentence","containing","all","text","in","the","image",".",">",">",">","from","mmf.models.mmbt","import","MMBT",">",">",">","model","=","MMBT.from_pretrained","(","``","mmbt.hateful_memes.images","''",")",">",">",">","model.classify","(","``","some_url","''",",","``","some_text","''",")","{","``","label","''",":","0",",","``","confidence","''",":","0.56","}","Args",":","image","(","ImageType",")",":","Image","to","be","classified","text","(","str",")",":","Text","in","the","image","Returns",":","bool",":","Whether","image","is","hateful","(","1",")","or","non","hateful","(","0",")"]
code: def classify(self, image: ImageType, text: str):
      """Classifies a given image and text in it into Hateful/Non-Hateful.
      Image can be a url or a local path or you can directly pass a PIL.Image.Image
      object. Text needs to be a sentence containing all text in the image.
  
          >>> from mmf.models.mmbt import MMBT
          >>> model = MMBT.from_pretrained("mmbt.hateful_memes.images")
          >>> model.classify("some_url", "some_text")
          {"label": 0, "confidence": 0.56}
  
      Args:
          image (ImageType): Image to be classified
          text (str): Text in the image
  
      Returns:
          bool: Whether image is hateful (1) or non hateful (0)
      """
      if isinstance(image, str):
          if image.startswith("http"):
              temp_file = tempfile.NamedTemporaryFile()
              download(
                  image,
                  *os.path.split(temp_file.name),
                  disable_tqdm=True,
              )
              image = tv_helpers.default_loader(temp_file.name)
              temp_file.close()
          else:
              image = tv_helpers.default_loader(image)
  
      text = self.processor_dict["text_processor"]({"text": text})
      image = self.processor_dict["image_processor"](image)
  
      sample = Sample()
      sample.text = text["text"]
      if "input_ids" in text:
          sample.update(text)
  
      sample.image = image
      sample_list = SampleList([sample])
      sample_list = sample_list.to(get_current_device())
  
      output = self.model(sample_list)
      scores = nn.functional.softmax(output["scores"], dim=1)
      confidence, label = torch.max(scores, dim=1)
  
      return {"label": label.item(), "confidence": confidence.item()}
  
file_path: mmf/mmf/models/interfaces/mmbt.py
imports: import os
  import tempfile
  from pathlib import Path
  from typing import Type, Union
  import torch
  import torchvision.datasets.folder
  from mmf.common.sample import Sample, SampleList
  from mmf.models.base_model import BaseModel
  from mmf.utils.build import build_processors
  from mmf.utils.download import download
  from mmf.utils.general import get_current_device
  from omegaconf import DictConfig
  from PIL import Image
  from torch import nn
label_desc: Use image node_id 4 for calling the MMBTGridHMInterface obj's underlying member method code with example usage: obj.classify(image, text) and returns: dict