MCPcopy Create free account
hub / github.com/VisionXLab/OF-Diff / ResnetBlock

Class ResnetBlock

ldm/modules/diffusionmodules/model.py:97–156  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95
96
97class ResnetBlock(nn.Module):
98 def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False,
99 dropout, temb_channels=512):
100 super().__init__()
101 self.in_channels = in_channels
102 out_channels = in_channels if out_channels is None else out_channels
103 self.out_channels = out_channels
104 self.use_conv_shortcut = conv_shortcut
105
106 self.norm1 = Normalize(in_channels)
107 self.conv1 = torch.nn.Conv2d(in_channels,
108 out_channels,
109 kernel_size=3,
110 stride=1,
111 padding=1)
112 if temb_channels > 0:
113 self.temb_proj = torch.nn.Linear(temb_channels,
114 out_channels)
115 self.norm2 = Normalize(out_channels)
116 self.dropout = torch.nn.Dropout(dropout)
117 self.conv2 = torch.nn.Conv2d(out_channels,
118 out_channels,
119 kernel_size=3,
120 stride=1,
121 padding=1)
122 if self.in_channels != self.out_channels:
123 if self.use_conv_shortcut:
124 self.conv_shortcut = torch.nn.Conv2d(in_channels,
125 out_channels,
126 kernel_size=3,
127 stride=1,
128 padding=1)
129 else:
130 self.nin_shortcut = torch.nn.Conv2d(in_channels,
131 out_channels,
132 kernel_size=1,
133 stride=1,
134 padding=0)
135
136 def forward(self, x, temb):
137 h = x
138 h = self.norm1(h)
139 h = nonlinearity(h)
140 h = self.conv1(h)
141
142 if temb is not None:
143 h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None]
144
145 h = self.norm2(h)
146 h = nonlinearity(h)
147 h = self.dropout(h)
148 h = self.conv2(h)
149
150 if self.in_channels != self.out_channels:
151 if self.use_conv_shortcut:
152 x = self.conv_shortcut(x)
153 else:
154 x = self.nin_shortcut(x)

Callers 6

__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