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)
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()
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)
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)
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] print("PLotting densemap errors...") counter = 0 titles = ["MDS (Metric)", "PLMP", "Projection By Clustering", "t-SNE", "UMAP"] for i in range(len(PROJS)): for j in range(len(CLFS)):
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)
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') plt.show()