y0k1n0的小破站 - 深度学习
type: channel
link: http://y0k1n0.online/index.php/category/deep_learning/
items: stable diffusion代码阅读笔记

stable diffusion代码阅读笔记
type: item
date: 2024-09-24T22:25:00+08:00
description: stable diffusion代码阅读笔记本文参考的配置文件信息:AutoencoderKL:stable-diffusion\configs\autoencoder\autoencoder_kl_32x32x4.yamllatent-diffusion:stable-diffusion\configs\latent-diffusion\lsun_churches-ldm-kl-8.yamlldmmodulesdiffusionmodulesmodel.pyNromalize 函数def Normalize(in_channels, num_groups=32): """创建GroupNorm层 Args: in_channels: 输入通道数 num_groups: 分组数量. Defaults to 32. Returns: 返回一个 torch.nn.GroupNorm 层的实例 """ return torch.nn.GroupNorm(num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True)这个方法定义了一个归一化层的方式,使用群归一化有利于提高训练速度和模型稳定性ResnetBlock类这个类定义了使用的残差块的模型,前向传播模型如下图所示注释代码如下:class ResnetBlock(nn.Module): def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, dropout, temb_channels=512): """Resnet模块实现 Args: in_channels: 输入通道数 dropout: Dropout率 out_channels: 输出通道数. Defaults to None. conv_shortcut: 是否使用卷积快速链接. Defaults to False. temb_channels: 时间嵌入通道数. Defaults to 512. """ super().__init__() self.in_channels = in_channels out_channels = in_channels if out_channels is None else out_channels self.out_channels = out_channels self.use_conv_shortcut = conv_shortcut self.norm1 = Normalize(in_channels) self.conv1 = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) if temb_channels > 0: self.temb_proj = torch.nn.Linear(temb_channels, out_channels) self.norm2 = Normalize(out_channels) self.dropout = torch.nn.Dropout(dropout) self.conv2 = torch.nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) if self.in_channels != self.out_channels: if self.use_conv_shortcut: self.conv_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) else: self.nin_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) def forward(self, x, temb): """前线传播方法,用于计算输入张量x经过Resnet block后的输出 Args: x: 输入张量 temb: 时间嵌入 Returns: 残差块的输出 """ h = x h = self.norm1(h) h = nonlinearity(h) h = self.conv1(h) if temb is not None: h = h + self.temb_proj(nonlinearity(temb))[:,:,None,…