In [1]:
%load_ext autoreload
%autoreload 2

In [2]:
import os
from time import time
import numpy as np
from data import get_hsi_data


/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [3]:
# get data
img = get_hsi_data.get_data()


-- Indian Pines dataset found locally --
-- Completed --

In [40]:
# reshape image as array
from utils.image import img_as_array
img_Vec = img_as_array(img['original'])
img_gt = img_as_array(img['groundtruth'], gt=True)

In [42]:
# normalize the data
# from utils.image import standardize
# img_Vec = standardize(img_Vec)
from sklearn.preprocessing import Normalizer

img_Vec = Normalizer().fit_transform(img_Vec)

In [43]:
from manifold_learning.se import SchroedingerEigenmaps
from utils.graph import compute_adjacency

t0 = time()
# W = compute_adjacency(X, n_neighbors=20,
#                       weight='heat',
#                       metric='euclidean',
#                       neighbors_algorithm='annoy')


se_model = SchroedingerEigenmaps(n_neighbors=20,
                                 neighbors_algorithm='brute',
                                 n_jobs=6,
                                 potential='ss',
                                 n_components=50,
                                 X_img=img['original'],
                                 sparse=True,
                                 eig_solver='multi')

se_model.fit(img_Vec)
t1 = time()

print('My SE class: {t:.2g} secs'.format(t=t1-t0))


My SE class: 74 secs

In [44]:
eigVals = se_model.eigVals
X_embedding = se_model.embedding_
import matplotlib.pyplot as plt
%matplotlib inline

fig, ax = plt.subplots()

ax.plot(eigVals)
plt.show()



In [45]:
print(np.shape(X_embedding))
print(np.shape(img_gt))


(21025, 50)
(21025,)

In [46]:
# pair x and y with gt
from utils.image import img_gt_idx
X, y = img_gt_idx(X_embedding, img_gt, printinfo=True)

# get training and testing samples
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=.10)


We have 10249 ground-truth samples.
The training data includes 16 classes: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Dimensions of matrix X: (10249, 50)
Dimensions of matrix y: (10249,)

In [47]:
# classification using LDA
from sklearn.lda import LDA

lda_model = LDA()
lda_model.fit(X_train,y_train)
y_pred = lda_model.predict(X_test)

# classification report
from sklearn.metrics import classification_report, confusion_matrix

cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)


print(cr)

plt.figure(figsize=(15,15))
plt.matshow(cm,aspect='auto')
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()


             precision    recall  f1-score   support

          1       0.17      0.65      0.27        37
          2       0.44      0.41      0.43      1286
          3       0.48      0.41      0.44       753
          4       0.35      0.51      0.42       204
          5       0.71      0.68      0.70       440
          6       0.75      0.76      0.75       660
          7       0.34      0.88      0.49        26
          8       0.97      0.76      0.86       429
          9       0.05      0.59      0.10        17
         10       0.51      0.58      0.54       880
         11       0.59      0.62      0.60      2212
         12       0.28      0.20      0.23       528
         13       0.68      0.79      0.73       190
         14       0.84      0.87      0.86      1130
         15       0.50      0.18      0.27       349
         16       0.99      0.87      0.92        84

avg / total       0.59      0.58      0.58      9225

<matplotlib.figure.Figure at 0x7fa24cdb6850>

In [48]:
# predict the entire HSI image
t0 = time()
lda_image = lda_model.predict(X_embedding)
t1 = time()

# reshape the image
lda_image = lda_image.reshape(img['original'][:,:,0].shape)


# Look at Ground Truth for Indian Pines
fig, ax = plt.subplots()
h = ax.imshow(lda_image, cmap=plt.cm.jet)
# colorbar_ax = fig.add_axes([0, 16, 1, 2])
fig.colorbar(h, ticks=[np.linspace(1,16,num=16,endpoint=True)])
plt.title('Indian Pines - Predicted Image - Schroedinger Eigenmaps')
plt.show()


Megman Package


In [65]:
from megaman.geometry import Geometry
from megaman.embedding import SpectralEmbedding

geom = Geometry()

radius = 1.5
adjacency_method = 'cyflann'
cyflann_kwds = {'index_type':'kmeans', 'branching':64, 'iterations':20, 'cb_index':0.4}
adjacency_kwds = {'radius':radius, 'cyflann_kwds':cyflann_kwds}
affinity_method = 'gaussian'
affinity_kwds = {'radius':radius}
laplacian_method = 'geometric'
laplacian_kwds = {'scaling_epps':radius}

geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds,
                affinity_method=affinity_method, affinity_kwds=affinity_kwds,
                laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds)
geom.set_data_matrix(X)

geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds,
                affinity_method=affinity_method, affinity_kwds=affinity_kwds,
                laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds)

t0 = time()

geom.set_data_matrix(img_Vec)
adjacency_matrix = geom.compute_adjacency_matrix()
spectral = SpectralEmbedding(n_components=50,
                             eigen_solver='arpack',
                             geom=geom,
                             drop_first=False)
X_embedding = spectral.fit_transform(X)
t1 = time()
print('Megaman Time: {t} secs'.format(t=t1-t0))


Megaman Time: 24.3622739315 secs

In [66]:
print(np.shape(X_embedding))


(21025, 50)

In [56]:
# pair x and y with gt
from utils.image import img_gt_idx
X, y = img_gt_idx(X_embedding, img_gt, printinfo=True)

# get training and testing samples
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=.50)

# classification using LDA
from sklearn.lda import LDA

lda_model = LDA()
lda_model.fit(X_train,y_train)
y_pred = lda_model.predict(X_test)

# classification report
from sklearn.metrics import classification_report, confusion_matrix

cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)


print(cr)

plt.figure(figsize=(15,15))
plt.matshow(cm,aspect='auto')
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()


We have 10249 ground-truth samples.
The training data includes 16 classes: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Dimensions of matrix X: (10249, 50)
Dimensions of matrix y: (10249,)
             precision    recall  f1-score   support

          1       0.37      0.62      0.46        26
          2       0.55      0.32      0.41       734
          3       0.38      0.30      0.34       403
          4       0.33      0.50      0.39       111
          5       0.60      0.58      0.59       244
          6       0.79      0.84      0.81       389
          7       0.18      0.82      0.29        11
          8       0.96      0.91      0.93       247
          9       0.11      0.58      0.19        12
         10       0.48      0.70      0.57       476
         11       0.62      0.58      0.60      1203
         12       0.22      0.34      0.27       280
         13       0.78      0.91      0.84       110
         14       0.81      0.86      0.83       631
         15       0.52      0.21      0.29       199
         16       1.00      0.86      0.92        49

avg / total       0.60      0.58      0.58      5125

<matplotlib.figure.Figure at 0x7fa2692457d0>

SKLEARN Package


In [68]:
from sklearn.manifold import SpectralEmbedding

t0 = time()

le = SpectralEmbedding(n_components=50,
                       n_neighbors=20,
                       affinity='rbf')
X_embedding = le.fit_transform(img_Vec)
t1 = time()
print('Sklearn Time: {t} secs'.format(t=t1-t0))


Sklearn Time: 464.198864222 secs

In [69]:
print(np.shape(X_embedding))
print(np.shape(img_gt))


(21025, 50)
(21025,)

In [70]:
# pair x and y with gt
from utils.image import img_gt_idx
X, y = img_gt_idx(X_embedding, img_gt, printinfo=True)

# get training and testing samples
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,train_size=.50)

# classification using LDA
from sklearn.lda import LDA

lda_model = LDA()
lda_model.fit(X_train,y_train)
y_pred = lda_model.predict(X_test)

# classification report
from sklearn.metrics import classification_report, confusion_matrix

cm = confusion_matrix(y_test, y_pred)
cr = classification_report(y_test, y_pred)


print(cr)

plt.figure(figsize=(15,15))
plt.matshow(cm,aspect='auto')
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()


We have 10249 ground-truth samples.
The training data includes 16 classes: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16]
Dimensions of matrix X: (10249, 50)
Dimensions of matrix y: (10249,)
             precision    recall  f1-score   support

          1       0.33      0.50      0.39        28
          2       0.66      0.56      0.60       744
          3       0.68      0.45      0.54       422
          4       0.40      0.43      0.42       132
          5       0.72      0.75      0.74       244
          6       0.92      0.86      0.89       354
          7       0.68      0.94      0.79        18
          8       0.91      0.85      0.88       234
          9       0.18      0.90      0.30        10
         10       0.56      0.47      0.51       488
         11       0.58      0.77      0.66      1179
         12       0.56      0.43      0.48       294
         13       0.88      0.90      0.89        98
         14       0.89      0.88      0.89       636
         15       0.61      0.52      0.56       197
         16       0.96      0.91      0.93        47

avg / total       0.68      0.67      0.67      5125

<matplotlib.figure.Figure at 0x7fa2517912d0>

In [ ]: