MCPcopy Create free account
hub / github.com/IceClear/StableSR / ResnetBlock

Class ResnetBlock

ldm/modules/diffusionmodules/model.py:120–179  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

118
119
120class ResnetBlock(nn.Module):
121 def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False,
122 dropout, temb_channels=512):
123 super().__init__()
124 self.in_channels = in_channels
125 out_channels = in_channels if out_channels is None else out_channels
126 self.out_channels = out_channels
127 self.use_conv_shortcut = conv_shortcut
128
129 self.norm1 = Normalize(in_channels)
130 self.conv1 = torch.nn.Conv2d(in_channels,
131 out_channels,
132 kernel_size=3,
133 stride=1,
134 padding=1)
135 if temb_channels > 0:
136 self.temb_proj = torch.nn.Linear(temb_channels,
137 out_channels)
138 self.norm2 = Normalize(out_channels)
139 self.dropout = torch.nn.Dropout(dropout)
140 self.conv2 = torch.nn.Conv2d(out_channels,
141 out_channels,
142 kernel_size=3,
143 stride=1,
144 padding=1)
145 if self.in_channels != self.out_channels:
146 if self.use_conv_shortcut:
147 self.conv_shortcut = torch.nn.Conv2d(in_channels,
148 out_channels,
149 kernel_size=3,
150 stride=1,
151 padding=1)
152 else:
153 self.nin_shortcut = torch.nn.Conv2d(in_channels,
154 out_channels,
155 kernel_size=1,
156 stride=1,
157 padding=0)
158
159 def forward(self, x, temb):
160 h = x
161 h = self.norm1(h)
162 h = nonlinearity(h)
163 h = self.conv1(h)
164
165 if temb is not None:
166 h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None]
167
168 h = self.norm2(h)
169 h = nonlinearity(h)
170 h = self.dropout(h)
171 h = self.conv2(h)
172
173 if self.in_channels != self.out_channels:
174 if self.use_conv_shortcut:
175 x = self.conv_shortcut(x)
176 else:
177 x = self.nin_shortcut(x)

Callers 8

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected