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: Encode Images.
  
  Encodes images given in `images`, if the image can be encoded (i.e. it
  is a tensor).
  
  :param images:
      either a list of objects of length N, of which some maybe be None, or a
      tensor of shape (batch size, self.img_dim)
  :param positions:
      positions for images, of size [len(images) x images.size(1)]
  :param segments:
      segments for images, of size [len(images)]
  
  :return:
      a (image_encoded, image_mask) tuple, where:
  
      - image_encoded is a torch.Tensor of dim N x self.n_image_tokens x
        self.embedding_size, representing the encoded batch of images
      - image_mask is a torch.Tensor of dim N x self.n_image_tokens
docstr_tok: ["Encode","Images",".","Encodes","images","given","in","`","images","`",",","if","the","image","can","be","encoded","(","i.e",".","it","is","a","tensor",")",".",":","param","images",":","either","a","list","of","objects","of","length","N",",","of","which","some","maybe","be","None",",","or","a","tensor","of","shape","(","batch","size",",","self.img_dim",")",":","param","positions",":","positions","for","images",",","of","size","[","len","(","images",")","x","images.size","(","1",")","]",":","param","segments",":","segments","for","images",",","of","size","[","len","(","images",")","]",":","return",":","a","(","image_encoded",",","image_mask",")","tuple",",","where",":","-","image_encoded","is","a","torch.Tensor","of","dim","N","x","self.n_image_tokens","x","self.embedding_size",",","representing","the","encoded","batch","of","images","-","image_mask","is","a","torch.Tensor","of","dim","N","x","self.n_image_tokens"]
code: def encode_images(
      self,
      images: Union[List[object], torch.Tensor],
      positions: Optional[torch.LongTensor] = None,
      segments: Optional[torch.LongTensor] = None,
  ) -> Tuple[torch.Tensor, torch.Tensor]:
      """
      Encode Images.
  
      Encodes images given in `images`, if the image can be encoded (i.e. it
      is a tensor).
  
      :param images:
          either a list of objects of length N, of which some maybe be None, or a
          tensor of shape (batch size, self.img_dim)
      :param positions:
          positions for images, of size [len(images) x images.size(1)]
      :param segments:
          segments for images, of size [len(images)]
  
      :return:
          a (image_encoded, image_mask) tuple, where:
  
          - image_encoded is a torch.Tensor of dim N x self.n_image_tokens x
            self.embedding_size, representing the encoded batch of images
          - image_mask is a torch.Tensor of dim N x self.n_image_tokens
      """
      if positions is None:
          positions = [None for _ in range(len(images))]  # type: ignore
      else:
          positions = self.position_embeddings(positions)
      if segments is None:
          segments = [None for _ in range(len(images))]  # type: ignore
      else:
          segments = self.segment_embeddings(segments)
  
      # assertions for typing
      assert positions is not None
      assert segments is not None
  
      valid_inds = [
          i
          for i, img in enumerate(images)
          if img is not None and isinstance(img, torch.Tensor)
      ]
  
      if valid_inds:
          image_mask_list = []
          image_encoded_list = []
  
          valid_imgs = torch.stack([images[i] for i in valid_inds])  # type: ignore
          valid_img_enc = self.image_encoder(valid_imgs)
  
          img_num = 0
          for i in range(len(images)):
              if i in valid_inds:
                  image_mask_list.append(self.ones_mask)
                  image_encoded_list.append(
                      self._add(
                          [
                              valid_img_enc[img_num, :],
                              positions[i],
                              segments[i],
                          ]
                      )
                  )
                  img_num += 1
              else:
                  image_mask_list.append(~self.ones_mask)  # type: ignore
                  image_encoded_list.append(self.dummy_image_enc)  # type: ignore
  
          image_masks = torch.stack(image_mask_list)  # type: ignore
          image_encoded = torch.stack(image_encoded_list).reshape(
              (
                  len(images),
                  self.n_image_tokens * self.n_image_channels,
                  self.embedding_size,
              )
          )
      else:
          image_encoded = torch.stack(
              [self.dummy_image_enc] * len(images)
          ).reshape(
              len(images),
              self.n_image_tokens * self.n_image_channels,
              self.embedding_size,
          )
          image_masks = torch.stack([~self.ones_mask] * len(images))
  
      assert image_masks.shape == image_encoded.shape[:2]
      return image_encoded, image_masks
  
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 3 for calling the ContextWithImageEncoder obj's underlying member method code with example usage: obj.encode_images(images, positions, segments) and returns: image_encoded,  image_masks