Ejemplo n.º 1
0
def train_svm(C=0.1, grid=False):
    pascal = PascalSegmentation()

    files_train = pascal.get_split("kTrain")
    superpixels = [slic_n(pascal.get_image(f), n_superpixels=100,
                          compactness=10)
                   for f in files_train]
    bow = SiftBOW(pascal, n_words=1000, color_sift=True)
    data_train = bow.fit_transform(files_train, superpixels)

    data_train = add_global_descriptor(data_train)

    svm = LinearSVC(C=C, dual=False, class_weight='auto')
    chi2 = AdditiveChi2Sampler()

    X, y = np.vstack(data_train.X), np.hstack(data_train.Y)
    X = chi2.fit_transform(X)
    svm.fit(X, y)
    print(svm.score(X, y))
    eval_on_sp(pascal, data_train, [svm.predict(chi2.transform(x)) for x in
                                    data_train.X], print_results=True)

    files_val = pascal.get_split("kVal")
    superpixels_val = [slic_n(pascal.get_image(f), n_superpixels=100,
                              compactness=10) for f in files_val]
    data_val = bow.transform(files_val, superpixels_val)
    data_val = add_global_descriptor(data_val)
    eval_on_sp(pascal, data_val, [svm.predict(chi2.transform(x)) for x in
                                  data_val.X], print_results=True)

    tracer()
Ejemplo n.º 2
0
def visualize_pascal(plot_probabilities=False):
    data = load_pascal('val')
    ds = PascalSegmentation()
    for x, y, f, sps in zip(data.X, data.Y, data.file_names, data.superpixels):
        fig, ax = plt.subplots(2, 3)
        ax = ax.ravel()
        image = ds.get_image(f)
        y_pixel = ds.get_ground_truth(f)
        x_raw = load_kraehenbuehl(f)

        boundary_image = mark_boundaries(image, sps)

        ax[0].imshow(image)
        ax[1].imshow(y_pixel, cmap=ds.cmap)
        ax[2].imshow(boundary_image)
        ax[3].imshow(np.argmax(x_raw, axis=-1), cmap=ds.cmap, vmin=0, vmax=256)
        ax[4].imshow(y[sps], cmap=ds.cmap, vmin=0, vmax=256)
        ax[5].imshow(np.argmax(x, axis=-1)[sps], cmap=ds.cmap, vmin=0,
                     vmax=256)
        for a in ax:
            a.set_xticks(())
            a.set_yticks(())
        plt.savefig("figures_pascal_val/%s.png" % f, bbox_inches='tight')
        plt.close()
        if plot_probabilities:
            fig, ax = plt.subplots(3, 7)
            for k in range(21):
                ax.ravel()[k].matshow(x[:, :, k], vmin=0, vmax=1)
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            plt.savefig("figures_pascal_val/%s_prob.png" % f,
                        bbox_inches='tight')
            plt.close()
    tracer()
def main():
    from pascal.pascal_helpers import load_pascal
    from datasets.pascal import PascalSegmentation
    from utils import add_edges
    from scipy.misc import imsave
    from skimage.segmentation import mark_boundaries

    ds = PascalSegmentation()
    data = load_pascal("train1")

    data = add_edges(data, independent=False)
    # X, Y, image_names, images, all_superpixels = load_data(
    # "train", independent=False)
    for x, name, sps in zip(data.X, data.file_names, data.superpixels):
        segments = get_km_segments(x, ds.get_image(name), sps, n_segments=25)
        boundary_image = mark_boundaries(mark_boundaries(ds.get_image(name), sps), segments[sps], color=[1, 0, 0])
        imsave("hierarchy_sp_own_25/%s.png" % name, boundary_image)
Ejemplo n.º 4
0
def main():
    from pascal.pascal_helpers import load_pascal
    from datasets.pascal import PascalSegmentation
    from utils import add_edges
    from scipy.misc import imsave
    from skimage.segmentation import mark_boundaries

    ds = PascalSegmentation()
    data = load_pascal("train1")

    data = add_edges(data, independent=False)
    #X, Y, image_names, images, all_superpixels = load_data(
    #"train", independent=False)
    for x, name, sps in zip(data.X, data.file_names, data.superpixels):
        segments = get_km_segments(x, ds.get_image(name), sps, n_segments=25)
        boundary_image = mark_boundaries(mark_boundaries(
            ds.get_image(name), sps),
                                         segments[sps],
                                         color=[1, 0, 0])
        imsave("hierarchy_sp_own_25/%s.png" % name, boundary_image)
Ejemplo n.º 5
0
def test_remove_small_segments():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    idx = 10
    image = pascal.get_image(train_files[idx])
    segments, superpixels = superpixels_segments(train_files[idx])
    new_regions, correspondences = merge_small_sp(image, superpixels)
    new_counts = np.bincount(new_regions.ravel())
    if np.any(new_counts < 50):
        raise ValueError("Stupid thing!")
Ejemplo n.º 6
0
def visualize_sps():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    for image_file in train_files:
        print(image_file)
        image = pascal.get_image(image_file)
        segments, superpixels = superpixels_segments(image_file)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        imsave("segment_sp_fixed/%s.png"
               % image_file, mark_boundaries(image, clean_regions))
def visualize_sps():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    for image_file in train_files:
        print(image_file)
        image = pascal.get_image(image_file)
        segments, superpixels = superpixels_segments(image_file)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        imsave("segment_sp_fixed/%s.png" % image_file,
               mark_boundaries(image, clean_regions))
