Example #1
0
    def __init__(self,
                 train_set,
                 test_set,
                 loss_name="WGAN-GP",
                 mixed_precision=False,
                 learning_rate=2e-4,
                 tmp_path=None,
                 out_path=None):
        super(CycleGAN, self).__init__()
        #接收数据集和相关参数
        self.train_set = train_set
        self.test_set = test_set
        self.tmp_path = tmp_path
        self.out_path = out_path
        #定义模型
        self.G = networks.Generator(name="G_X2Y")
        self.F = networks.Generator(name="G_Y2X")
        if loss_name in ["WGAN-SN", "WGAN-GP-SN"]:
            self.Dy = networks.Discriminator(name="If_is_real_Y",
                                             use_sigmoid=False,
                                             sn=True)
            self.Dx = networks.Discriminator(name="If_is_real_X",
                                             use_sigmoid=False,
                                             sn=True)
            self.loss_name = loss_name[:-3]
        elif loss_name in ["WGAN", "WGAN-GP"]:
            self.Dy = networks.Discriminator(name="If_is_real_Y",
                                             use_sigmoid=False,
                                             sn=False)
            self.Dx = networks.Discriminator(name="If_is_real_X",
                                             use_sigmoid=False,
                                             sn=False)
            self.loss_name = loss_name
        elif loss_name in ["Vanilla", "LSGAN"]:
            self.Dy = networks.Discriminator(name="If_is_real_Y",
                                             use_sigmoid=True,
                                             sn=False)
            self.Dx = networks.Discriminator(name="If_is_real_X",
                                             use_sigmoid=True,
                                             sn=False)
            self.loss_name = loss_name
        else:
            raise ValueError("Do not support the loss " + loss_name)

        self.model_list = [self.G, self.F, self.Dy, self.Dx]
        #定义损失函数 优化器 记录等
        self.gan_loss = GanLoss(self.loss_name)
        self.optimizers_list = self.optimizers_config(
            mixed_precision=mixed_precision, learning_rate=learning_rate)
        self.mixed_precision = mixed_precision
        self.matrics_list = self.matrics_config()
        self.checkpoint_config()
        self.get_seed()
Example #2
0
    def __init__(self, conf):
        # Acquire configuration
        self.conf = conf

        # Define the GAN
        self.G = networks.Generator(conf).cuda()
        self.U = networks.Upsampler().cuda()

        # Input tensors
        self.g_input = torch.FloatTensor(1, 3, conf.input_crop_size,
                                         conf.input_crop_size).cuda()

        # Define loss function
        self.L1 = nn.L1Loss()

        # Initialize networks weightsZ
        self.G.apply(networks.weights_init_G)
        self.U.apply(networks.weights_init_U)

        # Optimizers
        self.optimizer_G = torch.optim.Adam(self.G.parameters(),
                                            lr=conf.g_lr,
                                            betas=(conf.beta1, 0.999))
        self.optimizer_U = torch.optim.Adam(self.U.parameters(),
                                            lr=conf.u_lr,
                                            betas=(conf.beta1, 0.999))

        print('*' * 60 +
              '\nSTARTED DBPI-BlindSR on: \"%s\"...' % conf.input_image_path)
        self.ds = nn.AvgPool2d(2, ceil_mode=True)
Example #3
0
def main():
    opt = get_opt()

    # Define the Generators, only G_A is used for testing
    netG_A = networks.Generator(opt.input_nc, opt.output_nc, opt.ngf, opt.n_res, opt.dropout)
    if opt.u_net:
        netG_A = networks.U_net(opt.input_nc, opt.output_nc, opt.ngf)

    if opt.cuda:
        netG_A.cuda()
    # Do not need to track the gradients during testing
    utils.set_requires_grad(netG_A, False)
    netG_A.eval()
    netG_A.load_state_dict(torch.load(opt.net_GA))

    # Load the data
    transform = transforms.Compose([transforms.Resize((opt.sizeh, opt.sizew)),
                                    transforms.ToTensor(),
                                    transforms.Normalize((0.5,), (0.5,))])
    dataloader = DataLoader(ImageDataset(opt.rootdir, transform=transform, mode='val'), batch_size=opt.batch_size,
                            shuffle=False, num_workers=opt.n_cpu)

    Tensor = torch.cuda.FloatTensor if opt.cuda else torch.Tensor
    input_A = Tensor(opt.batch_size, opt.input_nc, opt.size, opt.size)

    for i, batch in enumerate(dataloader):
        name, image = batch
        real_A = input_A.copy_(image)
        fake_B = netG_A(real_A)
        batch_size = len(name)
        # Save the generated images
        for j in range(batch_size):
            image_name = name[j].split('/')[-1]
            path = 'generated_image/' + image_name
            utils.save_image(fake_B[j, :, :, :], path)
Example #4
0
    def __init__(self, conf):
        os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3,4,5,6,7'

        # Acquire configuration
        self.conf = conf
        self._device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

        # Define the GAN
        self.G = networks.Generator(conf)
        self.D = networks.Discriminator(conf)

        if torch.cuda.device_count() > 1:
            print("Let's use", torch.cuda.device_count(), "GPUs!")
            print("gpu num : ", torch.cuda.device_count())
            # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
            self.G = nn.DataParallel(self.G)
            self.D = nn.DataParallel(self.D)
        print("haha, gpu num : ", torch.cuda.device_count())
        self.G.to(self._device)
        self.D.to(self._device)

        # Calculate D's input & output shape according to the shaving done by the networks
        if torch.cuda.device_count() > 1:
            self.d_input_shape = self.G.module.output_size
            self.d_output_shape = self.d_input_shape - self.D.module.forward_shave
        else:
            self.d_input_shape = self.G.output_size
            self.d_output_shape = self.d_input_shape - self.D.forward_shave

        # Input tensors
        self.g_input = torch.FloatTensor(1, 3, conf.input_crop_size, conf.input_crop_size).cuda()
        self.d_input = torch.FloatTensor(1, 3, self.d_input_shape, self.d_input_shape).cuda()

        # The kernel G is imitating
        self.curr_k = torch.FloatTensor(conf.G_kernel_size, conf.G_kernel_size).cuda()

        # Losses
        self.GAN_loss_layer = loss.GANLoss(d_last_layer_size=self.d_output_shape).cuda()
        self.bicubic_loss = loss.DownScaleLoss(scale_factor=conf.scale_factor).cuda()
        self.sum2one_loss = loss.SumOfWeightsLoss().cuda()
        self.boundaries_loss = loss.BoundariesLoss(k_size=conf.G_kernel_size).cuda()
        self.centralized_loss = loss.CentralizedLoss(k_size=conf.G_kernel_size, scale_factor=conf.scale_factor).cuda()
        self.sparse_loss = loss.SparsityLoss().cuda()
        self.loss_bicubic = 0

        # Define loss function
        self.criterionGAN = self.GAN_loss_layer.forward

        # Initialize networks weights
        self.G.apply(networks.weights_init_G)
        self.D.apply(networks.weights_init_D)

        # Optimizers
        self.optimizer_G = torch.optim.Adam(self.G.parameters(), lr=conf.g_lr, betas=(conf.beta1, 0.999))
        self.optimizer_D = torch.optim.Adam(self.D.parameters(), lr=conf.d_lr, betas=(conf.beta1, 0.999))

        print('*' * 60 + '\nSTARTED KernelGAN on: \"%s\"...' % conf.input_image_path)
Example #5
0
    def __init__(self, conf):
        # Acquire configuration
        self.conf = conf

        # Define the GAN
        self.G = networks.Generator(conf).cuda()
        self.D = networks.Discriminator(conf).cuda()

        # Calculate D's input & output shape according to the shaving done by the networks
        self.d_input_shape = self.G.output_size
        self.d_output_shape = self.d_input_shape - self.D.forward_shave

        # Input tensors
        self.g_input = torch.FloatTensor(1, 3, conf.input_crop_size,
                                         conf.input_crop_size).cuda()
        self.d_input = torch.FloatTensor(1, 3, self.d_input_shape,
                                         self.d_input_shape).cuda()

        # The kernel G is imitating
        self.curr_k = torch.FloatTensor(conf.G_kernel_size,
                                        conf.G_kernel_size).cuda()

        # Losses
        self.GAN_loss_layer = loss.GANLoss(
            d_last_layer_size=self.d_output_shape).cuda()
        self.bicubic_loss = loss.DownScaleLoss(
            scale_factor=conf.scale_factor).cuda()
        self.sum2one_loss = loss.SumOfWeightsLoss().cuda()
        self.boundaries_loss = loss.BoundariesLoss(
            k_size=conf.G_kernel_size).cuda()
        self.centralized_loss = loss.CentralizedLoss(
            k_size=conf.G_kernel_size, scale_factor=conf.scale_factor).cuda()
        self.sparse_loss = loss.SparsityLoss().cuda()
        self.loss_bicubic = 0

        # Define loss function
        self.criterionGAN = self.GAN_loss_layer.forward

        # Initialize networks weights
        self.G.apply(networks.weights_init_G)
        self.D.apply(networks.weights_init_D)

        # Optimizers
        self.optimizer_G = torch.optim.Adam(self.G.parameters(),
                                            lr=conf.g_lr,
                                            betas=(conf.beta1, 0.999))
        self.optimizer_D = torch.optim.Adam(self.D.parameters(),
                                            lr=conf.d_lr,
                                            betas=(conf.beta1, 0.999))

        self.iteration = 0  # for tensorboard
        # self.ground_truth_kernel = np.loadtxt(conf.ground_truth_kernel_path)
        # writer.add_image("ground_truth_kernel", (self.ground_truth_kernel - np.min(self.ground_truth_kernel)) / (np.max(self.ground_truth_kernel - np.min(self.ground_truth_kernel))), 0, dataformats="HW")

        print('*' * 60 +
              '\nSTARTED KernelGAN on: \"%s\"...' % conf.input_image_path)
Example #6
0
def init_gan_model(model_dir, nb_of_classes):
    device = torch.device("cuda:0")
    
    generator_net = networks.Generator().to(device)
    discriminator_net = networks.ClassifierDiscriminator(nb_of_classes=nb_of_classes).to(device)
    
    generator_net.apply(networks.weights_init)
    discriminator_net.apply(networks.weights_init)
    
    utils.print_and_log(model_dir, generator_net)
    utils.print_and_log(model_dir, discriminator_net)
    
    learning_rate = 0.0002
    beta1 = 0.5
    discriminator_optimizer = optim.Adam(discriminator_net.parameters(), lr=learning_rate, betas=(beta1, 0.999))
    generator_optimizer = optim.Adam(generator_net.parameters(), lr=learning_rate, betas=(beta1, 0.999))
    
    criterion = nn.CrossEntropyLoss()
    
    return discriminator_net, generator_net, criterion, discriminator_optimizer, generator_optimizer
Example #7
0
    def __init__(self, latent_size, generator_folder, restore,
                 param_optimizer):

        self.strategy = tf.distribute.MirroredStrategy()

        # Dynamic parameters
        with self.strategy.scope():
            self.generator = networks.Generator(
                latent_size=latent_size, generator_folder=generator_folder)
            self.encoder = networks.Encoder(latent_size=latent_size)

        self.current_resolution = 1
        self.current_width = 2**self.current_resolution
        self.res_batch = {
            2: 64,
            4: 32,
            8: 16,
            16: 8,
            32: 4,
            64: 2,
            128: 1,
            256: 1
        }
        self.res_epoch = {
            2: 10,
            4: 20,
            8: 30,
            16: 50,
            32: 60,
            64: 80,
            128: 100,
            256: 400
        }

        # Static parameters
        self.generate = True
        self.learning_rate = 0.0001
        self.latent_size = 1024
        self.restore = restore
        self.optimizer = param_optimizer
Example #8
0
def init_model(model_dir):
    device = torch.device("cuda:0")

    generator_net = networks.Generator().to(device)
    discriminator_net = networks.Discriminator().to(device)

    generator_net.apply(networks.weights_init)
    discriminator_net.apply(networks.weights_init)

    utils.print_and_log(model_dir, generator_net)
    utils.print_and_log(model_dir, discriminator_net)

    learning_rate = 0.0002
    beta1 = 0.5
    discriminator_optimizer = optim.Adam(discriminator_net.parameters(),
                                         lr=learning_rate,
                                         betas=(beta1, 0.999))
    generator_optimizer = optim.Adam(generator_net.parameters(),
                                     lr=learning_rate,
                                     betas=(beta1, 0.999))

    return discriminator_net, generator_net, discriminator_optimizer, generator_optimizer
Example #9
0
def main(args):
    generator = networks.Generator()
    print(generator)
    discriminator = networks.Discriminator()
    print(discriminator)
Example #10
0
fp = os.path.join(os.path.abspath(''), dn)

if not os.path.exists(dn):
    os.makedirs(fp)

manual_seed = 999
torch.manual_seed(manual_seed)
batch_size = 1000
fixed_size = 10000

train_epoch = 20000
lr_d = 0.00002
lr_g = 0.0002
beta1 = 0.5

generator = networks.Generator(d_noise, d_data)
discriminator = networks.Discriminator(d_data)

s = sampler.SAMPLER()

# plt.ion()
fig = plt.figure(figsize=(6, 6))
fig.canvas.set_window_title("2D Generation")
fig.suptitle(f"dimension data:{d_data}, dimension noise:{d_noise}")
if d_data == 2:  # 感觉这个if 可以写在sampler类里面,没必要在这里拿出来
    sam = s.sampler(bs=batch_size)
    sam_show = s.sampler(bs=fixed_size)
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)
Example #11
0
def main():
    # Get training options
    opt = get_opt()

    device = torch.device("cuda") if opt.cuda else torch.device("cpu")

    # Define the networks
    # netG_A: used to transfer image from domain A to domain B
    netG_A = networks.Generator(opt.input_nc, opt.output_nc, opt.ngf, opt.n_res, opt.dropout)
    if opt.u_net:
        netG_A = networks.U_net(opt.input_nc, opt.output_nc, opt.ngf)

    # netD_B: used to test whether an image is from domain A
    netD_B = networks.Discriminator(opt.input_nc + opt.output_nc, opt.ndf)

    # Initialize the networks
    if opt.cuda:
        netG_A.cuda()
        netD_B.cuda()
    utils.init_weight(netG_A)
    utils.init_weight(netD_B)

    if opt.pretrained:
        netG_A.load_state_dict(torch.load('pretrained/netG_A.pth'))
        netD_B.load_state_dict(torch.load('pretrained/netD_B.pth'))


    # Define the loss functions
    criterion_GAN = utils.GANLoss()
    if opt.cuda:
        criterion_GAN.cuda()

    criterion_l1 = torch.nn.L1Loss()

    # Define the optimizers
    optimizer_G = torch.optim.Adam(netG_A.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
    optimizer_D_B = torch.optim.Adam(netD_B.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))

    # Create learning rate schedulers
    lr_scheduler_G = torch.optim.lr_scheduler.LambdaLR(optimizer_G, lr_lambda = utils.Lambda_rule(opt.epoch, opt.n_epochs, opt.n_epochs_decay).step)
    lr_scheduler_D_B = torch.optim.lr_scheduler.LambdaLR(optimizer_D_B, lr_lambda = utils.Lambda_rule(opt.epoch, opt.n_epochs, opt.n_epochs_decay).step)


    # Define the transform, and load the data
    transform = transforms.Compose([transforms.Resize((opt.sizeh, opt.sizew)),
                transforms.ToTensor(),
                transforms.Normalize((0.5,), (0.5,))])
    dataloader = DataLoader(PairedImage(opt.rootdir, transform = transform, mode = 'train'), batch_size=opt.batch_size, shuffle=True, num_workers=opt.n_cpu)

    # numpy arrays to store the loss of epoch
    loss_G_array = np.zeros(opt.n_epochs + opt.n_epochs_decay)
    loss_D_B_array = np.zeros(opt.n_epochs + opt.n_epochs_decay)

    # Training
    for epoch in range(opt.epoch, opt.n_epochs + opt.n_epochs_decay):
        start = time.strftime("%H:%M:%S")
        print("current epoch :", epoch, " start time :", start)
        # Empty list to store the loss of each mini-batch
        loss_G_list = []
        loss_D_B_list = []

        for i, batch in enumerate(dataloader):
            if i % 20 == 1:
                print("current step: ", i)
                current = time.strftime("%H:%M:%S")
                print("current time :", current)
                print("last loss G_A:", loss_G_list[-1],  "last loss D_B:", loss_D_B_list[-1])

            real_A = batch['A'].to(device)
            real_B = batch['B'].to(device)

            # Train the generator
            utils.set_requires_grad([netG_A], True)
            optimizer_G.zero_grad()

            # Compute fake images and reconstructed images
            fake_B = netG_A(real_A)


            # discriminators require no gradients when optimizing generators
            utils.set_requires_grad([netD_B], False)


            # GAN loss
            prediction_fake_B = netD_B(torch.cat((fake_B, real_A), dim=1))
            loss_gan = criterion_GAN(prediction_fake_B, True)

            #L1 loss
            loss_l1 = criterion_l1(real_B, fake_B) * opt.l1_loss

            # total loss without the identity loss
            loss_G = loss_gan + loss_l1

            loss_G_list.append(loss_G.item())
            loss_G.backward()
            optimizer_G.step()

            # Train the discriminator
            utils.set_requires_grad([netG_A], False)
            utils.set_requires_grad([netD_B], True)

            # Train the discriminator D_B
            optimizer_D_B.zero_grad()
            # real images
            pred_real = netD_B(torch.cat((real_B, real_A), dim=1))
            loss_D_real = criterion_GAN(pred_real, True)

            # fake images
            fake_B = netG_A(real_A)
            pred_fake = netD_B(torch.cat((fake_B, real_A), dim=1))
            loss_D_fake = criterion_GAN(pred_fake, False)

            # total loss
            loss_D_B = (loss_D_real + loss_D_fake) * 0.5
            loss_D_B_list.append(loss_D_B.item())
            loss_D_B.backward()
            optimizer_D_B.step()

        # Update the learning rate
        lr_scheduler_G.step()
        lr_scheduler_D_B.step()

        # Save models checkpoints
        torch.save(netG_A.state_dict(), 'model/netG_A_pix.pth')
        torch.save(netD_B.state_dict(), 'model/netD_B_pix.pth')

        # Save other checkpoint information
        checkpoint = {'epoch': epoch,
                      'optimizer_G': optimizer_G.state_dict(),
                      'optimizer_D_B': optimizer_D_B.state_dict(),
                      'lr_scheduler_G': lr_scheduler_G.state_dict(),
                      'lr_scheduler_D_B': lr_scheduler_D_B.state_dict()}
        torch.save(checkpoint, 'model/checkpoint.pth')



        # Update the numpy arrays that record the loss
        loss_G_array[epoch] = sum(loss_G_list) / len(loss_G_list)
        loss_D_B_array[epoch] = sum(loss_D_B_list) / len(loss_D_B_list)
        np.savetxt('model/loss_G.txt', loss_G_array)
        np.savetxt('model/loss_D_B.txt', loss_D_B_array)

        end = time.strftime("%H:%M:%S")
        print("current epoch :", epoch, " end time :", end)
        print("G loss :", loss_G_array[epoch], "D_B loss :", loss_D_B_array[epoch])
Example #12
0
# val_embeddings = dataservices.Doc2Vec(
#     "paragraph-vectors/data/sentences_train_model.dbow_numnoisewords\
#     .2_vecdim.100_batchsize.32_lr.0.001000_epoch.100_loss.0.781092.csv"
# )  # change this to val
# val_dataset = dataservices.RecipeQADataset(
#     "recipeqa/new_val_cleaned.json",
#     "recipeqa/features/val",
#     val_embeddings
# )
# val_dataloader = torch.utils.data.DataLoader(
#     val_dataset, batch_size=opt.batchSize,
#     shuffle=True, num_workers=int(opt.workers),
#     collate_fn=dataservices.batch_collator(device=device)
# )

netG = networks.Generator(opt).to(device)
netD = networks.Discriminator(opt).to(device)


def weights_init(m):
    pass


criterionD = nn.BCEWithLogitsLoss()  # logsigmoid + binary cross entropy
criterionG = nn.CrossEntropyLoss()  # logsoftmax + multi-class cross entropy

optimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))


def fscore(probabilities, labels):
Example #13
0
def mask(input):
    netG = networks.Generator(opt.input_nc, opt.output_nc, opt.ngf, opt.norm, not opt.no_dropout,opt.gpu_ids)
    _,mask = netG(input)
    return mask
Example #14
0
# ハイパーパラメータの定義
lr = 0.0001
betas = (0.5, 0.999)
batch_size = 1
input_size = 128  # patch_size = 8(128/2^4)
num_epochs = 200
lambda_cycle = 10.0
lambda_identity = 5.0

# GPUが使用できるか確認
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('使用デバイス:', device)

# 各生成器と判別器をインスタンス化
G_A2B = net.Generator()
G_B2A = net.Generator()
D_A = net.Discriminator()
D_B = net.Discriminator()

# GPUが使用できるならモデルをGPUに載せて初期化
if device == 'cuda:0':
    G_A2B.cuda()
    G_B2A.cuda()
    D_A.cuda()
    D_B.cuda()

