Esempio n. 1
0
def main():
    base_dir = '/home/caio/workspace/laps/'
    proj_tsne = np.load(base_dir + 'laps_X_proj_tsne.npy')

    min0 = proj_tsne[:, 0].min()
    min1 = proj_tsne[:, 1].min()
    max0 = proj_tsne[:, 0].max()
    max1 = proj_tsne[:, 1].max()
    proj_tsne[:, 0] = (proj_tsne[:, 0] - min0) / (max0 - min0)
    proj_tsne[:, 1] = (proj_tsne[:, 1] - min1) / (max1 - min1)

    X_train = np.load(base_dir + 'laps_X_train.npy')
    # y_train = np.load('data/fm/y_proj.npy')

    clf1 = CLF()
    clf1.LoadSKLearn(base_dir + 'lr_clf.joblib', "LR")
    # Plots the projected points coulored according to the label assigned by
    # the classifier.
    # As it is the first projection plotted, the legend is also save into a
    # separate file
    y_pred = clf1.Predict(X_train)
    labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    path = base_dir + "projection_clf1_tsne.pdf"
    path_leg = base_dir + "projection_leg.pdf"
    title = "t-SNE Projection"
    PlotProjection(proj_tsne, y_pred, path, title, path_leg, labels)

    # Run boundary map construction function
    R = 200
    N = [5]
    grid1 = Grid(proj_tsne, R)

    _, dmap = grid1.BoundaryMap(X_train, N[0], clf1)

    np.save(base_dir + 'dmap_tsne_lr_R_50_N_10.npy', dmap)
Esempio n. 2
0
def main():
    # Will try to plot errors caused by classfier
    # using segmentation dataset
    with open("data/segmentation/seg.json") as f:
        data_json = json.load(f)

    proj = np.load(data_json["proj"])

    X_train = np.load(data_json['X_train'])
    y_train = np.load(data_json['y_train'])
    X_test = np.load(data_json['X_test'])
    y_test = np.load(data_json['y_test'])

    # Logistic regression had the lowest accuracy, so it will be used in this
    # test
    clf_logreg = CLF()
    clf_logreg.LoadSKLearn(data_json['clfs'][0], "Logistic Regression")

    y_pred = clf_logreg.Predict(X_train)
    path = "data/segmentation/projection_logreg_err.pdf"
    title = "LAMP Projection Error (Logistic Regression)"

    R = 50
    N = 1
    grid_logreg = Grid(proj, R)
    PlotProjectionErr(grid_logreg, proj, y_pred, y_train, path, title)
    print("Create grid logreg")
    _, dmap = grid_logreg.BoundaryMap(X_train, N, clf_logreg)

    fig_title = "{}x{} DenseMap Err ({} samples, {})".format(
        grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name)
    fig_name = "data/segmentation/DenseMap_Err_{}x{}_N_{}_dense_map_{}".format(
        grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name)
    miss_classified = proj[y_train != y_pred]
    PlotDenseMapErr(grid_logreg, dmap, proj, y_train, y_pred, fig_title,
                    fig_name)

    errmap = CalcErrMap(grid_logreg, dmap, X_train, y_train, clf_logreg)
    tmp_err = np.flip(errmap, axis=0)
    plt.xticks([])
    plt.yticks([])
    #tmp_dense = np.flip(dmap, axis=0)
    #tmp_dense = TransferFunc(tmp_dense, 0.7)
    #rgb_img = hsv_to_rgb(tmp_dense)
    #plt.imshow(rgb_img, interpolation='none')
    plt.imshow(tmp_err, interpolation='none')
    plt.show()
Esempio n. 3
0
def main():
    # Ideal path:
    # 1 - Load dataset, projection and a trained classifier
    with open("data/toy/toy.json") as f:
        data_json = json.load(f)

    proj = np.load(data_json["proj"])

    X_train = np.load(data_json['X_train'])
    y_train = np.load(data_json['y_train'])
    X_test = np.load(data_json['X_test'])
    y_test = np.load(data_json['y_test'])

    clf = CLF()
    clf.LoadSKLearn(data_json['clfs'][0], "Logistic Regression")

    # Plots the projected points coulored according to the label assigned by
    # the classifier.
    # As it is the first projection plotted, the legend is also save into a
    # separate file
    y_pred = clf.Predict(X_train)
    labels = ["0", "1"]
    path = "data/toy/projection_clf1_tsne.pdf"
    path_leg = "data/toy/projection_leg.pdf"
    title = "LAMP Projection"
    PlotProjection(proj, y_pred, path, title, path_leg, labels)

    #PlotMatrix(proj, X_train, clf)

    # 2 - Run boundary map construction function
    grid = Grid(proj, 100)
    num_samples = 5
    _, dmap = grid.BoundaryMap(X_train, num_samples, clf)
    fig_title = "{}x{} DenseMap ({} samples, {})".format(
        grid.grid_size, grid.grid_size, num_samples, clf.name)
    fig_name = "data/toy/DenseMap_{}x{}_N_{}_dense_map_{}".format(
        grid.grid_size, grid.grid_size, num_samples, clf.name)
    PlotDenseMap(dmap, fig_title, fig_name)
Esempio n. 4
0
# print("\ttime: ", time.time() - s)

print("\n\nTRAINING CLASSIFIER")
s = time.time()
lr = LogisticRegression()
lr.fit(X_train, y_train)
print("train acc: ", lr.score(X_train, y_train))

y_proj_pred = lr.predict(X_proj)
pred_path = base_dir + "y_pred_clf.npy"
np.save(pred_path, y_proj_pred)

clf_sklearn_path = base_dir + "lr.joblib"
joblib.dump(lr, clf_sklearn_path)

clf = CLF(clf=lr, clf_type='sklearn', clf_path=clf_sklearn_path)
clf_path = base_dir + 'lr.json'
clf.save_json(clf_path)
print("\ttime: ", time.time() - s)

N = 1
R = 100

print("\n\nGRID ILAMP tSNE")
grid_ilamp_tsne_path = base_dir + 'grid_ilamp_tsne.joblib'
save_grid(grid_ilamp_tsne_path, R, N, clf, ilamp_tsne, X_train,
          tsne_proj, clf_path, ilamp_tsne_path)
ui_grid_ilamp_tsne_path = base_dir + 'laps_features_ilamp_tsne.json'
save_json_ui(ui_grid_ilamp_tsne_path, grid_ilamp_tsne_path, clf_path, "ilamp",
             ilamp_tsne_path, train_y_path, pred_path)
X_test = scaler.transform(X_test)

clf_lr = linear_model.LogisticRegression()
clf_lr.fit(X_train, y_train)
print("train acc: ", clf_lr.score(X_train, y_train))
print("test acc: ", clf_lr.score(X_test, y_test))

tsne = manifold.TSNE(perplexity=10)
X_proj = tsne.fit_transform(X_train)
proj_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
X_proj = proj_scaler.fit_transform(X_proj)

colors = [COLORS[v] for v in y_train]
plt.scatter(X_proj[:, 0], X_proj[:, 1], color=colors)

clf = CLF(clf=clf_lr, clf_type="sklearn")
basedir = '/tmp/syn_test/'

R = 200
N = [5]
grid1 = Grid(X_proj, R)
_, dmap = grid1.BoundaryMap(X_train, N[0], clf)

H, W, _ = dmap.shape
GRID_SIZE = dmap.shape[0]

dist_nd = dist_maps.dist_nd(dmap, X_train, X_proj, clf=clf_lr)
dist_nd /= dist_nd.max()
dist_nd = 1.0 - dist_nd

