def process_dataset(dataset, n_clusters, pca_enabled=False): X = np.load("./data/" + dataset + '.npy') if pca_enabled: pca = utils.load_model(dataset + '_pca') X = pca.transform(utils.flat(X)).reshape(X.shape[0], X.shape[1], -1) segments = segmentation(X, n_clusters) np.save("./results/" + dataset + "_segments.npy", segments) color_segments = color_true_map(segments, back_color=[1, 1, 1]) save_image(color_segments, dataset + "_segments") print("Segments:", len(np.bincount(segments.reshape(-1))) - 1)
def process_dataset(dataset, colors): y = np.load("./data/" + dataset + '_labels.npy') pred = np.load("./data/" + dataset + '_clasification.npy') segments = np.load("./results/" + dataset + '_segments.npy') test_mask = np.load("./data/" + dataset + '_test_mask.npy').reshape( y.shape) sc_pred = classify_segments(pred, segments) sc_score = utils.balanced_score(y[test_mask], sc_pred[test_mask]) sc_cm = utils.confusion_matrix(y[test_mask], sc_pred[test_mask]) utils.save_json({"sc": sc_score}, dataset + "_sc_score") utils.save_csv(sc_cm, dataset + "_sc_cm") color_map = color_true_map(sc_pred, labels_colors=colors) save_image(color_map, dataset + "_sc_clasification")
def process_dataset(dataset): X_img = np.load("./data/" + dataset + '.npy') segments = np.load("./results/" + dataset + '_segments.npy') seg_labels = range(len(np.bincount(segments.reshape(-1)))) n_endmembers = error_curve(dataset, X_img, segments, seg_labels) _, endmembers, abundances = unmix_segments(X_img, segments, seg_labels, n_endmembers) plot_endmembers(dataset + "_seg_endmember", endmembers) np.save("./results/" + dataset + "_seg_endmembers.npy", endmembers) np.save("./results/" + dataset + "_seg_abundances.npy", abundances) abun_map = np.empty((X_img.shape[0], X_img.shape[1], n_endmembers)) for seg in seg_labels: abun_map[segments == seg, :] = abundances[seg, :] for i in range(n_endmembers): abun_color = color_abundance_map(abun_map[:, :, i], [255, 255, 255]) save_image(abun_color, dataset + "_seg_abundances_" + str(i))
def process_dataset(dataset, colors): y = np.load("./data/" + dataset + '_labels.npy') pred = np.load("./data/" + dataset + '_clasification.npy') test_mask = np.load("./data/" + dataset + '_test_mask.npy').reshape( y.shape) mv_sizes = [3, 5, 9] mv_pred = [majority_vote(pred, size) for size in mv_sizes] mv_scores = [ utils.balanced_score(y[test_mask], p[test_mask]) for p in mv_pred ] mv_cm = [ utils.confusion_matrix(y[test_mask], p[test_mask]) for p in mv_pred ] keys = ["_mv_{}".format(size) for size in mv_sizes] utils.save_json(dict(zip(keys, mv_scores)), dataset + "_mv_scores") for i in range(len(mv_sizes)): utils.save_csv(mv_cm[i], dataset + keys[i] + "_cm") color_map = color_true_map(mv_pred[i], labels_colors=colors) save_image(color_map, dataset + keys[i] + "_clasification")
def generate_work_data(dataset, labels, colors, parameters, pca_enabled=False): X_img = np.load('./data/' + dataset + '.npy') y_img = np.load('./data/' + dataset + '_labels.npy') save_image(color_true_map(y_img, labels_colors=colors), dataset + "_labels") X = utils.flat(X_img) y = utils.flat(y_img) train_ratio, val_ratio = 0.1, 0.1 test_ratio = 1 - (train_ratio + val_ratio) tv_mask, test_mask = utils.balanced_train_test_mask( y, np.isin(y, labels), test_ratio) train_mask, val_mask = utils.balanced_train_test_mask( y, tv_mask, val_ratio / (val_ratio + train_ratio)) np.save("./data/" + dataset + "_train_mask.npy", train_mask) np.save("./data/" + dataset + "_val_mask.npy", val_mask) np.save("./data/" + dataset + "_test_mask.npy", test_mask) if pca_enabled: pca = utils.pca(X[tv_mask, :], 0.99) utils.save_model(pca, dataset + '_pca') train = pca.transform(X[train_mask, :]) test = pca.transform(X[test_mask]) flat = pca.transform(X) else: train = X[train_mask, :] test = X[test_mask, :] flat = X svc = utils.svc(train, y[train_mask], parameters["C"], parameters["gamma"]) utils.save_model(svc, dataset + '_svc') test_pred = svc.predict(test) np.save("./data/" + dataset + "_test_pred.npy", test_pred) classification = svc.predict(flat).reshape(y_img.shape).astype(np.uint8) np.save("./data/" + dataset + "_clasification.npy", classification) save_image(color_true_map(classification, labels_colors=colors), dataset + "_clasification") score = utils.balanced_score(y[test_mask], test_pred) utils.save_json({"original": score}, dataset + "_original_score") print("Test Score:", score)