print('ネットワークの初期化中...', end='')
G_A2B.apply(utils.weights_init)
G_B2A.apply(utils.weights_init)
D_A.apply(utils.weights_init)
Example #15
0
def main():
    # Get training options
    opt = get_opt()

    # Define the networks
    # netG_A: used to transfer image from domain A to domain B
    # netG_B: used to transfer image from domain B to domain A
    netG_A = networks.Generator(opt.input_nc, opt.output_nc, opt.ngf,
                                opt.n_res, opt.dropout)
    netG_B = networks.Generator(opt.output_nc, opt.input_nc, opt.ngf,
                                opt.n_res, opt.dropout)
    if opt.u_net:
        netG_A = networks.U_net(opt.input_nc, opt.output_nc, opt.ngf)
        netG_B = networks.U_net(opt.output_nc, opt.input_nc, opt.ngf)

    # netD_A: used to test whether an image is from domain B
    # netD_B: used to test whether an image is from domain A
    netD_A = networks.Discriminator(opt.input_nc, opt.ndf)
    netD_B = networks.Discriminator(opt.output_nc, opt.ndf)

    # Initialize the networks
    if opt.cuda:
        netG_A.cuda()
        netG_B.cuda()
        netD_A.cuda()
        netD_B.cuda()
    utils.init_weight(netG_A)
    utils.init_weight(netG_B)
    utils.init_weight(netD_A)
    utils.init_weight(netD_B)

    if opt.pretrained:
        netG_A.load_state_dict(torch.load('pretrained/netG_A.pth'))
        netG_B.load_state_dict(torch.load('pretrained/netG_B.pth'))
        netD_A.load_state_dict(torch.load('pretrained/netD_A.pth'))
        netD_B.load_state_dict(torch.load('pretrained/netD_B.pth'))

    # Define the loss functions
    criterion_GAN = utils.GANLoss()
    if opt.cuda:
        criterion_GAN.cuda()

    criterion_cycle = torch.nn.L1Loss()
    # Alternatively, can try MSE cycle consistency loss
    #criterion_cycle = torch.nn.MSELoss()
    criterion_identity = torch.nn.L1Loss()

    # Define the optimizers
    optimizer_G = torch.optim.Adam(itertools.chain(netG_A.parameters(),
                                                   netG_B.parameters()),
                                   lr=opt.lr,
                                   betas=(opt.beta1, 0.999))
    optimizer_D_A = torch.optim.Adam(netD_A.parameters(),
                                     lr=opt.lr,
                                     betas=(opt.beta1, 0.999))
    optimizer_D_B = torch.optim.Adam(netD_B.parameters(),
                                     lr=opt.lr,
                                     betas=(opt.beta1, 0.999))

    # Create learning rate schedulers
    lr_scheduler_G = torch.optim.lr_scheduler.LambdaLR(
        optimizer_G,
        lr_lambda=utils.Lambda_rule(opt.epoch, opt.n_epochs,
                                    opt.n_epochs_decay).step)
    lr_scheduler_D_A = torch.optim.lr_scheduler.LambdaLR(
        optimizer_D_A,
        lr_lambda=utils.Lambda_rule(opt.epoch, opt.n_epochs,
                                    opt.n_epochs_decay).step)
    lr_scheduler_D_B = torch.optim.lr_scheduler.LambdaLR(
        optimizer_D_B,
        lr_lambda=utils.Lambda_rule(opt.epoch, opt.n_epochs,
                                    opt.n_epochs_decay).step)

    Tensor = torch.cuda.FloatTensor if opt.cuda else torch.Tensor
    input_A = Tensor(opt.batch_size, opt.input_nc, opt.sizeh, opt.sizew)
    input_B = Tensor(opt.batch_size, opt.output_nc, opt.sizeh, opt.sizew)

    # Define two image pools to store generated images
    fake_A_pool = utils.ImagePool()
    fake_B_pool = utils.ImagePool()

    # Define the transform, and load the data
    transform = transforms.Compose([
        transforms.Resize((opt.sizeh, opt.sizew)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, ), (0.5, ))
    ])
    dataloader = DataLoader(ImageDataset(opt.rootdir,
                                         transform=transform,
                                         mode='train'),
                            batch_size=opt.batch_size,
                            shuffle=True,
                            num_workers=opt.n_cpu)

    # numpy arrays to store the loss of epoch
    loss_G_array = np.zeros(opt.n_epochs + opt.n_epochs_decay)
    loss_D_A_array = np.zeros(opt.n_epochs + opt.n_epochs_decay)
    loss_D_B_array = np.zeros(opt.n_epochs + opt.n_epochs_decay)

    # Training
    for epoch in range(opt.epoch, opt.n_epochs + opt.n_epochs_decay):
        start = time.strftime("%H:%M:%S")
        print("current epoch :", epoch, " start time :", start)
        # Empty list to store the loss of each mini-batch
        loss_G_list = []
        loss_D_A_list = []
        loss_D_B_list = []

        for i, batch in enumerate(dataloader):
            if i % 50 == 1:
                print("current step: ", i)
                current = time.strftime("%H:%M:%S")
                print("current time :", current)
                print("last loss G:", loss_G_list[-1], "last loss D_A",
                      loss_D_A_list[-1], "last loss D_B", loss_D_B_list[-1])
            real_A = input_A.copy_(batch['A'])
            real_B = input_B.copy_(batch['B'])

            # Train the generator
            optimizer_G.zero_grad()

            # Compute fake images and reconstructed images
            fake_B = netG_A(real_A)
            fake_A = netG_B(real_B)

            if opt.identity_loss != 0:
                same_B = netG_A(real_B)
                same_A = netG_B(real_A)

            # discriminators require no gradients when optimizing generators
            utils.set_requires_grad([netD_A, netD_B], False)

            # Identity loss
            if opt.identity_loss != 0:
                loss_identity_A = criterion_identity(
                    same_A, real_A) * opt.identity_loss
                loss_identity_B = criterion_identity(
                    same_B, real_B) * opt.identity_loss

            # GAN loss
            prediction_fake_B = netD_B(fake_B)
            loss_gan_B = criterion_GAN(prediction_fake_B, True)
            prediction_fake_A = netD_A(fake_A)
            loss_gan_A = criterion_GAN(prediction_fake_A, True)

            # Cycle consistent loss
            recA = netG_B(fake_B)
            recB = netG_A(fake_A)
            loss_cycle_A = criterion_cycle(recA, real_A) * opt.cycle_loss
            loss_cycle_B = criterion_cycle(recB, real_B) * opt.cycle_loss

            # total loss without the identity loss
            loss_G = loss_gan_B + loss_gan_A + loss_cycle_A + loss_cycle_B

            if opt.identity_loss != 0:
                loss_G += loss_identity_A + loss_identity_B

            loss_G_list.append(loss_G.item())
            loss_G.backward()
            optimizer_G.step()

            # Train the discriminator
            utils.set_requires_grad([netD_A, netD_B], True)

            # Train the discriminator D_A
            optimizer_D_A.zero_grad()
            # real images
            pred_real = netD_A(real_A)
            loss_D_real = criterion_GAN(pred_real, True)

            # fake images
            fake_A = fake_A_pool.query(fake_A)
            pred_fake = netD_A(fake_A.detach())
            loss_D_fake = criterion_GAN(pred_fake, False)

            #total loss
            loss_D_A = (loss_D_real + loss_D_fake) * 0.5
            loss_D_A_list.append(loss_D_A.item())
            loss_D_A.backward()
            optimizer_D_A.step()

            # Train the discriminator D_B
            optimizer_D_B.zero_grad()
            # real images
            pred_real = netD_B(real_B)
            loss_D_real = criterion_GAN(pred_real, True)

            # fake images
            fake_B = fake_B_pool.query(fake_B)
            pred_fake = netD_B(fake_B.detach())
            loss_D_fake = criterion_GAN(pred_fake, False)

            # total loss
            loss_D_B = (loss_D_real + loss_D_fake) * 0.5
            loss_D_B_list.append(loss_D_B.item())
            loss_D_B.backward()
            optimizer_D_B.step()

        # Update the learning rate
        lr_scheduler_G.step()
        lr_scheduler_D_A.step()
        lr_scheduler_D_B.step()

        # Save models checkpoints
        torch.save(netG_A.state_dict(), 'model/netG_A.pth')
        torch.save(netG_B.state_dict(), 'model/netG_B.pth')
        torch.save(netD_A.state_dict(), 'model/netD_A.pth')
        torch.save(netD_B.state_dict(), 'model/netD_B.pth')

        # Save other checkpoint information
        checkpoint = {
            'epoch': epoch,
            'optimizer_G': optimizer_G.state_dict(),
            'optimizer_D_A': optimizer_D_A.state_dict(),
            'optimizer_D_B': optimizer_D_B.state_dict(),
            'lr_scheduler_G': lr_scheduler_G.state_dict(),
            'lr_scheduler_D_A': lr_scheduler_D_A.state_dict(),
            'lr_scheduler_D_B': lr_scheduler_D_B.state_dict()
        }
        torch.save(checkpoint, 'model/checkpoint.pth')

        # Update the numpy arrays that record the loss
        loss_G_array[epoch] = sum(loss_G_list) / len(loss_G_list)
        loss_D_A_array[epoch] = sum(loss_D_A_list) / len(loss_D_A_list)
        loss_D_B_array[epoch] = sum(loss_D_B_list) / len(loss_D_B_list)
        np.savetxt('model/loss_G.txt', loss_G_array)
        np.savetxt('model/loss_D_A.txt', loss_D_A_array)
        np.savetxt('model/loss_D_b.txt', loss_D_B_array)

        if epoch % 10 == 9:
            torch.save(netG_A.state_dict(),
                       'model/netG_A' + str(epoch) + '.pth')
            torch.save(netG_B.state_dict(),
                       'model/netG_B' + str(epoch) + '.pth')
            torch.save(netD_A.state_dict(),
                       'model/netD_A' + str(epoch) + '.pth')
            torch.save(netD_B.state_dict(),
                       'model/netD_B' + str(epoch) + '.pth')

        end = time.strftime("%H:%M:%S")
        print("current epoch :", epoch, " end time :", end)
        print("G loss :", loss_G_array[epoch], "D_A loss :",
              loss_D_A_array[epoch], "D_B loss :", loss_D_B_array[epoch])
