def read_class(standardize=False):
    """read featuers into X and labels for instruments family into y. if standardize set to true, then it will return a scaler that is
    used to transform the test dataset.
    """
    X, y = read_features()
    instruments = [ins[0] for ins in y]
    X = np.asarray(X)
    scaler = None
    if standardize:
        scaler = preprocessing.StandardScaler().fit(X)
        X = scaler.transform(X)
    y = np.asarray(instruments)
    return X, y, scaler
def read_class(standardize=False):
    """read featuers into X and labels for instruments family into y. if standardize set to true, then it will return a scaler that is
    used to transform the test dataset.
    """
    X, y = read_features()
    instruments = [ins[0] for ins in y]
    X = np.asarray(X)
    scaler = None
    if standardize:
        scaler = preprocessing.StandardScaler().fit(X)
        X = scaler.transform(X)
    y = np.asarray(instruments)
    return X, y, scaler
示例#3
0
        layers=[
            Layer("Tanh", units=12),
            Layer("Softmax")],
        learning_rate=0.005,
        n_iter=25)

    # gs = GridSearchCV(nn, param_grid={
    #     'learning_rate': [0.05, 0.01, 0.005, 0.001],
    #     'hidden0__units': [4, 8, 12,100],
    #     'hidden0__type': ["Rectifier", "Sigmoid", "Tanh"]})
    # gs.fit(X_train, y_train)
    # print(gs.best_estimator_)
    nn.fit(X_train, y_train)
    predicted = nn.predict(X_test).flatten()
    labels = y_test
    return predicted, labels

if __name__ == "__main__":
    features = ['zcr', 'rms', 'sc', 'sr', 'sf','mfcc']
    X, y = read_features(features)
    X = clean_data(X)
    # train_score, test_score = train_nolearn_model(X, y)
    # print("train score: ", train_score)
    # print("test score: ", test_score)

    predicted, true_value = train_sknn(X,y)
    print("predicted: ", predicted)
    print("true_value: ", true_value)
    print("accuracy: ", np.sum(predicted == true_value) / float(len(predicted)))
    #the test accuracy is only 38%, need more tunning!!!
示例#4
0
def run_cnn(dimension, _load=False):
    # intialize the log file for current run of the code
    utils.initialize_log()
    # read audio files and parse them or simply load from pre-extracted feature files
    if _load:
        dictionary, tr_mnn_features, tr_mnn_labels, ts_mnn_features, ts_mnn_name_list, tr_cnn_features, tr_cnn_labels, ts_cnn_features, ts_cnn_name_list = read_audio_files(
        )
    else:
        dictionary, tr_mnn_features, tr_mnn_labels, ts_mnn_features, ts_mnn_name_list, tr_cnn_features, tr_cnn_labels, ts_cnn_features, ts_cnn_name_list = features.read_features(
        )

    if dimension == 1:
        # call the 2d convolutional network code here
        cnn_1d_probs = train.keras_convolution_1D(tr_cnn_features,
                                                  tr_cnn_labels,
                                                  ts_cnn_features,
                                                  len(dictionary),
                                                  training_epochs=50)
        # get top three predictions
        top3 = cnn_1d_probs.argsort()[:, -3:][:, ::-1]
    else:
        # call the 2d convolutional network code here
        cnn_2d_probs = train.keras_convolution_2D(tr_cnn_features,
                                                  tr_cnn_labels,
                                                  ts_cnn_features,
                                                  len(dictionary),
                                                  training_epochs=50)
        # get top three predictions
        top3 = cnn_2d_probs.argsort()[:, -3:][:, ::-1]
    # print the predicted results to a csv file.
    utils.print_csv_file(top3, ts_cnn_name_list, dictionary, OUTPUT_CSV)
from sklearn.ensemble import RandomForestClassifier
import random

from features import read_features

def get_class(num):
    if num < 0.17:
        return 0
    elif num < 0.5:
        return 1
    elif num < 0.83:
        return 2
    else:
        return 3

data, labels = read_features()
order = random.sample(range(len(data)), len(data))
train_idx = order[:14570]
test_idx = order[14570:]
# 2 classes
labels = [round(label) for label in labels]
# 4 classes
labels = [get_class(label) for label in labels]
train_data = []
train_labels = []
test_data = []
test_labels = []
for idx,d in enumerate(data): 
    if idx in train_idx:
        train_data.append(d)
        train_labels.append(labels[idx])
示例#6
0
        print("For C: %f, train_score=%f, test_score=%f" %
              (c, train_score, test_score))
        print()


def rf_tuning(X, y):
    n_trees = [100, 500, 800]
    for n in n_trees:
        rf = RandomForestClassifier(n_estimators=n)
        test_score, train_score, cms = train_model(X, y, rf)
        print(rf.feature_importances_)
        print("For n_tree: %f, train_score=%f, test_score=%f" %
              (n, train_score, test_score))
        print(np.sum(cms, axis=0))
        print()


if __name__ == "__main__":
    features = ['zcr', 'rms', 'sc', 'sr', 'sf', 'mfcc']
    X, y = read_features(features)
    # print(X.shape)
    X = clean_data(X)

    #find_best_k(X,y)
    rf_tuning(X, y)

    #plot learning curve for 8nn
    # clf = KNeighborsClassifier(n_neighbors=8, weights = 'distance')
    # crossvalidation = cross_validation.StratifiedKFold(y, n_folds=5)
    # plot_learning_curve(clf, "knn_learning_curve", X, y, cv= crossvalidation, n_jobs=4)
示例#7
0
def main(_load = False):
    # intialize the log file for current run of the code
    utils.initialize_log()  
    # read audio files and parse them or simply load from pre-extracted feature files
    if _load:
        dictionary, tr_mnn_features, tr_mnn_labels, ts_mnn_features, ts_mnn_name_list, tr_cnn_features, tr_cnn_labels, ts_cnn_features, ts_cnn_name_list  = read_audio_files()  
    else:
        dictionary, tr_mnn_features, tr_mnn_labels, ts_mnn_features, ts_mnn_name_list, tr_cnn_features, tr_cnn_labels, ts_cnn_features, ts_cnn_name_list  = features.read_features()
    # print a log message for status update
    utils.write_log_msg("starting multi-layer neural network training...")
    # use the above extracted features for the training of the model
    predictions_top3 = train.train(tr_mnn_features, tr_mnn_labels, ts_mnn_features, tr_cnn_features, tr_cnn_labels, ts_cnn_features, n_classes=len(dictionary))
    # print a log message for status update
    utils.write_log_msg("outputing prediction results to a csv file...")
    # print the predicted results to a csv file.
    utils.print_csv_file(predictions_top3, ts_mnn_name_list, dictionary, OUTPUT_CSV)
    # print a log message for status update
    utils.write_log_msg("done...")
示例#8
0
if __name__ == '__main__':
	from sys import argv

	# argv check / usage info	
	if len(argv) < 2:
		print("Usage: {} image_path".format(argv[0]))
		exit(1)

	# Read the image
	img = cv2.imread(argv[1])
	if img is None:
		print("Error loading image")
		exit(1)
	# Get the features from the file
	fsize, features = read_features('numbers.feat')
	
	# Get all contours from the image
	contours = get_all_contours(img)

	# Loop through all contours
	for contour in contours:
		# Get the symbol and attempt to match it to a known feature
		symbol = scale_symbol(img, contour, (fsize,fsize))
		ret = recognize_symbol(features, symbol, fsize)
		
		# Output the results
		if ret is not None:
			print("identified symbol: '{}'".format(ret))
			pyplot.imshow(symbol)
			pyplot.show()