def _init_network(self): def init_weights(m): if type(m) == nn.Conv2d: nn.init.normal_(m.weight.data, 0.0, 0.02) if type(m) == nn.BatchNorm2d: nn.init.normal_(m.weight.data, 0.0, 0.02) nn.init.constant_(m.bias.data, 0.0) generator = Generator(DCGAN_Config).to(self.device) discriminator = Discriminator(DCGAN_Config).to(self.device) if self.device.type =='cuda' and DCGAN_Config['ngpu']>1: generator = nn.DataParallel(generator, list(range(DCGAN_Config['ngpu']))) discriminator = nn.DataParallel(discriminator, list(range(DCGAN_Config['ngpu']))) generator.apply(init_weights) discriminator.apply(init_weights) print(generator) print(discriminator) return generator, discriminator
# img=dataset.__getitem__(10) # print(img['A'].size()) netG_A2B = Generator(opt.input_nc, opt.output_nc, opt.ngf) netG_B2A = Generator(opt.input_nc, opt.output_nc, opt.ngf) netD_A = Discriminator(opt.input_nc, opt.ndf) netD_B = Discriminator(opt.input_nc, opt.ndf) summary(netG_A2B, input_size=(3, 256, 256), device='cpu') summary(netD_A, input_size=(3, 256, 256), device='cpu') #instialize weights netG_A2B.apply(weights_init) netG_B2A.apply(weights_init) netD_A.apply(weights_init) netD_B.apply(weights_init) #print(netG_A2B) #print(netD_A) # Lossess gan_loss = torch.nn.MSELoss() cycle_consistency_loss = torch.nn.L1Loss() #optimizers optimizerG_A2B = optim.Adam(netG_A2B.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) optimizerG_B2A = optim.Adam(netG_B2A.parameters(),
classname = m.__class__.__name__ if classname.find('Conv') != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0) NetG = Decoder(nc, ngf, nz).to(device) NetD = Discriminator(imageSize, nc, ndf, nz).to(device) NetE = Encoder(imageSize, nc, ngf, nz).to(device) Sampler = Sampler().to(device) NetE.apply(weights_init) NetG.apply(weights_init) NetD.apply(weights_init) # load weights if opt.netE != '': NetE.load_state_dict(torch.load(opt.netE)) if opt.netG != '': NetG.load_state_dict(torch.load(opt.netG)) if opt.netD != '': NetD.load_state_dict(torch.load(opt.netD)) optimizer_encorder = optim.RMSprop(params=NetE.parameters(), lr=lr, alpha=0.9, eps=1e-8, weight_decay=0, momentum=0,
D_A = Discriminator().to(device) D_B = Discriminator().to(device) def weights_init(m): if isinstance(m, nn.Conv2d): torch.nn.init.normal_(m.weight.data, 0.0, 0.02) if opt.load_pretrained: G_A2B.load_state_dict(torch.load('pretrained/G_A2B.pth')) G_B2A.load_state_dict(torch.load('pretrained/G_B2A.pth')) D_A.load_state_dict(torch.load('pretrained/D_A.pth')) D_B.load_state_dict(torch.load('pretrained/D_B.pth')) else: G_A2B.apply(weights_init) G_B2A.apply(weights_init) D_A.apply(weights_init) D_B.apply(weights_init) criterion_GAN = torch.nn.MSELoss() criterion_cycle = torch.nn.L1Loss() criterion_identity = torch.nn.L1Loss() G_params = list(G_A2B.parameters()) + list(G_B2A.parameters()) D_params = list(D_A.parameters()) + list(D_B.parameters()) optimizer_G = optim.Adam(G_params, lr=0.0002, betas=[0.5, 0.999]) optimizer_D = optim.Adam(D_params, lr=0.0002, betas=[0.5, 0.999]) fake_A_buffer = utils.ReplayBuffer() fake_B_buffer = utils.ReplayBuffer() logger = utils.Logger(opt.n_epochs, len(loader_A), vis)
#BUILDING NETWORK STRUCTURE generator = Generator(128) discriminator = Discriminator(128) generator.cuda() discriminator.cuda() def weights_init_normal(m): classname = m.__class__.__name__ if classname.find("Conv") != -1: torch.nn.init.normal_(m.weight.data, 0.0, 0.02) elif classname.find("BatchNorm2d") != -1: torch.nn.init.normal_(m.weight.data, 1.0, 0.02) torch.nn.init.constant_(m.bias.data, 0.0) generator.apply(weights_init_normal) discriminator.apply(weights_init_normal) criterion = nn.BCELoss() criterion.cuda() optimizer_disc = optim.Adam(discriminator.parameters(),lr=learning_rate, betas=(0.5, 0.999)) optimizer_gen = optim.Adam(generator.parameters(), lr=learning_rate, betas=(0.5, 0.999)) Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor print("Training...") #TRAINING for epoch in range(n_epoch): for i, imgs in enumerate(training_data_loader): real = Variable(Tensor(imgs.shape[0], 1).fill_(0.9), requires_grad=False) fake = Variable(Tensor(imgs.shape[0], 1).fill_(0.1), requires_grad=False)
class MUNIT(nn.Module): def __init__(self, opt): super(MUNIT, self).__init__() # generators and discriminators self.gen_a = Generator(opt.ngf, opt.style_dim, opt.mlp_dim) self.gen_b = Generator(opt.ngf, opt.style_dim, opt.mlp_dim) self.dis_a = Discriminator(opt.ndf) self.dis_b = Discriminator(opt.ndf) #random style code self.s_a = torch.randn(opt.display_size, opt.style_dim, 1, 1, requires_grad=True).cuda() self.s_b = torch.randn(opt.display_size, opt.style_dim, 1, 1, requires_grad=True).cuda() #optimizers dis_params = list(self.dis_a.parameters()) + list( self.dis_b.parameters()) gen_params = list(self.gen_a.parameters()) + list( self.gen_b.parameters()) self.dis_opt = torch.optim.Adam(dis_params, lr=opt.lr, beta=opt.beta1, weight_delay=opt.weight_delay) self.gen_opt = torch.optim.Adam(gen_params, lr=opt.lr, beta=opt.beta1, weight_delay=opt.weight_delay) # nerwork weight initialization self.apply(weight_init('kaiming')) self.dis_a.apply(weight_init('gaussian')) self.dis_b.apply(weight_init('gaussian')) def forward(self, x_a, x_b): c_a, s_a_prime = self.gen_a.encode(x_a) c_b, s_b_prime = self.gen_b.encode(x_b) x_a2b = self.gen_b.decode(c_a, self.s_b) x_b2a = self.gen_a.decode(c_b, self.s_a) return x_a2b, x_b2a def backward_G(self, x_a, x_b): #encoding and decoding s_a = torch.randn(x_a.size(0), opt.style_dim, 1, 1, requires_grad=True).cuda() s_b = torch.randn(x_b.size(0), opt.style_dim, 1, 1, requires_grad=True).cuda() c_a, s_a_prime = self.gen_a.encode(x_a) c_b, s_b_prime = self.gen_b.encode(x_b) x_a_rec = self.gen_a.decode(c_a, s_a_prime) x_b_rec = self.gen_b.decode(c_b, s_b_prime) x_a2b = self.gen_b.decode(c_a, s_b) x_b2a = self.gen_a.decode(c_b, s_a) c_a_rec, s_b_rec = self.gen_b.encode(x_a2b) c_b_rec, s_a_rec = self.gen_a.encode(x_b2a) x_a_fake = self.dis_a(x_b2a) x_b_fake = self.dis_b(x_a2b) #loss function self.loss_xa = recon_loss(x_a_rec - x_a) self.loss_xb = recon_loss(x_b_rec - x_b) self.loss_ca = recon_loss(c_a_rec - c_a) self.loss_cb = recon_loss(c_b_rec - c_b) self.loss_sa = recon_loss(s_a_rec - s_a) self.loss_sb = recon_loss(s_b_rec - s_b) if opt.x_cyc_w > 0: x_a2b2a = self.gen_b.decode(c_a_rec, s_a_prime) x_b2a2b = self.gen_a.decode(c_b_rec, s_b_prime) self.loss_cyc_a2b = recon_loss(x_a2b2a, x_a) self.loss_cyc_b2a = recon_loss(x_b2a2b, x_b) else: self.loss_cyc_a2b = 0 self.loss_cyc_b2a = 0 self.loss_gen_a = gen_loss(x_a_fake, opt.gan_type) self.loss_gen_b = gen_loss(x_b_fake, opt.gan_type) self.gen_total_loss = opt.gan_w*(self.loss_gen_a+self.loss_gen_b)+\ opt.x_w*(self.loss_xa+self.loss_xb)+\ opt.c_w*(self.loss_ca+self.loss_cb)+\ opt.s_w*(self.loss_sa+self.loss_sb)+\ opt.x_cyc_w*(self.loss_cyc_a2b+self.loss_cyc_b2a) self.gen_total_loss.backward() return self.gen_total_loss def barkward_D(self, x_a, x_b): s_a = torch.randn(x_a.size(0), opt.style_dim, 1, 1, requires_grad=True).cuda() s_b = torch.randn(x_b.size(0), opt.style_dim, 1, 1, requires_grad=True).cuda() c_a, _ = self.gen_a.encode(x_a.detach()) c_b, _ = self.gen_b.encode(x_b.detach()) x_a2b = self.gen_b.decode(c_a, s_b) x_b2a = self.gen_a.decode(c_b, s_a) x_a_real = self.dis_a(x_a.detach()) x_a_fake = self.dis_a(x_b2a) x_b_real = self.dis_b(x_b.detach()) x_b_fake = self.dis_b(x_a2b) self.loss_dis_a = dis_loss(x_a.detach(), x_b2a, opt.gan_type) self.loss_dis_b = dis_loss(x_b.detach(), x_a2b, opt.gan_type) self.dis_total_loss = opt.gan_w * (self.loss_dis_a + self.loss.dis_b) self.dis_total_loss.backward() return self.dis_total_loss def optimize_parameters(self, x_a, x_b): self.gen_opt.zero_grad() self.dis_opt.zero_grad() self.backward_G(x_a, x_b) self.barkward_D(x_a, x_b) self.gen_opt.step() self.dis_opt.step() def sample(self, x_a, x_b, num_style): x_a2b = [] x_b2a = [] for i in range(x_a.size(0)): s_a = torch.randn(num_style, opt.style_dim, 1, 1).cuda() s_b = torch.randn(num_style, opt.style_dim, 1, 1).cuda() c_a_i, _ = self.gen_a.encode(x_a[i].unsqueeze(0)) c_b_i, _ = self.gen_b.encode(x_b[i].unsqueeze(0)) x_i_a2b = [] x_i_b2a = [] for j in range(num_style): #[1,opt.style_dim,1,1] s_a_j = s_a[j].unsqueeze(0) s_b_j = s_b[j].unsqueeze(0) x_i_a2b.append(self.gen_b.decode(c_a_i, s_b_j)) x_i_b2a.append(self.gen_a.decode(c_b_i, s_a_j)) #[num_style,c,h,w] x_i_a2b = torch.cat(x_i_a2b, dim=0) x_i_b2a = torch.cat(x_i_b2a, dim=0) x_a2b.append(x_i_a2b) x_b2a.append(x_i_b2a) #[batch_size,num_style,c,h,w] x_a2b = torch.stack(x_a2b) x_b2a = torch.stack(x_b2a) #[batch_size*num_style,c,h,w] x_a2b = x_a2b.view(-1, x_a2b.size()[2:]) x_b2a = x_b2a.view(-1, x_b2a.size()[2:]) return x_a2b, x_b2a def test(self, input, direction, style): output = [] if direction == 'a2b': encoder = self.gen_a.encode() decoder = self.gen_b.decode() else: encoder = self.gen_b.encode() decoder = self.gen_a.decode() content, _ = encode(input.unsqueeze(0)) for i in range(style.size()): output.append(decoder(content, style[i].unsqueeze(0))) output = torch.cat(output, dim=0) return output def save(self, save_dir, iterations): save_gen = os.path.join(save_dir, 'gen_%8d.pt' % (iteration + 1)) save_dis = os.path.join(save_dir, 'dis_%8d.pt' % (iteration + 1)) save_opt = os.path.join(save_dir, 'optimizer.pt') torch.save({ 'a': self.gen_a.state_dict(), 'b': self.gen_b.state_dict() }, save_gen) torch.save({ 'a': self.dis_a.state_dict(), 'b': self.dis_b.state_dict() }, save_dis) torch.save( { 'gen': self.gen_opt.state_dict(), 'dis': self.dis_opt.state_dict() }, save_opt)
# Decide which device we want to run on device = torch.device("cuda:0" if (torch.cuda.is_available() and args.ngpu > 0) else "cpu") # Create the generator netG = Generator(args.ngpu, args.nc, args.nz, args.ndf, args.ngf).to(device) if (device.type == 'cuda') and (args.ngpu > 1): netG = nn.DataParallel(netG, list(range(args.ngpu))) netG.apply(weights_init) print(netG) # Create the Discriminator netD = Discriminator(args.ngpu, args.nc, args.nz, args.ndf, args.ngf).to(device) if (device.type == 'cuda') and (args.ngpu > 1): netD = nn.DataParallel(netD, list(range(args.ngpu))) netD.apply(weights_init) print(netD) # Initialize BCELoss function criterion = nn.BCELoss() # Create batch of latent vectors that we will use to visualize # the progression of the generator if args.truncnorm: fixed_noise = torch.from_numpy(truncated_z_sample(64, args.nz, args.tc_th, args.manualSeed)).float().to(device) else: fixed_noise = torch.randn(64, args.nz, 1, 1, device=device) # Establish convention for real and fake labels during training real_label = 1
from torch.utils.data import DataLoader import torchvision.transforms as transforms from utils import imshow_grid, mse_loss, reparameterize, l1_loss from network import Generator, Discriminator from torchvision.utils import save_image batch_size = 8 num_epochs = 500 image_size = 128 generator = Generator(nc_dim=80) generator.apply(weights_init) generator = generator.cuda() discriminator = Discriminator() discriminator.apply(weights_init) discriminator = discriminator.cuda() ones_label = torch.ones(batch_size) ones_label = Variable(ones_label.cuda()) zeros_label = torch.zeros(batch_size) zeros_label = Variable(zeros_label.cuda()) loss = nn.BCEWithLogitsLoss() def to_img(x): x = x.clamp(0, 1) x = x.view(x.size(0), 3, 128, 128) return x
parser = argparse.ArgumentParser() parser.add_argument('--model_name', default='test') parser.add_argument('--batch_size', default=64) parser.add_argument('--lr', type=float, default=5e-3) parser.add_argument('--l1', type=float, default=1e1) parser.add_argument('--num_iters', type=int, default=100) parser.add_argument("--train", dest="train", default=False, action="store_true") # noqa parser.add_argument('--test_save_path', default='test') args = parser.parse_args() train_loader,val_loader,test_loader = read_data(args.batch_size) network = Network() network.apply(weights_init) d = Discriminator() d.apply(weights_init) bce_loss = torch.nn.BCEWithLogitsLoss() true_crit, fake_crit = torch.ones(args.batch_size, 1, device='cuda'), torch.zeros(args.batch_size, 1, device='cuda') print(network) train_loss = [] val_loss = [] if args.train: optimizer = torch.optim.Adam(network.parameters(), lr=args.lr) d_optimizer = torch.optim.Adam(d.parameters(), lr=args.lr) network.cuda() d.cuda() for epoch in range(args.num_iters): train_loader,val_loader,test_loader = read_data(args.batch_size) network.train() for idx, x in enumerate(train_loader):