Exemplo n.º 1
0
def main():
    device = randomseed()

    # exit()

    print(
        '1: temp test, 2: real train, 3: train continue, 4: feature extraction, 5: Batch size tester, else: exit'
    )
    x = int(input())
    global hyparam

    hyparam = open_yaml('./hyparam.yaml')
    hyparam['device'] = device

    if x == 1:
        temp_train_prepare(hyparam)
    elif x == 2:
        train_prepare(hyparam)
    elif x == 3:
        _ = randomseed(seed=7)
        train_prepare(hyparam, train_continue=True)
    elif x == 4:
        feature_extraction.extraction(hyparam)
    elif x == 5:
        x = batch_size_tester(hyparam, max_size=256)
        x.forward()
    else:
        print('exit!!')
Exemplo n.º 2
0
def predict_one(image_path):
    """
    Function takes in a image_path, and classifies what trick is being done.
    Args:
        Image path, does not need to be a string type

    Returns:
        Predicted class, and Probability of prediction. Also the image being
        pushed into the function
    """
    """
    The re-size will give not the image_path, so we need to save the resized image_path
    to a file location and use that new file location to be added into the feature vector object
    """

    re_size = image_resize([image_path])
    predicted_image_path = 'one_prediction/predicted.jpg'
    feature_vector = feature.extraction([predicted_image_path])
    model_path = 'pickle_files/svm_model.pkl'
    with open(model_path, 'rb') as s:
        model = pickle.load(s)

    prediction_class = model.predict(feature_vector)
    prediction = model.predict_proba(feature_vector)

    print('\nYour prediction...')
    if prediction_class == 'kickflip':
        return prediction_class[0], prediction[0][0]
        # image_show(image_path)
    else:
        return prediction_class[0], prediction[0][1]
def combine_test_report(folders, modification, ofile=None):
    """
    Prints and stores classification report for the
    input data.

    Parameters
    ----------
    folders:
        List of input folders. Each folder is a different class.

    ofile:
        Name of the output file that contains the classification report

    modification:
        Dictionary that contains all config parameters for reproducibility
        + a Classifier key that contains the classifier to be used.

    Returns
    -------

    Classification report as produced from sklearn.metrics.classification_report
    """

    model = modification['Classifier']

    print('Extracting features...')
    X, y = feature_extraction.extraction(folders, modification)
    print("Detailed classification report:")
    y_true, y_pred = y, model.predict(X)
    print(classification_report(y_true, y_pred))
    timestamp = time.ctime()
    if ofile is None:
        ofile = f"SVM_classification_report_{timestamp}.pt"
        ofile = ofile.replace(' ', '_')
    else:
        ofile = str(ofile)
        if '.pt' not in ofile or '.pkl' not in ofile:
            ofile = ''.join([ofile, '.pt'])

    if not os.path.exists(VARIABLES_FOLDER):
        os.makedirs(VARIABLES_FOLDER)
    out_path = os.path.join(
        VARIABLES_FOLDER, ofile)
    print(f"\nSaving classification report to: {out_path}\n")

    pickle.dump(classification_report, open(out_path, 'wb'))

    return classification_report
Exemplo n.º 4
0
def run_program(examples, lookbacks, projection_length, sentiment,
                smart_feature_extraction):
    if sentiment == 0:
        input_data = all_input_data([
            "gspc.csv", "financials.csv", "totals.csv", "30yearTbill.csv",
            "10yearTbill.csv", "2yearTbill.csv"
        ], examples, sentiment)
    else:  # Only process sentiment data if user input says to
        input_data = all_input_data([
            "gspc.csv", "financials.csv", "totals.csv", "sentiment2.csv",
            "30yearTbill.csv", "10yearTbill.csv", "2yearTbill.csv"
        ], examples, sentiment)
    second_input = fe.extraction(input_data, smart_feature_extraction)
    input_labels, stock_returns = get_input_labels("gspc.csv", examples,
                                                   lookbacks,
                                                   projection_length,
                                                   sentiment)
    input_data = second_input
    return input_data, input_labels, stock_returns
Exemplo n.º 5
0
def predict(ifile, modification):
    """
    Predicts and prints the predicted class for the input file.

    Parameters
    ----------
    ifile:
        Filename of the input file.

    modification:
        Dictionary that contains all config parameters for reproducibility
        + a Classifier key that contains the classifier to be used.
    """

    model = modification['Classifier']

    print('Extracting features...')
    X = feature_extraction.extraction(ifile,
                                      modification,
                                      folders=False,
                                      show_hist=False)

    y_pred = model.predict(X)
    print("Predicted class: {}".format(y_pred[0]))
Exemplo n.º 6
0
        # classifiers = ['svm', 'lda', 'knn']

        features_list = ['color_histogram', 'hu_moments']
        classifiers = ['svm', 'lda', 'knn']

        preds = []
        pred_probs = []

        for descriptor in features_list:

            X_train = []
            y_train = []
            X_test = []
            y_test = []

            X_train, y_train = extraction("data/Train/", descriptor)
            X_test, y_test = extraction("data/Valid/", descriptor)

            scaler = preprocessing.MinMaxScaler()
            X_train = scaler.fit_transform(X_train)
            X_test = scaler.transform(X_test)

            X_train = np.asarray(X_train)
            X_test = np.asarray(X_test)

            for method in classifiers:

                clf = classifier(method, X_train, y_train)

                y_pred = clf.predict(X_test)
                y_predProb = clf.predict_proba(X_test)
Exemplo n.º 7
0
		# classifiers = ['svm', 'lda', 'knn']

		features_list = ['color_histogram', 'hu_moments']
		classifiers = ['svm', 'lda', 'knn']

		preds = []
		pred_probs = []

		for descriptor in features_list :

			X_train = []
			y_train = []
			X_test = []
			y_test = []

			X_train, y_train = extraction ("data/Train/", descriptor)
			X_test, y_test = extraction ("data/Valid/", descriptor)

			scaler = preprocessing.MinMaxScaler()
			X_train = scaler.fit_transform(X_train)
			X_test = scaler.transform(X_test)

			X_train = np.asarray(X_train)
			X_test = np.asarray(X_test)

			for method in classifiers:

				clf = classifier (method, X_train, y_train)

				y_pred = clf.predict(X_test) 
				y_predProb = clf.predict_proba(X_test) 
Exemplo n.º 8
0
def train(folders, ofile=None):
    """
    Trains a classifier using combined features (pyAudioAnalysis & CNN
    models' fetures) and GridSearchCV to find best parameters. Reads
    config.yaml to set running parameters.

    Parameters
    ----------

    folders:
        List of input folders. Each folder is a different class.

    ofile:
        Output model name


    Returns
    -------

    modification:
        Dictionary that contains all config parameters for reproducibility
        + a Classifier key that contains the final classifier.
    """

    with open(r'combine/config.yaml') as file:
        modification = yaml.load(file, Loader=yaml.FullLoader)

    if modification['which_classifier']['type'] == 'svm':
        classifier_parameters = modification['which_classifier']['parameters']
        kernel = classifier_parameters['kernel']
        metric = classifier_parameters['metric']
    else:
        print('Supports only SVM classifier')
        return modification

    print('\nExtracting features...')
    if modification['extract_nn_features'] and 'dim_reduction' not in modification:
        X, y, pcas = feature_extraction.extraction(folders, modification)
        modification['dim_reduction'] = pcas
    else:
        X, y = feature_extraction.extraction(folders, modification)
    print('X (train data) shape: {}'.format(X.shape))
    print('y (train labels) shape: {}'.format(y.shape))

    clf = svm.SVC(kernel=kernel, class_weight='balanced')
    svm_parameters = {'gamma': ['auto', 1e-3, 1e-4, 1e-5, 1e-6],
                      'C': [1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3]}

    print('The classifier is an SVM with {} kernel '.format(kernel))
    pipe = Pipeline(steps=[('SVM', clf)],
                    memory='sklearn_tmp_memory')
    print('Running GridSearchCV to find best SVM parameters...')

    cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3)

    grid_clf = GridSearchCV(
        pipe, dict(SVM__gamma=svm_parameters['gamma'],
                   SVM__C=svm_parameters['C']), cv=cv,
                   scoring=metric, n_jobs=-1)

    grid_clf.fit(X, y)

    clf = grid_clf.best_estimator_
    clf_params = grid_clf.best_params_
    clf_score = grid_clf.best_score_
    clf_stdev = grid_clf.cv_results_['std_test_score'][grid_clf.best_index_]

    print("Best parameters: {}".format(clf_params))
    print("Best validation score:      {:0.5f} (+/-{:0.5f})".format(clf_score, clf_stdev))

    timestamp = time.ctime()
    if ofile is None:
        ofile = f"{'SVM'}_{timestamp}.pt"
        ofile = ofile.replace(' ', '_')
    else:
        ofile = str(ofile)
        if '.pt' not in ofile or '.pkl' not in ofile:
            ofile = ''.join([ofile, '.pt'])

    if not os.path.exists(VARIABLES_FOLDER):
        os.makedirs(VARIABLES_FOLDER)
    out_path = os.path.join(
        VARIABLES_FOLDER, ofile)
    print(f"\nSaving model to: {out_path}\n")

    modification['Classifier'] = clf
    with open(out_path, 'wb') as handle:
        pickle.dump(modification, handle, protocol=pickle.HIGHEST_PROTOCOL)

    return modification