Ejemplo n.º 1
0
    def __init__(self,
                 sess,
                 model_dir="./cgan_model",
                 max_objs=3,
                 result_dim=256,
                 font="ebgaramond.ttf",
                 font_size=40):
        self.sess = sess
        self.image_finder = image_finder.ImageFinder()
        self.db = MongoClient().albart
        self.cgan_model_dir = model_dir
        self.cgan = cgan.CGAN(self.sess, n_batches=150)
        self.cgan.init_model()
        self.lyrics_api = genius_api.LyricsApi()
        self.max_objs = max_objs
        self.result_dim = result_dim
        self.font_size = font_size
        self.font_string = font
        self.font = ImageFont.truetype(font, font_size)

        self.allowed_genres = [
            "country", "pop", "rap", "reggae", "indie rock", "hip hop"
        ]
Ejemplo n.º 2
0
def main(to_reload=None):

    cgan_train = dataset.load_data(TRAIN_FILES['A'],
                                   TRAIN_FILES['B'],
                                   skiprows=1,
                                   transpose=True)

    if to_reload:  # restore, get fake and cycle outputs

        m = cgan.CGAN(meta_graph=to_reload, config_name=CONFIG_FILE_NAME)
        print("Loaded!", flush=True)

        ### use training data as testing data
        test_a, test_b = cgan_train.data

        fake_a = m.generate_fake_a(test_b)
        print('generated fake a dimension: {}'.format(fake_a.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'fake_a.txt',
                   X=fake_a.T,
                   delimiter='\t',
                   comments='')

        fake_b = m.generate_fake_b(test_a)
        print('generated fake b dimension: {}'.format(fake_b.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'fake_b.txt',
                   X=fake_b.T,
                   delimiter='\t',
                   comments='')

        cycle_a = m.generate_cycle_a(test_a)
        print('generated cycle a dimension: {}'.format(cycle_a.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'cycle_a.txt',
                   X=cycle_a.T,
                   delimiter='\t',
                   comments='')

        cycle_b = m.generate_cycle_b(test_b)
        print('generated cycle b dimension: {}'.format(cycle_b.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'cycle_b.txt',
                   X=cycle_b.T,
                   delimiter='\t',
                   comments='')

        a2b_final_weights = m.a2b_final_w()
        print('a2b generator output weights size: {}'.format(
            a2b_final_weights.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'a2b_output_weights.txt',
                   X=a2b_final_weights.T,
                   delimiter='\t',
                   comments='')

        b2a_final_weights = m.b2a_final_w()
        print('b2a generator output weights size: {}'.format(
            b2a_final_weights.shape))

        np.savetxt(fname=MODEL_NAME + '_' + 'b2a_output_weights.txt',
                   X=b2a_final_weights.T,
                   delimiter='\t',
                   comments='')

    else:  # train
        """to try cont'd training, load data from previously saved meta graph"""

        m = cgan.CGAN(ARCHITECTURE,
                      HYPERPARAMS,
                      config_name=CONFIG_FILE_NAME + '_' + MODEL_NAME,
                      log_dir=LOG_DIR)

        m.train(cgan_train,
                max_iter=MAX_ITER,
                max_epochs=MAX_EPOCHS,
                verbose=True,
                save=True,
                outdir=METAGRAPH_DIR)
        print("Trained!", flush=True)
Ejemplo n.º 3
0
def main():
    start_time = time.time()  # clocking start

    # mnist data loading
    mnist = DataSet(dataset_name='mnist')
    mnist = mnist.mnist

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        # GAN Model
        model = cgan.CGAN(s)

        # initializing
        s.run(tf.global_variables_initializer())

        sample_x, _ = mnist.train.next_batch(model.sample_num)
        sample_y = np.zeros(shape=[model.sample_num, model.n_classes])
        sample_y[:, 3] = 1  # specify label number what u wanna get
        sample_z = np.random.uniform(
            -1., 1., [model.sample_num, model.z_dim]).astype(np.float32)

        d_overpowered = False
        for step in range(paras['global_step']):
            batch_x, batch_y = mnist.train.next_batch(model.batch_size)
            batch_z = np.random.uniform(-1.,
                                        1.,
                                        size=[model.batch_size,
                                              model.z_dim]).astype(np.float32)

            # update D network
            if not d_overpowered:
                s.run(model.d_op,
                      feed_dict={
                          model.x: batch_x,
                          model.c: batch_y,
                          model.z: batch_z
                      })

            # update G network
            s.run(model.g_op, feed_dict={model.c: batch_y, model.z: batch_z})

            if step % paras['logging_interval'] == 0:
                batch_x, batch_y = mnist.test.next_batch(model.batch_size)
                batch_z = np.random.uniform(
                    -1., 1.,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                d_loss, g_loss, summary = s.run(
                    [model.d_loss, model.g_loss, model.merged],
                    feed_dict={
                        model.x: batch_x,
                        model.c: batch_y,
                        model.z: batch_z
                    })

                # update d_overpowered
                d_overpowered = d_loss < g_loss / 3

                # print loss
                print("[+] Step %08d => " % (step),
                      "D loss : {:.8f}".format(d_loss),
                      " G loss : {:.8f}".format(g_loss))

                # training G model with sample image and noise
                samples = s.run(model.G,
                                feed_dict={
                                    model.c: sample_y,
                                    model.z: sample_z
                                })

                # summary saver
                model.writer.add_summary(summary, step)

                # export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = dirs['sample_output'] + 'train_{:08d}.png'.format(
                    step)

                # Generated image save
                iu.save_images(samples,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir)

                # model save
                model.saver.save(s, dirs['model'], global_step=step)

    end_time = time.time() - start_time

    # elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # close tf.Session
    s.close()
Ejemplo n.º 4
0
        return self.sess.run(self.model.latent,
                             feed_dict={
                                 self.model.orig: self.X,
                                 self.model.EPS_BS: 1,
                                 self.model.TRAIN: False
                             })

    def get_zdim(self):
        return self.model.lat_size


# Step 1: Create theano functions

# Initialize model
# model = IAN(config_path = 'IAN_simple.py', dnn = True)
net = cgan.CGAN()
model = fooler(model=net, log_dir='results/trial/LOGS')

# z_trial = np.eye(100)[45].reshape(-1, 100)
# out = model.sample_at(z_trial)
# imsave('trial.png', out[0])
# imsave('trial.png',out.reshape(5,5,64,64,3).swapaxes(1,2).reshape(5*64,-1,3))

# z_trial = np.random.uniform(-1.,1., size=(25,100))
# out = model.sample_at(z_trial)
# print(out.shape)
# imsave('trial2.png',out.reshape(5,5,64,64,3).swapaxes(1,2).reshape(5*64,-1,3))

# import sys
# sys.exit()
# # Prepare GUI functions
Ejemplo n.º 5
0
import cgan
import sys
import network
from data_interface import mnist

DATA_SET_SIZE = 65000

if __name__ == '__main__':
    tf.reset_default_graph()
    # load MNIST
    train_validation_set = mnist.train('../../dataset/MNIST_Dataset/train/')
    test_set = mnist.test('../../dataset/MNIST_Dataset/test/')
    data_set = train_validation_set.concatenate(test_set)

    # run cycle for current parameter settings
    gan = cgan.CGAN(generator_fn=network.cgan_generator,
                    discriminator_fn=network.cgan_discriminator)
    train_params = {
        'lr': 0.0002,
        'beta1': 0.5,
        'batch_size': 100,
        'max_epoch': 100,
    }
    g_model_params = {
        'input_shape': ((200, ), (10, )),
        'output_shape': (784, ),
        'output_image_shape': (28, 28, 1),
        'summ_shape_per_class': (10, 1),
        'z_dense_width': 256,
        'y_dense_width': 256,
        'mid_dense_widths': [512, 1024],
    }