Пример #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()
Пример #2
0
def load_pascal(which='train', year="2010", sp_type="slic", n_jobs=-1):
    pascal = PascalSegmentation()
    files = pascal.get_split(which=which, year=year)
    results = Parallel(n_jobs=n_jobs)(delayed(load_pascal_single)(
        f, which=which, sp_type=sp_type, pascal=pascal) for f in files)
    X, Y, superpixels, segments = zip(*results)
    if sp_type == "slic":
        return DataBunch(X, Y, files, superpixels)
    else:
        return HierarchicalDataBunch(X, Y, files, superpixels, segments)
Пример #3
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!")
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))
Пример #6
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()
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()