MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / ResnetBlock

Class ResnetBlock

sat/sgm/modules/autoencoding/vqvae/vqvae_blocks.py:70–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68
69
70class ResnetBlock(nn.Module):
71 def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, dropout, temb_channels=512):
72 super().__init__()
73 self.in_channels = in_channels
74 out_channels = in_channels if out_channels is None else out_channels
75 self.out_channels = out_channels
76 self.use_conv_shortcut = conv_shortcut
77
78 self.norm1 = Normalize(in_channels)
79 self.conv1 = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
80 if temb_channels > 0:
81 self.temb_proj = torch.nn.Linear(temb_channels, out_channels)
82 self.norm2 = Normalize(out_channels)
83 self.dropout = torch.nn.Dropout(dropout)
84 self.conv2 = torch.nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
85 if self.in_channels != self.out_channels:
86 if self.use_conv_shortcut:
87 self.conv_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
88 else:
89 self.nin_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
90
91 def forward(self, x, temb):
92 h = x
93 h = self.norm1(h)
94 h = nonlinearity(h)
95 h = self.conv1(h)
96
97 if temb is not None:
98 h = h + self.temb_proj(nonlinearity(temb))[:, :, None, None]
99
100 h = self.norm2(h)
101 h = nonlinearity(h)
102 h = self.dropout(h)
103 h = self.conv2(h)
104
105 if self.in_channels != self.out_channels:
106 if self.use_conv_shortcut:
107 x = self.conv_shortcut(x)
108 else:
109 x = self.nin_shortcut(x)
110
111 return x + h
112
113
114class AttnBlock(nn.Module):

Callers 2

__init__Method · 0.70
__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected