| 10 | |
| 11 | |
| 12 | class GANimation(BaseModel): |
| 13 | def __init__(self, opt): |
| 14 | super(GANimation, self).__init__(opt) |
| 15 | self._name = 'GANimation' |
| 16 | |
| 17 | # create networks |
| 18 | self._init_create_networks() |
| 19 | |
| 20 | # init train variables |
| 21 | if self._is_train: |
| 22 | self._init_train_vars() |
| 23 | |
| 24 | # load networks and optimizers |
| 25 | if not self._is_train or self._opt.load_epoch > 0: |
| 26 | self.load() |
| 27 | |
| 28 | # prefetch variables |
| 29 | self._init_prefetch_inputs() |
| 30 | |
| 31 | # init |
| 32 | self._init_losses() |
| 33 | |
| 34 | def _init_create_networks(self): |
| 35 | # generator network |
| 36 | self._G = self._create_generator() |
| 37 | self._G.init_weights() |
| 38 | if len(self._gpu_ids) > 1: |
| 39 | self._G = torch.nn.DataParallel(self._G, device_ids=self._gpu_ids) |
| 40 | self._G.cuda() |
| 41 | |
| 42 | # discriminator network |
| 43 | self._D = self._create_discriminator() |
| 44 | self._D.init_weights() |
| 45 | if len(self._gpu_ids) > 1: |
| 46 | self._D = torch.nn.DataParallel(self._D, device_ids=self._gpu_ids) |
| 47 | self._D.cuda() |
| 48 | |
| 49 | def _create_generator(self): |
| 50 | return NetworksFactory.get_by_name('generator_wasserstein_gan', c_dim=self._opt.cond_nc) |
| 51 | |
| 52 | def _create_discriminator(self): |
| 53 | return NetworksFactory.get_by_name('discriminator_wasserstein_gan', c_dim=self._opt.cond_nc) |
| 54 | |
| 55 | def _init_train_vars(self): |
| 56 | self._current_lr_G = self._opt.lr_G |
| 57 | self._current_lr_D = self._opt.lr_D |
| 58 | |
| 59 | # initialize optimizers |
| 60 | self._optimizer_G = torch.optim.Adam(self._G.parameters(), lr=self._current_lr_G, |
| 61 | betas=[self._opt.G_adam_b1, self._opt.G_adam_b2]) |
| 62 | self._optimizer_D = torch.optim.Adam(self._D.parameters(), lr=self._current_lr_D, |
| 63 | betas=[self._opt.D_adam_b1, self._opt.D_adam_b2]) |
| 64 | |
| 65 | def _init_prefetch_inputs(self): |
| 66 | self._input_real_img = self._Tensor(self._opt.batch_size, 3, self._opt.image_size, self._opt.image_size) |
| 67 | self._input_real_cond = self._Tensor(self._opt.batch_size, self._opt.cond_nc) |
| 68 | self._input_desired_cond = self._Tensor(self._opt.batch_size, self._opt.cond_nc) |
| 69 | self._input_real_img_path = None |