MCPcopy Create free account
hub / github.com/OpenGVLab/DragGAN / forward

Method forward

draggan/stylegan2/training/networks.py:232–267  ·  view source on GitHub ↗
(self, z, c, truncation_psi=1, truncation_cutoff=None, update_emas=False)

Source from the content-addressed store, hash-verified

230 self.register_buffer('w_avg', torch.zeros([w_dim]))
231
232 def forward(self, z, c, truncation_psi=1, truncation_cutoff=None, update_emas=False):
233 # Embed, normalize, and concat inputs.
234 x = None
235 with torch.autograd.profiler.record_function('input'):
236 if self.z_dim > 0:
237 misc.assert_shape(z, [None, self.z_dim])
238 x = normalize_2nd_moment(z.to(torch.float32))
239 if self.c_dim > 0:
240 misc.assert_shape(c, [None, self.c_dim])
241 y = normalize_2nd_moment(self.embed(c.to(torch.float32)))
242 x = torch.cat([x, y], dim=1) if x is not None else y
243
244 # Main layers.
245 for idx in range(self.num_layers):
246 layer = getattr(self, f'fc{idx}')
247 x = layer(x)
248
249 # Update moving average of W.
250 if update_emas and self.w_avg_beta is not None:
251 with torch.autograd.profiler.record_function('update_w_avg'):
252 self.w_avg.copy_(x.detach().mean(dim=0).lerp(self.w_avg, self.w_avg_beta))
253
254 # Broadcast.
255 if self.num_ws is not None:
256 with torch.autograd.profiler.record_function('broadcast'):
257 x = x.unsqueeze(1).repeat([1, self.num_ws, 1])
258
259 # Apply truncation.
260 if truncation_psi != 1:
261 with torch.autograd.profiler.record_function('truncate'):
262 assert self.w_avg_beta is not None
263 if self.num_ws is None or truncation_cutoff is None:
264 x = self.w_avg.lerp(x, truncation_psi)
265 else:
266 x[:, :truncation_cutoff] = self.w_avg.lerp(x[:, :truncation_cutoff], truncation_psi)
267 return x
268
269 def extra_repr(self):
270 return f'z_dim={self.z_dim:d}, c_dim={self.c_dim:d}, w_dim={self.w_dim:d}, num_ws={self.num_ws:d}'

Callers

nothing calls this directly

Calls 2

normalize_2nd_momentFunction · 0.85
meanMethod · 0.80

Tested by

no test coverage detected