Exemplo n.º 1
0
    def __init__(self, opt):
        super(_competitionGan, self).__init__(opt)
        self.opt = opt
        self.x_dim = opt.x_dim
        self.z_dim = opt.z_dim
        self.condition_D = opt.condition_D
        self.nums = opt.nums
        self.mb_size = opt.mb_size
        self.Lambda = opt.Lambda
        self.savepath = opt.savepath
        self.cnt = 0

        self.netCG = build_netCG(opt.g_model, nums=self.nums)
        self.netD = build_netD(opt.d_model, opt.x_dim, opt.condition_D)


        X = torch.FloatTensor(opt.mb_size, opt.x_dim, opt.img_size, opt.img_size)
        Z = torch.FloatTensor(opt.mb_size, opt.z_dim, 1, 1)
        label = torch.FloatTensor(opt.mb_size)

        if opt.condition_D:
            real_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size + opt.condition_D)
            fake_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size + opt.condition_D)
        else:
            real_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size)
            fake_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size)
        
        self.criterionGAN = torch.nn.BCELoss()
        self.criterionL1 = torch.nn.L1Loss()

        if self.cuda:
            netD.cuda()
            netG.cuda()
            self.criterionGAN.cuda()
            self.L1loss.cuda()
            X, Z = X.cuda(), Z.cuda()
            real_like_sample, fake_like_sample = real_like_sample.cuda(), fake_like_sample.cuda()
            label = label.cuda()

        self.X = Variable(X)
        self.Z = Variable(Z)
        self.real_like_sample = Variable(real_like_sample)
        self.fake_like_sample = Variable(fake_like_sample)
        self.label= Variable(label)

        if self.opt.cc:
            self.create_tensorboard()
            self.index_exp = create_sigle_experiment(self.cc, 'index')
        self.D_solver = torch.optim.Adam(self.netD.parameters(), lr=2e-4, betas=(0.5, 0.999))
        self.CG_solver = torch.optim.Adam(self.netCG.parameters(), lr=2e-4, betas=(0.5, 0.999))

        if opt.train == False:
            self.load_networkG(self.opt.g_network_path)
            self.load_networkD(self.opt.d_network_path)
        else:
            init_network(self.netD)
            init_network(self.netCG)
Exemplo n.º 2
0
    def __init__(self, opt):
        super(_competitionGan, self).__init__(opt)
        self.opt = opt
        self.x_dim = opt.x_dim
        self.z_dim = opt.z_dim
        self.condition_dim = opt.condition_dim
        self.nums = opt.nums
        self.Lambda = opt.Lambda
        self.savepath = opt.savepath
        self.cnt = 0

        self.netGs = create_nets(opt.g_model, opt.z_dim, opt.nums, type='G')
        self.netD = build_netD(opt.d_model, opt.x_dim)

        X = torch.FloatTensor(opt.mb_size, opt.x_dim + opt.condition_dim)
        Z = torch.FloatTensor(opt.mb_size, opt.z_dim)
        C = torch.FloatTensor(opt.condition_dim)
        real_like_sample = torch.FloatTensor(opt.mb_size,
                                             opt.img_size * opt.img_size)
        fake_like_sample = torch.FloatTensor(opt.mb_size,
                                             opt.img_size * opt.img_size)
        label = torch.FloatTensor(opt.mb_size)
        self.criterionGAN = torch.nn.BCELoss()
        self.criterionL1 = torch.nn.L1Loss()

        if self.cuda:
            netD.cuda()
            netG.cuda()
            self.criterionGAN.cuda()
            self.L1loss.cuda()
            X, Z = X.cuda(), Z.cuda()
            real_like_sample, fake_like_sample = real_like_sample.cuda(
            ), fake_like_sample.cuda()
            label = label.cuda()

        self.X = Variable(X)
        self.Z = Variable(Z)
        self.real_like_sample = Variable(real_like_sample)
        self.fake_like_sample = Variable(fake_like_sample)
        self.label = Variable(label)

        self.create_tensorboard()
        self.index_exp = create_sigle_experiment(self.cc, 'index')
        self.D_solver = torch.optim.Adam(self.netD.parameters(),
                                         lr=2e-4,
                                         betas=(0.5, 0.999))
        self.G_solvers = create_optims(self.netGs, [2e-4, (0.5, 0.999)])

        init_network(self.netD)
        init_network(self.netGs)
