In [72]:
import sys
sys.path.insert(0, '..')
import numpy as np
import matplotlib.pyplot as plt
from lib.segmentation import FormFeatureSelection
%matplotlib inline
In [73]:
from sklearn.decomposition import PCA
def plot_corrcoef(features):
num_features = features.shape[1]
corrcoef = np.corrcoef(features, rowvar=False)
plt.pcolor(corrcoef)
plt.colorbar()
plt.yticks(np.arange(0.5, num_features + 0.5, 5), np.arange(0, num_features, 5))
plt.xticks(np.arange(0.5, num_features + 0.5, 5), np.arange(0, num_features, 5))
plt.show()
def plot_pca(features):
pca = PCA()
pca.fit(features)
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('Number of components')
plt.ylabel('Cumulative explained variance')
plt.show()
In [74]:
from lib.datasets import MNIST
dataset = MNIST('../data/mnist').test
In [75]:
from lib.segmentation import slic_fixed
slic = slic_fixed(num_segments=100, compactness=5, max_iterations=10, sigma=0)
selector = FormFeatureSelection(dataset, slic, num_examples=1000)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [76]:
plot_corrcoef(selector.features)
In [77]:
plot_pca(selector.features)
In [78]:
selector.select_univariate(12)
In [79]:
plot_corrcoef(selector.features)
In [80]:
selector.eliminate_recursive(9)
In [81]:
plot_corrcoef(selector.features)
In [82]:
plot_pca(selector.features)
In [83]:
print(selector.selected_feature_indices)
print(selector.selected_features)
In [84]:
from lib.segmentation import quickshift_fixed
quickshift = quickshift_fixed(ratio=1, kernel_size=2, max_dist=2, sigma=0)
selector = FormFeatureSelection(dataset, quickshift, num_examples=1000)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [85]:
selector.select_univariate(12)
selector.eliminate_recursive(9)
print(selector.selected_feature_indices)
print(selector.selected_features)
In [86]:
from lib.datasets import Cifar10
dataset = Cifar10('../data/cifar_10').test
In [87]:
from lib.segmentation import slic_fixed
slic = slic_fixed(num_segments=200, compactness=5, max_iterations=10, sigma=0)
selector = FormFeatureSelection(dataset, slic, num_examples=1000)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [88]:
selector.select_univariate(12)
selector.eliminate_recursive(9)
print(selector.selected_feature_indices)
print(selector.selected_features)
In [89]:
from lib.segmentation import quickshift_fixed
quickshift = quickshift_fixed(ratio=1, kernel_size=1, max_dist=5, sigma=0)
selector = FormFeatureSelection(dataset, quickshift, num_examples=1000)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [90]:
selector.select_univariate(12)
selector.eliminate_recursive(9)
print(selector.selected_feature_indices)
print(selector.selected_features)
In [91]:
from lib.datasets import PascalVOC
NUM_EXAMPLES = 200
dataset = PascalVOC('../data/pascal_voc', val_size=NUM_EXAMPLES).test
In [92]:
from lib.segmentation import slic_fixed
slic = slic_fixed(num_segments=1600, compactness=30, max_iterations=10, sigma=0)
selector = FormFeatureSelection(dataset, slic, num_examples=NUM_EXAMPLES)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [93]:
selector.select_univariate(12)
selector.eliminate_recursive(9)
print(selector.selected_feature_indices)
print(selector.selected_features)
In [94]:
from lib.segmentation import quickshift_fixed
quickshift = quickshift_fixed(ratio=0.75, kernel_size=2, max_dist=8, sigma=0)
selector = FormFeatureSelection(dataset, quickshift, num_examples=NUM_EXAMPLES)
print('Number of segments collected:', selector.features.shape[0])
print('Number of features collected:', selector.num_features)
In [95]:
selector.select_univariate(12)
selector.eliminate_recursive(9)
print(selector.selected_feature_indices)
print(selector.selected_features)