Exemple #1
0
    def test(self):
        """
    Test models
    """
        start_time = time.time()
        tf.reset_default_graph()
        loaded_graph = tf.Graph()

        with tf.Session(graph=loaded_graph) as sess:

            # Load saved models
            loader = tf.train.import_meta_graph(self.checkpoint_path + '.meta')
            loader.restore(sess, self.checkpoint_path)

            # Get Tensors from loaded models
            if self.cfg.TEST_WITH_RECONSTRUCTION:
                inputs, labels, loss, accuracy, \
                    clf_loss, rec_loss, rec_images = \
                    self._get_tensors(loaded_graph)
            else:
                inputs, labels, loss, accuracy = self._get_tensors(
                    loaded_graph)
                clf_loss, rec_loss, rec_images = None, None, None

            utils.thick_line()
            print('Testing on test set...')

            utils.thin_line()
            print('Calculating loss and accuracy of test set...')

            loss_test, clf_loss_test, rec_loss_test, acc_test = \
                self._eval_on_batches(
                    sess, inputs, labels, loss, accuracy,
                    clf_loss, rec_loss, rec_images,
                    self.x_test, self.y_test, self.n_batch_test)

            # Print losses and accuracy
            utils.thin_line()
            print('Test_Loss: {:.4f}'.format(loss_test))
            if self.cfg.TEST_WITH_RECONSTRUCTION:
                print('Test_Classifier_Loss: {:.4f}\n'.format(clf_loss_test),
                      'Test_Reconstruction_Loss: {:.4f}'.format(rec_loss_test))
            print('Test_Accuracy: {:.2f}%'.format(acc_test * 100))

            # Save test log
            utils.save_test_log(self.test_log_path, loss_test, acc_test,
                                clf_loss_test, rec_loss_test,
                                self.cfg.TEST_WITH_RECONSTRUCTION)

            utils.thin_line()
            print('Testing finished! Using time: {:.2f}'.format(time.time() -
                                                                start_time))
            utils.thick_line()
    def _test_after_training(self, sess):
        """
    Evaluate on the test set after training.
    """
        test_start_time = time.time()

        utils.thick_line()
        print('Testing...')

        # Check directory of paths
        utils.check_dir([self.test_log_path])
        if self.cfg.WITH_RECONSTRUCTION:
            if self.cfg.TEST_SAVE_IMAGE_STEP is not None:
                utils.check_dir([self.test_image_path])

        # Load data
        utils.thin_line()
        print('Loading test set...')
        utils.thin_line()
        x_test = utils.load_data_from_pkl(
            join(self.preprocessed_path, 'x_test.p'))
        y_test = utils.load_data_from_pkl(
            join(self.preprocessed_path, 'y_test.p'))
        n_batch_test = len(y_test) // self.cfg.BATCH_SIZE

        utils.thin_line()
        print('Calculating loss and accuracy on test set...')
        loss_test_all = []
        acc_test_all = []
        clf_loss_test_all = []
        rec_loss_test_all = []
        step = 0
        _test_batch_generator = utils.get_batches(x_test, y_test,
                                                  self.cfg.BATCH_SIZE)

        if self.cfg.WITH_RECONSTRUCTION:
            for _ in tqdm(range(n_batch_test),
                          total=n_batch_test,
                          ncols=100,
                          unit=' batches'):
                step += 1
                test_batch_x, test_batch_y = next(_test_batch_generator)
                loss_test_i, clf_loss_i, rec_loss_i, acc_test_i = sess.run(
                    [self.loss, self.clf_loss, self.rec_loss, self.accuracy],
                    feed_dict={
                        self.inputs: test_batch_x,
                        self.labels: test_batch_y,
                        self.is_training: False
                    })
                loss_test_all.append(loss_test_i)
                acc_test_all.append(acc_test_i)
                clf_loss_test_all.append(clf_loss_i)
                rec_loss_test_all.append(rec_loss_i)

                # Save reconstruct images
                if self.cfg.TEST_SAVE_IMAGE_STEP is not None:
                    if step % self.cfg.TEST_SAVE_IMAGE_STEP == 0:
                        self._save_images(sess,
                                          self.test_image_path,
                                          test_batch_x,
                                          test_batch_y,
                                          step,
                                          silent=False)

            clf_loss_test = sum(clf_loss_test_all) / len(clf_loss_test_all)
            rec_loss_test = sum(rec_loss_test_all) / len(rec_loss_test_all)

        else:
            for _ in tqdm(range(n_batch_test),
                          total=n_batch_test,
                          ncols=100,
                          unit=' batches'):
                test_batch_x, test_batch_y = next(_test_batch_generator)
                loss_test_i, acc_test_i = sess.run(
                    [self.loss, self.accuracy],
                    feed_dict={
                        self.inputs: test_batch_x,
                        self.labels: test_batch_y,
                        self.is_training: False
                    })
                loss_test_all.append(loss_test_i)
                acc_test_all.append(acc_test_i)
            clf_loss_test, rec_loss_test = None, None

        loss_test = sum(loss_test_all) / len(loss_test_all)
        acc_test = sum(acc_test_all) / len(acc_test_all)

        # Print losses and accuracy
        utils.thin_line()
        print('Test_Loss: {:.4f}\n'.format(loss_test),
              'Test_Accuracy: {:.2f}%'.format(acc_test * 100))
        if self.cfg.WITH_RECONSTRUCTION:
            utils.thin_line()
            print('Test_Train_Loss: {:.4f}\n'.format(clf_loss_test),
                  'Test_Reconstruction_Loss: {:.4f}'.format(rec_loss_test))

        # Save test log
        utils.save_test_log(self.test_log_path, loss_test, acc_test,
                            clf_loss_test, rec_loss_test,
                            self.cfg.WITH_RECONSTRUCTION)

        utils.thin_line()
        print('Testing finished! Using time: {:.2f}'.format(time.time() -
                                                            test_start_time))
Exemple #3
0
    def tester(self,
               sess,
               inputs,
               labels,
               input_imgs,
               is_training,
               clf_preds,
               rec_imgs,
               start_time,
               loss=None,
               acc=None,
               clf_loss=None,
               rec_loss=None):

        utils.thin_line()
        print('Calculating loss and accuracy of test set...')

        # Get losses and accuracies
        clf_preds_vec_test, loss_test, clf_loss_test, rec_loss_test, acc_test = \
            self._eval_on_batches(
                sess, inputs, labels, input_imgs, is_training, clf_preds,
                loss, acc, clf_loss, rec_loss, rec_imgs)

        # Get integer predictions
        _ = self._get_preds_int(preds_vec=clf_preds_vec_test)

        # Get top N accuracy
        if self.cfg.TOP_N_LIST is not None:
            acc_top_n_list = self._get_top_n_accuracy(clf_preds_vec_test)
        else:
            acc_top_n_list = None

        # Print losses and accuracy
        utils.thin_line()
        print('Test Loss: {:.4f}'.format(loss_test))
        if self.cfg.TEST_WITH_REC:
            print('Test Classifier Loss: {:.4f}\n'.format(clf_loss_test),
                  'Test Reconstruction Loss: {:.4f}'.format(rec_loss_test))
        print('Test Accuracy: {:.4f}%'.format(acc_test * 100))
        if self.cfg.TOP_N_LIST is not None:
            utils.thin_line()
            for i, top_n in enumerate(self.cfg.TOP_N_LIST):
                print('Top_{} Test Accuracy: {:.4f}% \n'.format(
                    top_n, acc_top_n_list[i] * 100))

        # Save test log
        if self.during_training and (self.epoch_train != 'end'):
            utils.save_test_log_is_training(
                self.test_log_path, self.epoch_train, self.step_train,
                loss_test, acc_test, clf_loss_test, rec_loss_test,
                self.cfg.TEST_WITH_REC, self.cfg.TOP_N_LIST, acc_top_n_list)
        else:
            utils.save_test_log(self.test_log_path, loss_test, acc_test,
                                clf_loss_test, rec_loss_test,
                                self.cfg.TEST_WITH_REC, self.cfg.TOP_N_LIST,
                                acc_top_n_list)

        utils.thin_line()
        print('Testing finished! Using time: {:.2f}'.format(time.time() -
                                                            start_time))
        utils.thick_line()