Python generator.Generator() Examples

The following are 26 code examples of generator.Generator(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module generator , or try the search function .
Example #1
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 6 votes vote down vote up
def __init__(self, width = 28, height= 28, channels = 1, latent_size=100, epochs =50000, batch=32, checkpoint=50,model_type=-1):
        self.W = width
        self.H = height
        self.C = channels
        self.EPOCHS = epochs
        self.BATCH = batch
        self.CHECKPOINT = checkpoint
        self.model_type=model_type

        self.LATENT_SPACE_SIZE = latent_size

        self.generator = Generator(height=self.H, width=self.W, channels=self.C, latent_size=self.LATENT_SPACE_SIZE)
        self.discriminator = Discriminator(height=self.H, width=self.W, channels=self.C)
        self.gan = GAN(generator=self.generator.Generator, discriminator=self.discriminator.Discriminator)

        self.load_MNIST() 
Example #2
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 6 votes vote down vote up
def plot_checkpoint(self,e):
        filename = "/data/sample_"+str(e)+".png"

        noise = self.sample_latent_space(16)
        images = self.generator.Generator.predict(noise)
        
        plt.figure(figsize=(10,10))
        for i in range(images.shape[0]):
            plt.subplot(4, 4, i+1)
            image = images[i, :, :, :]
            image = np.reshape(image, [self.H,self.W])
            plt.imshow(image, cmap='gray')
            plt.axis('off')
        plt.tight_layout()
        plt.savefig(filename)
        plt.close('all')
        return 
Example #3
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 6 votes vote down vote up
def __init__(self, width = 28, height= 28, channels = 1, latent_size=100, epochs =50000, batch=32, checkpoint=50,model_type=-1,data_path = ''):
        self.W = width
        self.H = height
        self.C = channels
        self.EPOCHS = epochs
        self.BATCH = batch
        self.CHECKPOINT = checkpoint
        self.model_type=model_type

        self.LATENT_SPACE_SIZE = latent_size

        self.generator = Generator(height=self.H, width=self.W, channels=self.C, latent_size=self.LATENT_SPACE_SIZE,model_type = 'DCGAN')
        self.discriminator = Discriminator(height=self.H, width=self.W, channels=self.C,model_type = 'DCGAN')
        self.gan = GAN(generator=self.generator.Generator, discriminator=self.discriminator.Discriminator)

        #self.load_MNIST()
        self.load_npy(data_path) 
Example #4
Source File: inference_with_ckpt.py    From CartoonGan-tensorflow with Apache License 2.0 6 votes vote down vote up
def main(m_path, img_path, out_dir, light=False):
    logger = get_logger("inference")
    logger.info(f"generating image from {img_path}")
    try:
        g = Generator(light=light)
        g.load_weights(tf.train.latest_checkpoint(m_path))
    except ValueError as e:
        logger.error(e)
        logger.error("Failed to load specified weight.")
        logger.error("If you trained your model with --light, "
                     "consider adding --light when executing this script; otherwise, "
                     "do not add --light when executing this script.")
        exit(1)
    img = np.array(Image.open(img_path).convert("RGB"))
    img = np.expand_dims(img, 0).astype(np.float32) / 127.5 - 1
    out = ((g(img).numpy().squeeze() + 1) * 127.5).astype(np.uint8)
    if out_dir != "" and not os.path.isdir(out_dir):
        os.makedirs(out_dir)
    if out_dir == "":
        out_dir = "."
    out_path = os.path.join(out_dir, os.path.split(img_path)[1])
    imwrite(out_path, out)
    logger.info(f"generated image saved to {out_path}") 
Example #5
Source File: examples.py    From DDI-100 with MIT License 5 votes vote down vote up
def test_mask():
    gen = Generator("../data/pdf_dataset")
    _, mask1, _ = gen.get_doc()
    _, mask2, _ = gen.get_doc()
    mask2 = cv2.resize(mask2, (mask1.shape[1], mask1.shape[0]))
    img = combine_masks(mask1, mask2)
    cv2.imshow("image", cv2.resize(img, (0, 0), fx=.2, fy=.2))
    cv2.waitKey() 
Example #6
Source File: examples.py    From DDI-100 with MIT License 5 votes vote down vote up
def test_char():
    gen = Generator("../data/pdf_dataset")
    img, data = gen.get_char()
    cv2.imshow("image", cv2.resize(img, (0, 0), fx=2, fy=2))
    print(data)
    cv2.waitKey() 
Example #7
Source File: examples.py    From DDI-100 with MIT License 5 votes vote down vote up
def test_str():
    gen = Generator("../data/pdf_dataset")
    img, data, delimeters = gen.get_string()
    cv2.imshow("image", cv2.resize(img, (0, 0), fx=2, fy=2))
    print(data)
    for delim in delimeters:
        cv2.line(img, (delim, 0), (delim, 32), color=0, thickness=2)
    cv2.imshow("image with delims", cv2.resize(img, (0, 0), fx=2, fy=2))
    cv2.waitKey() 
Example #8
Source File: examples.py    From DDI-100 with MIT License 5 votes vote down vote up
def test_doc():
    gen = Generator("../data/pdf_dataset")
    img, mask, data = gen.get_doc()
    cv2.imshow("image", cv2.resize(img, (0, 0), fx=0.2, fy=0.2))
    cv2.imshow("mask", cv2.resize(mask, (0, 0), fx=0.2, fy=0.2))
    draw_word_boxes(img, data, word_color=0)
    cv2.imshow("image with boxes", cv2.resize(img, (0, 0), fx=0.2, fy=0.2))
    cv2.waitKey() 
Example #9
Source File: visualization.py    From DDI-100 with MIT License 5 votes vote down vote up
def show_dataset(dataset_path):
    gen = Generator(dataset_path)
    while True:
        random_show(gen) 
Example #10
Source File: pipeline.py    From Multi-Commander with Apache License 2.0 5 votes vote down vote up
def generator_wrapper(self, cnt_round, cnt_gen, dic_path, dic_exp_conf, dic_agent_conf, dic_traffic_env_conf,
                          best_round=None):
        generator = Generator(cnt_round=cnt_round,
                              cnt_gen=cnt_gen,
                              dic_path=dic_path,
                              dic_exp_conf=dic_exp_conf,
                              dic_agent_conf=dic_agent_conf,
                              dic_traffic_env_conf=dic_traffic_env_conf,
                              best_round=best_round
                              )
        print("make generator")
        generator.generate()
        print("generator_wrapper end")
        return 
Example #11
Source File: graph_gan.py    From GraphGAN with MIT License 5 votes vote down vote up
def build_generator(self):
        """initializing the generator"""

        with tf.variable_scope("generator"):
            self.generator = generator.Generator(n_node=self.n_node, node_emd_init=self.node_embed_init_g) 
Example #12
Source File: export.py    From CartoonGan-tensorflow with Apache License 2.0 5 votes vote down vote up
def main(m_path, out_dir, light):
    logger = get_logger("export")
    try:
        g = Generator(light=light)
        g.load_weights(tf.train.latest_checkpoint(m_path))
        t = tf.keras.Input(shape=[None, None, 3], batch_size=None)
        g(t, training=False)
        g.summary()
    except ValueError as e:
        logger.error(e)
        logger.error("Failed to load specified weight.")
        logger.error("If you trained your model with --light, "
                     "consider adding --light when executing this script; otherwise, "
                     "do not add --light when executing this script.")
        exit(1)
    m_num = 0
    smd = os.path.join(out_dir, "SavedModel")
    tfmd = os.path.join(out_dir, "tfjs_model")
    if light:
        smd += "Light"
        tfmd += "_light"
    saved_model_dir = f"{smd}_{m_num:04d}"
    tfjs_model_dir = f"{tfmd}_{m_num:04d}"
    while os.path.exists(saved_model_dir):
        m_num += 1
        saved_model_dir = f"{smd}_{m_num:04d}"
        tfjs_model_dir = f"{tfmd}_{m_num:04d}"
    tf.saved_model.save(g, saved_model_dir)
    cmd = ['tensorflowjs_converter', '--input_format', 'tf_saved_model',
           '--output_format', 'tfjs_graph_model', saved_model_dir, tfjs_model_dir]
    logger.info(" ".join(cmd))
    exit_code = Popen(cmd).wait()
    if exit_code == 0:
        logger.info(f"Model converted to {saved_model_dir} and {tfjs_model_dir} successfully")
    else:
        logger.error("tfjs model conversion failed") 
Example #13
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def train(self):
        for e in range(self.EPOCHS):

            b = 0
            X_real_temp = deepcopy(self.X_real)
            X_sim_temp = deepcopy(self.X_sim)
            combined_loss = np.zeros(shape=len(self.gan.gan_model.metrics_names))
            discriminator_loss_real = np.zeros(shape=len(self.discriminator.Discriminator.metrics_names))
            discriminator_loss_sim = np.zeros(shape=len(self.discriminator.Discriminator.metrics_names))

            while min(len(X_real_temp),len(X_sim_temp))>self.BATCH:
                # Keep track of Batches
                b=b+1

                count_real_images = int(self.BATCH)
                starting_indexs = randint(0, (min(len(X_real_temp),len(X_sim_temp))-count_real_images))
              
                real_images_raw = X_real_temp[ starting_indexs : (starting_indexs + count_real_images) ]
                real_images = real_images_raw.reshape( count_real_images, self.H, self.W, self.C )

                y_real = np.array([[[1.0, 0.0]] * self.discriminator.Discriminator.output_shape[1]] * self.BATCH)
                
                sim_images_raw = X_sim_temp[ starting_indexs : (starting_indexs + count_real_images) ]
                sim_images = sim_images_raw.reshape( count_real_images, self.H, self.W, self.C )

                y_sim = np.array([[[0.0, 1.0]] * self.discriminator.Discriminator.output_shape[1]] * self.BATCH)

                for _ in range(self.GEN_STEPS):
                    combined_loss = np.add(self.gan.gan_model.train_on_batch(sim_images,[sim_images, y_real]), combined_loss)
        
                for _ in range(self.DISC_STEPS):
                    improved_image_batch = self.refiner.Generator.predict_on_batch(sim_images)
                    discriminator_loss_real = np.add(self.discriminator.Discriminator.train_on_batch(real_images, y_real), discriminator_loss_real)
                    discriminator_loss_sim = np.add(self.discriminator.Discriminator.train_on_batch(improved_image_batch, y_sim),discriminator_loss_sim)

            print ('Epoch: '+str(int(e))+', [Real Discriminator :: Loss: '+str(discriminator_loss_real)+'], [ GAN :: Loss: '+str(combined_loss)+']')
                        
        return 
Example #14
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def __init__(self, height=55,width=35, channels=1,epochs =100, batch=16, checkpoint=50,sim_path='',real_path='',data_limit=0.001,generator_steps=2,discriminator_steps=1):
        self.W = width
        self.H = height
        self.C = channels
        self.EPOCHS = epochs
        self.BATCH = batch
        self.CHECKPOINT = checkpoint
        self.DATA_LIMIT=data_limit
        self.GEN_STEPS = generator_steps
        self.DISC_STEPS = discriminator_steps

        self.X_real = self.load_h5py(real_path)
        self.X_sim = self.load_h5py(sim_path)

        self.refiner = Generator(height=self.H, width=self.W, channels=self.C)
        self.discriminator = Discriminator(height=self.H, width=self.W, channels=self.C)
        self.discriminator.trainable = False

        self.synthetic_image = Input(shape=(self.H, self.W, self.C))
        self.real_or_fake = Input(shape=(self.H, self.W, self.C))


        self.refined_image = self.refiner.Generator(self.synthetic_image)
        self.discriminator_output = self.discriminator.Discriminator(self.real_or_fake)
        self.combined = self.discriminator.Discriminator(self.refined_image)

        model_inputs  = [self.synthetic_image]
        model_outputs = [self.refined_image, self.combined]
        self.gan = GAN(model_inputs=model_inputs,model_outputs=model_outputs) 
Example #15
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def plot_checkpoint(self,e,label):
        filename = "/out/epoch_"+str(e)+"_label_"+str(label)+".png"

        all_encoded_samples = self.X_test_2D_encoded[np.where(self.Y_test_2D==label)]
        index = randint(0, (len(all_encoded_samples)-1))
        batch_encoded_samples = all_encoded_samples[ index ]
        batch_encoded_samples = batch_encoded_samples.reshape( 1, 1, 1, 1,self.LATENT_SPACE_SIZE)

        images = self.generator.Generator.predict(batch_encoded_samples)
        xs = []
        ys = []
        zs = []
        cs = []
        for i in range(16):
            for j in range(16):
                for k in range(16):
                    color = images[0][i][j][k]
                    if np.mean(color)<0.75 and np.mean(color)>0.25:
                        xs.append(i)
                        ys.append(j)
                        zs.append(k)
                        cs.append(color)

        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.scatter(xs,ys,zs,alpha=0.1,c=cs)
        plt.savefig(filename)

        return 
Example #16
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def plot_checkpoint(self,b):
        orig_filename = "/out/batch_check_"+str(b)+"_original.png"

        r, c = 3, 3
        random_inds = random.sample(range(len(self.X_test_A)),3)
        imgs_A = self.X_test_A[random_inds].reshape(3, self.W, self.H, self.C )
        imgs_B = self.X_test_B[random_inds].reshape( 3, self.W, self.H, self.C )
        fake_A = self.generator.Generator.predict(imgs_B)

        gen_imgs = np.concatenate([imgs_B, fake_A, imgs_A])

        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5

        titles = ['Style', 'Generated', 'Original']
        fig, axs = plt.subplots(r, c)
        cnt = 0
        for i in range(r):
            for j in range(c):
                axs[i,j].imshow(gen_imgs[cnt])
                axs[i, j].set_title(titles[i])
                axs[i,j].axis('off')
                cnt += 1
        fig.savefig("/out/batch_check_"+str(b)+".png")
        plt.close('all')

        return 
Example #17
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def __init__(self, height = 256, width = 256, channels=3, epochs = 50000, batch = 1, checkpoint = 50, train_data_path = '',test_data_path=''):
        self.EPOCHS = epochs
        self.BATCH = batch
        self.H = height
        self.W = width
        self.C = channels
        self.CHECKPOINT = checkpoint

        self.X_train_B, self.X_train_A = self.load_data(train_data_path)
        self.X_test_B, self.X_test_A  = self.load_data(test_data_path)


        self.generator = Generator(height=self.H, width=self.W, channels=self.C)

        self.orig_A = Input(shape=(self.W, self.H, self.C))
        self.orig_B = Input(shape=(self.W, self.H, self.C))

        self.fake_A = self.generator.Generator(self.orig_B)

        self.discriminator = Discriminator(height=self.H, width=self.W, channels=self.C)
        self.discriminator.trainable = False
        self.valid = self.discriminator.Discriminator([self.fake_A,self.orig_B])

        model_inputs  = [self.orig_A,self.orig_B]
        model_outputs = [self.valid, self.fake_A]
        self.gan = GAN(model_inputs=model_inputs,model_outputs=model_outputs) 
Example #18
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def plot_checkpoint(self,b):
        orig_filename = "/data/batch_check_"+str(b)+"_original.png"

        image_A = self.X_test_A[5]
        image_A = np.reshape(image_A, [self.W_A_test,self.H_A_test,self.C_A_test])
        print("Image_A shape: " +str(np.shape(image_A)))
        fake_B = self.generator_A_to_B.Generator.predict(image_A.reshape(1, self.W_A, self.H_A, self.C_A ))
        fake_B = np.reshape(fake_B, [self.W_A_test,self.H_A_test,self.C_A_test])
        print("fake_B shape: " +str(np.shape(fake_B)))
        reconstructed_A = self.generator_B_to_A.Generator.predict(fake_B.reshape(1, self.W_A, self.H_A, self.C_A ))
        reconstructed_A = np.reshape(reconstructed_A, [self.W_A_test,self.H_A_test,self.C_A_test])
        print("reconstructed_A shape: " +str(np.shape(reconstructed_A)))
        # from IPython import embed; embed()

        checkpoint_images = np.array([image_A, fake_B, reconstructed_A])

        # Rescale images 0 - 1
        checkpoint_images = 0.5 * checkpoint_images + 0.5

        titles = ['Original', 'Translated', 'Reconstructed']
        fig, axes = plt.subplots(1, 3)
        for i in range(3):
            image = checkpoint_images[i]
            image = np.reshape(image, [self.H_A_test,self.W_A_test,self.C_A_test])
            axes[i].imshow(image)
            axes[i].set_title(titles[i])
            axes[i].axis('off')
        fig.savefig("/data/batch_check_"+str(b)+".png")
        plt.close('all')
        return 
Example #19
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def __init__(self, height = 64, width = 64, epochs = 50000, batch = 32, checkpoint = 50, train_data_path_A = '',train_data_path_B = '',test_data_path_A='',test_data_path_B='',lambda_cycle=10.0,lambda_id=1.0):
        self.EPOCHS = epochs
        self.BATCH = batch
        self.RESIZE_HEIGHT = height
        self.RESIZE_WIDTH = width
        self.CHECKPOINT = checkpoint

        self.X_train_A, self.H_A, self.W_A, self.C_A = self.load_data(train_data_path_A)
        self.X_train_B, self.H_B, self.W_B, self.C_B  = self.load_data(train_data_path_B)
        self.X_test_A, self.H_A_test, self.W_A_test, self.C_A_test = self.load_data(test_data_path_A)
        self.X_test_B, self.H_B_test, self.W_B_test, self.C_B_test  = self.load_data(test_data_path_B)

        self.generator_A_to_B = Generator(height=self.H_A, width=self.W_A, channels=self.C_A)
        self.generator_B_to_A = Generator(height=self.H_B, width=self.W_B, channels=self.C_B)

        self.orig_A = Input(shape=(self.W_A, self.H_A, self.C_A))
        self.orig_B = Input(shape=(self.W_B, self.H_B, self.C_B))

        self.fake_B = self.generator_A_to_B.Generator(self.orig_A)
        self.fake_A = self.generator_B_to_A.Generator(self.orig_B)
        self.reconstructed_A = self.generator_B_to_A.Generator(self.fake_B)
        self.reconstructed_B = self.generator_A_to_B.Generator(self.fake_A)
        self.id_A = self.generator_B_to_A.Generator(self.orig_A)
        self.id_B = self.generator_A_to_B.Generator(self.orig_B)


        self.discriminator_A = Discriminator(height=self.H_A, width=self.W_A, channels=self.C_A)
        self.discriminator_B = Discriminator(height=self.H_B, width=self.W_B, channels=self.C_B)
        self.discriminator_A.trainable = False
        self.discriminator_B.trainable = False
        self.valid_A = self.discriminator_A.Discriminator(self.fake_A)
        self.valid_B = self.discriminator_B.Discriminator(self.fake_B)

        model_inputs  = [self.orig_A,self.orig_B]
        model_outputs = [self.valid_A, self.valid_B,self.reconstructed_A,self.reconstructed_B,self.id_A, self.id_B]
        self.gan = GAN(model_inputs=model_inputs,model_outputs=model_outputs,lambda_cycle=lambda_cycle,lambda_id=lambda_id) 
Example #20
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 5 votes vote down vote up
def train(self):
        for e in range(self.EPOCHS):
            # Train Discriminator
            # Make the training batch for this model be half real, half noise
            # Grab Real Images for this training batch
            count_real_images = int(self.BATCH/2)
            starting_index = randint(0, (len(self.X_train)-count_real_images))
            real_images_raw = self.X_train[ starting_index : (starting_index + count_real_images) ]
            x_real_images = real_images_raw.reshape( count_real_images, self.W, self.H, self.C )
            y_real_labels = np.ones([count_real_images,1])

            # Grab Generated Images for this training batch
            latent_space_samples = self.sample_latent_space(count_real_images)
            x_generated_images = self.generator.Generator.predict(latent_space_samples)
            y_generated_labels = np.zeros([self.BATCH-count_real_images,1])

            # Combine to train on the discriminator
            x_batch = np.concatenate( [x_real_images, x_generated_images] )
            y_batch = np.concatenate( [y_real_labels, y_generated_labels] )

            # Now, train the discriminator with this batch
            discriminator_loss = self.discriminator.Discriminator.train_on_batch(x_batch,y_batch)[0]
        
            # Generate Noise
            x_latent_space_samples = self.sample_latent_space(self.BATCH)
            y_generated_labels = np.ones([self.BATCH,1])
            generator_loss = self.gan.gan_model.train_on_batch(x_latent_space_samples,y_generated_labels)

            print ('Epoch: '+str(int(e))+', [Discriminator :: Loss: '+str(discriminator_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                        
            if e % self.CHECKPOINT == 0 :
                self.plot_checkpoint(e)
        return 
Example #21
Source File: to_pb.py    From CartoonGan-tensorflow with Apache License 2.0 4 votes vote down vote up
def main(m_path, out_dir, light=False, test_out=True):
    logger = get_logger("tf1_export", debug=test_out)
    g = Generator(light=light)
    t = tf.placeholder(tf.string, [])
    x = tf.expand_dims(tf.image.decode_jpeg(tf.read_file(t), channels=3), 0)
    x = (tf.cast(x, tf.float32) / 127.5) - 1
    x = g(x, training=False)
    out = tf.cast((tf.squeeze(x, 0) + 1) * 127.5, tf.uint8)
    in_name, out_name = t.op.name, out.op.name
    try:
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            g.load_weights(tf.train.latest_checkpoint(m_path))
            in_graph_def = tf.get_default_graph().as_graph_def()
            out_graph_def = tf.graph_util.convert_variables_to_constants(
                sess, in_graph_def, [out_name])
        tf.reset_default_graph()
        tf.import_graph_def(out_graph_def, name='')
    except ValueError:
        logger.error("Failed to load specified weight.")
        logger.error("If you trained your model with --light, "
                     "consider adding --light when executing this script; otherwise, "
                     "do not add --light when executing this script.")
        exit(1)
    makedirs(out_dir)
    m_cnt = 0
    bpath = 'optimized_graph_light' if light else 'optimized_graph'
    out_path = os.path.join(out_dir, f'{bpath}_{m_cnt:04d}.pb')
    while os.path.exists(out_path):
        m_cnt += 1
        out_path = os.path.join(out_dir, f'{bpath}_{m_cnt:04d}.pb')
    with tf.gfile.GFile(out_path, 'wb') as f:
        f.write(out_graph_def.SerializeToString())
    if test_out:
        with tf.Graph().as_default():
            gd = tf.GraphDef()
            with tf.gfile.GFile(out_path, 'rb') as f:
                gd.ParseFromString(f.read())
            tf.import_graph_def(gd, name='')
            tf.get_default_graph().finalize()
            t = tf.get_default_graph().get_tensor_by_name(f"{in_name}:0")
            out = tf.get_default_graph().get_tensor_by_name(f"{out_name}:0")
            from time import time
            start = time()
            with tf.Session() as sess:
                img = Image.fromarray(sess.run(out, {t: "input_images/temple.jpg"}))
                img.show()
            elapsed = time() - start
            logger.debug(f"{elapsed} sec per img")
    logger.info(f"successfully exported ckpt to {out_path}")
    logger.info(f"input var name: {in_name}:0")
    logger.info(f"output var name: {out_name}:0") 
Example #22
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 4 votes vote down vote up
def train(self):
        
        count_generated_images = int(self.BATCH/2)
        count_real_images = int(self.BATCH/2)
        for e in range(self.EPOCHS):
            for label in self.LABELS:

                # Grab the Real 3D Samples
                all_3D_samples = self.X_train_3D[np.where(self.Y_train_3D==label)]
                starting_index = randint(0, (len(all_3D_samples)-count_real_images))
                real_3D_samples = all_3D_samples[ starting_index : int((starting_index + count_real_images)) ]
                y_real_labels =  np.ones([count_generated_images,1])

                # Grab Generated Images for this training batch
                all_encoded_samples = self.X_train_2D_encoded[np.where(self.Y_train_2D==label)]
                starting_index = randint(0, (len(all_encoded_samples)-count_generated_images))
                batch_encoded_samples = all_encoded_samples[ starting_index : int((starting_index + count_generated_images)) ]
                batch_encoded_samples = batch_encoded_samples.reshape( count_generated_images, 1, 1, 1,self.LATENT_SPACE_SIZE)

                x_generated_3D_samples = self.generator.Generator.predict(batch_encoded_samples)
                y_generated_labels = np.zeros([count_generated_images,1])

                # Combine to train on the discriminator
                x_batch = np.concatenate( [real_3D_samples, x_generated_3D_samples] )
                y_batch = np.concatenate( [y_real_labels, y_generated_labels] )

                # Now, train the discriminator with this batch
                self.discriminator.Discriminator.trainable = False
                discriminator_loss = self.discriminator.Discriminator.train_on_batch(x_batch,y_batch)[0]
                self.discriminator.Discriminator.trainable = True

                # Generate Noise
                starting_index = randint(0, (len(all_encoded_samples)-self.BATCH))
                x_batch_encoded_samples = all_encoded_samples[ starting_index : int((starting_index + self.BATCH)) ]
                x_batch_encoded_samples = x_batch_encoded_samples.reshape( int(self.BATCH), 1, 1, 1,self.LATENT_SPACE_SIZE)
                y_generated_labels = np.ones([self.BATCH,1])
                generator_loss = self.gan.gan_model.train_on_batch(x_batch_encoded_samples,y_generated_labels)
                print ('Epoch: '+str(int(e))+' Label: '+str(int(label))+', [Discriminator :: Loss: '+str(discriminator_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                if e % self.CHECKPOINT == 0 and e != 0 :
                    self.plot_checkpoint(e,label)
            
        return 
Example #23
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 4 votes vote down vote up
def train(self):
        for e in range(self.EPOCHS):
            b = 0
            X_train_A_temp = deepcopy(self.X_train_A)
            X_train_B_temp = deepcopy(self.X_train_B)

            number_of_batches = len(self.X_train_A)
        
            for b in range(number_of_batches):
                # Train Discriminator
                # Grab Real Images for this training batch
                starting_ind = randint(0, (len(X_train_A_temp)-1))
                real_images_raw_A = X_train_A_temp[ starting_ind : (starting_ind + 1) ]
                real_images_raw_B = X_train_B_temp[ starting_ind : (starting_ind + 1) ]

                # Delete the images used until we have none left
                X_train_A_temp = np.delete(X_train_A_temp,range(starting_ind,(starting_ind + 1)),0)
                X_train_B_temp = np.delete(X_train_B_temp,range(starting_ind,(starting_ind + 1)),0)

                batch_A = real_images_raw_A.reshape( 1, self.W, self.H, self.C )
                batch_B = real_images_raw_B.reshape( 1, self.W, self.H, self.C )

                # PatchGAN
                y_valid = np.ones((1,)+(int(self.W / 2**4), int(self.W / 2**4), 1))
                y_fake = np.zeros((1,)+(int(self.W / 2**4), int(self.W / 2**4), 1))

                fake_A = self.generator.Generator.predict(batch_B)

                # Now, train the discriminator with this batch of reals
                discriminator_loss_real = self.discriminator.Discriminator.train_on_batch([batch_A,batch_B],y_valid)[0]
                discriminator_loss_fake = self.discriminator.Discriminator.train_on_batch([fake_A,batch_B],y_fake)[0]
                full_loss = 0.5 * np.add(discriminator_loss_real, discriminator_loss_fake)

                generator_loss = self.gan.gan_model.train_on_batch([batch_A, batch_B],[y_valid,batch_A])    

                print ('Batch: '+str(int(b))+', [Full Discriminator :: Loss: '+str(full_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                if b % self.CHECKPOINT == 0 :
                    label = str(e)+'_'+str(b)
                    self.plot_checkpoint(label)

            print ('Epoch: '+str(int(e))+', [Full Discriminator :: Loss:'+str(full_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                        
        return 
Example #24
Source File: model.py    From Cycle-Dehaze with MIT License 4 votes vote down vote up
def __init__(self,
               X_train_file='',
               Y_train_file='',
               batch_size=1,
               image_size1=256,
               image_size2=256,
               use_lsgan=True,
               norm='instance',
               lambda1=10.0,
               lambda2=10.0,
               learning_rate=1e-4,
               beta1=0.5,
               ngf=64
              ):
    """
    Args:
      X_train_file: string, X tfrecords file for training
      Y_train_file: string Y tfrecords file for training
      batch_size: integer, batch size
      image_size: integer, image size
      lambda1: integer, weight for forward cycle loss (X->Y->X)
      lambda2: integer, weight for backward cycle loss (Y->X->Y)
      use_lsgan: boolean
      norm: 'instance' or 'batch'
      learning_rate: float, initial learning rate for Adam
      beta1: float, momentum term of Adam
      ngf: number of gen filters in first conv layer
    """
    self.lambda1 = lambda1
    self.lambda2 = lambda2
    self.use_lsgan = use_lsgan
    use_sigmoid = not use_lsgan
    self.batch_size = batch_size
    self.image_size1 = image_size1
    self.image_size2 = image_size2
    self.learning_rate = learning_rate
    self.beta1 = beta1
    self.X_train_file = X_train_file
    self.Y_train_file = Y_train_file

    self.is_training = tf.placeholder_with_default(True, shape=[], name='is_training')


    self.G = Generator('G', self.is_training, ngf=ngf, norm=norm, image_size1=image_size1, image_size2=image_size2)
    self.D_Y = Discriminator('D_Y',
        self.is_training, norm=norm, use_sigmoid=use_sigmoid)
    self.F = Generator('F', self.is_training, ngf=ngf, norm=norm, image_size1=image_size1, image_size2=image_size2)
    self.D_X = Discriminator('D_X',
        self.is_training, norm=norm, use_sigmoid=use_sigmoid)

    self.fake_x = tf.placeholder(tf.float32,
        shape=[batch_size, image_size1, image_size2, 3])
    self.fake_y = tf.placeholder(tf.float32,
        shape=[batch_size, image_size1, image_size2, 3])

    self.vgg = vgg16.Vgg16() 
Example #25
Source File: train.py    From Generative-Adversarial-Networks-Cookbook with MIT License 4 votes vote down vote up
def train(self):
        for e in range(self.EPOCHS):
            b = 0
            X_train_temp = deepcopy(self.X_train)
            while len(X_train_temp)>self.BATCH:
                # Keep track of Batches
                b=b+1

                # Train Discriminator
                # Make the training batch for this model be half real, half noise
                # Grab Real Images for this training batch
                if self.flipCoin():
                    count_real_images = int(self.BATCH)
                    starting_index = randint(0, (len(X_train_temp)-count_real_images))
                    real_images_raw = X_train_temp[ starting_index : (starting_index + count_real_images) ]
                    #self.plot_check_batch(b,real_images_raw)
                    # Delete the images used until we have none left
                    X_train_temp = np.delete(X_train_temp,range(starting_index,(starting_index + count_real_images)),0)
                    x_batch = real_images_raw.reshape( count_real_images, self.W, self.H, self.C )
                    y_batch = np.ones([count_real_images,1])
                else:
                    # Grab Generated Images for this training batch
                    latent_space_samples = self.sample_latent_space(self.BATCH)
                    x_batch = self.generator.Generator.predict(latent_space_samples)
                    y_batch = np.zeros([self.BATCH,1])

                # Now, train the discriminator with this batch
                discriminator_loss = self.discriminator.Discriminator.train_on_batch(x_batch,y_batch)[0]
            
                # In practice, flipping the label when training the generator improves convergence
                if self.flipCoin(chance=0.9):
                    y_generated_labels = np.ones([self.BATCH,1])
                else:
                    y_generated_labels = np.zeros([self.BATCH,1])
                x_latent_space_samples = self.sample_latent_space(self.BATCH)
                generator_loss = self.gan.gan_model.train_on_batch(x_latent_space_samples,y_generated_labels)
    
                print ('Batch: '+str(int(b))+', [Discriminator :: Loss: '+str(discriminator_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                if b % self.CHECKPOINT == 0 :
                    label = str(e)+'_'+str(b)
                    self.plot_checkpoint(label)

            print ('Epoch: '+str(int(e))+', [Discriminator :: Loss: '+str(discriminator_loss)+'], [ Generator :: Loss: '+str(generator_loss)+']')
                        
            if e % self.CHECKPOINT == 0 :
                self.plot_checkpoint(e)
        return 
Example #26
Source File: model.py    From CycleGAN-TensorFlow with MIT License 4 votes vote down vote up
def __init__(self,
               X_train_file='',
               Y_train_file='',
               batch_size=1,
               image_size=256,
               use_lsgan=True,
               norm='instance',
               lambda1=10,
               lambda2=10,
               learning_rate=2e-4,
               beta1=0.5,
               ngf=64
              ):
    """
    Args:
      X_train_file: string, X tfrecords file for training
      Y_train_file: string Y tfrecords file for training
      batch_size: integer, batch size
      image_size: integer, image size
      lambda1: integer, weight for forward cycle loss (X->Y->X)
      lambda2: integer, weight for backward cycle loss (Y->X->Y)
      use_lsgan: boolean
      norm: 'instance' or 'batch'
      learning_rate: float, initial learning rate for Adam
      beta1: float, momentum term of Adam
      ngf: number of gen filters in first conv layer
    """
    self.lambda1 = lambda1
    self.lambda2 = lambda2
    self.use_lsgan = use_lsgan
    use_sigmoid = not use_lsgan
    self.batch_size = batch_size
    self.image_size = image_size
    self.learning_rate = learning_rate
    self.beta1 = beta1
    self.X_train_file = X_train_file
    self.Y_train_file = Y_train_file

    self.is_training = tf.placeholder_with_default(True, shape=[], name='is_training')

    self.G = Generator('G', self.is_training, ngf=ngf, norm=norm, image_size=image_size)
    self.D_Y = Discriminator('D_Y',
        self.is_training, norm=norm, use_sigmoid=use_sigmoid)
    self.F = Generator('F', self.is_training, ngf=ngf, norm=norm, image_size=image_size)
    self.D_X = Discriminator('D_X',
        self.is_training, norm=norm, use_sigmoid=use_sigmoid)

    self.fake_x = tf.placeholder(tf.float32,
        shape=[batch_size, image_size, image_size, 3])
    self.fake_y = tf.placeholder(tf.float32,
        shape=[batch_size, image_size, image_size, 3])