示例#1
0
    def visualize_results(self, epoch):
        tot_num_samples = min(self.sample_num, self.batch_size)
        image_frame_dim = int(np.floor(np.sqrt(tot_num_samples)))
        """ random condition, random noise """

        z_sample = prior.gaussian(self.batch_size, self.z_dim)

        samples = self.sess.run(self.fake_images, feed_dict={self.z: z_sample})

        utils_parent.save_images(
            samples[:image_frame_dim * image_frame_dim, :, :, :],
            [image_frame_dim, image_frame_dim],
            utils_parent.check_folder(self.result_dir + '/' + self.model_dir) +
            '/' + self.model_name + '_epoch%03d' % epoch +
            '_test_all_classes.png')
        """ learned manifold """
        if self.z_dim == 2:
            assert self.z_dim == 2

            z_tot = None
            id_tot = None
            for idx in range(0, 100):
                #randomly sampling
                id = np.random.randint(0, self.num_batches)
                batch_images = self.data_X[id * self.batch_size:(id + 1) *
                                           self.batch_size]
                batch_labels = self.data_y[id * self.batch_size:(id + 1) *
                                           self.batch_size]

                z = self.sess.run(self.mu,
                                  feed_dict={self.inputs: batch_images})

                if idx == 0:
                    z_tot = z
                    id_tot = batch_labels
                else:
                    z_tot = np.concatenate((z_tot, z), axis=0)
                    id_tot = np.concatenate((id_tot, batch_labels), axis=0)

            utils_parent.save_scattered_image(
                z_tot,
                id_tot,
                -4,
                4,
                name=utils_parent.check_folder(self.result_dir + '/' +
                                               self.model_dir) + '/' +
                self.model_name + '_epoch%03d' % epoch +
                '_learned_manifold.png')
示例#2
0
def visualization(log_path, data_path):
    ## Get working directory
    PATH = os.getcwd()

    ## Path to save the embedding and checkpoints generated
    # LOG_DIR = PATH + '/project-tensorboard/log-1/'
    LOG_DIR = PATH + log_path
    utils_parent.check_folder(LOG_DIR)

    ## Load data
    data = np.load(data_path + "/z.npy")

    # Load the metadata file. Metadata consists your labels. This is optional. Metadata helps us visualize(color) different clusters that form t-SNE
    # metadata = os.path.join(data_path,'/pos_index_cluster_predict.tsv')
    metadata = data_path + "/pos_index_cluster_predict.tsv"
    # Generating PCA and
    # pca = PCA(n_components=50,
    #          random_state = 123,
    #          svd_solver = 'auto'
    #          )
    # df_pca = df
    # data_pca = pca.fit_transform(data)
    # df_pca = df_pca.values

    ## TensorFlow Variable from data
    tf_data = tf.Variable(data)
    # tf_data = tf.Variable(data_pca)
    ## Running TensorFlow Session
    with tf.Session() as sess:
        saver = tf.train.Saver([tf_data])
        sess.run(tf_data.initializer)
        saver.save(sess, os.path.join(LOG_DIR, 'tf_data.ckpt'))
        config = projector.ProjectorConfig()
        # One can add multiple embeddings.
        embedding = config.embeddings.add()
        embedding.tensor_name = tf_data.name
        # Link this tensor to its metadata(Labels) file
        embedding.metadata_path = metadata
        # Saves a config file that TensorBoard will read during startup.
        projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)
def train(X, y, val_x, val_y, test_x, test_y, args, i):
    # Train the model
    train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": X},
        y=y,
        batch_size=args.batch_size,
        num_epochs=args.epoch,
        shuffle=True)

    # Evaluate the model and print results
    eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": val_x},
                                                       y=val_y,
                                                       num_epochs=1,
                                                       shuffle=False)

    # Test the model using test data from other cluster
    test_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"x": test_x},
        y=test_y,
        batch_size=args.batch_size,
        num_epochs=1,
        shuffle=False)

    # Create the Estimator
    model_dir = "{}/convnet_{}_{}_{}_{}".format(args.result_dir, args.dataset,
                                                args.batch_size, args.epoch, i)
    utils_parent.check_folder(model_dir)
    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn,
                                              model_dir=model_dir)

    mnist_classifier.train(input_fn=train_input_fn, steps=2000)

    eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
    print("*********************eval************************")
    print(eval_results)
    test_results = mnist_classifier.evaluate(input_fn=test_input_fn)
    print("*********************test************************")
    print(test_results)
    return eval_results, test_results
def check_args(args):
    # --checkpoint_dir
    utils_parent.check_folder(args.checkpoint_dir)

    # --result_dir
    utils_parent.check_folder(args.result_dir)

    # --log_dir
    utils_parent.check_folder(args.log_dir)

    # --epoch
    assert args.epoch >= 1, 'number of epochs must be larger than or equal to one'

    # --batch_size
    assert args.batch_size >= 1, 'batch size must be larger than or equal to one'

    return args
def check_args(args):
    # --checkpoint_dir
    utils_parent.check_folder(args.checkpoint_dir)

    # --result_dir
    utils_parent.check_folder(args.result_dir)

    # --log_dir
    utils_parent.check_folder(args.log_dir)

    # --epoch
    assert args.epoch >= 1, 'number of epochs must be larger than or equal to one'

    # --batch_size
    assert args.batch_size >= 1, 'batch size must be larger than or equal to one'

    # --z_dim
    assert args.z_dim >= 1, 'dimension of noise vector must be larger than or equal to one'

    # --num_labels
    assert args.num_labels >= 1, 'number of labels must be larger than or equal to one'

    return args
    def visualize_results(self, epoch):
        tot_num_samples = min(self.sample_num, self.batch_size)
        image_frame_dim = int(np.floor(np.sqrt(tot_num_samples)))
        z_sample = np.random.uniform(-1, 1, size=(self.batch_size, self.z_dim))
        """ random noise, random discrete code, fixed continuous code """
        y = np.random.choice(self.len_discrete_code, self.batch_size)
        y_one_hot = np.zeros((self.batch_size, self.y_dim))
        y_one_hot[np.arange(self.batch_size), y] = 1

        samples = self.sess.run(self.fake_images,
                                feed_dict={
                                    self.z: z_sample,
                                    self.y: y_one_hot
                                })

        utils_parent.save_images(
            samples[:image_frame_dim * image_frame_dim, :, :, :],
            [image_frame_dim, image_frame_dim],
            utils_parent.check_folder(self.result_dir + '/' + self.model_dir) +
            '/' + self.model_name + '_epoch%03d' % epoch +
            '_test_all_classes.png')
        """ specified condition, random noise """
        n_styles = 10  # must be less than or equal to self.batch_size

        np.random.seed()
        si = np.random.choice(self.batch_size, n_styles)

        for l in range(self.len_discrete_code):
            y = np.zeros(self.batch_size, dtype=np.int64) + l
            y_one_hot = np.zeros((self.batch_size, self.y_dim))
            y_one_hot[np.arange(self.batch_size), y] = 1

            samples = self.sess.run(self.fake_images,
                                    feed_dict={
                                        self.z: z_sample,
                                        self.y: y_one_hot
                                    })
            utils_parent.save_images(
                samples[:image_frame_dim * image_frame_dim, :, :, :],
                [image_frame_dim, image_frame_dim],
                utils_parent.check_folder(self.result_dir + '/' +
                                          self.model_dir) + '/' +
                self.model_name + '_epoch%03d' % epoch +
                '_test_class_%d.png' % l)

            samples = samples[si, :, :, :]

            if l == 0:
                all_samples = samples
            else:
                all_samples = np.concatenate((all_samples, samples), axis=0)
        """ save merged images to check style-consistency """
        canvas = np.zeros_like(all_samples)
        for s in range(n_styles):
            for c in range(self.len_discrete_code):
                canvas[s * self.len_discrete_code +
                       c, :, :, :] = all_samples[c * n_styles + s, :, :, :]

        utils_parent.save_images(
            canvas, [n_styles, self.len_discrete_code],
            utils_parent.check_folder(self.result_dir + '/' + self.model_dir) +
            '/' + self.model_name + '_epoch%03d' % epoch +
            '_test_all_classes_style_by_style.png')
    def train(self):

        # initialize all variables
        tf.global_variables_initializer().run()

        # graph inputs for visualize training results
        self.sample_z = np.random.uniform(-1,
                                          1,
                                          size=(self.batch_size, self.z_dim))
        self.test_codes = self.data_y[0:self.batch_size]

        # saver to save model
        self.saver = tf.train.Saver()

        # summary writer
        self.writer = tf.summary.FileWriter(
            self.log_dir + '/' + self.model_name, self.sess.graph)

        # restore check-point if it exits
        could_load, checkpoint_counter = self.load(self.checkpoint_dir)
        if could_load:
            start_epoch = (int)(checkpoint_counter / self.num_batches)
            start_batch_id = checkpoint_counter - start_epoch * self.num_batches
            counter = checkpoint_counter
            print(" [*] Load SUCCESS")
        else:
            start_epoch = 0
            start_batch_id = 0
            counter = 1
            print(" [!] Load failed...")

        # loop for epoch
        start_time = time.time()
        for epoch in range(start_epoch, self.epoch):

            # get batch data
            for idx in range(start_batch_id, self.num_batches):
                batch_images = self.data_X[idx * self.batch_size:(idx + 1) *
                                           self.batch_size]
                batch_codes = self.data_y[idx * self.batch_size:(idx + 1) *
                                          self.batch_size]

                batch_z = np.random.uniform(
                    -1, 1, [self.batch_size, self.z_dim]).astype(np.float32)

                # update D network
                _, summary_str, d_loss = self.sess.run(
                    [self.d_optim, self.d_sum, self.d_loss],
                    feed_dict={
                        self.inputs: batch_images,
                        self.y: batch_codes,
                        self.z: batch_z
                    })
                self.writer.add_summary(summary_str, counter)

                # update G & Q network
                _, summary_str_g, g_loss, _, summary_str_q, q_loss = self.sess.run(
                    [
                        self.g_optim, self.g_sum, self.g_loss, self.q_optim,
                        self.q_sum, self.q_loss
                    ],
                    feed_dict={
                        self.z: batch_z,
                        self.y: batch_codes,
                        self.inputs: batch_images
                    })
                self.writer.add_summary(summary_str_g, counter)
                self.writer.add_summary(summary_str_q, counter)

                # display training status
                counter += 1
                print("Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f" \
                      % (epoch, idx, self.num_batches, time.time() - start_time, d_loss, g_loss))

                # save training results for every 300 steps
                if np.mod(counter, 300) == 0:
                    samples = self.sess.run(self.fake_images,
                                            feed_dict={
                                                self.z: self.sample_z,
                                                self.y: self.test_codes
                                            })
                    tot_num_samples = min(self.sample_num, self.batch_size)
                    manifold_h = int(np.floor(np.sqrt(tot_num_samples)))
                    manifold_w = int(np.floor(np.sqrt(tot_num_samples)))
                    utils_parent.save_images(
                        samples[:manifold_h * manifold_w, :, :, :],
                        [manifold_h, manifold_w], './' +
                        utils_parent.check_folder(self.result_dir + '/' +
                                                  self.model_dir) + '/' +
                        self.model_name +
                        '_train_{:02d}_{:04d}.png'.format(epoch, idx))

            # After an epoch, start_batch_id is set to zero
            # non-zero value is only for the first epoch after loading pre-trained model
            start_batch_id = 0

            # save model
            self.save(self.checkpoint_dir, counter)

            # show temporal results
            self.visualize_results(epoch)

        # save model for final step
        self.save(self.checkpoint_dir, counter)