MCPcopy Create free account
hub / github.com/AlayaLab/Hive / ResnetBlock

Class ResnetBlock

models/flowsep/latent_diffusion/modules/diffusionmodules/model.py:118–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 7

__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