MCPcopy Create free account
hub / github.com/Vchitect/Latte / MappingNetwork

Class MappingNetwork

tools/utils/layers.py:23–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21
22@persistence.persistent_class
23class MappingNetwork(torch.nn.Module):
24 def __init__(self,
25 z_dim, # Input latent (Z) dimensionality, 0 = no latent.
26 c_dim, # Conditioning label (C) dimensionality, 0 = no label.
27 w_dim, # Intermediate latent (W) dimensionality.
28 num_ws, # Number of intermediate latents to output, None = do not broadcast.
29 num_layers = 8, # Number of mapping layers.
30 embed_features = None, # Label embedding dimensionality, None = same as w_dim.
31 layer_features = None, # Number of intermediate features in the mapping layers, None = same as w_dim.
32 activation = 'lrelu', # Activation function: 'relu', 'lrelu', etc.
33 lr_multiplier = 0.01, # Learning rate multiplier for the mapping layers.
34 w_avg_beta = 0.995, # Decay for tracking the moving average of W during training, None = do not track.
35 cfg = {}, # Additional config
36 ):
37 super().__init__()
38
39 self.cfg = cfg
40 self.z_dim = z_dim
41 self.c_dim = c_dim
42 self.w_dim = w_dim
43 self.num_ws = num_ws
44 self.num_layers = num_layers
45 self.w_avg_beta = w_avg_beta
46
47 if embed_features is None:
48 embed_features = w_dim
49 if c_dim == 0:
50 embed_features = 0
51 if layer_features is None:
52 layer_features = w_dim
53
54 features_list = [z_dim + embed_features] + [layer_features] * (num_layers - 1) + [w_dim]
55
56 if c_dim > 0:
57 self.embed = FullyConnectedLayer(c_dim, embed_features)
58
59 for idx in range(num_layers):
60 in_features = features_list[idx]
61 out_features = features_list[idx + 1]
62 layer = FullyConnectedLayer(in_features, out_features, activation=activation, lr_multiplier=lr_multiplier)
63 setattr(self, f'fc{idx}', layer)
64
65 if num_ws is not None and w_avg_beta is not None:
66 self.register_buffer('w_avg', torch.zeros([w_dim]))
67
68 def forward(self, z, c, truncation_psi=1, truncation_cutoff=None, skip_w_avg_update=False):
69 # Embed, normalize, and concat inputs.
70 x = None
71 with torch.autograd.profiler.record_function('input'):
72 if self.z_dim > 0:
73 misc.assert_shape(z, [None, self.z_dim])
74 x = normalize_2nd_moment(z.to(torch.float32))
75
76 if self.c_dim > 0:
77 misc.assert_shape(c, [None, self.c_dim])
78 y = normalize_2nd_moment(self.embed(c.to(torch.float32)))
79 x = torch.cat([x, y], dim=1) if x is not None else y
80

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected