示例#1
0
def train():
    identifier = id()

    # directory 作成
    os.mkdir(identifier)

    # generator 単体で学習
    G = generator()
    G.compile(optimizer=Adam(lr=3e-04), loss='mae', metrics=['mae'])

    encoder = Model(inputs=G.get_input_at(0),
                    outputs=G.get_layer(name='code').output)

    # 学習する画像の準備
    X = get_training_image(250000)
    np.random.shuffle(X)

    # training-set, test-set を分割
    X_size = int(len(X))
    train_size = int(X_size * 0.8)
    test_size = X_size - train_size
    X_train = X[0:train_size, :, :, :]
    X_test = X[train_size:train_size + test_size, :, :, :]

    epochs = 400000
    batch_size = 16
    num_batch = int(train_size / batch_size)

    # TODO 調節
    checkpoint = 10

    for epoch in range(epochs):
        start_time = time.time()

        batch_x = 0
        for _ in range(num_batch):
            lb = batch_x
            ub = min([batch_x + batch_size, train_size - 1])
            G.train_on_batch(X_train[lb:ub, :, :, :], X_train[lb:ub, :, :, :])
            batch_x += batch_size

        # 訓練/テスト誤差の計算
        train_score = G.evaluate(x=X_train, y=X_train, verbose=0)
        test_score = G.evaluate(x=X_test, y=X_test, verbose=0)

        now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        elapsed = time.time() - start_time

        print("train epoch %06d: train loss %f: test loss %f: %.fs: %s" %
              (epoch, train_score[0], test_score[0], elapsed, now))

        if epoch % checkpoint == 0 and epoch > 0:
            filename = "%s/input-predict-%06d.png" % (identifier, epoch)
            Y_pred = G.predict(X_train[0:1, :, :, :])
            scipy.misc.imsave(filename, np.concatenate([X_train[0],
                                                        Y_pred[0]]))
            # モデルの保存
            G.save("%s/G.h5" % (identifier))
示例#2
0
 def generator(self, x_init, reuse=False, scope="generator"):
     if self.light:
         with tf.variable_scope(scope, reuse=reuse):
             G = generator_lite(x_init)
             return G.fake
     else:
         with tf.variable_scope(scope, reuse=reuse):
             G = generator(x_init)
             return G.fake
示例#3
0
def create_model(inputs, targets):
    def create_discriminator(discrim_inputs, discrim_targets):
        n_layers = 3
        layers = []

        # 2x [batch, height, width, in_channels] => [batch, height, width, in_channels * 2]
        input = tf.concat([discrim_inputs, discrim_targets], axis=3)

        # layer_1: [batch, 256, 256, in_channels * 2] => [batch, 128, 128, ndf]
        with tf.variable_scope("layer_1"):
            convolved = discrim_conv(input, a.ndf, stride=2)
            rectified = lrelu(convolved, 0.2)
            layers.append(rectified)

        # layer_2: [batch, 128, 128, ndf] => [batch, 64, 64, ndf * 2]
        # layer_3: [batch, 64, 64, ndf * 2] => [batch, 32, 32, ndf * 4]
        # layer_4: [batch, 32, 32, ndf * 4] => [batch, 31, 31, ndf * 8]
        for i in range(n_layers):
            with tf.variable_scope("layer_%d" % (len(layers) + 1)):
                out_channels = a.ndf * min(2**(i + 1), 8)
                stride = 1 if i == n_layers - 1 else 2  # last layer here has stride 1
                convolved = discrim_conv(layers[-1],
                                         out_channels,
                                         stride=stride)
                normalized = batchnorm(convolved)
                rectified = lrelu(normalized, 0.2)
                layers.append(rectified)

        # layer_5: [batch, 31, 31, ndf * 8] => [batch, 30, 30, 1]
        with tf.variable_scope("layer_%d" % (len(layers) + 1)):
            convolved = discrim_conv(rectified, out_channels=1, stride=1)
            output = tf.sigmoid(convolved)
            layers.append(output)

        return layers[-1]

    with tf.variable_scope("generator"):
        out_channels = int(targets.get_shape()[-1])
        outputs = generator(inputs)

    # create two copies of discriminator, one for real pairs and one for fake pairs
    # they share the same underlying variables
    with tf.name_scope("real_discriminator"):
        with tf.variable_scope("discriminator"):
            # 2x [batch, height, width, channels] => [batch, 30, 30, 1]
            predict_real = create_discriminator(inputs, targets)

    with tf.name_scope("fake_discriminator"):
        with tf.variable_scope("discriminator", reuse=True):
            # 2x [batch, height, width, channels] => [batch, 30, 30, 1]
            predict_fake = create_discriminator(inputs, outputs)
示例#4
0
def predict():
    cropsize = args.crop_size
    img_string = tf.compat.v1.read_file(args.input_dir)
    flash_input = tf.compat.v1.image.decode_image(img_string)
    flash_input = tf.image.convert_image_dtype(flash_input, dtype=tf.float32)
    begin = (int((args.img_h - cropsize) / 2), int(
        (args.img_w - cropsize) / 2), 0)
    flash_crop = tf.slice(flash_input, begin, [cropsize, cropsize, 3])
    flash_crop = tf.reshape(tf.expand_dims(flash_crop**2.2, axis=0),
                            [1, cropsize, cropsize, 3])
    latentcode = net.latentz_encoder(flash_crop, True)
    predictions = deprocess(net.generator(latentcode, True))
    wv, inten = net.generate_vl(cropsize * 2, cropsize * 2)
    rerender = net.CTRender(predictions, wv, wv)
    display_fetches = save_outputs(predictions, flash_crop, rerender)
    return display_fetches
示例#5
0
def main():
    parser = argparse.ArgumentParser(description='waveone flavor codec')
    parser.add_argument('--gmodel',
                        '-g',
                        help='Generator model (.h5)',
                        required=True)
    parser.add_argument('--lrmodel',
                        '-l',
                        help='LinearRegression model (.pkl)',
                        required=True)
    parser.add_argument('--mode',
                        '-m',
                        type=int,
                        default=0,
                        help='Codec mode [enc=0, dec=1]')
    parser.add_argument('--input_path',
                        '-i',
                        help='Input image or Input code',
                        required=True)
    parser.add_argument('--output_path',
                        '-o',
                        help='Output image or Output code',
                        required=True)

    args = parser.parse_args()

    mode = args.mode
    gmodel = args.gmodel
    lrmodel = args.lrmodel
    input_path = args.input_path
    output_path = args.output_path

    G = generator()
    G.load_weights(gmodel)
    with open(lrmodel, "rb") as f:
        LR = pickle.load(f)

    if mode == 0:
        encode(G, LR, input_path, output_path)
    else:
        decode(G, LR, input_path, output_path)
示例#6
0
def test():
    if not os.path.exists(args.test_output):
        os.makedirs(args.test_output)

    datalists = make_test_data_list(args.test_data_path)
    test_image = tf.placeholder(
        tf.float32,
        shape=[1, args.image_size[0], args.image_size[1], 3],
        name='test_image')

    fake = generator(image=test_image, reuse=False, name='generator')

    restore_var = [v for v in tf.global_variables() if 'generator' in v.name]

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

    saver = tf.train.Saver(var_list=restore_var, max_to_keep=1)
    checkpoint = tf.train.latest_checkpoint(args.snapshot_dir)
    saver.restore(sess, checkpoint)

    total_step = len(datalists)
    for step in range(total_step):
        test_image_name, test_img = TestImageReader(datalists, step,
                                                    args.image_size[1],
                                                    args.image_size[0])
        batch_image = np.expand_dims(np.array(test_img).astype(np.float32),
                                     axis=0)
        feed_dict = {test_image: batch_image}
        fake_value = sess.run(fake, feed_dict=feed_dict)
        write_image = get_picture(test_img, fake_value)
        write_image_name = args.test_output + "/" + test_image_name + ".jpg"

        cv2.imwrite(write_image_name, write_image)
        print('step {:d}'.format(step))
