Example #1
0
def train_test(features_vector: FeatureVector, classifier: Classifier,
               data: ClassifierData):
    # Build train features vector
    with Timer('building train features', VERBOSE):
        x_train_features = features_vector.convert_to_features(
            data.x_train, VERBOSE)

    # Train
    with Timer('training', VERBOSE):
        classifier.train(x_train_features, data.y_train)

    # Build test features vector
    with Timer('building test features', VERBOSE):
        x_test_features = features_vector.convert_to_features(
            data.x_test, VERBOSE)

    # Test
    if VERBOSE:
        print(features_vector.name)
        print(classifier.report(x_test_features, data.y_test))

    return classifier.f1_micro(x_test_features, data.y_test)
    # anlz = Analyzer(tr_path,
    #                 te_path,
    #                 cfg['data']['classes_list'],
    #                 cfg['analysis']['figures_path'])
    # anlz.run()

    ##############################################################
    #                        CNN MODEL                           #
    ##############################################################
    train_path = os.path.join(cfg['data']['sorted_path'], 'training')
    test_path = os.path.join(cfg['data']['sorted_path'], 'test')
    fig_full_path = os.path.abspath(cfg['model']['figures_path'])
    model_full_path = os.path.abspath(cfg['model']['models_path'])
    weights_full_path = os.path.abspath(cfg['model']['weights_path'])
    training_size = 18966
    test_size = 4742

    cnn = Classifier(train_path,
                     test_path,
                     training_size,
                     test_size,
                     cfg['data']['classes_list'],
                     cfg['model'],
                     fig_path=fig_full_path)
    cnn.compile()
    cnn.plot_model()
    cnn.train()
    cnn.show_training_history()
    cnn.plot_confusion_matrix((4742 // 32 + 1))
    cnn.save_model(model_full_path)