dist_nd_2 = dist_maps.distance_nd_2(X_train, X_proj, clf_lr, GRID_SIZE)
Esempio n. 6
0
def main():
    # Ideal path:
    # 1 - Load dataset, projection and a trained classifier
    with open("data/segmentation/seg.json") as f:
        data_json = json.load(f)

    proj = np.load(data_json["proj"])

    X_train = np.load(data_json['X_train'])
    y_train = np.load(data_json['y_train'])
    X_test = np.load(data_json['X_test'])
    y_test = np.load(data_json['y_test'])

    clf_logreg = CLF()
    clf_logreg.LoadSKLearn(data_json['clfs'][0], "Logistic Regression")

    # Plots the projected points coulored according to the label assigned by
    # the classifier.
    # As it is the first projection plotted, the legend is also save into a
    # separate file
    y_pred = clf_logreg.Predict(X_train)
    labels = ["0", "1", "2", "3", "4", "5", "6"]
    path = "data/segmentation/projection_logreg.pdf"
    path_leg = "data/segmentation/projection_leg.pdf"
    title = "LAMP Projection (Logistic Regression)"
    PlotProjection(proj, y_pred, path, title, path_leg, labels)

    clf_svm = CLF()
    clf_svm.LoadSKLearn(data_json['clfs'][1], "SVM")
    y_pred = clf_svm.Predict(X_train)
    path = "data/segmentation/projection_svm.pdf"
    title = "LAMP Projection (SVM)"
    PlotProjection(proj, y_pred, path, title)

    clf_knn5 = CLF()
    clf_knn5.LoadSKLearn(data_json['clfs'][2], "KNN (5)")
    y_pred = clf_knn5.Predict(X_train)
    path = "data/segmentation/projection_knn5.pdf"
    title = "LAMP Projection (KNN)"
    PlotProjection(proj, y_pred, path, title)

    # 2 - Run boundary map construction function on clf_logreg
    R = 500
    N = 5
    grid_logreg = Grid(proj, R)
    print("Create grid logreg")
    _, dmap = grid_logreg.BoundaryMap(X_train, N, clf_logreg)

    fig_title = "{}x{} DenseMap ({} samples, {})".format(
        grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name)
    fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format(
        grid_logreg.grid_size, grid_logreg.grid_size, N, clf_logreg.name)
    PlotDenseMap(dmap, fig_title, fig_name)

    # Run boundary map construction function on clf_svm
    grid_svm = Grid(proj, R)
    print("Create grid svm")
    _, dmap = grid_svm.BoundaryMap(X_train, N, clf_svm)

    fig_title = "{}x{} DenseMap ({} samples, {})".format(
        grid_svm.grid_size, grid_svm.grid_size, N, clf_svm.name)
    fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format(
        grid_svm.grid_size, grid_svm.grid_size, N, clf_svm.name)
    PlotDenseMap(dmap, fig_title, fig_name)

    # Run boundary map construction function on clf_knn5
    grid_knn5 = Grid(proj, R)
    print("Create grid knn")
    _, dmap = grid_knn5.BoundaryMap(X_train, N, clf_knn5)

    fig_title = "{}x{} DenseMap ({} samples, {})".format(
        grid_knn5.grid_size, grid_knn5.grid_size, N, clf_knn5.name)
    fig_name = "data/segmentation/DenseMap_{}x{}_N_{}_dense_map_{}".format(
        grid_knn5.grid_size, grid_knn5.grid_size, N, clf_knn5.name)
    PlotDenseMap(dmap, fig_title, fig_name)
Esempio n. 7
0
def main():
    if len(sys.argv) < 2:
        print("Usage: ./fashion_mnist.py <base_dir>")
        sys.exit(0)
    base_dir = sys.argv[1]

    print("Load dataset\n\n")
    s = time.time()
    X_train, y_train = data.LoadMNISTData('train', base_dir + 'orig/')

    train_y_path = base_dir + "y_train.npy"
    np.save(train_y_path, y_train)

    X_test, y_test = data.LoadMNISTData('test', base_dir + 'orig/')

    X_nd = np.copy(X_train)
    X_nd = X_nd.reshape((X_nd.shape[0], X_nd.shape[1] * X_nd.shape[2]))

    projection_size = 60000
    X_proj = np.copy(X_train[:projection_size])
    new_shape = (X_proj.shape[0], X_proj.shape[1] * X_proj.shape[2])
    X_proj = np.reshape(X_proj, new_shape)
    print("\ttime: ", time.time() - s)

    scaler = MinMaxScaler(feature_range=(0, 1))

    print("TSNE Projection")
    s = time.time()
    tsne = TSNE(n_components=2,
                random_state=420,
                perplexity=25.0,
                n_iter=3000,
                n_iter_without_progress=300,
                n_jobs=4)
    tsne_proj = tsne.fit_transform(X_proj)
    tsne_proj = scaler.fit_transform(tsne_proj)
    print("\ttime: ", time.time() - s)

    print("UMAP Projection")
    s = time.time()
    umap_proj = UMAP(n_components=2,
                     random_state=420,
                     n_neighbors=5,
                     min_dist=0.3).fit_transform(X_proj)
    umap_proj = scaler.fit_transform(umap_proj)
    print("\ttime: ", time.time() - s)

    subset_size = 15000
    print("\n\nILAMP tSNE")
    s = time.time()
    k_ilamp = 20
    ilamp_tsne = ILAMP(n_neighbors=k_ilamp)
    ilamp_tsne.fit(X_proj[:subset_size], tsne_proj[:subset_size])
    ilamp_tsne_path = base_dir + "ilamp_tsne.joblib"
    ilamp_tsne.save(ilamp_tsne_path)
    print("\ttime: ", time.time() - s)

    print("\n\nILAMP UMAP")
    s = time.time()
    ilamp_umap = ILAMP(n_neighbors=k_ilamp)
    ilamp_umap.fit(X_proj[:subset_size], umap_proj[:subset_size])
    ilamp_umap_path = base_dir + "ilamp_umap.joblib"
    ilamp_umap.save(ilamp_umap_path)
    print("\ttime: ", time.time() - s)

    print("\n\nRBFInv CTRL PTS TSNE")
    s = time.time()
    EPS = 50000
    irbfcp_tsne = RBFInv(num_ctrl=400,
                         mode='rols',
                         kernel='gaussian',
                         eps=EPS,
                         normalize_c=True,
                         normalize_d=True)
    irbfcp_tsne.fit(X_proj[:subset_size], tsne_proj[:subset_size])
    irbfcp_tsne_path = base_dir + "irbfcp_tsne.joblib"
    irbfcp_tsne.save(irbfcp_tsne_path)
    print("\ttime: ", time.time() - s)

    print("\n\nRBFInv CTRL PTS UMAP")
    s = time.time()
    EPS = 50000
    irbfcp_umap = RBFInv(num_ctrl=400,
                         mode='rols',
                         kernel='gaussian',
                         eps=EPS,
                         normalize_c=True,
                         normalize_d=True)
    irbfcp_umap.fit(X_proj[:subset_size], umap_proj[:subset_size])
    irbfcp_umap_path = base_dir + "irbfcp_umap.joblib"
    irbfcp_umap.save(irbfcp_umap_path)
    print("\ttime: ", time.time() - s)

    print("\n\nRBFInv CLUSTER TSNE")
    s = time.time()
    EPS = 50000
    irbfc_tsne = RBFInv(num_ctrl=50,
                        mode='cluster',
                        kernel='gaussian',
                        eps=EPS,
                        normalize_c=True,
                        normalize_d=True)
    irbfc_tsne.fit(X_proj[:subset_size], tsne_proj[:subset_size])
    irbfc_tsne_path = base_dir + "irbfc_tsne.joblib"
    irbfc_tsne.save(irbfc_tsne_path)
    print("\ttime: ", time.time() - s)

    print("\n\nRBFInv CLUSTER UMAP")
    s = time.time()
    EPS = 50000
    irbfc_umap = RBFInv(num_ctrl=50,
                        mode='cluster',
                        kernel='gaussian',
                        eps=EPS,
                        normalize_c=True,
                        normalize_d=True)
    irbfc_umap.fit(X_proj[:subset_size], tsne_proj[:subset_size])
    irbfc_umap_path = base_dir + "irbfc_umap.joblib"
    irbfc_umap.save(irbfc_umap_path)
    print("\ttime: ", time.time() - s)

    print("\n\nNNInv TSNE")
    s = time.time()
    nninv_tsne = NNInv()
    nninv_tsne.fit(X_proj[:subset_size], tsne_proj[:subset_size])
    nninv_tsne_path = base_dir + "nninv_tsne.joblib"
    nninv_tsne.save(nninv_tsne_path, base_dir + 'nninv_tsne_keras.hdf5')
    print("\ttime: ", time.time() - s)

    print("\n\nNNInv UMAP")
    s = time.time()
    nninv_umap = NNInv()
    nninv_umap.fit(X_proj[:subset_size], umap_proj[:subset_size])
    nninv_umap_path = base_dir + "nninv_umap.joblib"
    nninv_umap.save(nninv_umap_path, base_dir + 'nninv_umap_keras.hdf5')
    print("\ttime: ", time.time() - s)

    input_shape = (X_train.shape[1], X_train.shape[2], 1)
    X_train = X_train.reshape((X_train.shape[0], ) + input_shape)
    X_test = X_test.reshape((X_test.shape[0], ) + input_shape)
    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)

    X_proj = X_proj.reshape((X_proj.shape[0], ) + input_shape)

    print("\n\nTraining classifier")
    s = time.time()
    clf_keras = CNNModel(input_shape, 10)
    clf_keras.fit(X_train,
                  y_train,
                  batch_size=128,
                  epochs=14,
                  verbose=1,
                  validation_data=(X_test, y_test))
    print("\tAccuracy on test data: ",
          clf_keras.evaluate(X_test, y_test, verbose=0))
    clf_keras_path = base_dir + "mnist_cnn.hdf5"
    clf_keras.save(clf_keras_path)
    y_proj_pred = np.argmax(clf_keras.predict(X_proj), axis=1)
    pred_path = base_dir + "y_pred_clf.npy"
    np.save(pred_path, y_proj_pred)

    clf = CLF(clf=clf_keras,
              clf_type='keras_cnn',
              clf_path=clf_keras_path,
              shape=input_shape)
    clf_path = base_dir + 'mnist_cnn.json'
    clf.save_json(clf_path)
    print("\ttime: ", time.time() - s)

    N = 1
    R = 500

    print("\n\nGRID ILAMP tSNE")
    s = time.time()
    grid_ilamp_tsne_path = base_dir + 'grid_ilamp_tsne.joblib'
    save_grid(grid_ilamp_tsne_path, R, N, clf, ilamp_tsne, X_nd, tsne_proj,
              clf_path, ilamp_tsne_path)
    ui_grid_ilamp_tsne_path = base_dir + 'mnist_500_' + 'ui_ilamp_tsne.json'
    save_json_ui(ui_grid_ilamp_tsne_path, grid_ilamp_tsne_path, clf_path,
                 "ilamp", ilamp_tsne_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID ILAMP UMAP")
    s = time.time()
    grid_ilamp_umap_path = base_dir + 'grid_ilamp_umap.joblib'
    save_grid(grid_ilamp_umap_path, R, N, clf, ilamp_umap, X_nd, umap_proj,
              clf_path, ilamp_umap_path)
    ui_grid_ilamp_umap_path = base_dir + 'mnist_500_' + 'ui_ilamp_umap.json'
    save_json_ui(ui_grid_ilamp_umap_path, grid_ilamp_umap_path, clf_path,
                 "ilamp", ilamp_umap_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID NNInv tSNE")
    s = time.time()
    grid_nninv_tsne_path = base_dir + 'grid_nninv_tsne.joblib'
    save_grid(grid_nninv_tsne_path, R, N, clf, nninv_tsne, X_nd, tsne_proj,
              clf_path, nninv_tsne_path)
    ui_grid_nninv_tsne_path = base_dir + 'mnist_500_' + '_ui_nninv_tsne.json'
    save_json_ui(ui_grid_nninv_tsne_path, grid_nninv_tsne_path, clf_path,
                 "nninv", nninv_tsne_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID NNInv UMAP")
    s = time.time()
    grid_nninv_umap_path = base_dir + 'grid_nninv_umap.joblib'
    save_grid(grid_nninv_umap_path, R, N, clf, nninv_umap, X_nd, umap_proj,
              clf_path, nninv_umap_path)
    ui_grid_nninv_umap_path = base_dir + 'mnist_500_' + 'ui_nninv_umap.json'
    save_json_ui(ui_grid_nninv_umap_path, grid_nninv_umap_path, clf_path,
                 "nninv", nninv_umap_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID RBFInv CTRL PTS tSNE")
    s = time.time()
    grid_irbfcp_tsne_path = base_dir + 'grid_irbfcp_tsne.joblib'
    save_grid(grid_irbfcp_tsne_path, R, N, clf, irbfcp_tsne, X_nd, tsne_proj,
              clf_path, irbfcp_tsne_path)
    ui_grid_irbfcp_tsne_path = base_dir + 'mnist_500_' + 'ui_irbfcp_tsne.json'
    save_json_ui(ui_grid_irbfcp_tsne_path, grid_irbfcp_tsne_path, clf_path,
                 "rbf", irbfcp_tsne_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID RBFInv CTRL PTS UMAP")
    s = time.time()
    grid_irbfcp_umap_path = base_dir + 'grid_irbfcp_umap.joblib'
    save_grid(grid_irbfcp_umap_path, R, N, clf, irbfcp_umap, X_nd, umap_proj,
              clf_path, irbfcp_umap_path)
    ui_grid_irbfcp_umap_path = base_dir + 'mnist_500_' + 'ui_irbfcp_umap.json'
    save_json_ui(ui_grid_irbfcp_umap_path, grid_irbfcp_umap_path, clf_path,
                 "rbf", irbfcp_umap_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID RBFInv CLUSTER tSNE")
    s = time.time()
    grid_irbfc_tsne_path = base_dir + 'grid_irbfc_tsne.joblib'
    save_grid(grid_irbfc_tsne_path, R, N, clf, irbfc_tsne, X_nd, tsne_proj,
              clf_path, irbfc_tsne_path)
    ui_grid_irbfc_tsne_path = base_dir + 'mnist_500_' + 'ui_irbfc_tsne.json'
    save_json_ui(ui_grid_irbfc_tsne_path, grid_irbfc_tsne_path, clf_path,
                 "rbf", irbfc_tsne_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)

    print("\n\nGRID RBFInv CLUSTER UMAP")
    s = time.time()
    grid_irbfc_umap_path = base_dir + 'grid_irbfc_umap.joblib'
    save_grid(grid_irbfc_umap_path, R, N, clf, irbfc_umap, X_nd, umap_proj,
              clf_path, irbfc_umap_path)
    ui_grid_irbfc_umap_path = base_dir + 'mnist_500_' + 'ui_irbfc_umap.json'
    save_json_ui(ui_grid_irbfc_umap_path, grid_irbfc_umap_path, clf_path,
                 "rbf", irbfc_umap_path, train_y_path, pred_path)
    print("\ttime: ", time.time() - s)
Esempio n. 8
0
print("Loading dataset")
X_train = np.load(BASE_DIR + 'X_sample.npy')
y_train = np.load(BASE_DIR + 'y_sample.npy')

print("Checking if predictions exist, loading CLF and predicting otherwise")
y_preds_f = [BASE_DIR + 'y_sample_pred_' + c + '.npy' for c in CLFS]
y_pred_exists = [Path(y).is_file() for y in y_preds_f]

y_preds = [None] * len(CLFS)
for i in range(4):
    if y_pred_exists[i] is False:
        print("Needed to load classifier ", clfs_f[i])
        if i == 1:
            from keras import load_model
            clf = CLF(clf=load_model(clfs_f[i]),
                      clf_type="keras_cnn",
                      shape=(28, 28, 1))
        else:
            clf = CLF(clf=joblib.load(open(clfs_f[i], 'rb')),
                      clf_type="sklearn")
        y_preds[i] = clf.Predict(X_train)
        np.save(y_preds_f[i], y_preds[i])
    else:
        y_preds[i] = np.load(y_preds_f[i])

print("Loading projections")
projs = [LoadProjection(f) for f in projections_f]

print("Loading densemaps")
dmaps = [np.load(f) for f in dmaps_f]
Esempio n. 9
0
y_train[y_train == 9] = 1

# TODO: load y_pred if it exists
y_preds_f = [BASE_DIR + 'y_sample_bin_pred_' + c + '.npy' for c in CLFS]
y_pred_exists = [Path(y).is_file() for y in y_preds_f]
print("y_pred_exists")

clfs = [None] * len(CLFS)

y_preds = [None] * len(CLFS)
for i in range(4):
    if y_pred_exists[i] is False:
        if i == 1:
            from keras import load_model
            clfs[i] = CLF(clf=load_model(clfs_f[i]),
                          clf_type="keras_cnn",
                          shape=(28, 28, 1))
        else:
            clfs[i] = CLF(clf=joblib.load(open(clfs_f[i], 'rb')),
                          clf_type="sklearn")
        y_preds[i] = clfs[i].Predict(X_train)
        np.save(y_preds_f[i], y_preds[i])
    else:
        y_preds[i] = np.load(y_preds_f[i])

print("2 class fashion mnist - {} samples".format(len(y_train)))
for i in range(len(CLFS)):
    clf_name = CLFS[i]
    print('\t' + clf_name + " num errors: ", np.sum(y_train != y_preds[i]))

BASE_DIR = 'data/fashionmnist_full/'
Esempio n. 10
0
def main():
    # Ideal path:
    # 1 - Load dataset, projection and a trained classifier
    with open("data/mnist/mnist.json") as f:
        data_json = json.load(f)

    proj_lamp = np.load(data_json["proj1"])
    proj_tsne = np.load(data_json["proj2"])

    X_train = np.load(data_json['X_train'])
    y_train = np.load(data_json['y_train'])
    X_test = np.load(data_json['X_test'])
    y_test = np.load(data_json['y_test'])

    input_shape = (X_train.shape[1], X_train.shape[2], 1)

    clf1 = CLF()
    clf_path = data_json['clfs'][0]
    weights_path = data_json['weights'][0]
    clf1.LoadKeras(clf_path, weights_path, "CNN 1", input_shape)
    # Plots the projected points coulored according to the label assigned by
    # the classifier.
    # As it is the first projection plotted, the legend is also save into a
    # separate file
    y_pred = clf1.Predict(X_train[:len(proj_tsne)])
    labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    path = "data/mnist/projection_clf1_tsne.pdf"
    path_leg = "data/mnist/projection_leg.pdf"
    title = "t-SNE Projection"
    PlotProjection(proj_tsne, y_pred, path, title, path_leg, labels)

    # Plots the LAMP projection for the same dataset.
    path = "data/mnist/projection_clf1_lamp.pdf"
    title = "LAMP Projection"
    PlotProjection(proj_lamp, y_pred, path, title)

    clf2_1 = CLF()
    clf_path = data_json['clfs'][1]
    weights_path = data_json['weights'][1]
    clf2_1.LoadKeras(clf_path, weights_path, "CNN 2 - 1 epoch", input_shape)

    clf2_5 = CLF()
    weights_path = data_json['weights'][2]
    clf2_5.LoadKeras(clf_path, weights_path, "CNN 2 - 5 epochs", input_shape)

    clf2_10 = CLF()
    weights_path = data_json['weights'][3]
    clf2_10.LoadKeras(clf_path, weights_path, "CNN 2 - 10 epochs", input_shape)

    clf2_50 = CLF()
    weights_path = data_json['weights'][4]
    clf2_50.LoadKeras(clf_path, weights_path, "CNN 2 - 50 epochs", input_shape)

    # Run boundary map construction function
    R = 300
    N = [1, 5, 10, 15]
    grid1 = Grid(proj_tsne, R)

    for n in N:
        print("Create densemap for ", n)
        _, dmap = grid1.BoundaryMap(X_train[:len(proj_tsne)], n, clf1)
        fig_title = "{}x{} DenseMap ({} samples, {})".format(
            grid1.grid_size, grid1.grid_size, n, clf1.name)
        fig_name = "data/mnist/DenseMap_{}x{}_N_{}_dense_map_{}".format(
            grid1.grid_size, grid1.grid_size, n, clf1.name)
        PlotDenseMap(dmap, fig_title, fig_name)

    N = 1
    R = 300
    clf_epochs = [clf2_1, clf2_5, clf2_10, clf2_50]
    for clf in clf_epochs:
        print("Densemap for certain epoch")
        grid = Grid(proj_tsne, R)
        _, dmap = grid.BoundaryMap(X_train[:len(proj_tsne)], N, clf)
        fig_title = "{}x{} DenseMap ({} samples, {})".format(
            grid.grid_size, grid.grid_size, N, clf.name)
        fig_name = "data/mnist/DenseMap_{}x{}_N_{}_dense_map_{}".format(
            grid.grid_size, grid.grid_size, N, clf.name)
        PlotDenseMap(dmap, fig_title, fig_name)

    print("Create densemap for LAMP, N = 15", )
    N = 15
    R = 300
    grid2 = Grid(proj_lamp, R)
    _, dmap = grid2.BoundaryMap(X_train[:len(proj_tsne)], N, clf1)
    fig_title = "{}x{} DenseMap LAMP Projection ({} samples, {})".format(
        grid2.grid_size, grid2.grid_size, N, clf1.name)
    fig_name = "data/mnist/DenseMap_{}x{}_N_{}_dense_map_{}_LAMP".format(
        grid2.grid_size, grid2.grid_size, N, clf1.name)
    PlotDenseMap(dmap, fig_title, fig_name)
Esempio n. 11
0
GRID_SIZE = 400

# Load the dataset
print("Loading dataset")
X_train = np.load(BASE_DIR + 'X_sample_bin.npy')
y_train = np.load('data/fashionmnist/y_sample_bin.npy')
# TODO: y_train contains the values 0 and 9, replace all 9s for 1s?
y_train[y_train == 9] = 1

print("Loading projections")
projs = [LoadProjection(BASE_DIR + f) for f in projections_f]

print("Loading classifiers")
clfs = [
    CLF(clf=joblib.load(open(BASE_DIR + clfs_f[0], 'rb')), clf_type="sklearn"),
    CLF(clf=load_model(BASE_DIR + clfs_f[1]),
        clf_type="keras_cnn",
        shape=(28, 28, 1))
]

print("Loading densemaps")
dmaps = [np.load(BASE_DIR + f) for f in dmaps_f]

print("Predicting labels for training set")
y_preds = [clf.Predict(X_train) for clf in clfs]

print("PLotting densemap errors...")
# MMDS KNN
PlotDenseMapErr(GRID_SIZE, dmaps[0], projs[P_MMDS], y_train, y_preds[CLF_KNN],
                "ERR DENSEMAP MMDS KNN", BASE_DIR + 'err_densemap_MMDS_KNN')
Esempio n. 12
0
# two samples dataset
plt.imshow(X_zeros[0], cmap='gray')
plt.show()
plt.imshow(X_ones[0], cmap='gray')
plt.show()
X_train = np.array([X_zeros[0], X_ones[0]])
X_train = X_train.reshape((X_train.shape[0], img_size**2))
y_train = [0, 1]

X_proj = np.array([[0.25, 0.5], [0.75, 0.5]])

clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(X_train, y_train)
clf.predict(X_train)

dmap_clf = CLF(clf, "sklearn")
dmap_clf.Predict(X_train)

grid_size = 30
g = Grid(X_proj, grid_size)
_, dmap = g.BoundaryMap(X_train, 2, dmap_clf, k_ilamp=2)

dmap_rgb = hsv_to_rgb(dmap)
plt.imshow(dmap_rgb)
plt.show()

dist_2d = dist_maps.dist2d_grid(dmap)
dist_nd = dist_maps.distnd_grid(dmap, X_train, X_proj, k_ilamp=2)
plt.imshow(dist_2d, cmap='gray')
plt.show()
plt.imshow(dist_nd, cmap='gray')
Esempio n. 13
0
def main():
    base_dir = "data/cifar10/"

    with open(base_dir + "cifar.json") as f:
        data_json_base = json.load(f)

    X_train = np.load(data_json_base['X_train'])
    input_shape = (X_train.shape[1], X_train.shape[2], 3)

    clf = CLF()
    clf_path = data_json_base['clfs'][0]
    clf.LoadKerasModel(clf_path, "CNN", input_shape)

    inv_projs_path = data_json_base['inv_projs']
    ilamp_path = inv_projs_path[0]
    irbfcp_path = inv_projs_path[1]
    irbfn_path = inv_projs_path[2]
    irbfc_path = inv_projs_path[3]
    nninv_path = inv_projs_path[4]

    inv_projs = []
    for inv in inv_projs_path[:-1]:
        inv_projs.append(joblib.load(inv))

    ilamp = inv_projs[0]
    irbf_cp = inv_projs[1]
    irbf_neighbors = inv_projs[2]
    irbf_cluster = inv_projs[3]
    nninv = NNInv()
    nninv.load(nninv_path)

    N = 1
    R = 250

    grid_ilamp_path = base_dir + 'grid_ilamp.joblib'
    if not os.path.isfile(grid_ilamp_path):
        print("CIFAR ILAMP GRID")
        s = time.time()
        grid_ilamp = Grid()
        grid_ilamp.fit(R, N, clf, ilamp)
        grid_ilamp.BoundaryMapBatch()
        grid_ilamp.distnD3_batch()
        grid_ilamp.dist2D()
        grid_ilamp.distnD_batch()
        grid_ilamp.distnD2_batch()
        grid_ilamp.save(grid_ilamp_path, clf_path, ilamp_path)
        print("\ttime: ", time.time() - s)

    grid_irbfcp_path = base_dir + 'grid_irbfcp.joblib'
    if not os.path.isfile(grid_irbfcp_path):
        print("CIFAR RBF CTRL PTS GRID")
        s = time.time()
        grid_irbfcp = Grid()
        grid_irbfcp.fit(R, N, clf, irbf_cp)
        grid_irbfcp.BoundaryMapBatch()
        grid_irbfcp.dist2D()
        grid_irbfcp.distnD_batch()
        grid_irbfcp.distnD2_batch()
        grid_irbfcp.distnD3_batch()
        grid_irbfcp.save(grid_irbfcp_path, clf_path, irbfcp_path)
        print("\ttime: ", time.time() - s)

    grid_irbfn_path = base_dir + 'grid_irbfn.joblib'
    if not os.path.isfile(grid_irbfn_path):
        print("CIFAR RBF NEIGHBORS GRID")
        s = time.time()
        grid_irbfn = Grid()
        grid_irbfn.fit(R, N, clf, irbf_neighbors)
        grid_irbfn.BoundaryMapBatch()
        grid_irbfn.dist2D()
        grid_irbfn.distnD_batch()
        grid_irbfn.distnD2_batch()
        grid_irbfn.distnD3_batch()
        grid_irbfn.save(grid_irbfn_path, clf_path, irbfn_path)
        print("\ttime: ", time.time() - s)

    grid_irbfc_path = base_dir + 'grid_irbfc.joblib'
    if not os.path.isfile(grid_irbfc_path):
        print("CIFAR RBF CLUSTER GRID")
        s = time.time()
        grid_irbfc = Grid()
        grid_irbfc.fit(R, N, clf, irbf_cluster)
        grid_irbfc.BoundaryMapBatch()
        grid_irbfc.dist2D()
        grid_irbfc.distnD_batch()
        grid_irbfc.distnD2_batch()
        grid_irbfc.distnD3_batch()
        grid_irbfn.save(grid_irbfc_path, clf_path, irbfc_path)
        print("\ttime: ", time.time() - s)

    grid_nninv_path = base_dir + 'grid_nninv.joblib'
    if not os.path.isfile(grid_nninv_path):
        print("CIFAR NNInv GRID")
        s = time.time()
        grid_nn = Grid()
        grid_nn.fit(R, N, clf, nninv)
        grid_nn.BoundaryMapBatch()
        grid_nn.dist2D()
        grid_nn.distnD_batch()
        grid_nn.distnD2_batch()
        grid_nn.distnD3_batch()
        grid_nn.save(grid_nninv_path, clf_path, nninv_path)
        print("\ttime: ", time.time() - s)