Exemplo n.º 3
0
    def __init__(self, opt):
        super(_testGan, self).__init__(opt)
        self.netG = build_netG(opt.g_model, opt.z_dim)
        self.netD = build_netD(opt.d_model, opt.x_dim)

        netD.apply(weight_init)
        netG.apply(weight_init)

        x = torch.FloatTensor(opt.mb_size, opt.x_dim, opt.img_size, opt.img_size)
        z = torch.FloatTensor(opt.mb_size, opt.z_dim, 1, 1)
        label = torch.FloatTensor(opt.mb_size)

        if self.cuda:
            netD.cuda()
            netG.cuda()
            x, z = x.cuda(), z.cuda()
            label = label.cuda()
Exemplo n.º 4
0
z_dim = 100
h_dim = 128
x_dim_w, x_dim_h = train_loader.dataset.train_data.size()[1:3]
resize_w, resize_h = 64, 64
x_dim = 3
train_size = train_loader.dataset.train_data.size()[0]
y_dim = 10
lr = 1e-3
cnt = 0
nets_num = 10

cuda = False
niter = 24

# build gans
netD = build_netD(config['D'][5], x_dim)
netG = build_netG(config['G'][6], z_dim)

print netD

# init gans
netD.apply(weight_init)
netG.apply(weight_init)

# build gans's solver
G_solver = optim.Adam(netG.parameters(), lr=lr, betas=(0.5, 0.999))
D_solver = optim.Adam(netD.parameters(), lr=lr, betas=(0.5, 0.999))

# build exps of netG and netD
# G_exp = create_sigle_experiment(cc, 'G_loss')
# D_exp = create_sigle_experiment(cc, 'D_loss')
Exemplo n.º 5
0
    def __init__(self, opt):
        super(_competitionGan, self).__init__(opt)
        self.opt = opt
        self.x_dim = opt.x_dim
        self.z_dim = opt.z_dim
        self.condition_D = opt.condition_D
        self.mb_size = opt.mb_size
        self.Lambda = opt.Lambda
        self.continue_train = opt.continue_train
        self.train = opt.train
        self.test = True if self.continue_train and self.train else False
        self.savepath = '{}{}/'.format(opt.savepath, opt.gans_type)
        self.cnt = 0

        self.netGs = []
        for index in range(self.nums):
            netG = build_netG(opt.g_model, opt.z_dim)
            self.netGs.append(netG)
        self.netD = build_netD(opt.d_model, opt.x_dim, opt.condition_D)

        X = torch.FloatTensor(opt.mb_size, opt.x_dim, opt.img_size,
                              opt.img_size)
        Z = torch.FloatTensor(opt.mb_size, opt.z_dim, 1, 1)

        real_like_sample = torch.FloatTensor(opt.mb_size, opt.x_dim,
                                             opt.img_size, opt.img_size)
        fake_like_sample = torch.FloatTensor(opt.mb_size, opt.x_dim,
                                             opt.img_size, opt.img_size)

        label = torch.FloatTensor(opt.mb_size)
        self.criterionGAN = torch.nn.BCELoss()
        self.criterionL1 = torch.nn.L1Loss()

        if self.cuda:
            self.netD.cuda()
            for index in range(self.nums):
                self.netGs[index].cuda()
            self.criterionGAN.cuda()
            self.criterionL1.cuda()
            X, Z = X.cuda(), Z.cuda()
            real_like_sample, fake_like_sample = real_like_sample.cuda(
            ), fake_like_sample.cuda()
            label = label.cuda()

        self.X = Variable(X)
        self.Z = Variable(Z)
        self.real_like_sample = Variable(real_like_sample)
        self.fake_like_sample = Variable(fake_like_sample)
        self.label = Variable(label)

        info.log("Train: {}  Continue: {}  Test: {}".format(
            self.train, self.continue_train, self.test))

        if self.opt.cc:
            self.create_tensorboard()
            self.index_exp = create_sigle_experiment(self.cc, 'index')
        self.D_solver = torch.optim.Adam(self.netD.parameters(),
                                         lr=2e-4,
                                         betas=(0.5, 0.999))
        self.G_solvers = create_optims(self.netGs, [2e-4, (0.5, 0.999)])

        if opt.train == False:
            self.load_networkG(self.opt.g_network_path)
            self.load_networkD(self.opt.d_network_path)
        else:
            init_network(self.netD)
            init_network(self.netGs)
Exemplo n.º 6
0
    def __init__(self, opt):
        super(_competitionGan, self).__init__(opt)
        self.opt = opt
        self.x_dim = opt.x_dim
        self.z_dim = opt.z_dim
        self.condition_D = opt.condition_D
        self.nums = opt.nums
        self.mb_size = opt.mb_size
        self.Lambda = opt.Lambda
        self.savepath = opt.savepath
        self.cnt = 0

        self.netGs = create_nets(opt.g_model, opt.z_dim, opt.nums, type='G')
        self.netD = build_netD(opt.d_model, opt.x_dim, opt.condition_D)
        self.netC = build_netD(opt.c_model, opt.x_dim, opt.condition_D)


        X = torch.FloatTensor(opt.mb_size, opt.x_dim, opt.img_size, opt.img_size)
        Z = torch.FloatTensor(opt.mb_size, opt.z_dim, 1, 1)
        target = torch.LongTensor(opt.mb_size)

        condition_data = torch.FloatTensor(opt.mb_size, opt.img_size * opt.img_size + opt.condition_D)
        real_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size)
        fake_like_sample = torch.FloatTensor(opt.mb_size, opt.img_size*opt.img_size)
        
        label = torch.FloatTensor(opt.mb_size)
        self.criterionGAN = torch.nn.BCELoss()
        self.criterionL1 = torch.nn.L1Loss()
        self.criterionEntropy = torch.nn.CrossEntropyLoss()

        if self.cuda:
            netD.cuda()
            netG.cuda()
            self.criterionGAN.cuda()
            self.L1loss.cuda()
            X, Z = X.cuda(), Z.cuda()
            condition_data = condition_data.cuda()
            real_like_sample, fake_like_sample = real_like_sample.cuda(), fake_like_sample.cuda()
            label = label.cuda()
            target = target.cuda()

        self.X = Variable(X)
        self.Z = Variable(Z)
        self.target = Variable(target)
        self.condition_data = Variable(condition_data)
        self.real_like_sample = Variable(real_like_sample)
        self.fake_like_sample = Variable(fake_like_sample)
        self.label= Variable(label)

        if self.opt.cc:
            self.create_tensorboard()
            self.index_exp = create_sigle_experiment(self.cc, 'index')
            self.class_exp = create_sigle_experiment(self.cc, 'class')
        self.D_solver = torch.optim.Adam(self.netD.parameters(), lr=2e-4, betas=(0.5, 0.999))
        self.C_solver = torch.optim.Adam(self.netC.parameters(), lr=2e-4, betas=(0.5, 0.999))
        self.G_solvers = create_optims(self.netGs, [2e-4, (0.5, 0.999)])

        if opt.train == False:
            self.load_networkG(self.opt.g_network_path)
            self.load_networkD(self.opt.d_network_path)
        else:
            init_network(self.netD)
            init_network(self.netGs)