In [2]:
import numpy as np
import scipy
import os
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('th')
from keras.utils import np_utils
#from sklearn.cross_validation import StratifiedKFold


Using TensorFlow backend.
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])

In [3]:
PATH = os.getcwd()
print (PATH)


/Volumes/ADATA_HV620/indepented_projects/Classification-of-Hyperspectral-Image

In [4]:
# Global Variables
windowSize = 5
numPCAcomponents = 30
testRatio = 0.25

Load Training Dataset


In [5]:
X_train = np.load("/home/deeplearning/Desktop/GITHUB/XtrainWindowSize" 
                  + str(windowSize) + "PCA" + str(numPCAcomponents) + "testRatio" + str(testRatio)  + ".npy")

y_train = np.load("/home/deeplearning/Desktop/GITHUB/ytrainWindowSize" 
                  + str(windowSize) + "PCA" + str(numPCAcomponents) + "testRatio" + str(testRatio) + ".npy")

In [6]:
# Reshape into (numberofsumples, channels, height, width)
X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[3], X_train.shape[1], X_train.shape[2]))

In [7]:
# convert class labels to on-hot encoding
y_train = np_utils.to_categorical(y_train)

In [8]:
# Define the input shape 
input_shape= X_train[0].shape
print(input_shape)


(30, 5, 5)

In [9]:
# number of filters
C1 = 3*numPCAcomponents

In [10]:
# Define the model
model = Sequential()

model.add(Conv2D(C1, (3, 3), activation='relu', input_shape=input_shape))
model.add(Conv2D(3*C1, (3, 3), activation='relu'))
model.add(Dropout(0.25))



model.add(Flatten())
model.add(Dense(6*numPCAcomponents, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(16, activation='softmax'))

In [11]:
sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

In [12]:
model.fit(X_train, y_train, batch_size=32, epochs=15)


Epoch 1/15
29685/29685 [==============================] - 69s - loss: 2.5077 - acc: 0.2228    
Epoch 2/15
29685/29685 [==============================] - 65s - loss: 1.9555 - acc: 0.4355    
Epoch 3/15
29685/29685 [==============================] - 62s - loss: 1.4767 - acc: 0.5698    
Epoch 4/15
29685/29685 [==============================] - 58s - loss: 1.1480 - acc: 0.6492    
Epoch 5/15
29685/29685 [==============================] - 57s - loss: 0.9374 - acc: 0.7051    
Epoch 6/15
29685/29685 [==============================] - 63s - loss: 0.7952 - acc: 0.7394    
Epoch 7/15
29685/29685 [==============================] - 63s - loss: 0.7023 - acc: 0.7672    
Epoch 8/15
29685/29685 [==============================] - 63s - loss: 0.6307 - acc: 0.7914    
Epoch 9/15
29685/29685 [==============================] - 64s - loss: 0.5749 - acc: 0.8079    - 
Epoch 10/15
29685/29685 [==============================] - 68s - loss: 0.5299 - acc: 0.8226    
Epoch 11/15
29685/29685 [==============================] - 63s - loss: 0.4917 - acc: 0.8334    
Epoch 12/15
29685/29685 [==============================] - 64s - loss: 0.4563 - acc: 0.8471    
Epoch 13/15
29685/29685 [==============================] - 64s - loss: 0.4306 - acc: 0.8578    
Epoch 14/15
29685/29685 [==============================] - 64s - loss: 0.3979 - acc: 0.8682    
Epoch 15/15
29685/29685 [==============================] - 62s - loss: 0.3792 - acc: 0.8748    - ETA: 1s 
Out[12]:
<keras.callbacks.History at 0x7fe95e3862b0>

In [13]:
import h5py
from keras.models import load_model

In [14]:
model.save('my_model.h5')

In [ ]: