class Model(): def __init__(self, args): self.args = args self.pretrained = False self.epoch = 0 self.G = Generator() self.D = Discriminator() self.g_optimizer = optim.Adam(self.G.parameters(), lr=1E-4) self.d_optimizer = optim.Adam(self.D.parameters(), lr=1E-4) self.g_scheduler = optim.lr_scheduler.StepLR(self.g_optimizer, step_size=40) self.d_scheduler = optim.lr_scheduler.StepLR(self.d_optimizer, step_size=40) self.train_losses = [] self.val_losses = [] if args.load_model: self._load_state(args.load_model) # extract all layers prior to the last softmax of VGG-19 vgg19_layers = list(models.vgg19(pretrained=True).features)[:36] self.vgg19 = nn.Sequential(*vgg19_layers).eval() for param in self.vgg19.parameters(): param.requires_grad = False self.mse_loss = torch.nn.MSELoss() self.bce_loss = torch.nn.BCELoss() def train(self, train_dataloader, val_dataloader=None): self.D.to(device) self.G.to(device) self.vgg19.to(device) """ Pretrain Generator """ if not self.pretrained: log_message("Starting pretraining") self._pretrain(train_dataloader) self._save_state() if val_dataloader: val_g_loss, _ = self.evaluate(val_dataloader) log_message("Pretrain G loss: {:.4f}".format(val_g_loss)) """ Real Training """ log_message("Starting training") while self.epoch < self.args.epochs: # Train one epoch self.D.train() self.G.train() g_loss, d_loss = self._run_epoch(train_dataloader, train=True) self.train_losses.append([g_loss, d_loss]) self.g_scheduler.step() self.d_scheduler.step() self.epoch += 1 log_message("Epoch: {}/{}".format(self.epoch, self.args.epochs)) # Print evaluation train_string = "Train G loss: {:.4f} | Train D loss: {:.4f}".format( g_loss, d_loss) if self.epoch % self.args.eval_epochs == 0: if val_dataloader: val_g_loss, val_d_loss = self.evaluate(val_dataloader) self.val_losses.append([val_g_loss, val_d_loss]) train_string += " | Val G loss: {:.4f} | Val D loss: {:.4f}".format( val_g_loss, val_d_loss) log_message(train_string) # Save the model if self.epoch % self.args.save_epochs == 0: self._save_state() log_message("Finished training") self._save_state() def evaluate(self, dataloader): self.D.eval() self.G.eval() with torch.no_grad(): return self._run_epoch(dataloader, train=False) def generate(self, dataloader): def to_image(tensor): array = tensor.data.cpu().numpy() array = array.transpose((1, 2, 0)) array = np.clip(255.0 * (array + 1) / 2, 0, 255) array = np.uint8(array) return Image.fromarray(array) self.D.eval() self.G.eval() if not os.path.exists(self.args.generate_dir): os.mkdir(self.args.generate_dir) with torch.no_grad(): for batch in dataloader: low_res = batch['low_res'].to(device) hi_res = batch['high_res'] generated = self.G(low_res) for i in range(len(generated)): naive = np.clip( 255.0 * low_res[i].data.cpu().numpy().transpose( (1, 2, 0)), 0, 255) naive = Image.fromarray(np.uint8(naive)) naive = naive.resize((96, 96), Image.BICUBIC) fake_im = to_image(generated[i]) real_im = to_image(hi_res[i]) naive.save( os.path.join(self.args.generate_dir, "{}_naive.png".format(i))) fake_im.save( os.path.join(self.args.generate_dir, "{}_fake.png".format(i))) real_im.save( os.path.join(self.args.generate_dir, "{}_real.png".format(i))) if i > 10: return def _load_state(self, fname): if torch.cuda.is_available(): map_location = lambda storage, loc: storage.cuda() else: map_location = 'cpu' state = torch.load(fname, map_location=map_location) self.pretrained = state["pretrained"] self.epoch = state["epoch"] self.train_losses = state["train_losses"] self.val_losses = state["val_losses"] self.G.load_state_dict(state["G"]) self.D.load_state_dict(state["D"]) self.g_optimizer.load_state_dict(state["g_optimizer"]) self.d_optimizer.load_state_dict(state["d_optimizer"]) self.g_scheduler.load_state_dict(state["g_scheduler"]) self.d_scheduler.load_state_dict(state["d_scheduler"]) for state in self.d_optimizer.state.values(): for k, v in state.items(): if torch.is_tensor(v): state[k] = v.to(device) for state in self.g_optimizer.state.values(): for k, v in state.items(): if torch.is_tensor(v): state[k] = v.to(device) def _save_state(self): if not os.path.exists(self.args.save_dir): os.mkdir(self.args.save_dir) fname = "%s/save_%d.pkl" % (self.args.save_dir, self.epoch) state = { "pretrained": self.pretrained, "epoch": self.epoch, "G": self.G.state_dict(), "D": self.D.state_dict(), "g_optimizer": self.g_optimizer.state_dict(), "d_optimizer": self.d_optimizer.state_dict(), "g_scheduler": self.g_scheduler.state_dict(), "d_scheduler": self.d_scheduler.state_dict(), "train_losses": self.train_losses, "val_losses": self.val_losses } torch.save(state, fname) def _pretrain(self, dataloader): self.G.train() for i in range(self.args.pretrain_epochs): log_message("Pretrain Epoch: {}/{}".format( i, self.args.pretrain_epochs)) for batch in dataloader: low_res = batch['low_res'].to(device) high_res = batch['high_res'].to(device) self.g_optimizer.zero_grad() generated = self.G(low_res) # Optimize pixel loss g_loss = self.mse_loss(generated, high_res) g_loss.backward() self.g_optimizer.step() self.pretrained = True def _run_epoch(self, dataloader, train): g_losses, d_losses = [], [] for batch in dataloader: low_res = batch['low_res'].to(device) high_res = batch['high_res'].to(device) batch_size = high_res.size(0) real = torch.ones((batch_size, 1), requires_grad=False).to(device) fake = torch.zeros((batch_size, 1), requires_grad=False).to(device) """ Discriminator """ generated = self.G(low_res) self.d_optimizer.zero_grad() real_loss = self.bce_loss(self.D(high_res), real) fake_loss = self.bce_loss(self.D(generated), fake) d_loss = real_loss + fake_loss d_losses.append(d_loss.item()) if train: d_loss.backward() self.d_optimizer.step() """ Generator """ generated = self.G(low_res) self.g_optimizer.zero_grad() # take a [B, C, W, H] batch of [-1, 1] images, normalize, then run through vgg19 def vgg_features(image): mean = torch.tensor( [0.485, 0.456, 0.406]).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(device) std = torch.tensor( [0.229, 0.224, 0.225]).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(device) image = (image + 1) / 2 image = (image - mean) / std return self.vgg19(image) pixel_loss = self.mse_loss(high_res, generated) content_loss = self.mse_loss(vgg_features(high_res), vgg_features(generated)) adversarial_loss = self.bce_loss(self.D(generated), real) g_loss = pixel_loss + 0.006 * content_loss + 1E-3 * adversarial_loss g_losses.append(g_loss.item()) if train: g_loss.backward() self.g_optimizer.step() return np.mean(g_losses), np.mean(d_losses)
d_count += 1 Dx = D(x) z = torch.randn((BATCH_SIZE, Z_DIM)).to(device) Gz = G(z) DGz = D(Gz) gradient_penalty = calc_gradient_penalty(D, x, Gz) d_cost = d_loss(DGz, Dx, gradient_penalty) d_cost.backward() optim_D.step() if (d_count % D_STEPS) == 0: for p in D.parameters(): p.requires_grad = False G.zero_grad() z = torch.randn((BATCH_SIZE, Z_DIM)).to(device) Gz = G(z) DGz = D(Gz) g_cost = g_loss(DGz) g_cost.backward() optim_G.step() sys.stdout.write(f'\r {d_count} Epoch {e}/{EPOCHS} {d_count * 100 /len(dataloader):.2f}% ' f'D:{d_cost.item():.2f}, G:{g_cost.item():.2f} Dx: {Dx.mean().item():.2f} ' f'DGz: {DGz.mean().item():.2f}') if (d_count % 5000) == 0: Gz = (Gz + 1) / 2 torchvision.utils.save_image(Gz, f'./sample_{d_count}.jpg') torch.save(G.state_dict(), 'G.nn') torch.save(D.state_dict(), 'D.nn')
class Solver(object): def __init__(self, config, data_loader): self.generator = None self.discriminator = None self.g_optimizer = None self.d_optimizer = None self.g_conv_dim = config.g_conv_dim self.d_conv_dim = config.d_conv_dim self.z_dim = config.z_dim self.beta1 = config.beta1 self.beta2 = config.beta2 self.image_size = config.image_size self.data_loader = data_loader self.num_epochs = config.num_epochs self.batch_size = config.batch_size self.sample_size = config.sample_size self.lr = config.lr self.log_step = config.log_step self.sample_step = config.sample_step self.sample_path = config.sample_path self.model_path = config.model_path self.epoch = config.epoch self.build_model() self.plotter = Plotter() def build_model(self): """Build generator and discriminator.""" self.generator = Generator(z_dim=self.z_dim) print(count_parameters(self.generator)) self.discriminator = Discriminator() print(count_parameters(self.discriminator)) self.g_optimizer = optim.Adam(self.generator.parameters(), self.lr, (self.beta1, self.beta2)) self.d_optimizer = optim.Adam(self.discriminator.parameters(), self.lr*1, (self.beta1, self.beta2)) if self.epoch: g_path = os.path.join(self.model_path, 'generator-%d.pkl' % self.epoch) d_path = os.path.join(self.model_path, 'discriminator-%d.pkl' % self.epoch) g_optim_path = os.path.join(self.model_path, 'gen-optim-%d.pkl' % self.epoch) d_optim_path = os.path.join(self.model_path, 'dis-optim-%d.pkl' % self.epoch) self.generator.load_state_dict(torch.load(g_path)) self.discriminator.load_state_dict(torch.load(d_path)) self.g_optimizer.load_state_dict(torch.load(g_optim_path)) self.d_optimizer.load_state_dict(torch.load(d_optim_path)) if torch.cuda.is_available(): self.generator.cuda() self.discriminator.cuda() def to_variable(self, x): """Convert tensor to variable.""" if torch.cuda.is_available(): x = x.cuda() return Variable(x) def to_data(self, x): """Convert variable to tensor.""" if torch.cuda.is_available(): x = x.cpu() return x.data def reset_grad(self): """Zero the gradient buffers.""" self.discriminator.zero_grad() self.generator.zero_grad() def denorm(self, x): """Convert range (-1, 1) to (0, 1)""" out = (x + 1) / 2 return out.clamp(0, 1) def train(self): """Train generator and discriminator.""" fixed_noise = self.to_variable(torch.randn(self.batch_size, self.z_dim)) total_step = len(self.data_loader) for epoch in range(self.epoch, self.epoch + self.num_epochs) if self.epoch else range(self.num_epochs): for i, images in enumerate(self.data_loader): if len(images) != self.batch_size: continue # self.plotter.draw_kernels(self.discriminator) for p in self.discriminator.parameters(): p.requires_grad = True #===================== Train D =====================# images = self.to_variable(images) images.retain_grad() batch_size = images.size(0) noise = self.to_variable(torch.randn(batch_size, self.z_dim)) # Train D to recognize real images as real. outputs = self.discriminator(images) real_loss = torch.mean((outputs - 1) ** 2) # L2 loss instead of Binary cross entropy loss (this is optional for stable training) # real_loss = torch.mean(outputs - 1) # Train D to recognize fake images as fake. fake_images = self.generator(noise) fake_images.retain_grad() outputs = self.discriminator(fake_images) fake_loss = torch.mean(outputs ** 2) # fake_loss = torch.mean(outputs) # gradient penalty gp_loss = calc_gradient_penalty(self.discriminator, images, fake_images) # Backprop + optimize d_loss = fake_loss + real_loss + gp_loss self.reset_grad() d_loss.backward() self.d_optimizer.step() if i % 10 == 0: self.plotter.draw_activations(fake_images.grad[0], original=fake_images[0]) g_losses = [] for p in self.discriminator.parameters(): p.requires_grad = False #===================== Train G =====================# for g_batch in range(5): noise = self.to_variable(torch.randn(batch_size, self.z_dim)) # Train G so that D recognizes G(z) as real. fake_images = self.generator(noise) outputs = self.discriminator(fake_images) g_loss = torch.mean((outputs - 1) ** 2) # g_loss = -torch.mean(outputs) # Backprop + optimize self.reset_grad() g_loss.backward() # if g_loss.item() < 0.5 * d_loss.item(): # break self.g_optimizer.step() g_losses.append("%.3f"%g_loss.clone().item()) # print the log info if (i+1) % self.log_step == 0: print('Epoch [%d/%d], Step[%d/%d], d_real_loss: %.4f, ' 'd_fake_loss: %.4f, gp_loss: %s, g_loss: %s' %(epoch+1, self.num_epochs, i+1, total_step, real_loss.item(), fake_loss.item(), gp_loss.item(), ", ".join(g_losses))) # save the sampled images # print((i+1)%self.sample_step) if (i) % self.sample_step == 0: print("saving samples") fake_images = self.generator(fixed_noise) if not os.path.exists(self.sample_path): os.makedirs(self.sample_path) torchvision.utils.save_image(self.denorm(fake_images.data), os.path.join(self.sample_path, 'fake_samples-%d-%d.png' %(epoch+1, i+1))) # save the model parameters for each epoch if epoch % 5 == 0: if not os.path.exists(self.model_path): os.mkdir(self.model_path) g_path = os.path.join(self.model_path, 'generator-%d.pkl' %(epoch+1)) d_path = os.path.join(self.model_path, 'discriminator-%d.pkl' %(epoch+1)) g_optim_path = os.path.join(self.model_path, 'gen-optim-%d.pkl' % (epoch + 1)) d_optim_path = os.path.join(self.model_path, 'dis-optim-%d.pkl' % (epoch + 1)) torch.save(self.generator.state_dict(), g_path) torch.save(self.discriminator.state_dict(), d_path) torch.save(self.g_optimizer.state_dict(), g_optim_path) torch.save(self.d_optimizer.state_dict(), d_optim_path) def sample(self): # Load trained parameters g_path = os.path.join(self.model_path, 'generator-%d.pkl' % self.num_epochs) d_path = os.path.join(self.model_path, 'discriminator-%d.pkl' % self.num_epochs) self.generator.load_state_dict(torch.load(g_path)) self.discriminator.load_state_dict(torch.load(d_path)) self.generator.eval() self.discriminator.eval() # Sample the images noise = self.to_variable(torch.randn(self.sample_size, self.z_dim)) fake_images = self.generator(noise) sample_path = os.path.join(self.sample_path, 'fake_samples-final.png') torchvision.utils.save_image(self.denorm(fake_images.data), sample_path, nrow=12) print("Saved sampled images to '%s'" %sample_path)
writer.add_scalar('Discriminator B loss', discriminator_A_loss.item(), num_iterations) if (epoch % 50) == 0: # Model save with torch.no_grad(): if not os.path.exists("checkpoint"): os.mkdir("checkpoint") torch.save(generator_A2B.state_dict(), "checkpoint/generator_A2B.pt") torch.save(generator_B2A.state_dict(), "checkpoint/generator_B2A.pt") torch.save(discriminator_A.state_dict(), "checkpoint/discriminator_A.pt") torch.save(discriminator_B.state_dict(), "checkpoint/discriminator_B.pt") for i in range(len(SF1_test_data_source)): feature_A = SF1_test_data_source[i] feature_B = TF2_test_data_source[i] filename_A = os.path.basename( SF1_test_data_source.dataset.collected_files[i][0]) filename_B = os.path.basename( TF2_test_data_source.dataset.collected_files[i][0]) f0_A = feature_A[:, 0] f0_B = feature_B[:, 0]
class Trainer(object): def __init__(self, celeba_loader, config): # miscellaneous self.use_tensorboard = config.use_tensorboard self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # data loader self.dataload = celeba_loader # model configurations self.c64 = config.c64 self.c256 = config.c256 self.c2048 = config.c2048 self.rb6 = config.rb6 self.attr_dim = config.attr_dim self.hair_dim = config.hair_dim # training configurations self.selected_attrs = config.selected_attrs self.train_iters = config.train_iters self.num_iters_decay = config.num_iters_decay self.n_critic = config.n_critic self.d_lr = config.d_lr self.r_lr = config.r_lr self.t_lr = config.t_lr self.e_lr = config.e_lr self.decay_rate = config.decay_rate self.beta1 = config.beta1 self.beta2 = config.beta2 self.lambda_cls = config.lambda_cls self.lambda_cyc = config.lambda_cyc self.lambda_gp = config.lambda_gp # test configurations self.test_iters = config.test_iters # directories self.sample_dir = config.sample_dir self.model_save_dir = config.model_save_dir self.result_dir = config.result_dir self.log_dir = config.log_dir # step size self.log_step = config.log_step self.sample_step = config.sample_step self.model_save_step = config.model_save_step self.lr_update_step = config.lr_update_step # initial models self.build_models() if self.use_tensorboard: self.build_tensorboard() def build_models(self): self.E = Encoder(self.c64, self.rb6) self.T_Hair = Transformer(self.hair_dim, self.c256, self.rb6) self.T_Gender = Transformer(self.attr_dim, self.c256, self.rb6) self.T_Smailing = Transformer(self.attr_dim, self.c256, self.rb6) self.R = Reconstructor(self.c256) self.D_Hair = Discriminator(self.hair_dim, self.c64) self.D_Gender = Discriminator(self.attr_dim, self.c64) self.D_Smailing = Discriminator(self.attr_dim, self.c64) self.e_optim = torch.optim.Adam(self.E.parameters(), self.e_lr, [self.beta1, self.beta2]) self.th_optim = torch.optim.Adam(self.T_Hair.parameters(), self.t_lr, [self.beta1, self.beta2]) self.tg_optim = torch.optim.Adam(self.T_Gender.parameters(), self.t_lr, [self.beta1, self.beta2]) self.ts_optim = torch.optim.Adam(self.T_Smailing.parameters(), self.t_lr, [self.beta1, self.beta2]) self.r_optim = torch.optim.Adam(self.R.parameters(), self.r_lr, [self.beta1, self.beta2]) self.dh_optim = torch.optim.Adam(self.D_Hair.parameters(), self.d_lr, [self.beta1, self.beta2]) self.dg_optim = torch.optim.Adam(self.D_Gender.parameters(), self.d_lr, [self.beta1, self.beta2]) self.ds_optim = torch.optim.Adam(self.D_Smailing.parameters(), self.d_lr, [self.beta1, self.beta2]) self.print_network(self.E, 'Encoder') self.print_network(self.T_Hair, 'Transformer for Hair Color') self.print_network(self.T_Gender, 'Transformer for Gender') self.print_network(self.T_Smailing, 'Transformer for Smailing') self.print_network(self.R, 'Reconstructor') self.print_network(self.D_Hair, 'D for Hair Color') self.print_network(self.D_Gender, 'D for Gender') self.print_network(self.D_Smailing, 'D for Smailing') self.E.to(self.device) self.T_Hair.to(self.device) self.T_Gender.to(self.device) self.T_Smailing.to(self.device) self.R.to(self.device) self.D_Gender.to(self.device) self.D_Smailing.to(self.device) self.D_Hair.to(self.device) def print_network(self, model, name): """Print out the network information.""" num_params = 0 for p in model.parameters(): num_params += p.numel() print(name) print("The number of parameters: {}".format(num_params)) print(model) def build_tensorboard(self): """Build a tensorboard logger.""" from logger import Logger self.logger = Logger(self.log_dir) def gradient_penalty(self, y, x): """Compute gradient penalty: (L2_norm(dy/dx) - 1)**2.""" weight = torch.ones(y.size()).to(self.device) dydx = torch.autograd.grad(outputs=y, inputs=x, grad_outputs=weight, retain_graph=True, create_graph=True, only_inputs=True)[0] dydx = dydx.view(dydx.size(0), -1) dydx_l2norm = torch.sqrt(torch.sum(dydx**2, dim=1)) return torch.mean((dydx_l2norm-1)**2) def reset_grad(self): self.e_optim.zero_grad() self.th_optim.zero_grad() self.tg_optim.zero_grad() self.ts_optim.zero_grad() self.r_optim.zero_grad() self.dh_optim.zero_grad() self.dg_optim.zero_grad() self.ds_optim.zero_grad() def update_lr(self, e_lr, d_lr, r_lr, t_lr): """Decay learning rates of the generator and discriminator.""" for param_group in self.e_optim.param_groups: param_group['lr'] = e_lr for param_group in self.dh_optim.param_groups: param_group['lr'] = d_lr for param_group in self.dg_optim.param_groups: param_group['lr'] = d_lr for param_group in self.ds_optim.param_groups: param_group['lr'] = d_lr for param_group in self.r_optim.param_groups: param_group['lr'] = r_lr for param_group in self.th_optim.param_groups: param_group['lr'] = t_lr for param_group in self.tg_optim.param_groups: param_group['lr'] = t_lr for param_group in self.ts_optim.param_groups: param_group['lr'] = t_lr def create_labels(self, c_org, c_dim=5, selected_attrs=None): """Generate target domain labels for debugging and testing.""" # Get hair color indices. hair_color_indices = [] for i, attr_name in enumerate(selected_attrs): if attr_name in ['Black_Hair', 'Blond_Hair', 'Brown_Hair']: hair_color_indices.append(i) c_trg_list = [] for i in range(c_dim): c_trg = c_org.clone() if i in hair_color_indices: # Set one hair color to 1 and the rest to 0. c_trg[:, i] = 1 for j in hair_color_indices: if j != i: c_trg[:, j] = 0 else: c_trg[:, i] = (c_trg[:, i] == 0) # Reverse attribute value. c_trg_list.append(c_trg.to(self.device)) return c_trg_list def denorm(self, x): """Convert the range from [-1, 1] to [0, 1].""" out = (x + 1) / 2 return out.clamp_(0, 1) def train(self): data_loader = self.dataload # Fetch fixed inputs for debugging. data_iter = iter(data_loader) x_fixed, c_org = next(data_iter) x_fixed = x_fixed.to(self.device) c_fixed_list = self.create_labels(c_org, 5, self.selected_attrs) d_lr = self.d_lr r_lr = self.r_lr t_lr = self.t_lr e_lr = self.e_lr # Start training print('Starting point==============================') start_time = time.time() for i in range(0, self.train_iters): # =================================================================================== # # 1. Preprocess input data # # =================================================================================== # # Fetch real images and labels try: x_real, label_real = next(data_iter) except: data_iter = iter(data_loader) x_real, label_real = next(data_iter) rand_idx = torch.randperm(label_real.size(0)) label_feak = label_real[rand_idx] x_real = x_real.to(self.device) # labels for hair color label_h_real = label_real[:, 0:3] label_h_feak = label_feak[:, 0:3] # labels for gender label_g_real = label_real[:, 3:4] label_g_feak = label_feak[:, 3:4] # labels for smailing label_s_real = label_real[:, 4:] label_s_feak = label_feak[:, 4:] label_h_real = label_h_real.to(self.device) label_h_feak = label_h_feak.to(self.device) label_g_real = label_g_real.to(self.device) label_g_feak = label_g_feak.to(self.device) label_s_real = label_s_real.to(self.device) label_s_feak = label_s_feak.to(self.device) # =================================================================================== # # 2. Train the discriminator # # =================================================================================== # # Computer loss with real images h_src, h_cls = self.D_Hair(x_real) d_h_loss_real = -torch.mean(h_src) d_h_loss_cls = F.binary_cross_entropy_with_logits(h_cls, label_h_real, reduction='sum') / h_cls.size(0) g_src, g_cls = self.D_Gender(x_real) d_g_loss_real = -torch.mean(g_src) d_g_loss_cls = F.binary_cross_entropy_with_logits(g_cls, label_g_real, reduction='sum') / g_cls.size(0) s_src, s_cls = self.D_Smailing(x_real) d_s_loss_real = -torch.mean(s_src) d_s_loss_cls = F.binary_cross_entropy_with_logits(s_cls, label_s_real, reduction='sum') / s_cls.size(0) # Generate fake images and computer loss # Retrieve features of real image features = self.E(x_real) # Transform attributes from one value to an other t_h_features = self.T_Hair(features.detach(), label_h_feak) t_g_features = self.T_Gender(features.detach(), label_g_feak) t_s_features = self.T_Smailing(features.detach(), label_s_feak) # Reconstruct images from transformed attributes x_h_feak = self.R(t_h_features.detach()) x_g_feak = self.R(t_g_features.detach()) x_s_feak = self.R(t_s_features.detach()) # Computer loss with fake images h_src, h_cls = self.D_Hair(x_h_feak.detach()) d_h_loss_fake = torch.mean(h_src) g_src, g_cls = self.D_Gender(x_g_feak.detach()) d_g_loss_fake = torch.mean(g_src) s_src, s_cls = self.D_Smailing(x_s_feak.detach()) d_s_loss_fake = torch.mean(s_src) # Compute loss for gradient penalty alpha = torch.rand(x_real.size(0), 1, 1, 1).to(self.device) x_h_hat = (alpha * x_real.data + (1 - alpha) * x_h_feak.data).requires_grad_(True) #x_h_hat = (alpha * x_real.data + (1-alpha) * x_h_feak.data).requires_grad_(True).to(torch.float16) x_g_hat = (alpha * x_real.data + (1 - alpha) * x_g_feak.data).requires_grad_(True) #x_g_hat = (alpha * x_real.data + (1-alpha) * x_g_feak.data).requires_grad_(True).to(torch.float16) x_s_hat = (alpha * x_real.data + (1 - alpha) * x_s_feak.data).requires_grad_(True) #x_s_hat = (alpha * x_real.data + (1-alpha) * x_s_feak.data).requires_grad_(True).to(torch.float16) out_src, _ = self.D_Hair(x_h_hat) d_h_loss_gp = self.gradient_penalty(out_src, x_h_hat) out_src, _ = self.D_Gender(x_g_hat) d_g_loss_gp = self.gradient_penalty(out_src, x_g_hat) out_src, _ = self.D_Smailing(x_s_hat) d_s_loss_gp = self.gradient_penalty(out_src, x_s_hat) # Backward and optimize d_loss = d_h_loss_real + d_g_loss_real + d_s_loss_real + \ d_h_loss_fake + d_g_loss_fake + d_s_loss_fake + \ self.lambda_gp * (d_h_loss_gp + d_g_loss_gp + d_s_loss_gp) + \ self.lambda_cls * (d_h_loss_cls + d_g_loss_cls + d_s_loss_cls) #d_loss = d_h_loss_real + d_h_loss_fake + self.lambda_gp * d_h_loss_gp + self.lambda_cls * d_h_loss_cls self.reset_grad() d_loss.backward() self.dh_optim.step() self.dg_optim.step() self.ds_optim.step() # Logging loss = {} loss['D/h_loss_real'] = d_h_loss_real.item() loss['D/g_loss_real'] = d_g_loss_real.item() loss['D/s_loss_real'] = d_s_loss_real.item() loss['D/h_loss_fake'] = d_h_loss_fake.item() loss['D/g_loss_fake'] = d_g_loss_fake.item() loss['D/s_loss_fake'] = d_s_loss_fake.item() loss['D/h_loss_cls'] = d_h_loss_cls.item() loss['D/g_loss_cls'] = d_g_loss_cls.item() loss['D/s_loss_cls'] = d_s_loss_cls.item() loss['D/h_loss_gp'] = d_h_loss_gp.item() loss['D/g_loss_gp'] = d_g_loss_gp.item() loss['D/s_loss_gp'] = d_s_loss_gp.item() # =================================================================================== # # 3. Train the encoder, transformer and reconstructor # # =================================================================================== # if(i+1) % self.n_critic == 0: # Generate fake images and compute loss # Retrieve features of real image features = self.E(x_real) # Transform attributes from one value to an other t_h_features = self.T_Hair(features, label_h_feak) t_g_features = self.T_Gender(features, label_g_feak) t_s_features = self.T_Smailing(features, label_s_feak) # Reconstruct images from transformed attributes x_h_feak = self.R(t_h_features) x_g_feak = self.R(t_g_features) x_s_feak = self.R(t_s_features) # Computer loss with fake images h_src, h_cls = self.D_Hair(x_h_feak) etr_h_loss_fake = -torch.mean(h_src) etr_h_loss_cls = F.binary_cross_entropy_with_logits(h_cls, label_h_feak, reduction='sum') / h_cls.size(0) g_src, g_cls = self.D_Gender(x_g_feak) etr_g_loss_fake = -torch.mean(g_src) etr_g_loss_cls = F.binary_cross_entropy_with_logits(g_cls, label_g_feak, reduction='sum') / g_cls.size(0) s_src, s_cls = self.D_Smailing(x_s_feak) etr_s_loss_fake = -torch.mean(s_src) etr_s_loss_cls = F.binary_cross_entropy_with_logits(s_cls, label_s_feak, reduction='sum') / s_cls.size(0) # Real - Encoder - Reconstructor - Real loss x_re = self.R(features) er_loss_cyc = torch.mean(torch.abs(x_re - x_real)) # Real - Encoder - Transform, Real - Encoder - Transform - Reconstructor - Encoder loss h_fake_features = self.E(x_h_feak) g_fake_features = self.E(x_g_feak) s_fake_features = self.E(x_s_feak) etr_h_loss_cyc = torch.mean(torch.abs(t_h_features - h_fake_features)) etr_g_loss_cyc = torch.mean(torch.abs(t_g_features - g_fake_features)) etr_s_loss_cyc = torch.mean(torch.abs(t_s_features - s_fake_features)) # Backward and optimize etr_loss = etr_h_loss_fake + etr_g_loss_fake + etr_s_loss_fake + \ self.lambda_cls * (etr_h_loss_cls + etr_g_loss_cls + etr_s_loss_cls) + \ self.lambda_cyc * (er_loss_cyc + etr_h_loss_cyc + etr_g_loss_cyc + etr_s_loss_cyc) #etr_loss = etr_h_loss_fake + self.lambda_cls * etr_h_loss_cls + self.lambda_cyc * (er_loss_cyc + etr_h_loss_cyc) self.reset_grad() etr_loss.backward() self.e_optim.step() self.th_optim.step() self.tg_optim.step() self.ts_optim.step() self.r_optim.step() # Logging. loss['ETR/h_loss_fake'] = etr_h_loss_fake.item() loss['ETR/g_loss_fake'] = etr_g_loss_fake.item() loss['ETR/s_loss_fake'] = etr_s_loss_fake.item() loss['ETR/h_loss_cls'] = etr_h_loss_cls.item() loss['ETR/g_loss_cls'] = etr_g_loss_cls.item() loss['ETR/s_loss_cls'] = etr_s_loss_cls.item() loss['ER/er_loss_cyc'] = er_loss_cyc.item() loss['ETR/h_loss_cyc'] = etr_h_loss_cyc.item() loss['ETR/g_loss_cyc'] = etr_g_loss_cyc.item() loss['ETR/s_loss_cyc'] = etr_s_loss_cyc.item() # =================================================================================== # # 4. Miscellaneous # # =================================================================================== # # Translate fixed images for debugging. if (i + 1) % self.sample_step == 0: with torch.no_grad(): x_fake_list = [x_fixed] for c_fixed in c_fixed_list: xf = self.E(x_fixed) xth = self.T_Hair(xf, c_fixed[:, 0:3]) xtg = self.T_Gender(xth, c_fixed[:, 3:4]) xts = self.T_Smailing(xtg, c_fixed[:, 4:5]) x_fake_list.append(self.R(xts)) x_concat = torch.cat(x_fake_list, dim=3) sample_path = os.path.join(self.sample_dir, '{}-images.jpg'.format(i + 1)) save_image(self.denorm(x_concat.data.cpu()), sample_path, nrow=1, padding=0) print('Saved real and fake images into {}...'.format(sample_path)) # Print out training information. if (i + 1) % self.log_step == 0: et = time.time() - start_time et = str(datetime.timedelta(seconds=et))[:-7] log = "Elapsed [{}], Iteration [{}/{}]".format(et, i + 1, self.train_iters) for tag, value in loss.items(): log += ", {}: {:.4f}".format(tag, value) print(log) if self.use_tensorboard: for tag, value in loss.items(): self.logger.scalar_summary(tag, value, i+1) # save model checkpoints if (i+1) % self.model_save_step == 0: E_path = os.path.join(self.model_save_dir, '{}-E.ckpt'.format(i+1)) D_h_path = os.path.join(self.model_save_dir, '{}-D_h.ckpt'.format(i+1)) D_g_path = os.path.join(self.model_save_dir, '{}-D_g.ckpt'.format(i+1)) D_s_path = os.path.join(self.model_save_dir, '{}-D_s.ckpt'.format(i+1)) R_path = os.path.join(self.model_save_dir, '{}-R.ckpt'.format(i+1)) T_h_path = os.path.join(self.model_save_dir, '{}-T_h.ckpt'.format(i+1)) T_g_path = os.path.join(self.model_save_dir, '{}-T_g.ckpt'.format(i+1)) T_s_path = os.path.join(self.model_save_dir, '{}-T_s.ckpt'.format(i+1)) torch.save(self.E.state_dict(), E_path) torch.save(self.D_Hair.state_dict(), D_h_path) torch.save(self.D_Gender.state_dict(), D_g_path) torch.save(self.D_Smailing.state_dict(), D_s_path) torch.save(self.R.state_dict(), R_path) torch.save(self.T_Hair.state_dict(), T_h_path) torch.save(self.T_Gender.state_dict(), T_g_path) torch.save(self.T_Smailing.state_dict(), T_s_path) print('Saved model checkpoints into {}...'.format(self.model_save_dir)) # decay learning rates if (i+1) % self.lr_update_step == 0 and (i+1) > self.num_iters_decay: e_lr -= (self.e_lr / float(self.decay_rate)) d_lr -= (self.d_lr / float(self.decay_rate)) r_lr -= (self.r_lr / float(self.decay_rate)) t_lr -= (self.t_lr / float(self.decay_rate)) self.update_lr(e_lr, d_lr, r_lr, t_lr) print ('Decayed learning rates, e_lr: {}, d_lr: {}, r_lr: {}, t_lr: {}.'.format(e_lr, d_lr, r_lr, t_lr))
class Trainer(object): def __init__(self, data_loader, config): self.dataloader = data_loader self.imsize = config.imsize self.batch_size = config.batch_size self.g_lr = config.g_lr self.d_lr = config.d_lr self.g_dim = config.g_dim self.d_dim = config.d_dim self.beta1 = config.beta1 self.beta2 = config.beta2 self.lambda_gp = config.lambda_gp self.z_dim = config.z_dim self.num_iters = config.total_step self.num_iters_decay = config.iter_start_decay self.log_step = config.log_step self.sample_step = config.sample_step self.lr_update_step = config.lr_iter_decay self.lr_decay = config.lr_decay self.model_save_step = config.model_save_step self.resume_iters = config.resume_iter self.version = config.version self.device = torch.device('cuda:0') self.sample_path = os.path.join(config.sample_path, self.version) self.model_save_dir = os.path.join(config.model_save_path, self.version) self.build_model() def build_model(self): self.G = Generator(image_size=self.imsize, z_dim=self.z_dim, conv_dim=self.g_dim) self.D = Discriminator(conv_dim=self.d_dim) self.g_optimizer = torch.optim.Adam(self.G.parameters(), self.g_lr, [self.beta1, self.beta2]) self.d_optimizer = torch.optim.Adam(self.D.parameters(), self.d_lr, [self.beta1, self.beta2]) self.print_network(self.G, 'G') self.print_network(self.D, 'D') self.G.to(self.device) self.D.to(self.device) def reset_grad(self): self.g_optimizer.zero_grad() self.d_optimizer.zero_grad() def update_lr(self, g_lr, d_lr): for param_group in self.g_optimizer.param_groups: param_group['lr'] = g_lr for param_group in self.d_optimizer.param_groups: param_group['lr'] = d_lr def print_network(self, model, name): num_params = 0 for p in model.parameters(): num_params += p.numel() print(model) print(name) print("The number of parameters: {}".format(num_params)) def restore_model(self, resume_iters): print( 'Loading the trained models from step {}...'.format(resume_iters)) G_path = os.path.join(self.model_save_dir, '{}-G.ckpt'.format(resume_iters)) D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(resume_iters)) self.G.load_state_dict( torch.load(G_path, map_location=lambda storage, loc: storage)) self.D.load_state_dict( torch.load(D_path, map_location=lambda storage, loc: storage)) def gradient_penalty(self, y, x): weight = torch.ones(y.size()).to(self.device) dydx = torch.autograd.grad(outputs=y, inputs=x, grad_outputs=weight, retain_graph=True, create_graph=True, only_inputs=True)[0] dydx = dydx.view(dydx.size(0), -1) dydx_l2norm = torch.sqrt(torch.sum(dydx**2, dim=1)) return torch.mean((dydx_l2norm - 1)**2) def train(self): loss = {} vis = visdom.Visdom() data_iter = iter(self.dataloader) g_lr = self.g_lr d_lr = self.d_lr fixed_z = torch.randn(self.batch_size, self.z_dim).cuda() start_iters = 0 if self.resume_iters: start_iters = self.resume_iters self.restore_model(self.resume_iters) print('start training') start_time = time.time() for i in range(start_iters, self.num_iters): try: x_mb, _ = next(data_iter) except: data_iter = iter(self.dataloader) x_mb, _ = next(data_iter) x_mb = x_mb.cuda() z = torch.randn(x_mb.size(0), self.z_dim).cuda() # train the discriminator x_fake = self.G(z) d_real = self.D(x_mb) d_fake = self.D(x_fake) d_loss_real = -torch.mean(d_real) d_loss_fake = torch.mean(d_fake) alpha = torch.rand(x_mb.size(0), 1, 1, 1).to(self.device) # interpolate between real data and fake data x_hat = (alpha * x_mb.data + (1 - alpha) * x_fake.data).requires_grad_(True) out_src = self.D(x_hat) d_loss_gp = self.gradient_penalty(out_src, x_hat) d_loss = d_loss_real + d_loss_fake + self.lambda_gp * d_loss_gp d_loss.backward() self.d_optimizer.step() self.reset_grad() loss['D/loss_real'] = d_loss_real.item() loss['D/loss_fake'] = d_loss_fake.item() loss['D/loss_gp'] = d_loss_gp.item() # train generator d_fake = self.D(self.G(z)) g_loss = -torch.mean(d_fake) g_loss.backward() self.g_optimizer.step() self.reset_grad() loss['G/loss'] = g_loss.item() if (i + 1) % self.log_step == 0: # visualize real and fake imgs vis.images((x_fake + 1) / 2, win='fake_imgs') vis.images((x_mb + 1) / 2, win='real_imgs') # print and visualize losses et = time.time() - start_time et = str(datetime.timedelta(seconds=et))[:-7] log = "Elapsed [{}], Iteration [{}/{}]".format( et, i + 1, self.num_iters) for tag, value in loss.items(): log += ", {}: {:.4f}".format(tag, value) opts = dict(title='Losses', width=13, height=10, legend=list(loss.keys())) vis.line(Y=[list(loss.values())], X=[np.ones(len(loss))*(i+1)], win='Losses', \ update='append', opts=opts) print(log) if (i + 1) % self.lr_update_step == 0 and ( i + 1) > self.num_iters_decay: g_lr = self.g_lr * self.lr_decay d_lr = self.d_lr * self.lr_decay self.update_lr(g_lr, d_lr) print('Decayed learning rates, g_lr: {}, d_lr: {}.'.format( g_lr, d_lr)) # Sample images if (i + 1) % self.sample_step == 0: fake_images = self.G(fixed_z) save_image( denorm(fake_images.data), os.path.join(self.sample_path, '{}_fake.png'.format(i + 1))) if (i + 1) % self.model_save_step == 0: G_path = os.path.join(self.model_save_dir, '{}-G.ckpt'.format(i + 1)) D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(i + 1)) torch.save(self.G.state_dict(), G_path) torch.save(self.D.state_dict(), D_path) print('Saved model checkpoints into {}...'.format( self.model_save_dir))
class Solver(object): def __init__(self, config, dataloader): self.dataloader = dataloader self.data_size = config.data_size # self.iters = config.iters self.loss_type = config.loss_type self.G_lr = config.G_lr self.D_lr = config.D_lr self.beta1 = config.momentum self.batch_size = config.batch_size self.max_epoch = config.max_epoch self.z_dim = config.z_dim self.lr_update_step = config.lr_update_step self.lr_decay_after = config.lr_decay_after self.lr_decay = config.lr_decay # path self.sample_path = os.path.join(config.main_path, 'samples') self.ckpt_path = os.path.join(config.main_path, 'checkpoints') # misc self.log_step = config.log_step self.eval_step = config.eval_step self.save_step = config.save_step self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') self.build_model() def build_model(self): self.G = Generator() self.D = Discriminator() self.G_optim = optim.Adam(self.G.parameters(), self.G_lr, (self.beta1, 0.999)) self.D_optim = optim.Adam(self.D.parameters(), self.D_lr, (self.beta1, 0.999)) if self.loss_type == 'BCEwL': self.criterion = nn.BCEWithLogitsLoss() elif self.loss_type == 'WGAN': pass elif self.loss_type == 'WGAN+': pass self.fixed_sample = None self.fixed_noise = None # self.true = torch.ones([self.batch_size, 1, 1, 1], requires_grad=False).to(self.device) # self.false = torch.zeros([self.batch_size, 1, 1, 1], requires_grad=False).to(self.device) # Change to GPU mode print('Change CPU mode to GPU mode...') self.G.to(self.device) self.D.to(self.device) print('Creating models are success...') def restore_model(self, resume_iters): print('Load the trained models from step {}...'.format(resume_iters)) G_path = os.path.join(self.ckpt_path, '{}-G.ckpt'.format(resume_iters)) D_path = os.path.join(self.ckpt_path, '{}-D.ckpt'.format(resume_iters)) self.G.load_state_dict(torch.load(G_path)) self.D.load_state_dict(torch.load(D_path)) def train(self): iters = self.max_epoch * len(self.dataloader) data_iter = iter(self.dataloader) self.fixed_sample = next(data_iter) self.fixed_noise = torch.randn(self.batch_size, self.z_dim).to(self.device) num_data = 0 start_time = time.time() print('Start training...') for i in range(iters): # try: # sample = next(data_iter) # except: # print('error occur') # data_iter = iter(self.dataloader) # sample = next(data_iter) sample = next(data_iter) if i % len(self.dataloader) == 0: data_iter = iter(self.dataloader) # Load data. right_embd = sample['right_embd'].to(self.device) wrong_embd = sample['wrong_embd'].to(self.device) z_noise = torch.randn(right_embd.size(0), self.z_dim).to(self.device) real_img = sample['real_img'].to(self.device) fake_img = self.G(right_embd, z_noise) # print('right_embd size: {}'.format(right_embd.size())) # print('wrong_embd size: {}'.format(wrong_embd.size())) # print('real_img size: {}'.format(real_img.size())) num_data += right_embd.size(0) T = torch.ones([right_embd.size(0), 1, 1, 1], requires_grad=False).to(self.device) F = torch.zeros([right_embd.size(0), 1, 1, 1], requires_grad=False).to(self.device) ## Train Discriminator. sr = self.D(real_img, right_embd) # {real image, right text} rr_loss = self.criterion(sr, T) sw = self.D(real_img, wrong_embd) # {real image, wrong text} rw_loss = self.criterion(sw, F) sf = self.D(fake_img.detach(), right_embd) # {fake image, right text} fr_loss = self.criterion(sf, F) d_loss = rr_loss + rw_loss + fr_loss ## Backward and optimize for D. self.D_optim.zero_grad() d_loss.backward() self.D_optim.step() # For logs loss = {} loss['D/rr_loss'] = rr_loss.item() loss['D/rw_loss'] = rw_loss.item() loss['D/fr_loss'] = fr_loss.item() loss['D/d_loss'] = d_loss.item() ## Train Generator. sf = self.D(fake_img, right_embd) g_loss = self.criterion(sf, T) ## Backward and optimize for G. self.G_optim.zero_grad() g_loss.backward() self.G_optim.step() loss['G/g_loss'] = g_loss.item() ## Print training information. if (i + 1) % self.log_step == 0: et = time.time() - start_time et = str(datetime.timedelta(seconds=et))[:-7] logs = "Elapsed [{}], Iter [{}/{}], Epoch [{}/{}]".format( et, i + 1, iters, (i + 1) / len(self.dataloader), self.max_epoch) logs += ", Dataset [{}/{}]".format(num_data % self.data_size, self.data_size) for tag, value in loss.items(): logs += ', {} [{:.4f}]'.format(tag, value) print(logs) ## Debug sample images. if (i + 1) % self.eval_step == 0: #will be modified. with torch.no_grad(): image_path = os.path.join(self.sample_path, '{}.jpg'.format(i + 1)) fake_img = self.G(self.fixed_sample['right_embd'].to( self.device), self.fixed_noise) #size: [B, 3, 64, 64] real_img = self.fixed_sample['real_img'] img_list = [] for row in range(int(self.batch_size / 8)): #print multiple of 8 samples img_list += [ real_img[row * 8 + col] for col in range(8) ] img_list += [ fake_img[row * 8 + col].to('cpu') for col in range(8) ] sample_name = os.path.join(self.sample_path, '{}iter.jpg'.format(i + 1)) save_image(make_grid(img_list), sample_name) print('Save generated sample results {}iter.jpg into {}...'. format(i + 1, self.sample_path)) ## Save model checkpoints. if (i + 1) % self.save_step == 0: G_path = os.path.join(self.ckpt_path, '{}-G.ckpt'.format(i + 1)) D_path = os.path.join(self.ckpt_path, '{}-D.ckpt'.format(i + 1)) torch.save(self.G.state_dict(), G_path) torch.save(self.D.state_dict(), D_path) print('Save model checkpoints into {}...'.format( self.ckpt_path)) ## Decay learning rates. if (i + 1) % self.lr_update_step == 0: if (i + 1) >= self.lr_decay_after: self.G_lr = self.G_lr * self.lr_decay self.D_lr = self.D_lr * self.lr_decay for param_group in self.G_optim.param_groups: param_group['lr'] = self.G_lr for param_group in self.D_optim.param_groups: param_group['lr'] = self.D_lr print('Decay learning rates, g_lr: {}, d_lr: {}...'.format( self.G_lr, self.D_lr)) def test(self): pass
class BiGAN(nn.Module): def __init__(self,config): super(BiGAN,self).__init__() self._work_type = config.work_type self._epochs = config.epochs self._batch_size = config.batch_size self._encoder_lr = config.encoder_lr self._generator_lr = config.generator_lr self._discriminator_lr = config.discriminator_lr self._latent_dim = config.latent_dim self._weight_decay = config.weight_decay self._img_shape = (config.input_size,config.input_size) self._img_save_path = config.image_save_path self._model_save_path = config.model_save_path self._device = config.device if self._work_type == 'train': # Loss function self._adversarial_criterion = torch.nn.MSELoss() # Initialize generator, encoder and discriminator self._G = Generator(self._latent_dim,self._img_shape).to(self._device) self._E = Encoder(self._latent_dim,self._img_shape).to(self._device) self._D = Discriminator(self._latent_dim,self._img_shape).to(self._device) self._G.apply(self.weights_init) self._E.apply(self.weights_init) self._D.apply(self.discriminator_weights_init) self._G_optimizer = torch.optim.Adam([{'params' : self._G.parameters()},{'params' : self._E.parameters()}], lr=self._generator_lr,betas=(0.5,0.999),weight_decay=self._weight_decay) self._D_optimizer = torch.optim.Adam(self._D.parameters(),lr=self._discriminator_lr,betas=(0.5,0.999)) self._G_scheduler = lr_scheduler.ExponentialLR(self._G_optimizer, gamma= 0.99) self._D_scheduler = lr_scheduler.ExponentialLR(self._D_optimizer, gamma= 0.99) def train(self,train_loader): Tensor = torch.cuda.FloatTensor if self._device == 'cuda' else torch.FloatTensor n_total_steps = len(train_loader) for epoch in range(self._epochs): self._G_scheduler.step() self._D_scheduler.step() for i, (images, _) in enumerate(train_loader): # Adversarial ground truths valid = Variable(Tensor(images.size(0), 1).fill_(1), requires_grad=False) fake = Variable(Tensor(images.size(0), 1).fill_(0), requires_grad=False) # --------------------- # Train Encoder # --------------------- # Configure input images = images.reshape(-1,np.prod(self._img_shape)).to(self._device) # z_ is encoded latent vector (original_img,z_)= self._E(images) predict_encoder = self._D(original_img,z_) # --------------------- # Train Generator # --------------------- # Sample noise as generator input z = Variable(Tensor(np.random.normal(0, 1, (images.shape[0],self._latent_dim)))) (gen_img,z)=self._G(z) predict_generator = self._D(gen_img,z) G_loss = (self._adversarial_criterion(predict_generator,valid)+self._adversarial_criterion(predict_encoder,fake)) *0.5 self._G_optimizer.zero_grad() G_loss.backward() self._G_optimizer.step() # --------------------- # Train Discriminator # --------------------- z = Variable(Tensor(np.random.normal(0, 1, (images.shape[0],self._latent_dim)))) (gen_img,z)=self._G(z) (original_img,z_)= self._E(images) predict_encoder = self._D(original_img,z_) predict_generator = self._D(gen_img,z) D_loss = (self._adversarial_criterion(predict_encoder,valid)+self._adversarial_criterion(predict_generator,fake)) *0.5 self._D_optimizer.zero_grad() D_loss.backward() self._D_optimizer.step() if i % 100 == 0: print (f'Epoch [{epoch+1}/{self._epochs}], Step [{i+1}/{n_total_steps}]') print (f'Generator Loss: {G_loss.item():.4f} Discriminator Loss: {D_loss.item():.4f}') if i % 400 ==0: vutils.save_image(gen_img.unsqueeze(1).cpu().data[:64, ], f'{self._img_save_path}/E{epoch}_Iteration{i}_fake.png') vutils.save_image(original_img.unsqueeze(1).cpu().data[:64, ], f'{self._img_save_path}/E{epoch}_Iteration{i}_real.png') print('image saved') print('') if epoch % 100==0: torch.save(self._G.state_dict(), f'{self._model_save_path}/netG_{epoch}epoch.pth') torch.save(self._E.state_dict(), f'{self._model_save_path}/netE_{epoch}epoch.pth') torch.save(self._D.state_dict(), f'{self._model_save_path}/netD_{epoch}epoch.pth') def weights_init(self,m): classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0) elif classname.find('Linear') != -1: m.bias.data.fill_(0) def discriminator_weights_init(self,m): classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0.5) m.bias.data.fill_(0) elif classname.find('Linear') != -1: m.bias.data.fill_(0)