In [1]:
import sys
sys.path.insert(0, '..')
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
import numpy as np
import scipy.sparse as sp
from lib.segmentation import segmentation_adjacency
from lib.graph import coarsen_adj
def plot_laplacians(image, segmentation_algorithm):
segmentation = segmentation_algorithm(image)
adj, points, mass = segmentation_adjacency(segmentation)
adjs, _, _ = coarsen_adj(adj, points, mass, levels=4)
for i in range(5):
print('Level {}:'.format(i), adjs[i].shape[0], 'nodes,', adjs[i].nnz // 2, 'edges')
laps = [sp.csgraph.laplacian(adj) for adj in adjs]
for i, lap in enumerate(laps):
lamb, U = np.linalg.eig(lap.toarray())
perm = lamb.argsort()
lamb = lamb[perm]
U = U[:, perm]
step = 2**i
x = range(step // 2, laps[0].shape[0], step)
lb = 'L_{} spectrum in [{}, {:.5f}]'.format(i, 0, lamb[-1])
plt.plot(x, np.real(lamb), '.', label=lb)
plt.legend(loc='best')
plt.xlim(0, laps[0].shape[0])
plt.ylim(ymin=0)
In [3]:
from lib.datasets import MNIST, Cifar10, PascalVOC
mnist = MNIST('../data/mnist').test.next_batch(1, shuffle=False)[0][0]
cifar_10 = Cifar10('../data/cifar_10').test.next_batch(2, shuffle=False)[0][1]
pascal_voc = PascalVOC('../test_data').test.next_batch(3, shuffle=False)[0][2]
In [4]:
from lib.segmentation import slic_fixed, quickshift_fixed
In [5]:
slic = slic_fixed(num_segments=100, compactness=5, max_iterations=10, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(mnist, slic)
In [6]:
quickshift = quickshift_fixed(ratio=1, kernel_size=2, max_dist=2, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(mnist, quickshift)
In [7]:
slic = slic_fixed(num_segments=150, compactness=5, max_iterations=10, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(cifar_10, slic)
In [8]:
quickshift = quickshift_fixed(ratio=1, kernel_size=1, max_dist=5, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(cifar_10, quickshift)
In [9]:
slic = slic_fixed(num_segments=800, compactness=30, max_iterations=10, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(pascal_voc, slic)
In [10]:
quickshift = quickshift_fixed(ratio=1, kernel_size=3, max_dist=15, sigma=0)
plt.rcParams['figure.figsize'] = (10, 4)
plot_laplacians(pascal_voc, quickshift)