Beispiel #1
0
def plot_attAUC(GT, attributepattern, clf):
    AUC = []
    P = np.loadtxt(attributepattern)
    attributes = get_attributes()

    # Loading ground truth
    test_index = bzUnpickle('./CreatedData/test_features_index.txt')
    test_attributes = get_class_attributes('./Classes/', name='test')
    _, y_true = create_data('./CreatedData/test_featuresVGG19.pic.bz2',
                            test_index, test_attributes)

    for i in range(y_true.shape[1]):
        fp, tp, _ = roc_curve(y_true[:, i], P[:, i])
        roc_auc = auc(fp, tp)
        AUC.append(roc_auc)
    print("Mean attrAUC %g" % (np.nanmean(AUC)))

    xs = np.arange(y_true.shape[1])
    width = 0.5

    fig = plt.figure(figsize=(15, 5))
    ax = fig.add_subplot(1, 1, 1)
    rects = ax.bar(xs, AUC, width, align='center')
    ax.set_xticks(xs)
    ax.set_xticklabels(attributes, rotation=90)
    ax.set_ylabel("area under ROC curve")
    autolabel(rects, ax)
    plt.savefig('results/AwA-AttAUC-DAP-%s.pdf' % clf)
Beispiel #2
0
def indirectAttributePrediction(classifier='SVM'):
    # Get features index to recover samples
    train_index = bzUnpickle('./CreatedData/train_features_index.txt')
    test_index = bzUnpickle('./CreatedData/test_features_index.txt')

    # Get classes-attributes relationship
    train_attributes = get_class_attributes('./', name='train')
    test_attributes = get_class_attributes('./', name='test')

    # Create training Dataset
    print('Creating training dataset...')
    X_train, a_train = create_data('./CreatedData/train_featuresVGG19.pic.bz2',
                                   train_index, train_attributes)
    y_train = []
    for (animal, num) in train_index:
        y_train += num * [animal_dict[animal]]
    y_train = np.array(y_train)

    print('X_train to dense...')
    X_train = X_train.toarray()

    print('Creating test dataset...')
    X_test, a_test = create_data('./CreatedData/test_featuresVGG19.pic.bz2',
                                 test_index, test_attributes)

    print('X_test to dense...')
    X_test = X_test.toarray()

    clf = SVMClassifierIAP(n_components=100, C=1.0)

    print('Training model... (takes around 10 min)')
    t0 = time()
    clf.fit(X_train, y_train)
    print('Training finished in', time() - t0)
    y_pred = clf.predict(X_test)
    y_proba = clf.predict_proba(X_test)

    print('Saving files...')
    np.savetxt('./IAP/prediction_SVM', y_pred)
    np.savetxt('./IAP/probabilities_SVM', y_proba)
def DirectAttributePrediction(classifier='SVM',
                              predicate_type='binary',
                              C=10.0):
    # Get features index to recover samples
    train_index = bzUnpickle('./CreatedData/train_features_index.txt')
    test_index = bzUnpickle('./CreatedData/test_features_index.txt')

    # Get classes-attributes relationship
    train_attributes = get_class_attributes('./',
                                            name='train',
                                            predicate_type=predicate_type)
    test_attributes = get_class_attributes('./',
                                           name='test',
                                           predicate_type=predicate_type)
    N_ATTRIBUTES = train_attributes.shape[1]

    # Create training Dataset
    print('Creating training dataset...')
    X_train, y_train = create_data('./CreatedData/train_featuresVGG19.pic.bz2',
                                   train_index, train_attributes)

    print('X_train to dense...')
    X_train = X_train.toarray()

    Xplat_train, Xplat_val, yplat_train, yplat_val = train_test_split(
        X_train, y_train, test_size=0.10, random_state=42)

    print('Creating test dataset...')
    X_test, y_test = create_data('./CreatedData/test_featuresVGG19.pic.bz2',
                                 test_index, test_attributes)
    y_pred = np.zeros(y_test.shape)
    y_proba = np.copy(y_pred)

    print('X_test to dense...')
    X_test = X_test.toarray()

    # CHOOSING SVM
    if classifier == 'SVM':
        platt_params = []
        for i in range(N_ATTRIBUTES):
            print('--------- Attribute %d/%d ---------' %
                  (i + 1, N_ATTRIBUTES))
            t0 = time()

            # SVM classifier
            if predicate_type == 'binary':
                clf = SVMClassifier()
            else:
                clf = SVMRegressor()

            # Training
            clf.fit(X_train, y_train[:, i])
            print('Fitted classifier in: %fs' % (time() - t0))
            if predicate_type == 'binary':
                clf.set_platt_params(Xplat_val, yplat_val[:, i])

            # Predicting
            print('Predicting for attribute %d...' % (i + 1))
            y_pred[:, i] = clf.predict(X_test)
            if predicate_type == 'binary':
                y_proba[:, i] = clf.predict_proba(X_test)

            print('Saving files...')
            np.savetxt('./DAP_' + predicate_type + '/prediction_SVM', y_pred)
            if predicate_type == 'binary':
                np.savetxt('./DAP_' + predicate_type + '/platt_params_SVM',
                           platt_params)
                np.savetxt('./DAP_' + predicate_type + '/probabilities_SVM',
                           y_proba)

    # CHOOSING NEURAL NETWORK
    if classifier == 'NN':
        if predicate_type != 'binary':
            clf = NeuralNetworkRegressor(dim_features=X_train.shape[1],
                                         nb_attributes=N_ATTRIBUTES)
        else:
            clf = NeuralNetworkClassifier(dim_features=X_train.shape[1],
                                          nb_attributes=N_ATTRIBUTES)

        print('Fitting Neural Network...')
        clf.fit(X_train, y_train)

        print('Predicting attributes...')
        y_pred = np.array(clf.predict(X_test))
        y_pred = y_pred.reshape((y_pred.shape[0], y_pred.shape[1])).T
        y_proba = y_pred

        print('Saving files...')
        np.savetxt('./DAP_' + predicate_type + '/prediction_NN', y_pred)
        if predicate_type == 'binary':
            np.savetxt('./DAP_' + predicate_type + '/probabilities_NN',
                       y_proba)
Beispiel #4
0
def DirectAttributePrediction(predicate_type='binary'):
    # Get features index to recover samples
    train_index = bzUnpickle('./CreatedData/train_features_index.txt')
    test_index = bzUnpickle('./CreatedData/test_features_index.txt')
    val_index = bzUnpickle('./CreatedData/validation_features_index.txt')
    # Get classes-attributes relationship
    train_attributes = get_class_attributes('./Classes/',
                                            name='train',
                                            predicate_type=predicate_type)
    test_attributes = get_class_attributes('./Classes/',
                                           name='test',
                                           predicate_type=predicate_type)
    N_ATTRIBUTES = train_attributes.shape[1]

    # Create training Dataset
    print('Creating training dataset...')
    X_train, y_train = create_data('./CreatedData/train_featuresVGG19.pic.bz2',
                                   train_index, train_attributes)

    print('Creating seen test dataset...')
    X_test_seen, y_test_seen = create_data(
        './CreatedData/validation_featuresVGG19.pic.bz2', val_index,
        train_attributes)
    y_pred_ = np.zeros(y_test_seen.shape)
    y_proba_ = np.copy(y_pred_)

    print('X_train to dense...')
    X_train = X_train.toarray()

    print('X_test_seen to dense...')
    X_test_seen = X_test_seen.toarray()

    print('Creating test dataset...')
    X_test, y_test = create_data('./CreatedData/test_featuresVGG19.pic.bz2',
                                 test_index, test_attributes)
    y_pred = np.zeros(y_test.shape)
    y_proba = np.copy(y_pred)

    print('X_test to dense...')
    X_test = X_test.toarray()

    if predicate_type != 'binary':
        clf = NeuralNetworkRegressor(dim_features=X_train.shape[1],
                                     nb_attributes=N_ATTRIBUTES)
    else:
        clf = NeuralNetworkClassifier(dim_features=X_train.shape[1],
                                      nb_attributes=N_ATTRIBUTES)

    print('Fitting Neural Network...')
    # fix random seed for reproducibility
    # seed = 7
    # numpy.random.seed(seed)
    # X_train_, X_test_, y_train_, y_test_ = train_test_split(X_train, y_train, test_size=1, random_state=seed)
    his = clf.fit(X_train, y_train)

    print('Predicting attributes...')
    y_pred = np.array(clf.predict(X_test))
    y_pred = y_pred.reshape((y_pred.shape[0], y_pred.shape[1])).T
    y_proba = y_pred

    y_pred_ = np.array(clf.predict(X_test_seen))
    y_pred_ = y_pred_.reshape((y_pred_.shape[0], y_pred_.shape[1])).T
    y_proba_ = y_pred_

    print('Saving files...')
    np.savetxt('./DAP_' + predicate_type + '/prediction_NN', y_pred)
    np.savetxt('./DAP_' + predicate_type + '/xprediction_NN', y_pred_)
    if predicate_type == 'binary':
        np.savetxt('./DAP_' + predicate_type + '/probabilities_NN', y_proba)
        np.savetxt('./DAP_' + predicate_type + '/xprobabilities_NN', y_proba_)