示例#7
0
def main(FLAGS):
    if not os.path.isdir(FLAGS.data_dir):
        os.makedirs(FLAGS.data_dir)
    download_data(FLAGS.data_dir, FLAGS.train_data, FLAGS.test_data)

    net = GAN(real_size,
              z_size,
              learning_rate,
              alpha=FLAGS.alpha,
              beta1=FLAGS.beta1)
    '''--------Load data--------'''
    train_data = loadmat(FLAGS.data_dir + '/' + FLAGS.train_data)
    test_data = loadmat(FLAGS.data_dir + '/' + FLAGS.test_data)
    dataset = preprocessor.Dataset(train_data, test_data)
    '''--------Build net--------'''
    saver = tf.train.Saver()
    sample_z = np.random.uniform(-1, 1, size=(72, z_size))

    samples, losses = [], []
    steps = 0

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for e in range(FLAGS.epochs):
            for x, y in dataset.batches(FLAGS.batch_size):
                steps += 1

                # Sample random noise for G
                batch_z = np.random.uniform(-1,
                                            1,
                                            size=(FLAGS.batch_size, z_size))

                # Run optimizers
                _ = sess.run(net.d_opt,
                             feed_dict={
                                 net.input_real: x,
                                 net.input_z: batch_z
                             })
                _ = sess.run(net.g_opt,
                             feed_dict={
                                 net.input_z: batch_z,
                                 net.input_real: x
                             })

                if steps % FLAGS.print_every == 0:
                    # At the end of each epoch, get the losses and print them out
                    train_loss_d = net.d_loss.eval({
                        net.input_z: batch_z,
                        net.input_real: x
                    })
                    train_loss_g = net.g_loss.eval({net.input_z: batch_z})

                    print('Epoch {}/{}...'.format(e + 1, FLAGS.epochs),
                          'Discriminator Loss: {:.4f}'.format(train_loss_d),
                          'Generator loss: {:.4f}'.format(train_loss_g))
                    # Save losses to view after traning
                    losses.append((train_loss_d, train_loss_g))

                if steps % FLAGS.show_every == 0:
                    gen_samples = sess.run(generator(net.input_z,
                                                     3,
                                                     reuse=True,
                                                     training=False),
                                           feed_dict={net.input_z: sample_z})
                    samples.append(gen_samples)
                    _ = utils.view_samples(-1,
                                           samples,
                                           6,
                                           12,
                                           figsize=(FLAGS.h_figsize,
                                                    FLAGS.v_figsize))
                    plt.show()

            saver.save(sess, './checkpoints/generator.ckpt')

        with open('samples.pkl', 'wb') as f:
            pkl.dump(samples, f)

        fig, ax = plt.subplot()
        losses = np.array(losses)
        plt.plot(losses.T[0], label='Discriminator', alpha=0.5)
        plt.plot(losses.T[1], label='Generator', alpha=0.5)
        plt.title('Training Losses')
        plt.legend()
        plt.show()