Ejemplo n.º 8
0
def train_svm(C=0.1, grid=False):
    pascal = PascalSegmentation()

    files_train = pascal.get_split("kTrain")
    superpixels = [
        slic_n(pascal.get_image(f), n_superpixels=100, compactness=10)
        for f in files_train
    ]
    bow = SiftBOW(pascal, n_words=1000, color_sift=True)
    data_train = bow.fit_transform(files_train, superpixels)

    data_train = add_global_descriptor(data_train)

    svm = LinearSVC(C=C, dual=False, class_weight='auto')
    chi2 = AdditiveChi2Sampler()

    X, y = np.vstack(data_train.X), np.hstack(data_train.Y)
    X = chi2.fit_transform(X)
    svm.fit(X, y)
    print(svm.score(X, y))
    eval_on_sp(pascal,
               data_train,
               [svm.predict(chi2.transform(x)) for x in data_train.X],
               print_results=True)

    files_val = pascal.get_split("kVal")
    superpixels_val = [
        slic_n(pascal.get_image(f), n_superpixels=100, compactness=10)
        for f in files_val
    ]
    data_val = bow.transform(files_val, superpixels_val)
    data_val = add_global_descriptor(data_val)
    eval_on_sp(pascal,
               data_val, [svm.predict(chi2.transform(x)) for x in data_val.X],
               print_results=True)

    tracer()
def visualize_segments():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    for image_file in train_files:
        print(image_file)
        image = pascal.get_image(image_file)
        segments, superpixels = superpixels_segments(image_file)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        marked = mark_boundaries(image, clean_regions)
        edges = create_segment_sp_graph(segments, clean_regions)
        edges = np.array(edges)
        n_segments = segments.shape[2]
        segment_centers = [
            regionprops(segments.astype(np.int)[:, :, i],
                        ['Centroid'])[0]['Centroid'] for i in range(n_segments)
        ]
        segment_centers = np.vstack(segment_centers)[:, ::-1]
        superpixel_centers = get_superpixel_centers(clean_regions)
        grr = min(n_segments, 10)
        fig, axes = plt.subplots(3, grr // 3, figsize=(30, 30))

        for i, ax in enumerate(axes.ravel()):
            ax.imshow(mark_boundaries(marked, segments[:, :, i], (1, 0, 0)))
            ax.scatter(segment_centers[:, 0],
                       segment_centers[:, 1],
                       color='red')
            ax.scatter(superpixel_centers[:, 0],
                       superpixel_centers[:, 1],
                       color='blue')
            this_edges = edges[edges[:, 1] == i]
            for edge in this_edges:
                ax.plot([
                    superpixel_centers[edge[0]][0], segment_centers[edge[1]][0]
                ], [
                    superpixel_centers[edge[0]][1], segment_centers[edge[1]][1]
                ],
                        c='black')
            ax.set_xlim(0, superpixels.shape[1])
            ax.set_ylim(superpixels.shape[0], 0)
            ax.set_xticks(())
            ax.set_yticks(())
        #plt.show()
        #imsave("segments_test/%s.png" % image_file, marked)
        plt.savefig("segments_test/%s.png" % image_file)
        plt.close()
Ejemplo n.º 10
0
def eval_segment_best_possible():
    ds = PascalSegmentation()
    print("loading")
    data = load_pascal('train')
    print("getting edges")
    data = add_edges(data)
    print("computing segments")
    segments = [get_km_segments(x, ds.get_image(image_name), sps,
                                n_segments=25) for x, image_name, sps in
                zip(data.X, data.file_names, data.superpixels)]
    print("combining superpixels")
    segments = [seg[sp] for seg, sp in zip(segments, data.superpixels)]
    predictions = [gt_in_sp(ds, f, seg)[seg]
                   for seg, f in zip(segments, data.file_names)]
    Y_true = [ds.get_ground_truth(f) for f in data.file_names]
    hamming, jaccard = eval_on_pixels(ds, Y_true, predictions,
                                      print_results=True)
    tracer()
Ejemplo n.º 11
0
def visualize_segments():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    for image_file in train_files:
        print(image_file)
        image = pascal.get_image(image_file)
        segments, superpixels = superpixels_segments(image_file)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        marked = mark_boundaries(image, clean_regions)
        edges = create_segment_sp_graph(segments, clean_regions)
        edges = np.array(edges)
        n_segments = segments.shape[2]
        segment_centers = [regionprops(segments.astype(np.int)[:, :, i],
                                       ['Centroid'])[0]['Centroid'] for i in
                           range(n_segments)]
        segment_centers = np.vstack(segment_centers)[:, ::-1]
        superpixel_centers = get_superpixel_centers(clean_regions)
        grr = min(n_segments, 10)
        fig, axes = plt.subplots(3, grr // 3, figsize=(30, 30))

        for i, ax in enumerate(axes.ravel()):
            ax.imshow(mark_boundaries(marked, segments[:, :, i], (1, 0, 0)))
            ax.scatter(segment_centers[:, 0], segment_centers[:, 1],
                       color='red')
            ax.scatter(superpixel_centers[:, 0], superpixel_centers[:, 1],
                       color='blue')
            this_edges = edges[edges[:, 1] == i]
            for edge in this_edges:
                ax.plot([superpixel_centers[edge[0]][0],
                         segment_centers[edge[1]][0]],
                        [superpixel_centers[edge[0]][1],
                         segment_centers[edge[1]][1]], c='black')
            ax.set_xlim(0, superpixels.shape[1])
            ax.set_ylim(superpixels.shape[0], 0)
            ax.set_xticks(())
            ax.set_yticks(())
        #plt.show()
        #imsave("segments_test/%s.png" % image_file, marked)
        plt.savefig("segments_test/%s.png" % image_file)
        plt.close()