MCPcopy Create free account
hub / github.com/bbaaii/DreamDiffusion / FirstStagePostProcessor

Class FirstStagePostProcessor

code/dc_ldm/modules/diffusionmodules/model.py:770–834  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

768 return x
769
770class FirstStagePostProcessor(nn.Module):
771
772 def __init__(self, ch_mult:list, in_channels,
773 pretrained_model:nn.Module=None,
774 reshape=False,
775 n_channels=None,
776 dropout=0.,
777 pretrained_config=None):
778 super().__init__()
779 if pretrained_config is None:
780 assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None'
781 self.pretrained_model = pretrained_model
782 else:
783 assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None'
784 self.instantiate_pretrained(pretrained_config)
785
786 self.do_reshape = reshape
787
788 if n_channels is None:
789 n_channels = self.pretrained_model.encoder.ch
790
791 self.proj_norm = Normalize(in_channels,num_groups=in_channels//2)
792 self.proj = nn.Conv2d(in_channels,n_channels,kernel_size=3,
793 stride=1,padding=1)
794
795 blocks = []
796 downs = []
797 ch_in = n_channels
798 for m in ch_mult:
799 blocks.append(ResnetBlock(in_channels=ch_in,out_channels=m*n_channels,dropout=dropout))
800 ch_in = m * n_channels
801 downs.append(Downsample(ch_in, with_conv=False))
802
803 self.model = nn.ModuleList(blocks)
804 self.downsampler = nn.ModuleList(downs)
805
806
807 def instantiate_pretrained(self, config):
808 model = instantiate_from_config(config)
809 self.pretrained_model = model.eval()
810 # self.pretrained_model.train = False
811 for param in self.pretrained_model.parameters():
812 param.requires_grad = False
813
814
815 @torch.no_grad()
816 def encode_with_pretrained(self,x):
817 c = self.pretrained_model.encode(x)
818 if isinstance(c, DiagonalGaussianDistribution):
819 c = c.mode()
820 return c
821
822 def forward(self,x):
823 z_fs = self.encode_with_pretrained(x)
824 z = self.proj_norm(z_fs)
825 z = self.proj(z)
826 z = nonlinearity(z)
827

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected