Last active
June 29, 2017 16:36
-
-
Save samehkamaleldin/d6622391a982aa85a63cdca697256096 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy.io | |
import keras | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
# (get the data from files) | |
# read train data into python from .mat file | |
mat = scipy.io.loadmat('classifier_data.mat') | |
x_train = mat['TrainData_2d'] | |
y_train = keras.utils.to_categorical(mat['Train_Lables'], num_classes=10) | |
# read validation data into python from .mat file | |
x_val = mat['ValData_2d'] | |
y_val = keras.utils.to_categorical(mat['Val_Lables'], num_classes=10) | |
# read test data into python from a .mat file | |
x_test = mat['TestData_2d'] | |
y_test = keras.utils.to_categorical(mat['Test_Lables'], num_classes=10) | |
num_classes = 10 | |
# first conv layer config | |
conv1_batch = 30 | |
conv1_window = (5, 5) | |
# second conv layer config | |
conv2_batch = 15 | |
conv2_window = (3, 3) | |
model = Sequential() | |
model.add(Conv2D(conv1_batch, conv1_window, data_format='channels_first', input_shape=(1, 20, 146), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Conv2D(conv2_batch, conv2_window, activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.2)) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dense(50, activation='relu')) | |
model.add(Dense(num_classes, activation='softmax')) | |
# Compile model [adam optimizer] | |
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy']) | |
model.fit(x_train, y_train, validation_data=(x_val, y_val), batch_size=32, epochs=10, verbose=1, shuffle=True) | |
performance = model.evaluate(x_test, y_test, batch_size=32, verbose=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment