MCPcopy Create free account
hub / github.com/dek924/PerX2CT / ResnetBlock

Class ResnetBlock

taming/modules/diffusionmodules/model.py:78–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 6

__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected