diff --git a/venv/outputmodel1/Dataset2-2.h5 b/venv/outputmodel1/Dataset2-2.h5 index 1b18d20..239d746 100644 Binary files a/venv/outputmodel1/Dataset2-2.h5 and b/venv/outputmodel1/Dataset2-2.h5 differ diff --git a/venv/src/IdentifyInvalidSignals.py b/venv/src/IdentifyInvalidSignals.py new file mode 100644 index 0000000..fffe9f2 --- /dev/null +++ b/venv/src/IdentifyInvalidSignals.py @@ -0,0 +1,146 @@ +# pip install keras +# pip install tensorflow +import keras +import numpy as np +import scipy.io + +from keras.models import Sequential +from keras.layers import Activation, Flatten +from keras.layers import Convolution2D +from keras.layers.core import Dense +from keras.utils import np_utils +import matplotlib.pyplot as plt +import pywt +from keras.models import load_model + + +def spectrum(vector): + A = np.fft.fft(vector) + ps = np.abs(A) ** 2 + ps = ps[:len(ps) // 2] + return ps + + +def normalize(someList): + amin = np.min(someList) + amax = np.max(someList) + mean = np.mean(someList) + for i in range(0, len(someList)): + someList[i] = someList[i] / amax + return someList + + +def removereadings(someList): + return someList[:150] + + +def customeProcess(someList): + return removereadings(normalize(someList)) + + +file = "/Users/rmramesh/Downloads/EEG_BCI_MI_AllSub/SubC_6chan_2LF_s3.mat" +data = scipy.io.loadmat(file) +print(data.keys()) + +xdata = data.get("EEGDATA") +print(xdata.shape) +xdata = xdata.transpose(2, 0, 1) +print(xdata.shape) + +ydata = data.get("LABELS") +print(ydata.shape) + +xshape = xdata.shape +numberOfItems = xshape[0] +numberOfElectrodes = xshape[1] +numberOfReadings = xshape[2] + +# Array shape after processing +nrap = int(numberOfReadings / 2) + +xprocessed = np.zeros([numberOfItems, 150, 150], dtype=float) + +for i in range(0, numberOfItems): + for j in range(0, numberOfElectrodes): + # xprocessed[i][j] = spectrum(xdata[i][j]) + xprocessed[i][j] = customeProcess(spectrum(xdata[i][j])) + +xshape = xprocessed.shape +numberOfItems = xshape[0] +numberOfElectrodes = xshape[1] +numberOfReadings = xshape[2] + +# plt.plot((xprocessed[6][2]), color='green', linewidth=1) +# plt.show() +# exit(0) + +ydata = ydata.transpose(1, 0) +ydata = list(ydata[0]) + +# reducce y data from 1, 2 to 0, 1 respectively +for i in range(0, numberOfItems): + ydata[i] = int(ydata[i] / 2) + +ydata = np.array(ydata) + +print("Shape: ") +print(xprocessed.shape) +print(ydata.shape) +print(ydata) + +print("NRAP:") +print(nrap) + +X_train = xprocessed[:(numberOfItems - 20)] +y_train = ydata[:(numberOfItems - 20)] +X_test = xprocessed[(numberOfItems - 20):] +y_test = ydata[(numberOfItems - 20):] +print("Data formation: ") +print(X_train.shape) +print(X_test.shape) +print(y_train.shape) +print(y_test.shape) + +X_train = X_train.reshape(X_train.shape[0], numberOfReadings, numberOfReadings, 1) +X_test = X_test.reshape(X_test.shape[0], numberOfReadings, numberOfReadings, 1) +X_train = X_train.astype('float32') +X_test = X_test.astype('float32') +Y_train = np_utils.to_categorical(y_train) +Y_test = np_utils.to_categorical(y_test) + +print("Data formation: ") +print(X_train.shape) +print(X_test.shape) +print(Y_train.shape) +print(Y_test.shape) + +# model = Sequential() +# model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(numberOfReadings,numberOfReadings,1))) +# model.add(Convolution2D(40, 1, activation='relu')) +# model.add(Convolution2D(20, 1, activation='relu')) +# model.add(Convolution2D(10, 1, activation='relu')) +# model.add(Convolution2D(2, (numberOfReadings-2))) +# model.add(Flatten()) +# model.add(Activation('softmax')) +# +# model.summary() +# +# model.compile(loss='categorical_crossentropy', +# optimizer='adam', +# metrics=['accuracy']) +# +# model.fit(X_train, Y_train, batch_size=60, epochs=4, verbose=1) +# +# score = model.evaluate(X_test, Y_test, verbose=0) +# +# print("Score:") +# print(score) +model = load_model('../outputmodel1/Dataset2-2.h5') +y_pred = model.predict(X_test.reshape(X_test.shape[0], numberOfReadings, numberOfReadings, 1)) + +print("y predict:") +print(y_pred[:10]) +print("y test:") +print(y_test[:10]) + +# model.save("../outputmodel1/Dataset2-2.h5") diff --git a/venv/src/SyntheticData.py b/venv/src/SyntheticData.py index 028b76a..749c867 100644 --- a/venv/src/SyntheticData.py +++ b/venv/src/SyntheticData.py @@ -93,7 +93,8 @@ def spectrum (vector): model = Sequential() model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(576,576,1))) -model.add(Convolution2D(10, 1, activation='relu')) +model.add(Convolution2D(40, 1, activation='relu')) +model.add(Convolution2D(20, 1, activation='relu')) model.add(Convolution2D(10, 1, activation='relu')) # model.add(Dense(500, activation='relu')) model.add(Convolution2D(2, 574)) @@ -111,7 +112,7 @@ def spectrum (vector): print("Y_train:") print(Y_train.shape) -model.fit(X_train, Y_train, batch_size=40, epochs=2, verbose=1) +model.fit(X_train, Y_train, batch_size=40, epochs=3, verbose=1) score = model.evaluate(X_test, Y_test, verbose=0) model.save('my_model.h5') diff --git a/venv/src/my_model.h5 b/venv/src/my_model.h5 index 1a32cb4..78da49c 100644 Binary files a/venv/src/my_model.h5 and b/venv/src/my_model.h5 differ