Exemplo n.º 1
0
def plotfeats(dict_train_features, mask=arange(10) + 8):
    for feat_index, feat in enumerate(mask):

        plt.figure(figsize=(10, 10))
        for i in arange(len(classes())):
            sns.distplot(dict_train_features[i][:, feat],
                         label='Histogram for {} of feature {}'.format(
                             classes()[i],
                             featuresnames()[feat]))

        plt.legend()
        plt.grid(True)

    for d, c in enumerate(mask):
        plt.figure(figsize=(10, 10))
        for i in arange(len(classes())):
            plt.subplot(len(classes()), 1, i + 1)
            plt.ylim(0, 1)
            feature = dict_train_features[i].transpose()[c]
            plt.stem(feature,
                     markerfmt='C3.',
                     basefmt='C9-',
                     use_line_collection=True)
            if i == 0:
                plt.title(featuresnames()[c] + ' coefficients')
            plt.ylabel(classes()[i])
            plt.grid(True)
    plt.show()
Exemplo n.º 2
0
def print_features(featurelst):
    print('\n\nSelected matrix:')
    print([featuresnames()[i] for i in featurelst['selectedcolumns']])
    print(featurelst['featurematrix'])
    print('\nfeature scores:')
    print(featuresnames())
    print(featurelst['scores'])
    print('\nselected features')
    print([featuresnames()[i] for i in featurelst['selectedcolumns']])
Exemplo n.º 3
0
def plotfeature(feat, i=-1):
    plt.figure(figsize=(18, 4))
    plt.subplot(1, 1, 1)
    plt.stem(feat, use_line_collection=True)
    if i != -1:
        plt.ylabel(featuresnames()[i] + ' coefficients')
        plt.title(featuresnames()[i] + ' coefficients {}'.format(classes()[i]))
    plt.grid(True)
    plt.show()
Exemplo n.º 4
0
def plotfeats_per_class(dict_train_features):
    for c in arange(len(classes())):
        plt.figure(figsize=(10, 10))
        for i in arange(len(featuresnames())):
            plt.subplot(len(featuresnames()), 1, i + 1)
            plt.ylim(0, 1)
            feature = dict_train_features[c].transpose()[i]
            plt.stem(feature, use_line_collection=True)
            if i == 0:
                plt.title(classes()[c] + ' coefficients')
            plt.ylabel(featuresnames()[i])
            plt.grid(True)
        plt.show()
def traintest(path):
    dict_features = trainingloop.getdicttrainfeatures(
        path)  # compute train features
    classes = user_interface.classes()
    test_size = user_interface.test_size()

    X = [dict_features[c] for c in classes]
    y = [np.ones(X[i].shape[0], ) * i for i in np.arange(len(classes))]  # keys
    y_mc = np.concatenate((y[0], y[1], y[2]), axis=0)
    X_mc = np.concatenate((X[0], X[1], X[2]), axis=0)

    X_train, X_test, y_train, y_test = train_test_split(X_mc,
                                                        y_mc,
                                                        test_size=test_size)

    dict_train_feats = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
    dict_test_feats = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
    n_features = len(user_interface.featuresnames())

    for c in np.arange(len(classes)):
        condition = np.mod(y_train, 3) == c
        n_train = len(y_train[condition])

        train_feats = np.zeros((n_train, n_features))
        k = 0

        for i in np.arange(len(y_train)):
            if y_train[i] == c:
                train_feats[k, :] = X_train[i, :]
                k = k + 1
        dict_train_feats[classes[c]] = train_feats

    for c in np.arange(len(classes)):
        condition = np.mod(y_test, 3) == c
        n_test = len(y_test[condition])

        test_feats = np.zeros((n_test, n_features))
        k = 0
        for i in np.arange(len(y_test)):
            if y_test[i] == c:
                test_feats[k, :] = X_test[i, :]
                k = k + 1
        dict_test_feats[classes[c]] = test_feats

    savetraindata.save_datasets(dict_train_feats, dict_test_feats)

    return dict_train_feats, dict_test_feats
Exemplo n.º 6
0
def train():
    path = pathlib.Path(__file__).parent.absolute()
    classes = user_interface.classes()
    if user_interface.generate_datasets():
        main_traintest.traintest(path)
        dict_train_features = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
        for c in classes:
            dict_train_features[c] = dataloader.dict_train_features(c)
    else:
        dict_train_features = trainingloop.getdicttrainfeatures(
            path)  # compute train features
    X_train = [dict_train_features[c] for c in classes]
    y_train = [
        np.ones(X_train[i].shape[0], ) * i
        for i in np.arange(len(user_interface.classes()))
    ]  # keys
    feat_max = np.max(np.concatenate((X_train[0], X_train[1], X_train[2]),
                                     axis=0),
                      axis=0)
    feat_min = np.min(np.concatenate((X_train[0], X_train[1], X_train[2]),
                                     axis=0),
                      axis=0)
    X_train_normalized = [(X_train[c] - feat_min) / (feat_max - feat_min)
                          for c in np.arange(len(user_interface.classes()))
                          ]  # normalized matrix
    X_train_mc_normalized = np.concatenate(
        (X_train_normalized[0], X_train_normalized[1], X_train_normalized[2]),
        axis=0)
    y_train_mc = np.concatenate((y_train[0], y_train[1], y_train[2]), axis=0)

    featurelst = featureselection.getfeaturelist(
        X_train_mc_normalized, y_train_mc)  # feature selection
    if user_interface.do_plot():
        plotfeatures.plotfeats(X_train_normalized,
                               mask=np.arange(10) + 7)  # plot train features
    else:
        plotselected.plotsel(X_train_normalized, featurelst['selectedcolumns'])
    savetraindata.savedata(dict_train_features, featurelst, feat_max,
                           feat_min)  # save data
    print('All features')
    print(user_interface.featuresnames())
    print_feature_sel.print_features(featurelst)
    return True
Exemplo n.º 7
0
def getdicttrainfeatures(path):
    dict_train_features = {'NoFX': [], 'Distortion': [], 'Tremolo': []}
    fullpath = str(path) + '/environment/databases/train/{}'

    for c in user_interface.classes():  # loops over classes
        n_features = len(user_interface.featuresnames())
        train_root = fullpath.format(c)
        class_train_files = [
            f for f in os.listdir(train_root) if f.endswith('.wav')
        ]
        n_train = len(class_train_files)
        train_features = np.zeros((n_train, n_features))

        for index, f in enumerate(
                class_train_files):  # loops over all the files of the class
            audio, fs = librosa.load(os.path.join(train_root, f), sr=None)
            train_features[index, :] = features.getfeatures(audio)

        dict_train_features[c] = train_features
    return dict_train_features