示例#8
0
def train_gan():
    identifier = id()
    dir_path = "%s-gan-w" % identifier
    os.mkdir(dir_path)

    G = generator()
    D = discriminator()
    # TODO optimizer 調節
    D.compile(optimizer=Adam(lr=3e-04),
              loss='binary_crossentropy',
              metrics=['accuracy'])

    # build GAN
    G_input = Input(shape=(128, 128, 3))
    G_output = G(G_input)
    raw_image_input = Input(shape=(128, 128, 3))
    D_output = D([raw_image_input, G_output])
    GAN = Model(inputs=[G_input, raw_image_input],
                outputs=[G_output, D_output])
    gan_loss = ['mae', 'binary_crossentropy']
    gan_loss_weight = [1e2, 1]
    D.trainable = False

    # TODO optimizer 調節
    GAN.compile(optimizer=Adam(lr=1e-06),
                loss=gan_loss,
                loss_weights=gan_loss_weight)

    X = get_training_image(100000)
    X_size = int(len(X) * 0.5)
    train_size = int(X_size * 0.8)
    test_size = X_size - train_size
    X_train = X[0:train_size, :, :, :]
    X_test = X[train_size:train_size + test_size, :, :, :]

    epochs = 400000
    batch_size = 16
    num_batch = int(train_size / batch_size)
    checkpoint = 1

    for epoch in range(epochs):
        start_time = time.time()

        batch_x = 0

        # D の学習を行うか判定
        # acc が一定以上ならば学習しない
        X_test_from_G = G.predict(X_test)
        y_test_D = np.tile([1, 0], [test_size, 1])
        loss_D = D.evaluate([X_test, X_test_from_G], y_test_D, verbose=0)
        acc_D = loss_D[1]
        trainable_D = True
        if acc_D > 0.95:
            trainable_D = False

        for _ in range(num_batch):
            lb = batch_x
            ub = min([batch_x + batch_size, train_size - 1])
            sz = ub - lb

            # D の学習データを用意する
            # G の生成データと生画像データのペアを用意しシャッフルして学習
            X_from_G = G.predict(X_train[lb:ub, :, :, :])
            X_as_Gen = np.zeros(shape=(sz, 128, 128, 3))
            X_as_raw = np.zeros(shape=(sz, 128, 128, 3))
            swap_labels = np.random.randint(2, size=sz)
            y_train_D = np.zeros(shape=(sz, 2))
            for i, bit in enumerate(swap_labels):
                if bit == 0:
                    X_as_Gen[i] = X_from_G[i]
                    X_as_raw[i] = X_train[sz + i]
                    y_train_D[i] = np.asarray([1, 0])
                else:
                    X_as_Gen[i] = X_train[sz + i]
                    X_as_raw[i] = X_from_G[i]
                    y_train_D[i] = np.asarray([0, 1])

            # GAN の学習
            y_train_GAN = np.tile([1, 0], [sz, 1])
            GAN.train_on_batch(
                [X_train[lb:ub, :, :, :], X_train[lb:ub, :, :, :]],
                [X_from_G, y_train_GAN])

            # D の学習
            if trainable_D:
                D.train_on_batch([X_as_raw, X_as_Gen], y_train_D)

            # 状態更新
            batch_x += batch_size

        # logging
        elapsed = time.time() - start_time
        X_test_from_G = G.predict(X_test)
        y_test_D = np.tile([1, 0], [test_size, 1])
        loss_D = D.evaluate([X_test, X_test_from_G], y_test_D, verbose=0)
        loss_GAN = GAN.evaluate([X_test, X_test], [X_test_from_G, y_test_D],
                                verbose=0)
        now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
        print(
            "epoch %06d: D loss %.3f, acc %.3f: GAN loss %.3f, G loss %.3f D loss %.3f: trainable_D %d: %.3fs: %s"
            % (epoch, loss_D[0], loss_D[1], loss_GAN[0], loss_GAN[1],
               loss_GAN[2], trainable_D, elapsed, now))

        # checkpoint
        if epoch > 0 and (epoch % checkpoint == 0):
            # 画像の保存
            pred_image_path = "%s/G-predict-%06d.png" % (dir_path, epoch)
            pred = G.predict(X_test)
            tests = np.hstack(X_test[0:10])
            preds = np.hstack(pred[0:10])
            if epoch == checkpoint:
                imsave("%s/tests-%06d.png" % (dir_path, epoch), tests)
            imsave("%s/preds-%06d.png" % (dir_path, epoch), preds)

            # モデルの保存
            G.save("%s/G.h5" % dir_path)
            D.save("%s/D.h5" % dir_path)
            GAN.save("%s/GAN.h5" % dir_path)
示例#9
0
def main():
    kernel1 = gaussian_kernel(101, 101)
    input_2b_sliced = concat_inputs(args.input_dir, kernel1)
    examples = load_examples(input_2b_sliced, inputsize)

    examples_flashes = examples.concats[:, :, :, 0:3]
    examples_inputs = tf.map_fn(lambda x: tf.image.central_crop(x, 0.5),
                                elems=examples_flashes)
    examples_inputs = tf.reshape(examples_inputs,
                                 [BATCH_SIZE, inputsize, inputsize, 3])
    wv = examples.concats[:, :, :, 3:6]
    #    inten = examples_concats[:,:,:,6:9]
    initd = examples.concats[:, :, :, 9:12]

    latentcode = net.latentz_encoder(examples_inputs)
    predictions = deprocess(net.generator(latentcode))
    net_rerender = net.CTRender(predictions, wv, wv)

    prediffuse = predictions[:, :, :, 3:6]

    dis_real = net.Discriminator_patch(examples_flashes, reuse=False)
    dis_fake = net.Discriminator_patch(net_rerender, reuse=True)
    dis_cost = net.patchGAN_d_loss(dis_fake, dis_real)
    gen_fake = net.patchGAN_g_loss(dis_fake)

    train_vars = tf.compat.v1.trainable_variables()
    #    encoder_vars = [v for v in train_vars if 'en_' in v.name]
    decodernr_vars = [v for v in train_vars if 'denr_' in v.name]
    decoderds_vars = [v for v in train_vars if 'deds_' in v.name]
    discriminator_vars = [v for v in train_vars if 'd_' in v.name]

    gnr_vars = decodernr_vars
    gds_vars = decoderds_vars

    diffuseloss = tf.reduce_mean(tf.abs(prediffuse - initd))
    gnr_cost = ganscale * (gen_fake) + diffuseloss
    gds_cost = ganscale * (gen_fake) + diffuseloss

    gnr_optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=lr,
                                                     name='gnr_opt',
                                                     beta1=0.,
                                                     beta2=0.9).minimize(
                                                         gnr_cost,
                                                         var_list=gnr_vars)
    gds_optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=lr,
                                                     name='gds_opt',
                                                     beta1=0.,
                                                     beta2=0.9).minimize(
                                                         gds_cost,
                                                         var_list=gds_vars)
    d_optimizer = tf.compat.v1.train.AdamOptimizer(
        learning_rate=lr, name='d_opt', beta1=0.,
        beta2=0.9).minimize(dis_cost, var_list=discriminator_vars)

    config = tf.compat.v1.ConfigProto()
    config.gpu_options.allow_growth = True

    sess = tf.compat.v1.Session(config=config)

    g_loss_summary = tf.summary.scalar("g_loss", gnr_cost)
    d_loss_summary = tf.summary.scalar("d_loss", dis_cost)
    train_summary = tf.compat.v1.summary.merge([
        tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.SUMMARIES,
                                    'g_loss'),
        tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.SUMMARIES, 'd_loss')
    ])
    train_writer = tf.compat.v1.summary.FileWriter(args.log_dir, sess.graph)

    saver = tf.compat.v1.train.Saver(max_to_keep=10)
    sess.run([
        examples.iterator.initializer,
        tf.compat.v1.global_variables_initializer()
    ])

    for step in range(args.max_step):

        for i in range(5):
            _, g_loss = sess.run([gnr_optimizer, gnr_cost])
        for i in range(1):
            _, g_loss = sess.run([gds_optimizer, gds_cost])

        _, d_loss = sess.run([d_optimizer, dis_cost])

        if step % 500 == 0 or (step + 1) == args.max_step:
            print('Step %d,  g_loss = %.4f, d_loss = %.4f' %
                  (step, g_loss, d_loss))
            summary_str = sess.run(train_summary)
            train_writer.add_summary(summary_str, step)
        if step % 1000 == 0 or (step + 1) == args.max_step:
            checkpoint_path = os.path.join(args.log_dir, 'model.ckpt')
            saver.save(sess, checkpoint_path, global_step=step)
            display_fetches = save_outputs(predictions, examples_flashes,
                                           net_rerender)
            results = sess.run(display_fetches)
            save_images(results, args.output_dir, step, "training_tiles")

            prediction_fetches = predict()
            predict_maps = sess.run(prediction_fetches)
            save_images(predict_maps, args.output_dir, step, "predicted_maps")
示例#10
0
clip_value = [-0.01,0.01]

tf.reset_default_graph()
image_record = data.readRecord('../data/train_clean.tfrecords')
train_from_checkpoint = True
checkpoint_dir = "../model/20190110-090118_ft_from_20190109-090711/"
stddev_scheme = [0] if train_from_checkpoint else [1e-3]#[ii*1e-5 for ii in range(50,0,-1)]+[0] #[0.01,0.009,...,0.001]
scheme_step = 2000


## define input
real_image = tf.placeholder(tf.float32, (batch_size,64,64,3))
inptG = tf.placeholder(tf.float32, (batch_size, noise_size))
gn_stddev = tf.placeholder(tf.float32, [])
training = tf.placeholder(tf.bool, [])
fake_image = net.generator(inptG, training)

real_image_batch = data.get_batch_image([image_record], batch_size)
noise_batch = data.get_batch_noise(noise_size, batch_size)
noise_batch_show = data.get_batch_noise(noise_size, top_k)

real_scores = net.discriminator(real_image, gn_stddev, training)
fake_scores = net.discriminator(fake_image, gn_stddev, training)
# topk_scores, topk_index = tf.nn.top_k(tf.reshape(fake_scores,[-1,]),top_k)
m_real_score = tf.reduce_mean(real_scores)
m_fake_score = tf.reduce_mean(fake_scores)

# define losses
d_loss = net.loss_fn_d(real_scores, fake_scores)
g_loss = net.loss_fn_g(fake_scores)