class_docstr: ## Sampler class
class_docstr_tok: ["#","#","Sampler","class"]
docstr: #### Interpolate two images $x_0$ and $x'_0$ and make a video
  
  * `x1` is $x_0$
  * `x2` is $x'_0$
  * `n_frames` is the number of frames for the image
  * `t_` is $t$
  * `create_video` specifies whether to make a video or to show each frame
docstr_tok: ["#","#","#","#","Interpolate","two","images","$","x_0","$","and","$","x'_0","$","and","make","a","video","*","`","x1","`","is","$","x_0","$","*","`","x2","`","is","$","x'_0","$","*","`","n_frames","`","is","the","number","of","frames","for","the","image","*","`","t_","`","is","$","t","$","*","`","create_video","`","specifies","whether","to","make","a","video","or","to","show","each","frame"]
code: def interpolate_animate(
      self,
      x1: torch.Tensor,
      x2: torch.Tensor,
      n_frames: int = 100,
      t_: int = 100,
      create_video=True,
  ):
      """
      #### Interpolate two images $x_0$ and $x'_0$ and make a video
  
      * `x1` is $x_0$
      * `x2` is $x'_0$
      * `n_frames` is the number of frames for the image
      * `t_` is $t$
      * `create_video` specifies whether to make a video or to show each frame
      """
  
      # Show original images
      self.show_image(x1, "x1")
      self.show_image(x2, "x2")
      # Add batch dimension
      x1 = x1[None, :, :, :]
      x2 = x2[None, :, :, :]
      # $t$ tensor
      t = torch.full((1,), t_, device=self.device)
      # $x_t \sim q(x_t|x_0)$
      x1t = self.diffusion.q_sample(x1, t)
      # $x'_t \sim q(x'_t|x_0)$
      x2t = self.diffusion.q_sample(x2, t)
  
      frames = []
      # Get frames with different $\lambda$
      for i in monit.iterate(
          "Interpolate", n_frames + 1, is_children_silent=True
      ):
          # $\lambda$
          lambda_ = i / n_frames
          # $$\bar{x}_t = (1 - \lambda)x_t + \lambda x'_0$$
          xt = (1 - lambda_) * x1t + lambda_ * x2t
          # $$\bar{x}_0 \sim \textcolor{lightgreen}{p_\theta}(x_0|\bar{x}_t)$$
          x0 = self._sample_x0(xt, t_)
          # Add to frames
          frames.append(x0[0])
          # Show frame
          if not create_video:
              self.show_image(x0[0], f"{lambda_ :.2f}")
  
      # Make video
      if create_video:
          self.make_video(frames)
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/diffusion/ddpm/evaluate.py
imports: import numpy
  import torch
  from matplotlib import pyplot
  from torchvision.transforms.functional import to_pil_image, resize
  from labml import experiment, monit
  from labml_nn.diffusion.ddpm import DenoiseDiffusion, gather
  from labml_nn.diffusion.ddpm.experiment import Configs
label_desc: Use image node_id 6 for calling the Sampler obj's underlying member method code with example usage: obj.interpolate_animate(x1, x2, n_frames, t_, create_video) without return types