Example #16
0
    def __init__(self, opt):
        """Initialize the CycleGAN class.

                Parameters:
                    opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
                """

        self.opt = opt
        self.isTrain = opt.isTrain
        self.device = opt.device
        self.model_save_dir = opt.model_dir
        self.loss_names = []
        self.model_names = []
        self.optimizers = []
        self.image_paths = []

        self.epoch = 0
        self.num_epochs = opt.nr_epochs

        # specify the training losses you want to print out. The training/test scripts will call <BaseModel.get_current_losses>
        self.loss_names = ['D_A', 'G_A', 'cycle_A', 'D_B', 'G_B', 'cycle_B']
        # specify the images you want to save/display. The training/test scripts will call <BaseModel.get_current_visuals>

        if self.isTrain:
            self.model_names = ['G_A', 'G_B', 'D_A', 'D_B']
        else:  # during test time, only load Gs
            self.model_names = ['G_A', 'G_B']

        # define networks (both Generators and discriminators)
        # The naming is different from those used in the paper.
        # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X)
        self.netG_A = networks.Generator(opt.input_nc, opt.output_nc, opt.ngf,
                                         opt.n_layers_G).to(self.device)
        self.netG_B = networks.Generator(opt.output_nc, opt.input_nc, opt.ngf,
                                         opt.n_layers_G).to(self.device)

        if self.isTrain:  # define discriminators
            self.netD_A = networks.NLayerDiscriminator(
                opt.output_nc, opt.ndf, opt.n_layers_D).to(self.device)
            self.netD_B = networks.NLayerDiscriminator(
                opt.input_nc, opt.ndf, opt.n_layers_D).to(self.device)
            # self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.netD,
            #                                 opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
            # self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.netD,
            #                                 opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)

        if self.isTrain:
            self.fake_A_pool = networks.MotionPool(
                opt.pool_size
            )  # create image buffer to store previously generated images
            self.fake_B_pool = networks.MotionPool(
                opt.pool_size
            )  # create image buffer to store previously generated images
            # define loss functions
            self.criterionGAN = networks.GANLoss(opt.gan_mode).to(
                self.device)  # define GAN loss.
            self.criterionCycle = torch.nn.L1Loss()
            # self.criterionIdt = torch.nn.L1Loss()
            # initialize optimizers; schedulers will be automatically created by function <BaseModel.setup>.
            self.optimizer_G = torch.optim.Adam(itertools.chain(
                self.netG_A.parameters(), self.netG_B.parameters()),
                                                lr=opt.lr,
                                                betas=(opt.beta1, 0.999))
            self.optimizer_D = torch.optim.Adam(itertools.chain(
                self.netD_A.parameters(), self.netD_B.parameters()),
                                                lr=opt.lr,
                                                betas=(opt.beta1, 0.999))
            self.optimizers.append(self.optimizer_G)
            self.optimizers.append(self.optimizer_D)

            self.schedulers = [
                networks.get_scheduler(optimizer, opt)
                for optimizer in self.optimizers
            ]
Example #17
0
    def __init__(self, conf):
        # Acquire configuration
        self.conf = conf
        self.cur_iter = 0
        self.max_iters = conf.max_iters

        # Define input tensor
        self.input_tensor = torch.FloatTensor(1, 3, conf.input_crop_size,
                                              conf.input_crop_size).cuda()
        self.real_example = torch.FloatTensor(1, 3, conf.output_crop_size,
                                              conf.output_crop_size).cuda()

        # Define networks
        self.G = networks.Generator(conf.G_base_channels, conf.G_num_resblocks,
                                    conf.G_num_downscales, conf.G_use_bias,
                                    conf.G_skip)
        self.D = networks.MultiScaleDiscriminator(conf.output_crop_size,
                                                  self.conf.D_max_num_scales,
                                                  self.conf.D_scale_factor,
                                                  self.conf.D_base_channels)
        self.GAN_loss_layer = networks.GANLoss()
        self.Reconstruct_loss = networks.WeightedMSELoss(use_L1=conf.use_L1)
        self.RandCrop = networks.RandomCrop(
            [conf.input_crop_size, conf.input_crop_size],
            must_divide=conf.must_divide)
        self.SwapCrops = networks.SwapCrops(conf.crop_swap_min_size,
                                            conf.crop_swap_max_size)

        # Make all networks run on GPU
        self.G.cuda()
        self.D.cuda()
        self.GAN_loss_layer.cuda()
        self.Reconstruct_loss.cuda()
        self.RandCrop.cuda()
        self.SwapCrops.cuda()

        # Define loss function
        self.criterionGAN = self.GAN_loss_layer.forward
        self.criterionReconstruction = self.Reconstruct_loss.forward

        # Keeping track of losses- prepare tensors
        self.losses_G_gan = torch.FloatTensor(conf.print_freq).cuda()
        self.losses_D_real = torch.FloatTensor(conf.print_freq).cuda()
        self.losses_D_fake = torch.FloatTensor(conf.print_freq).cuda()
        self.losses_G_reconstruct = torch.FloatTensor(conf.print_freq).cuda()
        if self.conf.reconstruct_loss_stop_iter > 0:
            self.losses_D_reconstruct = torch.FloatTensor(
                conf.print_freq).cuda()

        # Initialize networks
        self.G.apply(networks.weights_init)
        self.D.apply(networks.weights_init)

        # Initialize optimizers
        self.optimizer_G = torch.optim.Adam(self.G.parameters(),
                                            lr=conf.g_lr,
                                            betas=(conf.beta1, 0.999))
        self.optimizer_D = torch.optim.Adam(self.D.parameters(),
                                            lr=conf.d_lr,
                                            betas=(conf.beta1, 0.999))

        # Learning rate scheduler
        # First define linearly decaying functions (decay starts at a special iter)
        start_decay = conf.lr_start_decay_iter
        end_decay = conf.max_iters
        # def lr_function(n_iter):
        #     return 1 - max(0, 1.0 * (n_iter - start_decay) / (conf.max_iters - start_decay))
        lr_function = LRPolicy(start_decay, end_decay)
        # Define learning rate schedulers
        self.lr_scheduler_G = torch.optim.lr_scheduler.LambdaLR(
            self.optimizer_G, lr_function)
        self.lr_scheduler_D = torch.optim.lr_scheduler.LambdaLR(
            self.optimizer_D, lr_function)
Example #18
0
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if torch.backends.cudnn.enabled:
        torch.backends.cudnn.benchmark = True

    prepare_result()
    make_edge_promoting_img()

    # data_loader
    landscape_dataloader = CreateTrainDataLoader(args, "landscape")
    anime_dataloader = CreateTrainDataLoader(args, "anime")
    landscape_test_dataloader = CreateTestDataLoader(args, "landscape")
    anime_test_dataloader = CreateTestDataLoader(args, "anime")

    generator = networks.Generator(args.ngf)
    if args.latest_generator_model != '':
        if torch.cuda.is_available():
            generator.load_state_dict(torch.load(args.latest_generator_model))
        else:
            # cpu mode
            generator.load_state_dict(
                torch.load(args.latest_generator_model,
                           map_location=lambda storage, loc: storage))

    discriminator = networks.Discriminator(args.in_ndc, args.out_ndc, args.ndf)
    if args.latest_discriminator_model != '':
        if torch.cuda.is_available():
            discriminator.load_state_dict(
                torch.load(args.latest_discriminator_model))
        else:
            discriminator.load_state_dict(
                torch.load(args.latest_discriminator_model,
                           map_location=lambda storage, loc: storage))

    VGG = networks.VGG19(init_weights=args.vgg_model, feature_mode=True)

    generator.to(device)
    discriminator.to(device)
    VGG.to(device)

    generator.train()
    discriminator.train()

    VGG.eval()

    G_optimizer = optim.Adam(generator.parameters(),
                             lr=args.lrG,
                             betas=(args.beta1, args.beta2))
    D_optimizer = optim.Adam(discriminator.parameters(),
                             lr=args.lrD,
                             betas=(args.beta1, args.beta2))
    # G_scheduler = optim.lr_scheduler.MultiStepLR(optimizer=G_optimizer, milestones=[args.train_epoch // 2, args.train_epoch // 4 * 3], gamma=0.1)
    # D_scheduler = optim.lr_scheduler.MultiStepLR(optimizer=D_optimizer, milestones=[args.train_epoch // 2, args.train_epoch // 4 * 3], gamma=0.1)

    print('---------- Networks initialized -------------')
    utils.print_network(generator)
    utils.print_network(discriminator)
    utils.print_network(VGG)
    print('-----------------------------------------------')

    BCE_loss = nn.BCELoss().to(device)
    Hinge_loss = nn.HingeEmbeddingLoss().to(device)
    L1_loss = nn.L1Loss().to(device)
    MSELoss = nn.MSELoss().to(device)

    Adv_loss = BCE_loss

    pre_train_hist = {}
    pre_train_hist['Recon_loss'] = []
    pre_train_hist['per_epoch_time'] = []
    pre_train_hist['total_time'] = []
    """ Pre-train reconstruction """
    if args.latest_generator_model == '':
        print('Pre-training start!')
        start_time = time.time()
        for epoch in range(args.pre_train_epoch):
            epoch_start_time = time.time()
            Recon_losses = []
            for lcimg, lhimg, lsimg in landscape_dataloader:
                lcimg, lhimg, lsimg = lcimg.to(device), lhimg.to(
                    device), lsimg.to(device)

                # train generator G
                G_optimizer.zero_grad()

                x_feature = VGG((lcimg + 1) / 2)

                mask = mask_gen()
                hint = torch.cat((lhimg * mask, mask), 1)
                gen_img = generator(lsimg, hint)
                G_feature = VGG((gen_img + 1) / 2)

                Recon_loss = 10 * L1_loss(G_feature, x_feature.detach())
                Recon_losses.append(Recon_loss.item())
                pre_train_hist['Recon_loss'].append(Recon_loss.item())

                Recon_loss.backward()
                G_optimizer.step()

            per_epoch_time = time.time() - epoch_start_time
            pre_train_hist['per_epoch_time'].append(per_epoch_time)
            print('[%d/%d] - time: %.2f, Recon loss: %.3f' %
                  ((epoch + 1), args.pre_train_epoch, per_epoch_time,
                   torch.mean(torch.FloatTensor(Recon_losses))))

            # Save
            if (epoch + 1) % 5 == 0:
                with torch.no_grad():
                    generator.eval()
                    for n, (lcimg, lhimg,
                            lsimg) in enumerate(landscape_dataloader):
                        lcimg, lhimg, lsimg = lcimg.to(device), lhimg.to(
                            device), lsimg.to(device)
                        mask = mask_gen()
                        hint = torch.cat((lhimg * mask, mask), 1)
                        g_recon = generator(lsimg, hint)
                        result = torch.cat((lcimg[0], g_recon[0]), 2)
                        path = os.path.join(
                            args.name + '_results', 'Reconstruction',
                            args.name + '_train_recon_' + f'epoch_{epoch}_' +
                            str(n + 1) + '.png')
                        plt.imsave(
                            path,
                            (result.cpu().numpy().transpose(1, 2, 0) + 1) / 2)
                        if n == 4:
                            break

                    for n, (lcimg, lhimg,
                            lsimg) in enumerate(landscape_test_dataloader):
                        lcimg, lhimg, lsimg = lcimg.to(device), lhimg.to(
                            device), lsimg.to(device)
                        mask = mask_gen()
                        hint = torch.cat((lhimg * mask, mask), 1)
                        g_recon = generator(lsimg, hint)
                        result = torch.cat((lcimg[0], g_recon[0]), 2)
                        path = os.path.join(
                            args.name + '_results', 'Reconstruction',
                            args.name + '_test_recon_' + f'epoch_{epoch}_' +
                            str(n + 1) + '.png')
                        plt.imsave(
                            path,
                            (result.cpu().numpy().transpose(1, 2, 0) + 1) / 2)
                        if n == 4:
                            break

        total_time = time.time() - start_time
        pre_train_hist['total_time'].append(total_time)
        with open(os.path.join(args.name + '_results', 'pre_train_hist.pkl'),
                  'wb') as f:
            pickle.dump(pre_train_hist, f)
        torch.save(
            generator.state_dict(),
            os.path.join(args.name + '_results', 'generator_pretrain.pkl'))

    else:
        print('Load the latest generator model, no need to pre-train')

    train_hist = {}
    train_hist['Disc_loss'] = []
    train_hist['Gen_loss'] = []
    train_hist['Con_loss'] = []
    train_hist['per_epoch_time'] = []
    train_hist['total_time'] = []
    print('training start!')
    start_time = time.time()

    real = torch.ones(args.batch_size, 1, args.input_size // 4,
                      args.input_size // 4).to(device)
    fake = torch.zeros(args.batch_size, 1, args.input_size // 4,
                       args.input_size // 4).to(device)
    for epoch in range(args.train_epoch):
        epoch_start_time = time.time()
        generator.train()
        Disc_losses = []
        Gen_losses = []
        Con_losses = []
        for i, ((acimg, ac_smooth_img, _), (lcimg, lhimg, lsimg)) in enumerate(
                zip(anime_dataloader, landscape_dataloader)):
            acimg, ac_smooth_img, lcimg, lhimg, lsimg = acimg.to(
                device), ac_smooth_img.to(device), lcimg.to(device), lhimg.to(
                    device), lsimg.to(device)

            if i % args.n_dis == 0:
                # train G
                G_optimizer.zero_grad()

                mask = mask_gen()
                hint = torch.cat((lhimg * mask, mask), 1)
                gen_img = generator(lsimg, hint)
                D_fake = discriminator(gen_img)
                D_fake_loss = Adv_loss(D_fake, real)

                x_feature = VGG((lcimg + 1) / 2)
                G_feature = VGG((gen_img + 1) / 2)
                Con_loss = args.con_lambda * L1_loss(G_feature,
                                                     x_feature.detach())

                Gen_loss = D_fake_loss + Con_loss
                Gen_losses.append(D_fake_loss.item())
                train_hist['Gen_loss'].append(D_fake_loss.item())
                Con_losses.append(Con_loss.item())
                train_hist['Con_loss'].append(Con_loss.item())

                Gen_loss.backward()
                G_optimizer.step()
                # G_scheduler.step()

            # train D
            D_optimizer.zero_grad()

            D_real = discriminator(acimg)
            D_real_loss = Adv_loss(D_real, real)  # Hinge Loss (?)

            mask = mask_gen()
            hint = torch.cat((lhimg * mask, mask), 1)

            gen_img = generator(lsimg, hint)
            D_fake = discriminator(gen_img)
            D_fake_loss = Adv_loss(D_fake, fake)

            D_edge = discriminator(ac_smooth_img)
            D_edge_loss = Adv_loss(D_edge, fake)

            Disc_loss = D_real_loss + D_fake_loss + D_edge_loss
            # Disc_loss = D_real_loss + D_fake_loss
            Disc_losses.append(Disc_loss.item())
            train_hist['Disc_loss'].append(Disc_loss.item())

            Disc_loss.backward()
            D_optimizer.step()

    #     G_scheduler.step()
    #     D_scheduler.step()

        per_epoch_time = time.time() - epoch_start_time
        train_hist['per_epoch_time'].append(per_epoch_time)
        print(
            '[%d/%d] - time: %.2f, Disc loss: %.3f, Gen loss: %.3f, Con loss: %.3f'
            % ((epoch + 1), args.train_epoch, per_epoch_time,
               torch.mean(torch.FloatTensor(Disc_losses)),
               torch.mean(torch.FloatTensor(Gen_losses)),
               torch.mean(torch.FloatTensor(Con_losses))))

        if epoch % 2 == 1 or epoch == args.train_epoch - 1:
            with torch.no_grad():
                generator.eval()
                for n, (lcimg, lhimg,
                        lsimg) in enumerate(landscape_dataloader):
                    lcimg, lhimg, lsimg = lcimg.to(device), lhimg.to(
                        device), lsimg.to(device)
                    mask = mask_gen()
                    hint = torch.cat((lhimg * mask, mask), 1)
                    g_recon = generator(lsimg, hint)
                    result = torch.cat((lcimg[0], g_recon[0]), 2)
                    path = os.path.join(
                        args.name + '_results', 'Transfer',
                        str(epoch + 1) + '_epoch_' + args.name + '_train_' +
                        str(n + 1) + '.png')
                    plt.imsave(path,
                               (result.cpu().numpy().transpose(1, 2, 0) + 1) /
                               2)
                    if n == 4:
                        break

                for n, (lcimg, lhimg,
                        lsimg) in enumerate(landscape_test_dataloader):
                    lcimg, lhimg, lsimg = lcimg.to(device), lhimg.to(
                        device), lsimg.to(device)
                    mask = mask_gen()
                    hint = torch.cat((lhimg * mask, mask), 1)
                    g_recon = generator(lsimg, hint)
                    result = torch.cat((lcimg[0], g_recon[0]), 2)
                    path = os.path.join(
                        args.name + '_results', 'Transfer',
                        str(epoch + 1) + '_epoch_' + args.name + '_test_' +
                        str(n + 1) + '.png')
                    plt.imsave(path,
                               (result.cpu().numpy().transpose(1, 2, 0) + 1) /
                               2)
                    if n == 4:
                        break

                torch.save(
                    generator.state_dict(),
                    os.path.join(args.name + '_results',
                                 'generator_latest.pkl'))
                torch.save(
                    generator.state_dict(),
                    os.path.join(args.name + '_results',
                                 'discriminator_latest.pkl'))

    total_time = time.time() - start_time
    train_hist['total_time'].append(total_time)

    print("Avg one epoch time: %.2f, total %d epochs time: %.2f" %
          (torch.mean(torch.FloatTensor(
              train_hist['per_epoch_time'])), args.train_epoch, total_time))
    print("Training finish!... save training results")

    torch.save(generator.state_dict(),
               os.path.join(args.name + '_results', 'generator_param.pkl'))
    torch.save(discriminator.state_dict(),
               os.path.join(args.name + '_results', 'discriminator_param.pkl'))
    with open(os.path.join(args.name + '_results', 'train_hist.pkl'),
              'wb') as f:
        pickle.dump(train_hist, f)
    def __init__(self, config):

        super(PGGAN, self).__init__()

        self.tf_record_dir = config.tf_record_dir
        self.latent_size = config.latent_size
        self.label_size = config.label_size
        self.labels_exist = self.label_size > 0

        self.dimensionality = config.dimensionality
        self.num_channels = config.num_channels
        self.learning_rate = config.lr
        self.gpus = config.gpus

        self.d_repeats = config.d_repeats

        self.iters_per_transition = config.kiters_per_transition
        self.iters_per_resolution = config.kiters_per_resolution

        self.start_resolution = config.start_resolution
        self.target_resolution = config.target_resolution
        self.resolution_batch_size = config.resolution_batch_size

        self.img_ext = config.img_ext

        self.generator = networks.Generator(self.latent_size,
                                            dimensionality=self.dimensionality,
                                            num_channels=self.num_channels,
                                            fmap_base=config.g_fmap_base)
        self.discriminator = networks.Discriminator(
            self.label_size,
            dimensionality=self.dimensionality,
            num_channels=self.num_channels,
            fmap_base=config.d_fmap_base)

        self.run_id = Path(config.run_id)
        self.generated_dir = self.run_id.joinpath(config.generated_dir)
        self.model_dir = self.run_id.joinpath(config.model_dir)
        self.log_dir = self.run_id.joinpath(config.log_dir)

        self.run_id.mkdir(exist_ok=True)
        self.generated_dir.mkdir(exist_ok=True)
        self.model_dir.mkdir(exist_ok=True)

        self.train_summary_writer = tf.summary.create_file_writer(
            str(self.log_dir))

        current_resolution = 2
        self.add_resolution()

        while 2**current_resolution < self.start_resolution:
            self.add_resolution()
            current_resolution += 1

        self.strategy = config.strategy
        if self.strategy is not None:
            with self.strategy.scope():

                self.generator = networks.Generator(
                    self.latent_size,
                    dimensionality=self.dimensionality,
                    num_channels=self.num_channels,
                    fmap_base=config.g_fmap_base)
                self.discriminator = networks.Discriminator(
                    self.label_size,
                    dimensionality=self.dimensionality,
                    num_channels=self.num_channels,
                    fmap_base=config.d_fmap_base)

                current_resolution = 2
                self.add_resolution()

                while 2**current_resolution < self.start_resolution:
                    self.add_resolution()
                    current_resolution += 1
Example #20
0

# get the datasets ready for iterations
test_dataset = get_data.TestDatasetFromFolder(opt.image_dir, test_patients, opt.num_cross, opt.num_source, opt.input_ch, opt.output_ch, \
										   opt.log_file, opt.height_image, opt.cross_ID, opt.test_target, transform=transforms.Compose([
																																transforms.ToTensor()
																																# , normalize
																																]))


test_loader = DataLoader(test_dataset, batch_size=opt.batch_size, num_workers=opt.workers)

print('length of test dataset: ', len(test_dataset))


netG = networks.Generator(opt).cuda()
# print('\nGenerator:\n', netG)
netD = networks.NLayerDiscriminator(opt).cuda()
# print('\nDiscriminator:\n', netD)
Conv3D = networks.Conv3DBlock(opt).cuda()
# print('\nConv3D blocks:\n', Conv3D)


print('\n\n\nUploading model...')
model_path = os.path.join(opt.chkpnt_dir, 'model_best.pth.tar')

if os.path.isfile(model_path):
	utils.print_and_save_msg(f"=> loading Model '{model_path}'", opt.log_file)
	checkpoint = torch.load(model_path) 
	netG.load_state_dict(checkpoint['Generator'])
	# netD.load_state_dict(checkpoint['Discriminator'])
Example #21
0
test_data = DatasetProcessing(TEST_DIR)

num_train, num_test = len(train_data) , len(test_data)

train_loader = DataLoader(train_data,batch_size = opt.batch_size, shuffle = True, num_workers = 4)
test_loader = DataLoader(test_data,batch_size = opt.batch_size, shuffle = False, num_workers = 1)


train_labels = LoadLabel(TRAIN_DIR)
train_labels_onehot = EncodingOnehot(train_labels, nclasses)
test_labels = LoadLabel(TEST_DIR)
test_labels_onehot = EncodingOnehot(test_labels, nclasses)
Y = train_labels_onehot


G = networks.Generator(opt.g_input_size,opt.g_hidden_size,opt.g_output_size)
H = networks.Hashnet(opt.h_input_size,opt.h_hidden_size,opt.bit)
# print(G)
# print(D)
# print(H)

G_dict = G.state_dict()
pretrained_dict = torch.load('./G2_models.pt')
pretrained_dict = pretrained_dict.state_dict()
pretrained_dict = {k : v for k, v in pretrained_dict.items() if  k in G_dict}
G_dict.update(pretrained_dict)
G.load_state_dict(G_dict)



G.cuda()
Example #22
0
def train(epochs, batch_size, input_dir, model_save_dir):

    # Make an instance of the VGG class
    vgg_model = VGG_MODEL(image_shape)

    # Get High-Resolution(HR) [148,148,3] in this case and corresponding Low-Resolution(LR) images
    x_train_lr, x_train_hr = utils.load_training_data(input_dir, [148, 148, 3])

    #Based on the the batch size, get the total number of batches
    batch_count = int(x_train_hr.shape[0] / batch_size)

    #Get the downscaled image shape based on the downscale factor
    image_shape_downscaled = utils.get_downscaled_shape(
        image_shape, downscale_factor)

    # Initialize the generator network with the input image shape as the downscaled image shape (shape of LR images)
    generator = networks.Generator(input_shape=image_shape_downscaled)

    # Initialize the discriminator with the input image shape as the original image shape (HR image shape)
    discriminator = networks.Discriminator(image_shape)

    # Get the optimizer to tweak parameters based on loss
    optimizer = vgg_model.get_optimizer()

    # Compile the three models - generator, discriminator and gan(comb of both gen and disc - this network will train generator and will not tweak discriminator)
    generator.compile(loss=vgg_model.vgg_loss, optimizer=optimizer)
    discriminator.compile(loss="binary_crossentropy", optimizer=optimizer)
    gan = networks.GAN_Network(generator, discriminator,
                               image_shape_downscaled, optimizer,
                               vgg_model.vgg_loss)

    # Run training for the number of epochs defined
    for e in range(1, epochs + 1):
        print('-' * 15, 'Epoch %d' % e, '-' * 15)
        for _ in tqdm(range(batch_count)):

            # Get the next batch of LR and HR images
            image_batch_lr, image_batch_hr = utils.get_random_batch(
                x_train_lr, x_train_hr, x_train_hr.shape[0], batch_size)

            generated_images_sr = generator.predict(image_batch_lr)
            print(generated_images_sr.shape)

            real_data_Y = np.ones(
                batch_size) - np.random.random_sample(batch_size) * 0.2
            fake_data_Y = np.random.random_sample(batch_size) * 0.2

            discriminator.trainable = True
            print(real_data_Y.shape)
            d_loss_real = discriminator.train_on_batch(image_batch_hr,
                                                       real_data_Y)
            d_loss_fake = discriminator.train_on_batch(generated_images_sr,
                                                       fake_data_Y)
            discriminator_loss = 0.5 * np.add(d_loss_fake, d_loss_real)

            rand_nums = np.random.randint(0,
                                          x_train_hr.shape[0],
                                          size=batch_size)
            image_batch_hr = x_train_hr[rand_nums]
            image_batch_lr = x_train_lr[rand_nums]

            gan_Y = np.ones(
                batch_size) - np.random.random_sample(batch_size) * 0.2
            discriminator.trainable = False
            gan_loss = gan.train_on_batch(image_batch_lr,
                                          [image_batch_hr, gan_Y])

        print("discriminator_loss : %f" % discriminator_loss)
        print("gan_loss :", gan_loss)
        gan_loss = str(gan_loss)

        if e % 50 == 0:
            generator.save_weights(model_save_dir + 'gen_model%d.h5' % e)
            discriminator.save_weights(model_save_dir + 'dis_model%d.h5' % e)

    networks.save_model(gan)
Example #23
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import torch
import torch.nn as nn
import torch.utils.data as data
import matplotlib.pyplot as plt

import networks as net
import dataloader as dl

##### Generatorの動作チェック #####
G = net.Generator()
input_image = torch.randn(128, 128, 3)
input_image = input_image.view(1, 3, 128, 128)
fake_image = G(input_image)
print('fake image:', fake_image.shape)

##### Discriminatorの動作チェック #####
D = net.Discriminator()
d_out = D(fake_image)
print('Discriminator output',
      nn.Sigmoid()(d_out).shape)  # 出力にSigmoidをかけて[0, 1]に変換

##### Dataset, DataLoaderの動作チェック #####
train_img_A, train_img_B = dl.make_datapath_list(is_train=True)

# Datasetを作成
mean = (0.5, )
std = (0.5, )
train_dataset = dl.UnpairedDataset(train_img_A,
Example #24
0
    ], 0)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    return mask.to(device)


outputPath = f"predict_results/{args.name}"
os.makedirs(outputPath + "/predicts", exist_ok=True)
os.makedirs(outputPath + "/lines", exist_ok=True)
os.makedirs(outputPath + "/originals", exist_ok=True)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

landscape_test_dataloader = CreateTestDataLoader(args, "landscape")

generator = networks.Generator(args.ngf)

if args.latest_generator_model != '':
    if torch.cuda.is_available():
        generator.load_state_dict(torch.load(args.latest_generator_model))
    else:
        # cpu mode
        generator.load_state_dict(
            torch.load(args.latest_generator_model,
                       map_location=lambda storage, loc: storage))

generator.to(device)
generator.eval()

i = 0
with torch.no_grad():