MCPcopy Create free account
hub / github.com/MegaScenes/nvs / ResnetBlock

Class ResnetBlock

ldm/modules/diffusionmodules/model.py:82–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

80
81
82class ResnetBlock(nn.Module):
83 def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False,
84 dropout, temb_channels=512):
85 super().__init__()
86 self.in_channels = in_channels
87 out_channels = in_channels if out_channels is None else out_channels
88 self.out_channels = out_channels
89 self.use_conv_shortcut = conv_shortcut
90
91 self.norm1 = Normalize(in_channels)
92 self.conv1 = torch.nn.Conv2d(in_channels,
93 out_channels,
94 kernel_size=3,
95 stride=1,
96 padding=1)
97 if temb_channels > 0:
98 self.temb_proj = torch.nn.Linear(temb_channels,
99 out_channels)
100 self.norm2 = Normalize(out_channels)
101 self.dropout = torch.nn.Dropout(dropout)
102 self.conv2 = torch.nn.Conv2d(out_channels,
103 out_channels,
104 kernel_size=3,
105 stride=1,
106 padding=1)
107 if self.in_channels != self.out_channels:
108 if self.use_conv_shortcut:
109 self.conv_shortcut = torch.nn.Conv2d(in_channels,
110 out_channels,
111 kernel_size=3,
112 stride=1,
113 padding=1)
114 else:
115 self.nin_shortcut = torch.nn.Conv2d(in_channels,
116 out_channels,
117 kernel_size=1,
118 stride=1,
119 padding=0)
120
121 def forward(self, x, temb):
122 h = x
123 h = self.norm1(h)
124 h = nonlinearity(h)
125 h = self.conv1(h)
126
127 if temb is not None:
128 h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None]
129
130 h = self.norm2(h)
131 h = nonlinearity(h)
132 h = self.dropout(h)
133 h = self.conv2(h)
134
135 if self.in_channels != self.out_channels:
136 if self.use_conv_shortcut:
137 x = self.conv_shortcut(x)
138 else:
139 x = self.nin_shortcut(x)

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