MCPcopy Create free account
hub / github.com/NVlabs/SPADE / SPADE

Class SPADE

models/networks/normalization.py:66–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64# |norm_nc|: the #channels of the normalized activations, hence the output dim of SPADE
65# |label_nc|: the #channels of the input semantic map, hence the input dim of SPADE
66class SPADE(nn.Module):
67 def __init__(self, config_text, norm_nc, label_nc):
68 super().__init__()
69
70 assert config_text.startswith('spade')
71 parsed = re.search('spade(\D+)(\d)x\d', config_text)
72 param_free_norm_type = str(parsed.group(1))
73 ks = int(parsed.group(2))
74
75 if param_free_norm_type == 'instance':
76 self.param_free_norm = nn.InstanceNorm2d(norm_nc, affine=False)
77 elif param_free_norm_type == 'syncbatch':
78 self.param_free_norm = SynchronizedBatchNorm2d(norm_nc, affine=False)
79 elif param_free_norm_type == 'batch':
80 self.param_free_norm = nn.BatchNorm2d(norm_nc, affine=False)
81 else:
82 raise ValueError('%s is not a recognized param-free norm type in SPADE'
83 % param_free_norm_type)
84
85 # The dimension of the intermediate embedding space. Yes, hardcoded.
86 nhidden = 128
87
88 pw = ks // 2
89 self.mlp_shared = nn.Sequential(
90 nn.Conv2d(label_nc, nhidden, kernel_size=ks, padding=pw),
91 nn.ReLU()
92 )
93 self.mlp_gamma = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)
94 self.mlp_beta = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw)
95
96 def forward(self, x, segmap):
97
98 # Part 1. generate parameter-free normalized activations
99 normalized = self.param_free_norm(x)
100
101 # Part 2. produce scaling and bias conditioned on semantic map
102 segmap = F.interpolate(segmap, size=x.size()[2:], mode='nearest')
103 actv = self.mlp_shared(segmap)
104 gamma = self.mlp_gamma(actv)
105 beta = self.mlp_beta(actv)
106
107 # apply scale and bias
108 out = normalized * (1 + gamma) + beta
109
110 return out

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected