def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'CGAN':
        gan = CGAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    elif args.gan_type == 'infoGAN':
        gan = infoGAN(args, SUPERVISED=True)
    elif args.gan_type == 'EBGAN':
        gan = EBGAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'DRAGAN':
        gan = DRAGAN(args)
    elif args.gan_type == 'LSGAN':
        gan = LSGAN(args)
    elif args.gan_type == 'BEGAN':
        gan = BEGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session
    gan.train()
    print(" [*] Training finished!")

    # visualize learned generator
    gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
Example #2
0
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'GAN_hinge':
        gan = GAN_hinge(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session
    gan.load()
    print('load GAN')

    # visualize learned generator
    gan.visualize_results_fid()
    # visualize_results(gan, args)
    print(" [*] Testing finished!")
Example #3
0
def main():
    # ハイパーパラメータの設定
    args = parse_args()
    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

    # GANのモデルを決定
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

    gan.train()
    print(" [*] Training finished!")

    # 学習済みの生成器が生成するイメージの可視化
    gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args, test_only=True)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args, test_only=True)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

    # Loads the model
    gan.load()
    print('[*] Model sucessfully loaded')

    # Creates samples of the model
    gan.G.eval()

    # Applies the operations asked in question 5 a) b) and c)
    #generate_9_samples(gan)
    #make_changes_in_latent_space(gan)
    interpolate_between_two_points(gan)
Example #5
0
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    elif args.gan_type == 'infoGAN':
        gan = infoGAN(args, SUPERVISED=False)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'WGAN_GP':
        gan = WGAN_GP(args)
    elif args.gan_type == 'LSGAN':
        gan = LSGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session
    gan.train()
    print(" [*] Training finished!")

    # visualize learned generator
    gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
Example #6
0
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    print(args)

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'CGAN':
        gan = CGAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    elif args.gan_type == 'infoGAN':
        gan = infoGAN(args, SUPERVISED=False)
    elif args.gan_type == 'EBGAN':
        gan = EBGAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'WGAN_GP':
        gan = WGAN_GP(args)
    elif args.gan_type == 'DRAGAN':
        gan = DRAGAN(args)
    elif args.gan_type == 'LSGAN':
        generator = model.InfoGANGenerator(input_dim=62,
                                           output_dim=3,
                                           input_size=args.input_size)
        discriminator = model.InfoGANDiscriminator(input_dim=3,
                                                   output_dim=1,
                                                   input_size=args.input_size)
        gan = LSGAN(args, generator, discriminator)
    elif args.gan_type == 'LSGAN_classifier':
        generator = model.InfoGANGenerator(input_dim=62,
                                           output_dim=3,
                                           input_size=args.input_size)
        discriminator = model.InfoGANDiscriminatorClassifier(
            input_dim=3,
            output_dim=1,
            input_size=args.input_size,
            save_dir=args.save_dir,
            model_name=args.gan_type)
        gan = LSGAN(args, generator, discriminator)
    elif args.gan_type == 'BEGAN':
        gan = BEGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session
    gan.train()
    print(" [*] Training finished!")

    # visualize learned generator
    gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
Example #7
0
File: main.py Project: AIMarkov/GAN
def main():
    # parse arguments

    args = parse_args()
    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True
        '''
        如果网络的输入数据维度或类型上变化不大,设置
        torch.backends.cudnn.benchmark = true
        可以增加运行效率;如果网络的输入数据在每次
        iteration都变化的话,会导致cudnn
        每次都会去寻找一遍最优配置,这样反而会降低运行效率。
        '''
        # declare instance for GAN
    if args.gan_type == 'GAN':
        print("GAN is "+args.gan_type)
        gan = GAN(args)
    elif args.gan_type == 'CGAN':
        print("GAN is " + args.gan_type)
        gan = CGAN(args)
    elif args.gan_type == 'ACGAN':
        print("GAN is " + args.gan_type)
        gan = ACGAN(args)
    elif args.gan_type == 'infoGAN':
        print("GAN is " + args.gan_type)
        gan = infoGAN(args, SUPERVISED=False)
    elif args.gan_type == 'EBGAN':
        print("GAN is " + args.gan_type)
        gan = EBGAN(args)
    elif args.gan_type == 'WGAN':
        print("GAN is " + args.gan_type)
        gan = WGAN(args)
    elif args.gan_type == 'WGAN_GP':
        print("GAN is " + args.gan_type)
        gan = WGAN_GP(args)
    elif args.gan_type == 'DRAGAN':
        print("GAN is " + args.gan_type)
        gan = DRAGAN(args)
    elif args.gan_type == 'LSGAN':
        print("GAN is " + args.gan_type)
        gan = LSGAN(args)
    elif args.gan_type == 'BEGAN':
        print("GAN is " + args.gan_type)
        gan = BEGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session
    gan.train()
    print(" [*] Training finished!")

    # visualize learned generator
    gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
Example #8
0
def main(_):
    check_dir()
    print_config()
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
    run_option = tf.ConfigProto(gpu_options=gpu_options)
    with tf.Session(config=run_option) as sess:
        wgan = WGAN(config=FLAGS, sess=sess)
        wgan.build_model()
        if FLAGS.is_training:
            wgan.train_model()
        if FLAGS.is_testing:
            wgan.test_model()
Example #9
0
def main(_):
    args = parser.parse_args()
    if args is None:
        exit()

    num_mixtures = 8
    radius = 2.0
    std = 0.02
    thetas = np.linspace(0, 2 * np.pi, num_mixtures + 1)[:num_mixtures]
    xs, ys = radius * np.sin(thetas), radius * np.cos(thetas)

    if args.gan_type == 'D2GAN':
        model = D2GAN(num_z=args.num_z,
                      hidden_size=args.hidden_size,
                      alpha=args.alpha,
                      beta=args.beta,
                      mix_coeffs=tuple([1 / num_mixtures] * num_mixtures),
                      mean=tuple(zip(xs, ys)),
                      cov=tuple([(std, std)] * num_mixtures),
                      batch_size=args.batch_size,
                      learning_rate=args.learning_rate,
                      num_epochs=args.num_epochs,
                      result_dir=args.result_dir,
                      disp_freq=args.disp_freq,
                      random_seed=6789)
    elif args.gan_type == 'GAN':
        model = GAN(num_z=args.num_z,
                    hidden_size=args.hidden_size,
                    alpha=args.alpha,
                    beta=args.beta,
                    mix_coeffs=tuple([1 / num_mixtures] * num_mixtures),
                    mean=tuple(zip(xs, ys)),
                    cov=tuple([(std, std)] * num_mixtures),
                    batch_size=args.batch_size,
                    learning_rate=args.learning_rate,
                    num_epochs=args.num_epochs,
                    result_dir=args.result_dir,
                    disp_freq=args.disp_freq,
                    random_seed=6789)
    elif args.gan_type == 'WGAN':
        model = WGAN(num_z=args.num_z,
                     hidden_size=args.hidden_size,
                     alpha=args.alpha,
                     beta=args.beta,
                     mix_coeffs=tuple([1 / num_mixtures] * num_mixtures),
                     mean=tuple(zip(xs, ys)),
                     cov=tuple([(std, std)] * num_mixtures),
                     batch_size=args.batch_size,
                     learning_rate=args.learning_rate,
                     num_epochs=args.num_epochs,
                     result_dir=args.result_dir,
                     disp_freq=args.disp_freq,
                     random_seed=6789)
    model.fit()
def main():

    # open session
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        # declare instance for GAN
        classes = ['angry']
        gan = WGAN(classes=classes,
                   sess=sess,
                   epoch=EPOCH,
                   batch_size=BATCH_SIZE,
                   z_dim=Z_DIM,
                   dataset_name=DATASET,
                   augmentation=False,
                   aug_ratio=12,
                   checkpoint_dir=CHECKPOINT_DIR,
                   result_dir=RESULT_DIR,
                   log_dir=LOG_DIR)

        # build graph
        gan.build_model()

        # show network architecture
        # show_all_variables()

        # launch the graph in a session
        gan.train()
        print(" [*] Training finished!")

        # visualize learned generator
        # gan.visualize_results(EPOCH - 1)
        print(" [*] Testing finished!")
Example #11
0
def train_wgan_mnist(X_train, z_dim, n_hidden, opt_gen, opt_dis, activation, epoch, batch_size, k):
    sqrtbs = int(batch_size**0.5)

    rng = np.random.RandomState(1)

    generator = Sequential(z_dim, rng)
    generator.add(BatchNormalization(Dense(n_hidden, init=normal(0, 0.001))))
    generator.add(Activation(activation))
    generator.add(BatchNormalization(Dense(n_hidden, init=normal(0, 0.001))))
    generator.add(Activation(activation))
    generator.add(Dense(X_train.shape[1], init=normal(0, 0.001)))
    generator.add(Activation('tanh'))

    discriminator = Sequential(X_train.shape[1], rng)
    discriminator.add(Dense(n_hidden, init=normal(0, 0.001)))
    discriminator.add(Activation(activation))
    discriminator.add(Dense(n_hidden, init=normal(0, 0.001)))
    discriminator.add(Activation(activation))
    discriminator.add(Dense(100, init=normal(0, 0.001)))

    gan = WGAN(rng, generator=generator, discriminator=discriminator, clipping=(-0.05, 0.05))
    gan.compile(opt_gen=opt_gen, opt_dis=opt_dis)

    z_plot = np.random.standard_normal((batch_size, z_dim)).astype(np.float32)
    z_batch = BatchIterator([z_plot], batch_size)
    train_batches = BatchIterator([X_train*2. - 1.], batch_size, shuffle=True)

    # training
    for i in xrange(epoch/10):
        print('epoch:{0}'.format(i+1))
        gan.fit(train_batches, epoch=10*k, iprint=True, k=k)
        generation = 127.5 * (generator.predict(z_batch)+1.)
        saveimg(generation.reshape(batch_size, 28, 28), (sqrtbs, sqrtbs),
                'imgs/WGAN/WGAN_MNIST_epoch' + str((i + 1)*10) + '.png')
        print(np.max(np.abs(discriminator.layers[0].W.get_value())))

    return gan
Example #12
0
def main():
    with tf.Session() as sess:
        gan = WGAN(sess,
                   epoch=10000,
                   batch_size=16,
                   dataset_name='fire2.tfrecords',
                   checkpoint_dir='checkpoint',
                   result_dir='results',
                   log_dir='logs')
        # build graph

        gan.build_model()
        show_all_variables()
        gan.train()
        print(" [*] Training finished!")
        gan.visualize_results(20 - 1)
        print(" [*] Testing finished!")
Example #13
0
def main():
    """main"""

    # parse arguments

    args = parse_args()
    print('Training {},started at {}'.format(
        args.gan_type, time.asctime(time.localtime(time.time()))))

    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    # elif args.gan_type == 'CGAN':
    #     gan = CGAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    # elif args.gan_type == 'infoGAN':
    #     gan = infoGAN(args, SUPERVISED=False)
    # elif args.gan_type == 'EBGAN':
    #     gan = EBGAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'WGAN_GP':
        gan = WGAN_GP(args)
    # elif args.gan_type == 'DRAGAN':
    #     gan = DRAGAN(args)
    elif args.gan_type == 'LSGAN':
        gan = LSGAN(args)
    elif args.gan_type == 'BEGAN':
        gan = BEGAN(args)
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

        # launch the graph in a session

    # return
    gan.train()
    print('Training {},finished at {}'.format(
        args.gan_type, time.asctime(time.localtime(time.time()))))
Example #14
0
def main():

    # Parsing des arguments
    args = parse_args()

    # Initialisation du generateur
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'InfoGAN':
        gan = infoGAN(args)
        # Lancer l'entrainement
    gan.train()
    print("Entrainement terminé")

        # Visualiser les resultats
    print("Tout est fini")
Example #15
0
File: main.py Project: laur0312/GAN
def main(_):
    pprint.PrettyPrinter().pprint(flags.FLAGS.__flags)

    if not os.path.exists(FLAGS.result_dir):
        os.makedirs(FLAGS.result_dir)

    if not os.path.isdir(FLAGS.model_dir):
        os.makedirs(FLAGS.model_dir)

    run_config = tf.ConfigProto()
    run_config.gpu_options.allow_growth = True

    with tf.Session(config=run_config) as sess:
        if FLAGS.model == 'GAN':
            gan = GAN(sess,
                      result_dir=FLAGS.result_dir,
                      model_dir=FLAGS.model_dir)
        elif FLAGS.model == 'DCGAN':
            gan = DCGAN(sess,
                        result_dir=FLAGS.result_dir,
                        model_dir=FLAGS.model_dir)
        elif FLAGS.model == 'CDCGAN':
            gan = CDCGAN(sess,
                         result_dir=FLAGS.result_dir,
                         model_dir=FLAGS.model_dir)
        elif FLAGS.model == 'WGAN':
            gan = WGAN(sess,
                       result_dir=FLAGS.result_dir,
                       model_dir=FLAGS.model_dir)
        else:
            print('Error! Invalid input.')
            exit(0)

        gan.build_model()
        show_all_variables()
        gan.train(FLAGS.epochs)
Example #16
0
def train():

    image_lr = tf.placeholder(dtype=tf.float32,
                              shape=(None, 16, 16, 3),
                              name='lr')
    image_hr = tf.placeholder(dtype=tf.float32,
                              shape=(None, 64, 64, 3),
                              name='hr')

    net = WGAN(gamma)

    gen = net.generator(image_lr, bottleneck_num=2)

    real_score = net.discrimintor(gen)
    fake_score = net.discrimintor(image_hr, reuse=True)

    with tf.name_scope('SR_loss'):

        residual = image_hr - gen
        square = tf.abs(residual)
        SR_loss = tf.reduce_mean(square)

        tf.summary.scalar('SR_loss', SR_loss)
    print('test1')

    with tf.name_scope('gan_loss'):

        D_loss = tf.reduce_mean(fake_score) - tf.reduce_mean(real_score)

        G_loss = -tf.reduce_mean(fake_score)

        tf.summary.scalar('G_loss', G_loss)
        tf.summary.scalar('D_loss', D_loss)

        G_overall_loss = gan_ratio * G_loss + SR_loss  # this part might need modification

    print('test2')

    # get variable from G and D
    var_g = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator')
    var_d = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                              'discriminator')

    with tf.name_scope('optim'):

        optim_g = tf.train.RMSPropOptimizer(learning_rate=LEARNING_RATE)\
            .minimize(G_overall_loss, var_list=var_g)
        optim_d = tf.train.RMSPropOptimizer(learning_rate=LEARNING_RATE) \
            .minimize(-D_loss, var_list=var_d)

    clip_D = [p.assign(tf.clip_by_value(p, -0.01, 0.01)) for p in var_d]

    print('test3')
    # for gradient, var in var_g:
    #     tf.summary.histogram(var.name + '/gradient', gradient)
    #
    # # Add the variables we train to the summary
    # for var in var_g:
    #     tf.summary.histogram(var.name, var)

    # set up logging for tensorboard
    writer = tf.summary.FileWriter(filewriter_path)
    writer.add_graph(tf.get_default_graph())
    summaries = tf.summary.merge_all()

    # saver for storing/restoring checkpoints of the model
    saver = tf.train.Saver()

    data_path = 'train_espcn.tfrecords'

    feature = {
        'train/image_small': tf.FixedLenFeature([], tf.string),
        'train/image_origin': tf.FixedLenFeature([], tf.string)
    }

    # create a list of file names
    filename_queue = tf.train.string_input_producer([data_path],
                                                    num_epochs=NUM_EPOCHS)

    reader = tf.TFRecordReader()
    _, tfrecord_serialized = reader.read(filename_queue)

    features = tf.parse_single_example(tfrecord_serialized, features=feature)

    # Convert the image data from string back to the numbers
    image_blur = tf.decode_raw(features['train/image_small'], tf.uint8)
    image_origin = tf.decode_raw(features['train/image_origin'], tf.uint8)

    image_blur = tf.reshape(image_blur, [32, 32, 3])
    image_origin = tf.reshape(image_origin, [128, 128, 3])

    images, labels = tf.train.shuffle_batch([image_blur, image_origin],
                                            batch_size=BATCH_SIZE,
                                            capacity=30,
                                            num_threads=16,
                                            min_after_dequeue=10)

    images = tf.image.resize_images(images, (16, 16))
    labels = tf.image.resize_images(labels, (64, 64))

    print('test4')

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session() as sess:

        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)

        steps, start_average, end_average = 0, 0, 0
        start_time = time.clock()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for ii in range(NUM_EPOCHS):

            batch_average = 0
            batch_num = int(np.floor(192794 / BATCH_SIZE / 6.0))

            for jj in range(batch_num):

                g_ops = [optim_g, G_loss, summaries]
                d_ops = [optim_d, D_loss]

                for kk in range(critic):

                    steps += 1
                    img_lr, img_hr = sess.run([images, labels])
                    img_lr = (img_lr.astype(np.float32) - 127.5) / 127.5
                    img_hr = (img_hr.astype(np.float32) - 127.5) / 127.5

                    _, loss_d = sess.run(d_ops,
                                         feed_dict={
                                             image_lr: img_lr,
                                             image_hr: img_hr
                                         })

                    sess.run(clip_D)

                steps += 1
                img_lr, img_hr = sess.run([images, labels])
                img_lr = (img_lr.astype(np.float32) - 127.5) / 127.5
                img_hr = (img_hr.astype(np.float32) - 127.5) / 127.5

                _, loss_g, summary = sess.run(g_ops,
                                              feed_dict={
                                                  image_lr: img_lr,
                                                  image_hr: img_hr
                                              })

                # update W_loss and Kt

                writer.add_summary(summary, steps)
                batch_average += loss_g

                if (steps % 100 == 0):
                    print('step: {:d}, G_loss: {:.9f}, D_loss: {:.9f}'.format(
                        steps, loss_g, loss_d))
                    print('time:', time.clock())

            batch_average = float(batch_average) / batch_num

            duration = time.time() - start_time
            print('Epoch: {}, step: {:d}, loss: {:.9f}, '
                  '({:.3f} sec/epoch)'.format(ii, steps, batch_average,
                                              duration))

            start_time = time.time()
            net.save(sess, saver, checkpoint_path, steps)
        coord.request_stop()

        # Wait for threads to stop
        coord.join(threads)
        sess.close()
Example #17
0
def train():

    image_lr = tf.placeholder(dtype=tf.float32,
                              shape=(BATCH_SIZE, 16, 16, 3),
                              name='lr')
    image_hr = tf.placeholder(dtype=tf.float32,
                              shape=(BATCH_SIZE, 64, 64, 3),
                              name='hr')
    #sigma = tf.placeholder(dtype=tf.float32, name='sigma')
    net = WGAN(gamma)

    gen = net.generator(image_lr, bottleneck_num=2)

    real_score = net.discrimintor(gen)
    fake_score = net.discrimintor(image_hr, reuse=True)

    with tf.name_scope('SR_loss'):

        residual = image_hr - gen
        square = tf.abs(residual)
        SR_loss = tf.reduce_mean(square)

        tf.summary.scalar('SR_loss', SR_loss)
    print('test1')

    with tf.name_scope('gan_loss'):

        D_loss = tf.reduce_mean(fake_score) - tf.reduce_mean(real_score)

        G_loss = -tf.reduce_mean(fake_score)

        def interpolate(a, b):
            shape = tf.concat(
                (tf.shape(a)[0:1], tf.tile([1], [a.shape.ndims - 1])), axis=0)
            alpha = tf.random_uniform(shape=shape, minval=0., maxval=1.)
            inter = a + alpha * (b - a)
            inter.set_shape(a.get_shape().as_list())
            return inter

        gp_sample = interpolate(gen, image_hr)

        # sigma = tf.random_uniform(
        #     shape=[BATCH_SIZE, 1],
        #     minval=0.,
        #     maxval=1.
        # )
        #
        # gp_sample = gen*sigma + image_hr*(1 - sigma)

        #gp_sample = tf.reshape(gp_sample, [-1, 128, 128, 3])

        print(gen.get_shape(), 'test2')

        print(image_hr.get_shape())

        print(gp_sample.get_shape())

        gp_gradient = tf.gradients(net.discrimintor(gp_sample, reuse=True),
                                   gp_sample)

        grad_norm = tf.sqrt(
            tf.reduce_sum(tf.square(gp_gradient[0]), reduction_indices=[-1]))

        gp_loss = tf.reduce_mean(tf.square(grad_norm - 1.))

        D_overall_loss = D_loss + gp_rate * gp_loss

        tf.summary.scalar('G_loss', (G_loss))
        tf.summary.scalar('D_loss', (D_loss))
        tf.summary.scalar('GP_loss', gp_loss)

        G_overall_loss = gan_ratio * G_loss + SR_loss  # this part might need modification

    print('test2')

    # get variable from G and D
    var_g = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator')
    var_d = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                              'discriminator')

    with tf.name_scope('optim'):

        optim_g = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE, beta1=0.5, beta2=0.9)\
            .minimize(G_overall_loss, var_list=var_g)
        optim_d = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE, beta1=0.5, beta2=0.9) \
            .minimize(D_overall_loss, var_list=var_d)

    # gradient penalty

    print('test3')
    # for gradient, var in var_g:
    #     tf.summary.histogram(var.name + '/gradient', gradient)
    #
    # # Add the variables we train to the summary
    # for var in var_g:
    #     tf.summary.histogram(var.name, var)

    # set up logging for tensorboard
    writer = tf.summary.FileWriter(filewriter_path)
    writer.add_graph(tf.get_default_graph())
    summaries = tf.summary.merge_all()

    # saver for storing/restoring checkpoints of the model
    saver = tf.train.Saver()

    data_path = 'train_espcn.tfrecords'

    feature = {
        'train/image_small': tf.FixedLenFeature([], tf.string),
        'train/image_origin': tf.FixedLenFeature([], tf.string)
    }

    # create a list of file names
    filename_queue = tf.train.string_input_producer([data_path],
                                                    num_epochs=NUM_EPOCHS)

    reader = tf.TFRecordReader()
    _, tfrecord_serialized = reader.read(filename_queue)

    features = tf.parse_single_example(tfrecord_serialized, features=feature)

    # Convert the image data from string back to the numbers
    image_blur = tf.decode_raw(features['train/image_small'], tf.uint8)
    image_origin = tf.decode_raw(features['train/image_origin'], tf.uint8)

    image_blur = tf.reshape(image_blur, [32, 32, 3])
    image_origin = tf.reshape(image_origin, [128, 128, 3])

    images, labels = tf.train.shuffle_batch([image_blur, image_origin],
                                            batch_size=BATCH_SIZE,
                                            capacity=30,
                                            num_threads=16,
                                            min_after_dequeue=10)

    images = tf.image.resize_images(images, (16, 16))
    labels = tf.image.resize_images(labels, (64, 64))

    print('test4')
    loss_d = 0

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session() as sess:

        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)

        steps, start_average, end_average = 0, 0, 0
        start_time = time.clock()

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for ii in range(NUM_EPOCHS):

            batch_average = 0
            batch_num = int(np.floor(192794 / BATCH_SIZE / 6.0))

            for jj in range(batch_num):

                g_ops = [optim_g, G_overall_loss]
                d_ops = [optim_d, D_overall_loss]

                for kk in range(critic):

                    steps += 1

                    img_lr, img_hr = sess.run([images, labels])
                    img_lr = (img_lr.astype(np.float32) - 127.5) / 127.5
                    img_hr = (img_hr.astype(np.float32) - 127.5) / 127.5

                    _, loss_d = sess.run(d_ops,
                                         feed_dict={
                                             image_lr: img_lr,
                                             image_hr: img_hr
                                         })

                steps += 1

                img_lr, img_hr = sess.run([images, labels])
                img_lr = (img_lr.astype(np.float32) - 127.5) / 127.5
                img_hr = (img_hr.astype(np.float32) - 127.5) / 127.5

                _, loss_g = sess.run(g_ops,
                                     feed_dict={
                                         image_lr: img_lr,
                                         image_hr: img_hr
                                     })

                if steps % 10 == 0:
                    summary = sess.run(summaries,
                                       feed_dict={
                                           image_lr: img_lr,
                                           image_hr: img_hr
                                       })
                    writer.add_summary(summary, steps)

                batch_average += loss_g

                if (steps % 100 == 0):
                    print('step: {:d}, G_loss: {:.9f}, D_loss: {:.9f}'.format(
                        steps, loss_g, loss_d))
                    print('time:', time.clock())

            batch_average = float(batch_average) / batch_num

            duration = time.time() - start_time
            print('Epoch: {}, step: {:d}, loss: {:.9f}, '
                  '({:.3f} sec/epoch)'.format(ii, steps, batch_average,
                                              duration))

            start_time = time.time()
            net.save(sess, saver, checkpoint_path, steps)
        coord.request_stop()

        # Wait for threads to stop
        coord.join(threads)
        sess.close()
Example #18
0
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    # open session
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        # declare instance for GAN
        if args.gan_type == 'GAN':
            gan = GAN(sess,
                      epoch=args.epoch,
                      batch_size=args.batch_size,
                      z_dim=args.z_dim,
                      dataset_name=args.dataset,
                      checkpoint_dir=args.checkpoint_dir,
                      result_dir=args.result_dir,
                      log_dir=args.log_dir)
        elif args.gan_type == 'CGAN':
            gan = CGAN(sess,
                       epoch=args.epoch,
                       batch_size=args.batch_size,
                       z_dim=args.z_dim,
                       dataset_name=args.dataset,
                       checkpoint_dir=args.checkpoint_dir,
                       result_dir=args.result_dir,
                       log_dir=args.log_dir)
        elif args.gan_type == 'ACGAN':
            gan = ACGAN(sess,
                        epoch=args.epoch,
                        batch_size=args.batch_size,
                        z_dim=args.z_dim,
                        dataset_name=args.dataset,
                        checkpoint_dir=args.checkpoint_dir,
                        result_dir=args.result_dir,
                        log_dir=args.log_dir)
        elif args.gan_type == 'infoGAN':
            gan = infoGAN(sess,
                          epoch=args.epoch,
                          batch_size=args.batch_size,
                          z_dim=args.z_dim,
                          dataset_name=args.dataset,
                          checkpoint_dir=args.checkpoint_dir,
                          result_dir=args.result_dir,
                          log_dir=args.log_dir)
        elif args.gan_type == 'EBGAN':
            gan = EBGAN(sess,
                        epoch=args.epoch,
                        batch_size=args.batch_size,
                        z_dim=args.z_dim,
                        dataset_name=args.dataset,
                        checkpoint_dir=args.checkpoint_dir,
                        result_dir=args.result_dir,
                        log_dir=args.log_dir)
        elif args.gan_type == 'WGAN':
            gan = WGAN(sess,
                       epoch=args.epoch,
                       batch_size=args.batch_size,
                       z_dim=args.z_dim,
                       dataset_name=args.dataset,
                       checkpoint_dir=args.checkpoint_dir,
                       result_dir=args.result_dir,
                       log_dir=args.log_dir)
        elif args.gan_type == 'WGAN_GP':
            gan = WGAN_GP(sess,
                          epoch=args.epoch,
                          batch_size=args.batch_size,
                          z_dim=args.z_dim,
                          dataset_name=args.dataset,
                          checkpoint_dir=args.checkpoint_dir,
                          result_dir=args.result_dir,
                          log_dir=args.log_dir)
        elif args.gan_type == 'DRAGAN':
            gan = DRAGAN(sess,
                         epoch=args.epoch,
                         batch_size=args.batch_size,
                         z_dim=args.z_dim,
                         dataset_name=args.dataset,
                         checkpoint_dir=args.checkpoint_dir,
                         result_dir=args.result_dir,
                         log_dir=args.log_dir)
        elif args.gan_type == 'LSGAN':
            gan = LSGAN(sess,
                        epoch=args.epoch,
                        batch_size=args.batch_size,
                        z_dim=args.z_dim,
                        dataset_name=args.dataset,
                        checkpoint_dir=args.checkpoint_dir,
                        result_dir=args.result_dir,
                        log_dir=args.log_dir)
        elif args.gan_type == 'BEGAN':
            gan = BEGAN(sess,
                        epoch=args.epoch,
                        batch_size=args.batch_size,
                        z_dim=args.z_dim,
                        dataset_name=args.dataset,
                        checkpoint_dir=args.checkpoint_dir,
                        result_dir=args.result_dir,
                        log_dir=args.log_dir)
        elif args.gan_type == 'VAE':
            gan = VAE(sess,
                      epoch=args.epoch,
                      batch_size=args.batch_size,
                      z_dim=args.z_dim,
                      dataset_name=args.dataset,
                      checkpoint_dir=args.checkpoint_dir,
                      result_dir=args.result_dir,
                      log_dir=args.log_dir)
        elif args.gan_type == 'CVAE':
            gan = CVAE(sess,
                       epoch=args.epoch,
                       batch_size=args.batch_size,
                       z_dim=args.z_dim,
                       dataset_name=args.dataset,
                       checkpoint_dir=args.checkpoint_dir,
                       result_dir=args.result_dir,
                       log_dir=args.log_dir)
        elif args.gan_type == 'VAE_GAN':
            gan = VAE_GAN(sess,
                          epoch=args.epoch,
                          batch_size=args.batch_size,
                          z_dim=args.z_dim,
                          dataset_name=args.dataset,
                          checkpoint_dir=args.checkpoint_dir,
                          result_dir=args.result_dir,
                          log_dir=args.log_dir)
        else:
            raise Exception("[!] There is no option for " + args.gan_type)

        # build graph
        gan.build_model()

        # show network architecture
        show_all_variables()

        # launch the graph in a session
        gan.train()
        print(" [*] Training finished!")

        # visualize learned generator
        gan.visualize_results(args.epoch - 1)
        print(" [*] Testing finished!")
Example #19
0
    x = tf.image.random_brightness(x, 0.05)
    x = tf.image.random_contrast(x, 0.7, 1.3)
    return x


real_ds = tf.data.Dataset.from_tensor_slices(x_train)
real_ds = real_ds.shuffle(60000)
real_ds = real_ds.repeat()
real_ds = real_ds.apply(
    tf.data.experimental.map_and_batch(preprocess_fn,
                                       64,
                                       num_parallel_batches=6,
                                       drop_remainder=True))
real_ds = real_ds.prefetch(tf.data.experimental.AUTOTUNE)

wgan = WGAN(real_ds)

for step in range(0, 15001):
    for i in range(0, 5):
        wgan.C_train_on_batch()
        wgan.clip_weights()
    wgan.G_train_on_batch()

    if step % 50 == 0:
        pass
    print(f"Step: {step}")

import numpy as np
import random
import matplotlib.pyplot as plt
noise = np.random.uniform(-1.0, 1.0, size=(
Example #20
0
def main():
    # parse arguments
    opts = parse_args()
    if opts is None:
        exit()

        # declare instance for GAN
    if opts.gan_type == 'GAN':
        gan = GAN(opts)
    elif opts.gan_type == 'CGAN':
        gan = CGAN(opts)
    elif opts.gan_type == 'ACGAN':
        gan = ACGAN(opts)
    elif opts.gan_type == 'infoGAN':
        gan = infoGAN(opts, SUPERVISED=True)
    elif opts.gan_type == 'EBGAN':
        gan = EBGAN(opts)
    elif opts.gan_type == 'WGAN':
        gan = WGAN(opts)
    elif opts.gan_type == 'WGAN_GP':
        gan = WGAN_GP(opts)
    elif opts.gan_type == 'DRAGAN':
        gan = DRAGAN(opts)
    elif opts.gan_type == 'LSGAN':
        gan = LSGAN(opts)
    elif opts.gan_type == 'BEGAN':
        gan = BEGAN(opts)
    elif opts.gan_type == 'DRGAN':
        gan = DRGAN(opts)
    elif opts.gan_type == 'AE':
        gan = AutoEncoder(opts)
    elif opts.gan_type == 'GAN3D':
        gan = GAN3D(opts)
    elif opts.gan_type == 'VAEGAN3D':
        gan = VAEGAN3D(opts)
    elif opts.gan_type == 'DRGAN3D':
        gan = DRGAN3D(opts)
    elif opts.gan_type == 'Recog3D':
        gan = Recog3D(opts)
    elif opts.gan_type == 'Recog2D':
        gan = Recog2D(opts)
    elif opts.gan_type == 'VAEDRGAN3D':
        gan = VAEDRGAN3D(opts)
    elif opts.gan_type == 'DRcycleGAN3D':
        gan = DRcycleGAN3D(opts)
    elif opts.gan_type == 'CycleGAN3D':
        gan = CycleGAN3D(opts)
    elif opts.gan_type == 'AE3D':
        gan = AutoEncoder3D(opts)
    elif opts.gan_type == 'DRGAN2D':
        gan = DRGAN2D(opts)
    elif opts.gan_type == 'DRecon3DGAN':
        gan = DRecon3DGAN(opts)
    elif opts.gan_type == 'DRecon2DGAN':
        gan = DRecon2DGAN(opts)
    elif opts.gan_type == 'DReconVAEGAN':
        gan = DReconVAEGAN(opts)
    else:
        raise Exception("[!] There is no option for " + opts.gan_type)

    if opts.resume or len(opts.eval) > 0:
        print(" [*] Loading saved model...")
        gan.load()
        print(" [*] Loading finished!")

    # launch the graph in a session
    if len(opts.eval) == 0:
        gan.train()
        print(" [*] Training finished!")
    else:
        print(" [*] Training skipped!")

    # visualize learned generator
    if len(opts.eval) == 0:
        print(" [*] eval mode is not specified!")
    else:
        if opts.eval == 'generate':
            gan.visualize_results(opts.epoch)
        elif opts.eval == 'interp_z':
            gan.interpolate_z(opts)
        elif opts.eval == 'interp_id':
            gan.interpolate_id(opts)
        elif opts.eval == 'interp_expr':
            gan.interpolate_expr(opts)
        elif opts.eval == 'recon':
            gan.reconstruct()
        elif opts.eval == 'control_expr':
            gan.control_expr()
        else:
            gan.manual_inference(opts)
        print(" [*] Testing finished!")
from WGAN import WGAN
import time
import cv2
import glob


BATCH_SIZE = 20
NUM_EPOCHS = 10
LEARNING_RATE = 0.0001
filewriter_path = "./WGAN_v1/filewriter"
checkpoint_path = "./WGAN_v1"
RATIO = 4
BOTTLENECK_NUM = 5
gamma = 0.5

net = WGAN(gamma)
data_path = 'train_espcn.tfrecords'

lr_image = tf.placeholder(tf.float32, shape=[None, 16, 16, 3])

sr_image = net.generate(lr_image)

# saver for storing/restoring checkpoints of the model
saver = tf.train.Saver()

feature = {'train/image_small': tf.FixedLenFeature([], tf.string),
           'train/image_origin': tf.FixedLenFeature([], tf.string)}

# create a list of file names
filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
Example #22
0
def main():
    # parse arguments
    args = parse_args()
    if args is None:
        exit()

    if args.benchmark_mode:
        torch.backends.cudnn.benchmark = True

        # declare instance for GAN
    if args.gan_type == 'GAN':
        gan = GAN(args)
    elif args.gan_type == 'CGAN':
        gan = CGAN(args)
    elif args.gan_type == 'ACGAN':
        gan = ACGAN(args)
    elif args.gan_type == 'infoGAN':
        gan = infoGAN(args, SUPERVISED=False)
    elif args.gan_type == 'EBGAN':
        gan = EBGAN(args)
    elif args.gan_type == 'WGAN':
        gan = WGAN(args)
    elif args.gan_type == 'WGAN_GP':
        gan = WGAN_GP(args)
    elif args.gan_type == 'DRAGAN':
        gan = DRAGAN(args)
    elif args.gan_type == 'LSGAN':
        gan = LSGAN(args)
    elif args.gan_type == 'BEGAN':
        gan = BEGAN(args)
    elif args.gan_type == 'TOGAN':
        gan = TOGAN(args)
    elif args.gan_type == 'CVAE':
        gan = CVAE(args)
    elif args.gan_type == None:
        pass
    else:
        raise Exception("[!] There is no option for " + args.gan_type)

    if args.use_fake_data:
        fakedata = gan.load()
    else:
        fakedata = None

    if args.clf_type == 'clf':
        clf = CLF(args, fakedata)
        clf.load()
        # clf.train()

    else:
        gan.train()
        # gan()
        # gan.load()

    # launch the graph in a session
    # clf.train()
    print(" [*] Training finished!")

    # visualize learned generator
    # gan.visualize_results(args.epoch)
    print(" [*] Testing finished!")
Example #23
0
from utils import check_folder
import tensorflow as tf
import argparse

desc = "dimension"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--dim', type=int, help='input dimension')
args = parser.parse_args()
dim = args.dim

with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:

    gan = WGAN(sess,
               epoch=20,
               batch_size=128,
               z_dim=dim,
               dataset_name='fashion-mnist',
               checkpoint_dir='checkpoints',
               result_dir='results',
               log_dir='logs')
    if gan is None:
        raise Exception("[!] There is no option for " + args.gan_type)

    # build graph
    gan.build_model()

    # show network architecture
    show_all_variables()

    # launch the graph in a session
    gan.train()
    print(" [*] Training finished!")