Commit a33b2f4e authored by Ard Kastrati's avatar Ard Kastrati
Browse files

Finished the clean structure. InceptionTime and EEGNet works.

parent 69ff0eb5
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
......
......@@ -39,7 +39,7 @@ class SplitCNN(nn.Module):
self.split = cluster_split
self.stride = stride
self.cnn = nn.ModuleList()
for i in range(len(split)):
for i in range(len(self.split)):
self.cnn.append(nn.conv1d(cluster_split[i],self.channels[i],
self.kernel,self.stride,self.padding))
......
import keras
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Activation, Permute, Dropout
from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D
......@@ -7,17 +6,14 @@ from tensorflow.keras.layers import SeparableConv2D, DepthwiseConv2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.constraints import max_norm
from config import general_params
from config import config
from utils.utils import *
import seaborn as sns
sns.set_style('darkgrid')
from sklearn.model_selection import train_test_split
import os
def run(trainX,trainY):
classifier = Classifier_DEEPEYE(output_directory=general_params['root_dir'], input_shape=(129, 500))
classifier = Classifier_DEEPEYE(output_directory=config['root_dir'], input_shape=(129, 500))
hist = classifier.fit(deepeye_x=trainX, y=trainY)
plot_loss(hist, 'DeepEye', True)
plot_acc(hist, 'DeepEye', True)
......
import tensorflow as tf
import tensorflow.keras as keras
from config import general_params
from config import config
from utils.utils import *
from tensorflow.keras.models import Model
......@@ -13,7 +13,7 @@ from tensorflow.keras.layers import Input, Flatten
from tensorflow.keras.constraints import max_norm
def run(trainX, trainY):
classifier = Classifier_EEGNet(output_directory=general_params['root_dir'])
classifier = Classifier_EEGNet(output_directory=config['root_dir'])
hist = classifier.fit(trainX, trainY)
plot_loss(hist, 'EEGNet', True)
plot_acc(hist, 'EEGNet', True)
......@@ -80,7 +80,7 @@ class Classifier_EEGNet:
return Model(inputs=input1, outputs=softmax)
def fit(self, inception_x, y):
def fit(self, eegnet_x, y):
self.model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
hist = self.model.fit(inception_x, y, verbose=1, validation_split=0.2, epochs=10)
hist = self.model.fit(eegnet_x, y, verbose=1, validation_split=0.2, epochs=10)
return hist
\ No newline at end of file
import tensorflow as tf
import tensorflow.keras as keras
from config import general_params
from config import config
from utils.utils import *
def run(trainX, trainY):
classifier = Classifier_INCEPTION(output_directory=general_params['root_dir'], input_shape=(500, 129))
print("Starting InceptionTime.")
classifier = Classifier_INCEPTION(output_directory=config['root_dir'], input_shape=(500, 129))
hist = classifier.fit(trainX, trainY)
plot_loss(hist, 'Inception', True)
plot_acc(hist, 'Inception', True)
......@@ -27,7 +28,7 @@ class Classifier_INCEPTION:
self.verbose = verbose
if build:
self.model = self.build_model(input_shape)
self.model = self._build_model(input_shape)
if self.verbose:
self.model.summary()
# self.model.save_weights(self.output_directory + 'model_init.hdf5')
......@@ -56,11 +57,9 @@ class Classifier_INCEPTION:
use_bias=False)(max_pool_1)
conv_list.append(conv_6)
x = tf.keras.layers.Concatenate(axis=2)(conv_list)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation(activation='relu')(x)
return x
def _shortcut_layer(self, input_tensor, out_tensor):
......@@ -90,9 +89,9 @@ class Classifier_INCEPTION:
gap_layer = tf.keras.layers.GlobalAveragePooling1D()(x)
output_layer = tf.keras.layers.Dense(1, activation='sigmoid')(gap_layer)
model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
return model
def fit(self, inception_x, y):
self.model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(), metrics=['accuracy'])
hist = self.model.fit(inception_x, y, verbose=1, validation_split=0.2, epochs=10)
return hist
return hist
\ No newline at end of file
# configuration used by the training and evaluation methods
# let's keep it here to have a clean code on other methods that we try
general_params = {}
CNN_params = {}
InceptionTime_params = {}
import time
import os
config = dict()
##################################################################
# Please note that the following fields will be set by our scripts to re-train and re-evaluate our model.
# Where experiment results are stored.
config['log_dir'] = './runs/'
# In case the pre/post-processing scripts generate intermediate results, we may use config['tmp_dir'] to store them.
config['tmp_dir'] = './tmp/'
# Path to training, validation and test data folders.
# PS: Note that we have to upload the data to the server!!!
config['data_dir'] = '/Volumes/methlab/ETH_AS/preprocessed2/'
config['data_dir_server'] = '/cluster/project/infk/zigeng/preprocessed2/'
# Path of root
config['root_dir'] = '.'
##################################################################
# You can modify the rest or add new fields as you need.
# Hyper-parameters and training configuration.
config['learning_rate'] = 1e-4
"""
Models:
CNN-1: First try: CNN to predict movement towards left or right (prosaccade) with 1 second data.
InceptionTime: The InceptionTime baseline
EEGNet: The other baseline
DeepEye: Our method
cnn: First try: CNN to predict movement towards left or right (prosaccade) with 1 second data.
inception: The InceptionTime baseline
eegnet: The other baseline
deepeye: Our method
"""
# general parameters
general_params['model'] = "InceptionTime"
general_params['data_path'] = "/Volumes/methlab/ETH_AS/preprocessed2/"
general_params['root_dir'] = "."
# Choosing model
config['model'] = 'eegnet'
# CNN - 1
CNN_params['trainX_variable1'] = "EEGprocue"
CNN_params['trainX_variable2'] = "data"
CNN_params['trainX_filename'] = "EEGprocue"
CNN_params['trainY_variable1'] = "trialinfopro"
CNN_params['trainY_variable2'] = "cues"
CNN_params['trainY_filename'] = "trialinfocuelocked"
config['cnn'] = {}
config['cnn']['trainX_variable1'] = "EEGprocue"
config['cnn']['trainX_variable2'] = "data"
config['cnn']['trainX_filename'] = "EEGprocue"
config['cnn']['trainY_variable1'] = "trialinfopro"
config['cnn']['trainY_variable2'] = "cues"
config['cnn']['trainY_filename'] = "trialinfocuelocked"
# InceptionTime
InceptionTime_params['trainX_variable1'] = "EEGprocue"
InceptionTime_params['trainX_variable2'] = "data"
InceptionTime_params['trainX_filename'] = "EEGprocue"
config['inception'] = {}
config['inception']['trainX_variable1'] = "EEGprocue"
config['inception']['trainX_variable2'] = "data"
config['inception']['trainX_filename'] = "EEGprocue"
config['inception']['trainY_variable1'] = "trialinfopro"
config['inception']['trainY_variable2'] = "cues"
config['inception']['trainY_filename'] = "trialinfocuelocked"
# DeepEye
config['deepeye'] = {}
config['deepeye']['trainX_variable1'] = "EEGprocue"
config['deepeye']['trainX_variable2'] = "data"
config['deepeye']['trainX_filename'] = "EEGprocue"
config['deepeye']['trainY_variable1'] = "trialinfopro"
config['deepeye']['trainY_variable2'] = "cues"
config['deepeye']['trainY_filename'] = "trialinfocuelocked"
# You can set descriptive experiment names or simply set empty string ''.
config['model_name'] = 'skeleton_code'
InceptionTime_params['trainY_variable1'] = "trialinfopro"
InceptionTime_params['trainY_variable2'] = "cues"
InceptionTime_params['trainY_filename'] = "trialinfocuelocked"
\ No newline at end of file
# Create a unique output directory for this experiment.
timestamp = str(int(time.time()))
model_folder_name = timestamp if config['model_name'] == '' else timestamp + "_" + config['model_name']
config['model_id'] = timestamp
config['model_dir'] = os.path.abspath(os.path.join(config['log_dir'], model_folder_name))
print("Writing to {}\n".format(config['model_dir']))
\ No newline at end of file
from config import general_params as params
from config import config
import time
from CNN import CNN
from utils import IOHelper
from DeepEye import deepEye
from InceptionTime import inception
from EEGNet import eegNet
import numpy as np
def main():
start_time = time.time()
try:
trainX, trainY = IOHelper.get_data(verbose=True)
# IOHelper.store(trainX, trainY)
except:
return
if params['model'] == 'CNN-1':
if config['model'] == 'cnn':
print("Started running CNN-1. If you want to run other methods please choose another model in the config.py file.")
CNN.run(trainX, trainY)
elif params['model'] == 'InceptionTime':
elif config['model'] == 'inception':
print("Started running InceptionTime. If you want to run other methods please choose another model in the config.py file.")
inception.run(trainX=trainX, trainY=trainY)
elif params['model'] == 'EEGNet':
elif config['model'] == 'eegnet':
print("Started running EEGNet. If you want to run other methods please choose another model in the config.py file.")
eegNet.run(trainX=trainX, trainY=trainY)
eegnet_x = np.transpose(trainX, (0, 2, 1))
print(eegnet_x.shape)
eegNet.run(trainX=eegnet_x, trainY=trainY)
elif params['model'] == 'DeepEye':
elif config['model'] == 'deepeye':
print("Started running DeepEye. If you want to run other methods please choose another model in the config.py file.")
deepEye.run(trainX=trainX, trainY=trainY)
......
File added
File added
from config import general_params as params
from config import CNN_params as CNN_params
from config import config
import numpy as np
import scipy.io as sio
import os
import torch
import pickle
def get_data(verbose=True):
pkl_file_x = open('x.pkl', 'rb')
x = pickle.load(pkl_file_x)
pkl_file_x.close()
if verbose:
print("X training loaded.")
print(x.shape)
pkl_file_y = open('y.pkl', 'rb')
y = pickle.load(pkl_file_y)
pkl_file_y.close()
if verbose:
print("Y training loaded.")
print(y.shape)
return x, y
def store(x, y, clip=True):
if clip:
x = x[:10000]
y = y[:10000]
output_x = open('x_clip.pkl', 'wb')
pickle.dump(x, output_x)
output_x.close()
output_y = open('y_clip.pkl', 'wb')
pickle.dump(y, output_y)
output_y.close()
def store_data(verbose=True):
"""
Load the data for training.
:param datapath: matlab data directory
:param variable: variable of the matlab file
:return: the data as numpy array
"""
train_x = torch_data(data_path=params['data_path'], filename=CNN_params['trainX_filename'],
variable1=CNN_params['trainX_variable1'], variable2=CNN_params['trainX_variable2'],
train_x = collect_data(data_path=config['data_path'], filename=config['cnn']['trainX_filename'],
variable1=config['cnn']['trainX_variable1'], variable2=config['cnn']['trainX_variable2'],
verbose=verbose, detailed_verbose=True)
train_y = torch_data(data_path=params['data_path'], filename=CNN_params['trainY_filename'],
variable1=CNN_params['trainY_variable1'], variable2=CNN_params['trainY_variable2'],
train_y = collect_data(data_path=config['data_path'], filename=config['cnn']['trainY_filename'],
variable1=config['cnn']['trainY_variable1'], variable2=config['cnn']['trainY_variable2'],
verbose=verbose, detailed_verbose=True)
train_y = train_y.type(torch.LongTensor)
return train_x, train_y
def torch_data(data_path, filename, variable1, variable2, verbose=True, detailed_verbose=False):
def collect_data(data_path, filename, variable1, variable2, verbose=True, detailed_verbose=False):
"""
Extract data from the file.
:param data_path: name of the file to open.
......@@ -49,12 +76,8 @@ def torch_data(data_path, filename, variable1, variable2, verbose=True, detailed
except:
print("Trying other trials...")
if verbose: print("Data loaded.")
if verbose: print("Tensoring data...")
full_data_tensor = torch.from_numpy(full_data)
if verbose: print("Data in tensor form.")
return full_data_tensor
return full_data
def load_matlab_trial(datapath, trial, filename, variable1, variable2):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment