def main():
    parser = argparse.ArgumentParser(
        description='Sentiment classifier argument parser.')
    parser.add_argument(
        '--build_data',
        action='store_true',
        help='build and save data sets (only needed for the first time).')
    parser.add_argument('--alg',
                        choices=['CNN', 'BiLSTM', 'BiRNN'],
                        required=True,
                        help='algorithm to train the sentiment classifier')
    parser.add_argument(
        '--small_subsets',
        action='store_true',
        help='train and evaluate on smaller subsets of the data.')
    parser.add_argument('--outfile',
                        type=str,
                        help='output file name to save trained model.')
    args = parser.parse_args()

    # build and save data for the first time
    if args.build_data:
        data_loader = DataLoader()
        data_loader.build_data()

    # load data from file
    data_loader = DataLoader()
    data_loader.load_data()

    # train model
    train_classifier(alg=args.alg,
                     data_loader=data_loader,
                     small_subsets=args.small_subsets,
                     outfile=args.outfile)
示例#2
0
def learning_curve(df, classifier, n_ticks=10):
    target = df['class'].values.reshape(len(X), 1)
    train_sizes = np.linspace(0, 1.0, n_ticks)
    classifier = Classifier.train_classifier(classifier_type)
    num_training_examples, train_scores, valid_scores = learning_curve(
        classifier, df, target, train_sizes=train_sizes)
    print valid_scores
示例#3
0
    def run_svm(self):
        corpus_file, labels, test_data, test_labels = corpus.import_corpus_setup(
            False, self.svm_corpus_num, self.classifier_type)

        self.training_set_size = min(len(corpus_file),
                                     self.svm_training_set_size)
        corpus_file = corpus_file[:int(self.training_set_size)]
        labels = labels[:int(self.training_set_size)]

        my_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                               "classifiers")
        classifier_file = os.path.join(
            my_path, "svm_corpus_" + str(self.svm_corpus_num) + ".pickle")
        if os.path.isfile(classifier_file) and not self.bool_run_setup:
            classifier_obj = classifier.open_classifier(classifier_file, False)
            vectorizer = TfidfVectorizer(min_df=5,
                                         max_df=0.95,
                                         sublinear_tf=True,
                                         use_idf=True,
                                         ngram_range=(1, 2))
            train_vectors = vectorizer.fit_transform(corpus_file)

            classifier_obj.fit(train_vectors, labels)

            return classifier_obj, vectorizer
        else:
            classifier_obj, vectorizer, train_vectors = classifier.train_classifier(
                corpus_file, self.training_set_size, False,
                self.classifier_type, labels
            )  # TODO: Contrast different automated ways to train Naive Bayesian Classifier
            classifier.save_classifier(str(classifier_file), False)
            classifier_obj.fit(train_vectors, labels)
            return classifier, vectorizer
示例#4
0
def rumor_validation(labled_data,
                     classifier_type,
                     features_to_use,
                     verbose=True,
                     outfile=None):
    scores = OrderedDict()
    scores['f1'] = []
    scores['recall'] = []
    scores['precision'] = []
    confusion = np.array([[0, 0], [0, 0]])
    train_and_test = rumor_split(labled_data)
    for x, y in train_and_test:
        train_data = labled_data.loc[x]
        test_data = labled_data.loc[y]
        test_lables = labled_data.loc[y]['class'].values

        classifier = Classifier.train_classifier(train_data, classifier_type,
                                                 features_to_use)
        predictions = classifier.predict(test_data)

        confusion += metrics.confusion_matrix(test_lables, predictions)
        f1_score = metrics.f1_score(test_lables, predictions, pos_label=1)
        recall = metrics.recall_score(test_lables, predictions, pos_label=1)
        precision = metrics.precision_score(test_lables,
                                            predictions,
                                            pos_label=1)
        if verbose:
            print 'tweets classified:', len(y)
            print 'f1: %s' % f1_score
            print 'recall: %s' % recall
            print 'precision: %s\n' % precision
        scores['f1'].append(f1_score)
        scores['recall'].append(recall)
        scores['precision'].append(precision)

    print 'Classifier: {0}'.format(classifier_type)
    print 'Total tweets classified:', len(labled_data)
    for score in scores:
        scores[score] = sum(scores[score]) / len(train_and_test)
    for score in scores:
        print '%s: %s' % (score, scores[score])
    print('Confusion matrix:')
    print(confusion)
    return pd.DataFrame([scores])
示例#5
0
def run_random_xp(n_iteration, n_selected_per_iteration, n_sampling, class_func, init_X):

    X = init_X

    all_info = []
    for _ in range(n_iteration):
        y = class_func(X)
        clf = train_classifier(X, y)
        all_X_selected = np.random.rand(n_selected_per_iteration, X.shape[1])
        #
        info = {}
        info['X'] = X
        info['y'] = y
        info['clf'] = clf
        info['all_X_selected'] = all_X_selected
        all_info.append(info)
        #
        X = np.vstack((X, all_X_selected))

    return all_info
示例#6
0
def run_full_xp(n_iteration, n_selected_per_iteration, n_sampling, class_func, init_X, batch_repulsion=True):

    X = init_X

    all_info = []
    for _ in range(n_iteration):
        y = class_func(X)
        clf = train_classifier(X, y)
        all_X_selected, all_run_info = generate_next_samples(n_selected_per_iteration, clf, X.shape[1], n_sampling, batch_repulsion=batch_repulsion)
        #
        info = {}
        info['X'] = X
        info['y'] = y
        info['clf'] = clf
        info['all_X_selected'] = all_X_selected
        info['all_run_info'] = all_run_info
        all_info.append(info)
        #
        X = np.vstack((X, all_X_selected))

    return all_info
示例#7
0
文件: fnc-1.py 项目: Kamaros/fnc-1
if __name__ == "__main__":
    print('Reading data...')
    raw_data = read_dataset('data')
    data = preprocess_dataframe(raw_data, 'raw_data')
    labels = extract_labels(data)

    print('Extracting features...')
    features = extract_features(data, raw_data)

    print('Flattening features...')
    flattened_features = flatten_features(features)

    print('Generating hold-out split...')
    training_data, testing_data, unused_data = generate_hold_out_split(raw_data)
    training_features, testing_features = flattened_features.iloc[training_data.index], flattened_features.iloc[testing_data.index]
    training_labels, testing_labels = labels.iloc[training_data.index], labels.iloc[testing_data.index]

    print('Oversampling minority classes...')
    oversampled_training_features, oversampled_training_labels = oversample_minority_classes(training_features, training_labels)

    print('Training classifier...')
    classifier = train_classifier(oversampled_training_features, oversampled_training_labels)

    print('Cross-validating...')
    print_cv_score(classifier, training_features, training_labels, cv=5)

    print('Generating predictions...')
    predictions = testing_data.copy()
    predictions['Stance'] = decipher_labels(classifier.predict(testing_features), index=testing_features.index)
    evaluate_submission(testing_data, predictions)
示例#8
0
#
# # Test the classifier with test data
# with open("eval.bayes.results", 'w') as file:
#     file.write('Id,Prediction\n')
#     for index in range(len(eval_feature_data)):
#         file.write(str(eval_data_id[index]) +
#                    ',' +
#                    str(classifier.decide_bayes(eval_feature_data[index], weights, bias)) +
#                    '\n')

#################
# BAGGES FORESTS
#################

# Train a classifier on the data
forest, bias = classifier.train_classifier(train_feature_data, mode='forest')

success_rate = classifier.test_classifier(test_feature_data,
                                          forest,
                                          0,
                                          mode='forest')

print(success_rate)

# Test the classifier with test data
with open("eval.forest.results", 'w') as file:
    file.write('Id,Prediction\n')
    for index in range(len(eval_feature_data)):
        file.write(
            str(eval_data_id[index]) + ',' +
            str(classifier.decide_forest(eval_feature_data[index], forest)) +
示例#9
0
def train_local_global_autoencoder(model, optimizer, dataset, train_dataset,
                                   test_dataset, config):
    RUN_NAME = datetime.now().strftime("%Y%m%d-%H%M%S")
    # wandb.init(config = config, project = "lg-vae-project", tags = [config.tag], name = RUN_NAME, reinit = True)
    data_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             'output/')
    if not os.path.exists(data_path):
        print('data folder doesn\'t exist, create data folder')
        os.mkdir(data_path)
    RUN_DIR = 'output/' + RUN_NAME + '/'
    os.mkdir(RUN_DIR)
    if config.label:
        if not os.path.exists('models/svhn_classifier_weights.h5'):
            print('Classifer model not found, training a new classifier')
            train_classifier()
        # Check classifier performance
        classifier = Classifier(target_shape=10)
        classifier(tf.zeros([8, 32, 32, 3]))  #build model
        classifier.summary()
        classifier.load_weights('models/svhn_classifier_weights.h5')
        test_acc = tf.keras.metrics.CategoricalAccuracy()

        for test_images, labels in test_dataset:
            x = test_images[:, :, :, :3]
            pred = classifier(x)
            test_acc(labels, pred)
        print('Test acc: {:.4f}'.format(test_acc.result()))
        del test_acc

    x_recon_train_loss = tf.keras.metrics.Mean(name='x_recon_train_loss')
    x_kl_train_loss = tf.keras.metrics.Mean(name='x_kl_train_loss')
    x_recon_test_loss = tf.keras.metrics.Mean(name='x_recon_test_loss')
    x_kl_test_loss = tf.keras.metrics.Mean(name='x_kl_test_loss')

    total_kl_train_loss = tf.keras.metrics.Mean(name='total_kl_train_loss')
    total_kl_test_loss = tf.keras.metrics.Mean(name='total_test_loss')

    x_hat_recon_train_loss = tf.keras.metrics.Mean(
        name='x_hat_recon_train_loss')
    x_hat_kl_train_loss = tf.keras.metrics.Mean(name='x_hat_kl_train_loss')
    x_hat_recon_test_loss = tf.keras.metrics.Mean(name='x_hat_recon_test_loss')
    x_hat_kl_test_loss = tf.keras.metrics.Mean(name='x_hat_kl_test_loss')

    y_kl_train_loss = tf.keras.metrics.Mean(name='y_kl_train_loss')
    y_kl_test_loss = tf.keras.metrics.Mean(name='y_kl_test_loss')

    classifier_recon_acc = tf.keras.metrics.CategoricalAccuracy()
    classifier_random_z_l_acc = tf.keras.metrics.CategoricalAccuracy()
    classifier_random_z_g_acc = tf.keras.metrics.CategoricalAccuracy()
    classifier_cluster_acc = tf.keras.metrics.CategoricalAccuracy()

    @tf.function
    def train_step_lg_vae(model, images, optimizer):
        with tf.GradientTape() as tape:
            x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat, x_hat_mean, x_hat_log_scale, z_mean_x_hat, z_sig_x_hat = model(
                images)

            x, x_hat = images[:, :, :, :3], images[:, :, :, 3:]

            x_recon_loss = tf.reduce_mean(
                tf.reduce_sum(discretised_logistic_loss(
                    x, x_mean, x_log_scale),
                              axis=[1, 2, 3]))
            x_hat_recon_loss = tf.reduce_mean(
                tf.reduce_sum(discretised_logistic_loss(
                    x_hat, x_hat_mean, x_hat_log_scale),
                              axis=[1, 2, 3]))

            total_kl_loss = config.beta * kl_divergence(
                tf.concat([z_mean_x, z_mean_x_hat], axis=1),
                tf.concat([z_sig_x, z_sig_x_hat], axis=1))
            x_kl_loss = kl_divergence(z_mean_x, z_sig_x)
            x_hat_kl_loss = kl_divergence(z_mean_x_hat, z_sig_x_hat)

            total_loss = x_recon_loss + x_hat_recon_loss + total_kl_loss

        gradients = tape.gradient(total_loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        x_recon_train_loss(x_recon_loss)
        x_kl_train_loss(x_kl_loss)
        x_hat_recon_train_loss(x_hat_recon_loss)
        x_hat_kl_train_loss(x_hat_kl_loss)
        total_kl_train_loss(total_kl_loss)

    @tf.function
    def train_step_lg_gm_vae(model, images, optimizer):
        with tf.GradientTape() as tape:
            x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat, x_hat_mean, x_hat_log_scale, z_mean_x_hat, z_sig_x_hat, y, y_logits, z_prior_mean, z_prior_sig = model(
                images, training=True)

            x, x_hat = images[:, :, :, :3], images[:, :, :, 3:]

            x_recon_loss = tf.reduce_mean(
                tf.reduce_sum(discretised_logistic_loss(
                    x, x_mean, x_log_scale),
                              axis=[1, 2, 3]))
            x_hat_recon_loss = tf.reduce_mean(
                tf.reduce_sum(discretised_logistic_loss(
                    x_hat, x_hat_mean, x_hat_log_scale),
                              axis=[1, 2, 3]))

            x_kl_loss = kl_divergence_two_gauss(z_mean_x, z_sig_x,
                                                z_prior_mean, z_prior_sig)
            x_hat_kl_loss = kl_divergence_two_gauss(z_mean_x_hat, z_sig_x_hat,
                                                    0., 1.)

            py = tf.nn.softmax(y_logits, axis=1)
            y_kl_loss = tf.reduce_mean(
                tf.reduce_sum(
                    py *
                    (tf.math.log(py + 1e-8) - tf.math.log(1.0 / model.y_size)),
                    axis=1))

            total_loss = x_recon_loss + x_hat_recon_loss + config.beta * (
                x_kl_loss + x_hat_kl_loss) + config.alpha * y_kl_loss

        gradients = tape.gradient(total_loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        x_recon_train_loss(x_recon_loss)
        x_kl_train_loss(x_kl_loss)
        x_hat_recon_train_loss(x_hat_recon_loss)
        x_hat_kl_train_loss(x_hat_kl_loss)
        y_kl_train_loss(y_kl_loss)

    @tf.function
    def train_step_gm_vae(model, images, optimizer):
        with tf.GradientTape() as tape:
            x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, y, y_logits, z_prior_mean, z_prior_sig = model(
                images, training=True)

            x = images[:, :, :, :3]

            x_recon_loss = tf.reduce_mean(
                tf.reduce_sum(discretised_logistic_loss(
                    x, x_mean, x_log_scale),
                              axis=[1, 2, 3]))
            x_kl_loss = kl_divergence_two_gauss(z_mean_x, z_sig_x,
                                                z_prior_mean, z_prior_sig)

            py = tf.nn.softmax(y_logits, axis=1)
            y_kl_loss = tf.reduce_mean(
                tf.reduce_sum(
                    py *
                    (tf.math.log(py + 1e-8) - tf.math.log(1.0 / model.y_size)),
                    axis=1))

            total_loss = x_recon_loss + config.beta * x_kl_loss + config.alpha * y_kl_loss

        gradients = tape.gradient(total_loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        x_recon_train_loss(x_recon_loss)
        x_kl_train_loss(x_kl_loss)
        y_kl_train_loss(y_kl_loss)

    @tf.function
    def test_step_lg_vae(model, images, labels=None):
        x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat, x_hat_mean, x_hat_log_scale, z_mean_x_hat, z_sig_x_hat = model(
            images)

        x, x_hat = images[:, :, :, :3], images[:, :, :, 3:]

        x_recon_loss = tf.reduce_mean(
            tf.reduce_sum(discretised_logistic_loss(x, x_mean, x_log_scale),
                          axis=[1, 2, 3]))
        x_hat_recon_loss = tf.reduce_mean(
            tf.reduce_sum(discretised_logistic_loss(x_hat, x_hat_mean,
                                                    x_hat_log_scale),
                          axis=[1, 2, 3]))

        total_kl_loss = config.beta * kl_divergence(
            tf.concat([z_mean_x, z_mean_x_hat], axis=1),
            tf.concat([z_sig_x, z_sig_x_hat], axis=1))
        x_kl_loss = kl_divergence(z_mean_x, z_sig_x)
        x_hat_kl_loss = kl_divergence(z_mean_x_hat, z_sig_x_hat)

        if labels is not None:
            pred = classifier(x_mean)
            classifier_recon_acc(labels, pred)

            # vary z_l
            random_z_l = np.random.normal(
                size=[z_x.shape[0], model.local_latent_dims]).astype(
                    np.float32)
            x_recon_with_random_z_l, _ = model.decode(z_x, random_z_l)
            pred_random_z_l = classifier(x_recon_with_random_z_l)
            classifier_random_z_l_acc(labels, pred_random_z_l)
            #vary z_g
            random_z_g = np.random.normal(
                size=[z_x_hat.shape[0], model.global_latent_dims]).astype(
                    np.float32)
            x_recon_with_random_z_g, _ = model.decode(random_z_g, z_x_hat)
            pred_random_z_g = classifier(x_recon_with_random_z_g)
            classifier_random_z_g_acc(labels, pred_random_z_g)

        x_recon_test_loss(x_recon_loss)
        x_kl_test_loss(x_kl_loss)
        x_hat_recon_test_loss(x_hat_recon_loss)
        x_hat_kl_test_loss(x_hat_kl_loss)
        total_kl_test_loss(total_kl_loss)

    @tf.function
    def test_step_lg_gm_vae(model, images, labels=None):
        x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat, x_hat_mean, x_hat_log_scale, z_mean_x_hat, z_sig_x_hat, y, y_logits, z_prior_mean, z_prior_sig = model(
            images)

        x, x_hat = images[:, :, :, :3], images[:, :, :, 3:]

        x_recon_loss = tf.reduce_mean(
            tf.reduce_sum(discretised_logistic_loss(x, x_mean, x_log_scale),
                          axis=[1, 2, 3]))
        x_hat_recon_loss = tf.reduce_mean(
            tf.reduce_sum(discretised_logistic_loss(x_hat, x_hat_mean,
                                                    x_hat_log_scale),
                          axis=[1, 2, 3]))

        x_kl_loss = kl_divergence_two_gauss(z_mean_x, z_sig_x, z_prior_mean,
                                            z_prior_sig)
        x_hat_kl_loss = kl_divergence_two_gauss(z_mean_x_hat, z_sig_x_hat, 0.,
                                                1.)

        py = tf.nn.softmax(y_logits, axis=1)
        y_kl_loss = tf.reduce_mean(
            tf.reduce_sum(
                py *
                (tf.math.log(py + 1e-8) - tf.math.log(1.0 / model.y_size)),
                axis=1))

        if labels is not None:
            pred = classifier(x_mean)
            classifier_recon_acc(labels, pred)

            # vary z_l
            random_z_l = np.random.normal(
                size=[z_x_hat.shape[0], model.local_latent_dims]).astype(
                    np.float32)
            x_recon_with_random_z_l, _ = model.decode(z_x, random_z_l)
            pred_random_z_l = classifier(x_recon_with_random_z_l)
            classifier_random_z_l_acc(labels, pred_random_z_l)
            #vary z_g
            random_z_g = z_prior_mean + np.random.normal(
                size=[z_prior_mean.shape[0], model.global_latent_dims]).astype(
                    np.float32) * z_prior_sig
            x_recon_with_random_z_g, _ = model.decode(random_z_g, z_x_hat)
            pred_random_z_g = classifier(x_recon_with_random_z_g)
            classifier_random_z_g_acc(labels, pred_random_z_g)

        x_recon_test_loss(x_recon_loss)
        x_kl_test_loss(x_kl_loss)
        x_hat_recon_test_loss(x_hat_recon_loss)
        x_hat_kl_test_loss(x_hat_kl_loss)
        y_kl_test_loss(y_kl_loss)

        return x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat, x_hat_mean, x_hat_log_scale, z_mean_x_hat, z_sig_x_hat, y, y_logits, z_prior_mean, z_prior_sig

    @tf.function
    def test_step_gm_vae(model, images, labels=None):
        x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, y, y_logits, z_prior_mean, z_prior_sig = model(
            images)

        x = images[:, :, :, :3]

        x_recon_loss = tf.reduce_mean(
            tf.reduce_sum(discretised_logistic_loss(x, x_mean, x_log_scale),
                          axis=[1, 2, 3]))

        x_kl_loss = kl_divergence_two_gauss(z_mean_x, z_sig_x, z_prior_mean,
                                            z_prior_sig)

        py = tf.nn.softmax(y_logits, axis=1)
        y_kl_loss = tf.reduce_mean(
            tf.reduce_sum(
                py *
                (tf.math.log(py + 1e-8) - tf.math.log(1.0 / model.y_size)),
                axis=1))

        x_recon_test_loss(x_recon_loss)
        x_kl_test_loss(x_kl_loss)
        y_kl_test_loss(y_kl_loss)
        return x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, y, y_logits, z_prior_mean, z_prior_sig

    if isinstance(model, LGVae):
        train_step = train_step_lg_vae
        test_step = test_step_lg_vae
    elif isinstance(model, LGGMVae):
        train_step = train_step_lg_gm_vae
        test_step = test_step_lg_gm_vae
    elif isinstance(model, GMVae):
        train_step = train_step_gm_vae
        test_step = test_step_gm_vae

    # Train
    start = time.time()
    for step, train_data in enumerate(train_dataset):
        if config.label:
            images = train_data[0]
        else:
            images = train_data
        train_step(model, images, optimizer)

        if (step % 10000 == 0):
            print('Training time: {:.2f}'.format(time.time() - start))

            start = time.time()
            # Evaluation
            all_labels = []
            all_pred = []
            for test_data in test_dataset:
                if config.label:
                    test_images, labels = test_data[0], test_data[1]

                    if isinstance(model, LGGMVae):
                        (x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, z_x_hat,
                         x_hat_mean, x_hat_log_scale, z_mean_x_hat,
                         z_sig_x_hat, y, y_logits, z_prior_mean,
                         z_prior_sig) = test_step(model, test_images, labels)
                        # cluster_pred = linear_assignment(labels,y_logits)
                        # classifier_cluster_acc(labels,cluster_pred)
                        all_labels += tf.unstack(labels)
                        all_pred += tf.unstack(y_logits)

                    elif isinstance(model, GMVae):
                        (x_mean, x_log_scale, z_x, z_mean_x, z_sig_x, y,
                         y_logits, z_prior_mean,
                         z_prior_sig) = test_step(model, test_images, labels)
                        # cluster_pred = linear_assignment(labels,y_logits)
                        # classifier_cluster_acc(labels,cluster_pred)
                        all_labels += tf.unstack(labels)
                        all_pred += tf.unstack(y_logits)

                    else:
                        test_step(model, test_images, labels)
                else:
                    test_images = test_data
                    test_step(model, test_images)

            if config.label and (isinstance(model, LGGMVae)
                                 or isinstance(model, GMVae)):
                all_labels = tf.stack(all_labels)
                all_pred = tf.stack(all_pred)
                cluster_pred = linear_assignment(all_labels, all_pred)
                classifier_cluster_acc(all_labels, cluster_pred)
            print('Testing time: {:.2f}'.format(time.time() - start))

            template = 'Training step {}\n\
            X Recon Loss: {:.4f}, X KLD loss: {:.4f}, Total X loss: {:.4f} \n\
            X hat Recon Loss: {:.4f}, X hat KLD loss: {:.4f}, Total X hat loss: {:.4f} \n\
            Test X Recon Loss: {:.4f}, Test X KLD loss: {:.4f}, Test Total X loss: {:.4f} \n\
            Test X hat Recon Loss: {:.4f}, Test X hat KLD loss: {:.4f}, Test Total X hat loss: {:.4f}\n\
            Total KL train loss: {:.4f}, Total KL test loss: {:.4f}\n\
            Classifier recon acc: {:.4f}, Classifier random z_g acc: {:.4f}, Classifier random z_l acc: {:.4f}\n\
            Classifier cluster acc: {:.4f}\n\
            Y KL train loss: {:.4f}, Y KL test loss: {:.4f}'

            print(
                template.format(
                    step, x_recon_train_loss.result(),
                    x_kl_train_loss.result(),
                    x_recon_train_loss.result() + x_kl_train_loss.result(),
                    x_hat_recon_train_loss.result(),
                    x_hat_kl_train_loss.result(),
                    x_hat_recon_train_loss.result() +
                    x_hat_kl_train_loss.result(), x_recon_test_loss.result(),
                    x_kl_test_loss.result(),
                    x_recon_test_loss.result() + x_kl_test_loss.result(),
                    x_hat_recon_test_loss.result(),
                    x_hat_kl_test_loss.result(),
                    x_hat_recon_test_loss.result() +
                    x_hat_kl_test_loss.result(), total_kl_train_loss.result(),
                    total_kl_test_loss.result(), classifier_recon_acc.result(),
                    classifier_random_z_g_acc.result(),
                    classifier_random_z_l_acc.result(),
                    classifier_cluster_acc.result(), y_kl_train_loss.result(),
                    y_kl_test_loss.result()))

            #VISUALIZATION
            if not isinstance(model, GMVae):
                # FOR LGVAE and LGGMVAE
                visualizer.generate(model,
                                    filename='generate_it_' + str(step),
                                    filepath=RUN_DIR)
                visualizer.reconstruction_test_lg_vae(model,
                                                      test_dataset,
                                                      label=config.label,
                                                      filename='_it_' +
                                                      str(step),
                                                      filepath=RUN_DIR)
                visualizer.generate_varying_latent(model,
                                                   vary='lower',
                                                   filename='vary_lower_it_' +
                                                   str(step),
                                                   filepath=RUN_DIR)
                visualizer.generate_varying_latent(model,
                                                   vary='upper',
                                                   filename='vary_upper_it_' +
                                                   str(step),
                                                   filepath=RUN_DIR)
                if config.dataset == 'svhn':
                    visualizer.style_transfer_test(model,
                                                   test_dataset,
                                                   label=config.label,
                                                   filename='_it_' + str(step),
                                                   filepath=RUN_DIR)
                else:
                    visualizer.style_transfer_celeba(model,
                                                     test_dataset,
                                                     label=config.label,
                                                     filename='_it_' +
                                                     str(step),
                                                     filepath=RUN_DIR)

            if config.viz:
                # FOR LGGMVAE ONLY
                if isinstance(model, LGGMVae):
                    visualizer.unseen_cluster_lg(model,
                                                 test_dataset,
                                                 label=config.label,
                                                 filename='_it_' + str(step),
                                                 filepath=RUN_DIR)
                    visualizer.generate_cluster(
                        model,
                        vary='zg',
                        filename='generate_cluster_fix_zl_it_' + str(step),
                        filepath=RUN_DIR)
                    visualizer.generate_cluster(
                        model,
                        vary='zg_zl',
                        filename='generate_cluster_it_' + str(step),
                        filepath=RUN_DIR)
                    visualizer.generate_cluster(
                        model,
                        vary='y_zg',
                        filename='generate_multi_cluster_it_' + str(step),
                        filepath=RUN_DIR)

            x_recon_train_loss.reset_states()
            x_kl_train_loss.reset_states()
            x_recon_test_loss.reset_states()
            x_kl_test_loss.reset_states()
            total_kl_test_loss.reset_states()
            total_kl_train_loss.reset_states()
            classifier_recon_acc.reset_states()
            classifier_random_z_g_acc.reset_states()
            classifier_random_z_l_acc.reset_states()
            classifier_cluster_acc.reset_states()
            gc.collect()
            start = time.time()
        if (step >= config.training_steps):
            print('Training done!')
            break

    model.save_weights('models/' + RUN_NAME + '.h5')
示例#10
0
def process(imdb, args, validation=False):

    if validation:
        # test on the validation set
        features = compute_features(imdb, args, useValSet=False)
        print('Experiment setup: trainining set: train, test set: val')
        clf = train_classifier(
            features[
                imdb.train_indices, :],  # get rows corresponding to training
            imdb.class_ids[imdb.train_indices],
            args)
        val_preds, val_scores = make_predictions(clf,
                                                 features[imdb.val_indices, :])
        if validation:
            return get_confusion(imdb.class_ids[imdb.val_indices], val_preds)
        #show_confusion(imdb.class_ids[imdb.val_indices], val_preds)

    else:
        features = compute_features(imdb, args, useValSet=True)
        # ensure that indices haven't been accidentally modified:
        assert imdb.train_indices[0] == 0 and imdb.train_indices[-1] == 297
        assert imdb.val_indices[0] == 1 and imdb.val_indices[-1] == 298
        assert imdb.test_indices[0] == 2 and imdb.test_indices[-1] == 299
        print('Experiment setup: trainining set: train+val, test set: test')
        clf = train_classifier(
            features[np.hstack((imdb.train_indices, imdb.val_indices)), :],
            imdb.class_ids[np.hstack(
                (imdb.train_indices, imdb.val_indices))], args)
        test_preds, test_scores = make_predictions(
            clf, features[imdb.test_indices, :])
        show_confusion(imdb.class_ids[imdb.test_indices], test_preds)

        # confusion matrix of images: (store their indices in imdb.test_indices)
        # find first cat and first dog:
        cat, dog = -1, -1
        for i in range(len(
                imdb.test_indices)):  # location in imdb.test_indices
            if cat == -1 and imdb.class_ids[imdb.test_indices[i]] == 0:
                cat = i
            if dog == -1 and imdb.class_ids[imdb.test_indices[i]] == 1:
                dog = i
        top = np.array([[cat, cat], [dog, dog]])
        for i in range(len(
                imdb.test_indices)):  # location in imdb.test_indices
            # cat: 0, dog: 1 (labels)
            ans = imdb.class_ids[imdb.test_indices[i]]
            pred = test_preds[i]
            score = test_scores[i]
            if ans == 0 and pred == 0:  # look for most cat-like cat
                if score > test_scores[top[0, 0]]:
                    top[0, 0] = i
            if ans == 0 and pred == 1:  # look for most dog-like cat
                if score > test_scores[top[0, 1]]:
                    top[0, 1] = i
            if ans == 1 and pred == 1:  # look for most dog-like dog
                if score > test_scores[top[1, 1]]:
                    top[1, 1] = i
            if ans == 1 and pred == 0:  # look for most cat-like dog
                if score > test_scores[top[1, 0]]:
                    top[1, 0] = i
        # show the top images side by side
        fig, axarr = plt.subplots(2, 2, figsize=(5, 5))
        for i in range(0, 2):
            for j in range(0, 2):
                img = cv2.imread(imdb.image_dir + "/" +
                                 imdb.image_names[imdb.test_indices[top[i,
                                                                        j]]])
                axarr[i, j].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        fig.savefig("confusion.jpg", dpi=fig.dpi)
        plt.show()
示例#11
0
def main(args):
    parser = argparse.ArgumentParser(
        description='Train and evaluate a model on the Cats vs. Dogs dataset')

    parser.add_argument('-d',
                        '--dataset-dir',
                        required=True,
                        type=str,
                        help='Path to the dataset')
    parser.add_argument('-f',
                        '--feature',
                        required=True,
                        choices=FEATURES,
                        help='Select which feature representation to use. '
                        'Choices are {' + ', '.join(FEATURES) + '}')
    parser.add_argument('-c',
                        '--classifier',
                        required=True,
                        choices=CLASSIFIERS,
                        help='Select which classifier to use. '
                        'Choices are {' + ', '.join(CLASSIFIERS) + '}')
    parser.add_argument('-k',
                        '--knn-k',
                        default=3,
                        type=int,
                        help='Number of neighbors for kNN classifier')
    parser.add_argument('-l',
                        '--svm-lambda',
                        default=1.0,
                        type=float,
                        help='Lambda paramter for SVM')
    parser.add_argument('--tinyimage-patchdim', default=16, type=int)
    parser.add_argument('--patches-dictionarysize', default=128, type=int)
    parser.add_argument('--patches-radius', default=8, type=float)
    parser.add_argument('--patches-stride', default=12, type=int)
    parser.add_argument('--sift-dictionarysize', default=128, type=int)
    parser.add_argument('--sift-binsize',
                        default=8,
                        type=int,
                        help='Size of the bin in terms of number of pixels in '
                        'the image. Recall that SIFT has 4x4=16 bins.')
    parser.add_argument('--sift-stride',
                        default=12,
                        type=int,
                        help='Spacing between succesive x (and y) coordinates '
                        'for sampling dense features.')

    args = parser.parse_args(args)

    imdb = read_dataset(args.dataset_dir)

    features = compute_features(imdb, args)

    if args.feature != 'tinyimage':
        features = normalize_features(features)

    print(f'Experiment setup: trainining set: train, test set: val')
    clf = train_classifier(features[imdb.train_indices, :],
                           imdb.class_ids[imdb.train_indices], args)
    val_preds, val_scores = make_predictions(clf,
                                             features[imdb.val_indices, :])
    show_confusion(imdb.class_ids[imdb.val_indices], val_preds)

    print(f'Experiment setup: trainining set: train+val, test set: test')
    clf = train_classifier(
        features[np.hstack((imdb.train_indices, imdb.val_indices)), :],
        imdb.class_ids[np.hstack(
            (imdb.train_indices, imdb.val_indices))], args)
    test_preds, test_scores = make_predictions(clf,
                                               features[imdb.test_indices, :])
    show_confusion(imdb.class_ids[imdb.test_indices], test_preds)
示例#12
0
    train_dir = CKPT_ROOT + args.run_name
    for action in args.action.split(','):
        tf.reset_default_graph()
        if action == 'train_gan':
            train(train_dir)
        elif action == 'generate':
            if args.dataset == 'imagenet':
                generate_imagenet(train_dir)
            else:
                generate(train_dir)
        elif action == 'train_all_classifiers':
            cfg = classifier.get_config(args.dataset, args.image_size)
            cfg.training.split = 'gan_100_' + args.run_name
            cfg.evaluation.split = args.test_split
            train_acc = classifier.train_classifier(train_dir+"_resnet_classifier", cfg)
            log.info("Accuracy (GAN-train) = %.4f", train_acc)
            tf.reset_default_graph()

            cls_train_dir = CKPT_ROOT + args.dataset + '_classifier_ms_decay'
            if args.train_split == 'train_shuffled':
                cls_train_dir = CKPT_ROOT + args.dataset + '_shuffled_classifier_ms_decay'
            cfg.training.split = args.train_split
            cfg.evaluation.split = 'gan_100_' + args.run_name
            rev_acc = classifier.evaluate_classifier(cls_train_dir, cfg)
            log.info("Accuracy (GAN-test) = %.4f", rev_acc)

            # Final summary
            log.info("Summary for classification experiments on %s:"
                     "\n | Acc (GAN-train) | Acc (GAN-test) |"
                     "\n | %.4f | %.4f |", args.run_name, train_acc, rev_acc)
示例#13
0
    
    # If this module is called directly, train the classifier and check the 
    # features selected.
    
    import classifier as cs
    import pickle
    import search_and_classify as sc

    print('Training SVM classifier')
    cs.check_datasets()    

    svc, X_scaler = cs.train_classifier(   cars, 
                                        notcars,
                                        color_space = color_space, 
                                        spatial_size = spatial_size, 
                                        hist_bins = hist_bins, 
                                        orient = orient, 
                                        pix_per_cell = pix_per_cell, 
                                        cell_per_block = cell_per_block, 
                                        hog_channel = hog_channel, 
                                        spatial_feat = spatial_feat, 
                                        hist_feat = hist_feat, 
                                        hog_feat = hog_feat)
    
    pickle.dump( (svc, X_scaler), open( "trained_svc.p", "wb" ) )

    cs.test_hog_features()
    
    sc.plot_grid()        
    sc.test_search_and_classify()
    
示例#14
0
def main():

	global X_scaler
	global svc
	global prev_labels
	prev_labels=None

	# if we have already trained the classifier just load from disk
	if not os.path.exists('./classifier.pkl'):

		print('-'*50)
		print('Training classifier now')

		# get vehicle/non-vehicle examples for SVM
		cars,notcars = getDatabaseStruct()

		# classifier parameters
		color_space = 'YCrCb'
		orient=9
		pix_per_cell=8
		cell_per_block=2
		hog_channel='ALL'
		spatial_size=(32,32)
		hist_bins=32
		spatial_feat=True
		hist_feat=True
		hog_feat=True

		# --------------------------------------------------------------------------------------------------
		# train classifier 

		t = time.time()
		n_samples = len(cars)
		random_idxs = np.random.randint(0,len(cars), n_samples)

		test_cars = cars#np.array(cars)[random_idxs]
		test_notcars = notcars#np.array(notcars)[random_idxs]

		car_features = extract_features(test_cars,color_space,spatial_size,hist_bins,
									orient,pix_per_cell,cell_per_block,hog_channel,spatial_feat,
									hist_feat,hog_feat)

		notcar_features = extract_features(test_notcars,color_space,spatial_size,hist_bins,
										orient,pix_per_cell,cell_per_block,hog_channel,spatial_feat,
										hist_feat,hog_feat)

		print(time.time()-t, 'Seconds to compute features ...')

		svc , X_scaler = train_classifier(car_features,notcar_features,
									orient,pix_per_cell,cell_per_block,hist_bins,spatial_size)

		with open('classifier.pkl','wb') as f:
			pickle.dump(svc,f)
		with open('scaler.pkl','wb') as f:
			pickle.dump(X_scaler,f)

	else:
		with open('classifier.pkl','rb') as f:
			svc = pickle.load(f)
		with open('scaler.pkl','rb') as f:
			X_scaler = pickle.load(f)

	# ---------------------------------------------------------------------------------------------------
	# Per image hog

	# extractEntireHogMap(example_images[1],window=64)
	# process_image(example_images[3])


	print('-'*50)
	print('Processing project video')

	output = 'output2.mp4'
	clip = VideoFileClip("project_video.mp4")
	test_clip = clip.fl_image(process_image)
	test_clip.write_videofile(output,audio=False)