(self, current_step)
| 500 | # train Generator |
| 501 | # ----------------------------------------------------------------------------- |
| 502 | def train_generator(self, current_step): |
| 503 | # make GAN be trainable before starting training |
| 504 | misc.make_GAN_trainable(self.Gen, self.Gen_ema, self.Dis) |
| 505 | # toggle gradients of the generator and discriminator |
| 506 | misc.toggle_grad(model=self.Dis, grad=False, num_freeze_layers=-1, is_stylegan=self.is_stylegan) |
| 507 | misc.toggle_grad(model=self.Gen, grad=True, num_freeze_layers=-1, is_stylegan=self.is_stylegan) |
| 508 | if self.MODEL.info_type in ["discrete", "both"]: |
| 509 | misc.toggle_grad(getattr(misc.peel_model(self.Dis), self.MISC.info_params[0]), grad=True, num_freeze_layers=-1, is_stylegan=False) |
| 510 | if self.MODEL.info_type in ["continuous", "both"]: |
| 511 | misc.toggle_grad(getattr(misc.peel_model(self.Dis), self.MISC.info_params[1]), grad=True, num_freeze_layers=-1, is_stylegan=False) |
| 512 | misc.toggle_grad(getattr(misc.peel_model(self.Dis), self.MISC.info_params[2]), grad=True, num_freeze_layers=-1, is_stylegan=False) |
| 513 | self.Gen.apply(misc.track_bn_statistics) |
| 514 | for step_index in range(self.OPTIMIZATION.g_updates_per_step): |
| 515 | self.OPTIMIZATION.g_optimizer.zero_grad() |
| 516 | for acml_step in range(self.OPTIMIZATION.acml_steps): |
| 517 | with torch.cuda.amp.autocast() if self.RUN.mixed_precision and not self.is_stylegan else misc.dummy_context_mgr() as mpc: |
| 518 | # sample fake images and labels from p(G(z), y) |
| 519 | fake_images, fake_labels, fake_images_eps, trsp_cost, ws, info_discrete_c, info_conti_c = sample.generate_images( |
| 520 | z_prior=self.MODEL.z_prior, |
| 521 | truncation_factor=-1.0, |
| 522 | batch_size=self.OPTIMIZATION.batch_size, |
| 523 | z_dim=self.MODEL.z_dim, |
| 524 | num_classes=self.DATA.num_classes, |
| 525 | y_sampler="totally_random", |
| 526 | radius=self.LOSS.radius, |
| 527 | generator=self.Gen, |
| 528 | discriminator=self.Dis, |
| 529 | is_train=True, |
| 530 | LOSS=self.LOSS, |
| 531 | RUN=self.RUN, |
| 532 | MODEL=self.MODEL, |
| 533 | device=self.local_rank, |
| 534 | generator_mapping=self.Gen_mapping, |
| 535 | generator_synthesis=self.Gen_synthesis, |
| 536 | is_stylegan=self.is_stylegan, |
| 537 | style_mixing_p=self.cfgs.STYLEGAN.style_mixing_p, |
| 538 | stylegan_update_emas=False, |
| 539 | cal_trsp_cost=True if self.LOSS.apply_lo else False) |
| 540 | |
| 541 | # blur images for stylegan3-r |
| 542 | if self.MODEL.backbone == "stylegan3" and self.STYLEGAN.stylegan3_cfg == "stylegan3-r" and self.blur_init_sigma != "N/A": |
| 543 | blur_sigma = max(1 - (self.effective_batch_size * current_step) / (self.blur_fade_kimg * 1e3), 0) * self.blur_init_sigma |
| 544 | blur_size = np.floor(blur_sigma * 3) |
| 545 | if blur_size > 0: |
| 546 | f = torch.arange(-blur_size, blur_size + 1, device=fake_images.device).div(blur_sigma).square().neg().exp2() |
| 547 | fake_images = upfirdn2d.filter2d(fake_images, f / f.sum()) |
| 548 | |
| 549 | # apply differentiable augmentations if "apply_diffaug" is True |
| 550 | fake_images_ = self.AUG.series_augment(fake_images) |
| 551 | |
| 552 | # calculate adv_output, embed, proxy, and cls_output using the discriminator |
| 553 | fake_dict = self.Dis(fake_images_, fake_labels) |
| 554 | |
| 555 | # accumulate discriminator output informations for logging |
| 556 | if self.AUG.apply_ada or self.AUG.apply_apa: |
| 557 | self.dis_sign_fake += torch.tensor((fake_dict["adv_output"].sign().sum().item(), |
| 558 | self.OPTIMIZATION.batch_size), |
| 559 | device=self.local_rank) |
no test coverage detected