class_docstr: Class defining a region where an image guided diffusion process is acting
class_docstr_tok: ["Class","defining","a","region","where","an","image","guided","diffusion","process","is","acting"]
docstr: Encodes the reference image for this Image2Image region into the latent space
docstr_tok: ["Encodes","the","reference","image","for","this","Image2Image","region","into","the","latent","space"]
code: def encode_reference_image(
      self, encoder, device, generator, cpu_vae=False
  ):
      """Encodes the reference image for this Image2Image region into the latent space"""
      # Place encoder in CPU or not following the parameter cpu_vae
      if cpu_vae:
          # Note here we use mean instead of sample, to avoid moving also generator to CPU, which is troublesome
          self.reference_latents = (
              encoder.cpu()
              .encode(self.reference_image)
              .latent_dist.mean.to(device)
          )
      else:
          self.reference_latents = encoder.encode(
              self.reference_image.to(device)
          ).latent_dist.sample(generator=generator)
      self.reference_latents = 0.18215 * self.reference_latents
  
imports: import re
  from copy import deepcopy
  from dataclasses import asdict, dataclass
  from enum import Enum
  from typing import List, Optional, Union
  import numpy
  import torch
  from numpy import exp, pi, sqrt
  from torchvision.transforms.functional import resize
  from tqdm.auto import tqdm
  from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
  from diffusers.models import AutoencoderKL, UNet2DConditionModel
  from diffusers.pipeline_utils import DiffusionPipeline
  from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
  from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
label_desc: Use image node_id 2 for calling the Image2ImageRegion obj's underlying member method code with example usage: obj.encode_reference_image(encoder, device, generator, cpu_vae) without return types