class_docstr: ContextWithImage Module.
  
  Encodes image features and context, and combines by summing or concatenation.
class_docstr_tok: ["ContextWithImage","Module",".","Encodes","image","features","and","context",",","and","combines","by","summing","or","concatenation","."]
docstr: Override TransformerEncoder __init__.
  
  Setup the image encoder; create some dummy tensors for inserting image into
  input
docstr_tok: ["Override","TransformerEncoder","__init__",".","Setup","the","image","encoder",";","create","some","dummy","tensors","for","inserting","image","into","input"]
code: def __init__(
      self,
      opt: Opt,
      vocabulary_size: int,
      embedding=None,
      padding_idx=0,
      reduction_type="mean",
      n_segments=None,
      embeddings_scale=None,
      image_encoder_num_layers=1,
      image_features_dim=2048,
      image_combination_mode="append",
      n_image_tokens=1,
      fusion="late",
      n_image_channels=1,
  ):
      """
      Override TransformerEncoder __init__.
  
      Setup the image encoder; create some dummy tensors for inserting image into
      input
      """
  
      self.padding_idx = padding_idx
      self.n_img_layers = image_encoder_num_layers
      self.img_dim = image_features_dim
      self.image_combination_mode = image_combination_mode
      self.n_image_tokens = n_image_tokens
      self.fusion = FusionType(fusion)
      self.n_image_channels = n_image_channels
      if (
          self.image_combination_mode == "add"
          and self.n_image_tokens > 1
      ):
          raise ValueError(
              "Image encoding cannot be added to context encoding if there is more than one image token!"
          )
      if self.fusion is FusionType.EARLY:
          assert (
              opt["n_segments"] == 2
          ), "must use segment embeddings for early fusion"
      reduction_type = (
          None  # Must pass back unreduced encoding and mask
      )
      super().__init__(
          opt=opt,
          vocabulary_size=vocabulary_size,
          embedding=embedding,
          padding_idx=padding_idx,
          reduction_type=reduction_type,
          n_segments=n_segments,
          embeddings_scale=embeddings_scale,
      )
      self.full_embedding_size = (
          self.embedding_size * self.n_image_tokens
      )
      # Images will be embedded to this size, and then the embedding will be folded
      # into however many tokens are needed
      self._build_image_encoder()
      dummy_image_size = (n_image_channels, self.full_embedding_size)
      self.register_buffer(
          "dummy_image_enc", torch.zeros(dummy_image_size)
      )
      self.register_buffer(
          "ones_mask",
          torch.ones(
              self.n_image_tokens * self.n_image_channels
          ).bool(),
      )
  
imports: from enum import Enum
  from functools import reduce
  from typing import List, Tuple, Optional, Union
  import torch
  import torch.nn
  from parlai.agents.transformer.modules import TransformerGeneratorModel, TransformerEncoder
  from parlai.core.dict import DictionaryAgent
  from parlai.core.opt import Opt
label_desc: Use image node_id 1 to create a new ContextWithImageEncoder object from inherited base classes: TransformerEncoder with example: obj = ContextWithImageEncoder(opt, vocabulary_size, embedding, padding_idx, reduction_type, n_segments, embeddings_scale, image_encoder_num_layers, image_features_dim, image_combination_mode, n_image_tokens, fusion, n_image_channels)