[5ec66b]: / EEGLearn_ShortDemo.ipynb

Download this file

406 lines (405 with data), 13.9 kB

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pytorch Implementation of EEGLearn - P. Bashivan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook describes a short summary of Pytorch implementation of the models described in \"Learning Representations from EEG with Deep Recurrent-Convolutional Neural Networks.\" Bashivan et al. at International conference on learning representations (2016).\n",
    "\n",
    "The rest of the code is in the different python scripts of this repo.\n",
    "\n",
    "All the codes have been inspired from the [original github](https://github.com/pbashivan/EEGLearn)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Librairies Import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import scipy.io as sio\n",
    "import torch\n",
    "import os \n",
    "\n",
    "import torch.optim as optim\n",
    "import torch.nn.functional as F\n",
    "\n",
    "from torch.autograd import Variable\n",
    "from torch.utils.data.dataset import Dataset\n",
    "from torch.utils.data import DataLoader,random_split\n",
    "\n",
    "from Utils import *\n",
    "from Models import *\n",
    "\n",
    "torch.manual_seed(1234)\n",
    "np.random.seed(1234)\n",
    "\n",
    "import warnings\n",
    "warnings.simplefilter(\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading the original Images \n",
    "The images have directly been taken from original implementation, given that they remain the same nevermind the implementation (Pytorch, Tensorflow, Theano)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2670, 3, 32, 32)\n",
      "(2670, 7, 3, 32, 32)\n",
      "(2670,)\n",
      "(2670,)\n"
     ]
    }
   ],
   "source": [
    "Mean_Images = sio.loadmat(\"Sample Data/images.mat\")[\"img\"] #corresponding to the images mean for all the seven windows\n",
    "print(np.shape(Mean_Images)) \n",
    "Images = sio.loadmat(\"Sample Data/images_time.mat\")[\"img\"] #corresponding to the images mean for all the seven windows\n",
    "print(np.shape(Images)) \n",
    "Label = (sio.loadmat(\"Sample Data/FeatureMat_timeWin\")[\"features\"][:,-1]-1).astype(int) #corresponding to the signal label (i.e. load levels).\n",
    "print(np.shape(Label)) \n",
    "Patient_id = sio.loadmat(\"Sample Data/trials_subNums.mat\")['subjectNum'][0] #corresponding to the patient id\n",
    "print(np.shape(Patient_id))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading patient dataset \n",
    "From the total data, we select the images corresponding patient. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Choose among the patient : [ 1  2  3  4  6  7  8  9 10 11 12 14 15]\n"
     ]
    }
   ],
   "source": [
    "print(\"Choose among the patient : \"+str(np.unique(Patient_id)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "choosen_patient = 9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction: BasicCNN\n",
    "First Implementation of a CNN on the Mean Images from each patient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_part = 0.8\n",
    "test_part = 0.2\n",
    "\n",
    "batch_size = 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "EEG = EEGImagesDataset(label=Label[Patient_id==choosen_patient], image=Mean_Images[Patient_id==choosen_patient])\n",
    "\n",
    "lengths = [int(len(EEG)*train_part+1), int(len(EEG)*test_part)]\n",
    "Train, Test = random_split(EEG, lengths)\n",
    "\n",
    "Trainloader = DataLoader(Train,batch_size=batch_size)\n",
    "Testloader = DataLoader(Test, batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = TrainTest_Model(BasicCNN, Trainloader, Testloader, n_epoch=50, learning_rate=0.001, print_epoch=-1, opti='Adam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Maxpool CNN\n",
    "Build the Max-pooling model performing a maxpool over the 7 parallel convnets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_part = 0.8\n",
    "test_part = 0.2\n",
    "\n",
    "batch_size = 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "EEG = EEGImagesDataset(label=Label[Patient_id==choosen_patient], image=Images[Patient_id==choosen_patient])\n",
    "\n",
    "lengths = [int(len(EEG)*train_part+1), int(len(EEG)*test_part)]\n",
    "Train, Test = random_split(EEG, lengths)\n",
    "\n",
    "Trainloader = DataLoader(Train,batch_size=batch_size)\n",
    "Testloader = DataLoader(Test, batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Training for Patient 9\n",
      "[5,  45]\tloss: 0.850\tAccuracy : 0.617\t\tval-loss: 0.644\tval-Accuracy : 0.875\n",
      "[10,  45]\tloss: 0.159\tAccuracy : 0.920\t\tval-loss: 0.980\tval-Accuracy : 0.825\n",
      "[15,  45]\tloss: 0.094\tAccuracy : 0.963\t\tval-loss: 0.233\tval-Accuracy : 0.900\n",
      "[20,  45]\tloss: 0.003\tAccuracy : 1.000\t\tval-loss: 0.090\tval-Accuracy : 0.950\n",
      "[25,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 0.017\tval-Accuracy : 1.000\n",
      "[30,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 0.017\tval-Accuracy : 1.000\n",
      "[35,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 0.015\tval-Accuracy : 1.000\n",
      "[40,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 0.011\tval-Accuracy : 1.000\n",
      "[45,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 0.006\tval-Accuracy : 1.000\n"
     ]
    }
   ],
   "source": [
    "print('Begin Training for Patient '+str(choosen_patient))\n",
    "res = TrainTest_Model(MaxCNN, Trainloader, Testloader, n_epoch=45, learning_rate=0.001, print_epoch=5, opti='Adam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Temp CNN\n",
    "FBuild the Conv1D model performing a convolution1D over the 7 parallel convnets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Training for Patient 9\n",
      "[5,  45]\tloss: 0.438\tAccuracy : 0.821\t\tval-loss: 1.963\tval-Accuracy : 0.750\n",
      "[10,  45]\tloss: 0.153\tAccuracy : 0.938\t\tval-loss: 2.079\tval-Accuracy : 0.875\n",
      "[15,  45]\tloss: 0.005\tAccuracy : 1.000\t\tval-loss: 4.025\tval-Accuracy : 0.825\n",
      "[20,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 3.416\tval-Accuracy : 0.900\n",
      "[25,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 3.587\tval-Accuracy : 0.900\n",
      "[30,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 3.672\tval-Accuracy : 0.900\n",
      "[35,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 3.827\tval-Accuracy : 0.900\n",
      "[40,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 4.332\tval-Accuracy : 0.900\n",
      "[45,  45]\tloss: 0.000\tAccuracy : 1.000\t\tval-loss: 5.142\tval-Accuracy : 0.900\n",
      "Finished Training \n",
      " loss: 0.000\tAccuracy : 1.000\t\tval-loss: 5.142\tval-Accuracy : 0.900\n"
     ]
    }
   ],
   "source": [
    "print('Begin Training for Patient '+str(choosen_patient))\n",
    "res = TrainTest_Model(TempCNN, Trainloader, Testloader, n_epoch=45, learning_rate=0.001, print_epoch=5, opti='Adam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LSTM CNN\n",
    "Build the LSTM model applying a RNN over the 7 parallel convnets outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "EEG = EEGImagesDataset(label=Label[Patient_id==choosen_patient], image=Images[Patient_id==choosen_patient])\n",
    "\n",
    "lengths = [int(len(EEG)*train_part+1), int(len(EEG)*test_part)]\n",
    "Train, Test = random_split(EEG, lengths)\n",
    "\n",
    "Trainloader = DataLoader(Train,batch_size=batch_size)\n",
    "Testloader = DataLoader(Test, batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Training for Patient 9\n",
      "[5,  45]\tloss: 1.321\tAccuracy : 0.333\t\tval-loss: 1.386\tval-Accuracy : 0.225\n",
      "[10,  45]\tloss: 1.225\tAccuracy : 0.364\t\tval-loss: 1.427\tval-Accuracy : 0.325\n",
      "[15,  45]\tloss: 1.002\tAccuracy : 0.562\t\tval-loss: 1.115\tval-Accuracy : 0.450\n",
      "[20,  45]\tloss: 0.664\tAccuracy : 0.704\t\tval-loss: 0.744\tval-Accuracy : 0.600\n",
      "[25,  45]\tloss: 0.392\tAccuracy : 0.895\t\tval-loss: 0.515\tval-Accuracy : 0.775\n",
      "[30,  45]\tloss: 0.187\tAccuracy : 0.988\t\tval-loss: 0.285\tval-Accuracy : 0.900\n",
      "[35,  45]\tloss: 0.082\tAccuracy : 1.000\t\tval-loss: 0.236\tval-Accuracy : 0.825\n",
      "[40,  45]\tloss: 0.040\tAccuracy : 1.000\t\tval-loss: 0.170\tval-Accuracy : 0.900\n",
      "[45,  45]\tloss: 0.022\tAccuracy : 1.000\t\tval-loss: 0.141\tval-Accuracy : 0.950\n",
      "Finished Training \n",
      " loss: 0.022\tAccuracy : 1.000\t\tval-loss: 0.141\tval-Accuracy : 0.950\n"
     ]
    }
   ],
   "source": [
    "print('Begin Training for Patient '+str(choosen_patient))\n",
    "res = TrainTest_Model(LSTM, Trainloader, Testloader, n_epoch=45, learning_rate=0.0001, print_epoch=5, opti='Adam')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mix CNN\n",
    "Build the LSTM model applying a RNN and a CNN over the 7 parallel convnets outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "EEG = EEGImagesDataset(label=Label[Patient_id==choosen_patient], image=Images[Patient_id==choosen_patient])\n",
    "\n",
    "lengths = [int(len(EEG)*train_part+1), int(len(EEG)*test_part)]\n",
    "Train, Test = random_split(EEG, lengths)\n",
    "\n",
    "Trainloader = DataLoader(Train,batch_size=batch_size)\n",
    "Testloader = DataLoader(Test, batch_size=batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Begin Training for Patient 9\n",
      "[5, 100]\tloss: 1.375\tAccuracy : 0.296\t\tval-loss: 1.376\tval-Accuracy : 0.150\n",
      "[10, 100]\tloss: 1.362\tAccuracy : 0.302\t\tval-loss: 1.371\tval-Accuracy : 0.225\n",
      "[15, 100]\tloss: 1.352\tAccuracy : 0.302\t\tval-loss: 1.368\tval-Accuracy : 0.225\n",
      "[20, 100]\tloss: 1.342\tAccuracy : 0.302\t\tval-loss: 1.362\tval-Accuracy : 0.225\n",
      "[25, 100]\tloss: 1.312\tAccuracy : 0.302\t\tval-loss: 1.335\tval-Accuracy : 0.225\n",
      "[30, 100]\tloss: 1.191\tAccuracy : 0.302\t\tval-loss: 1.250\tval-Accuracy : 0.225\n",
      "[35, 100]\tloss: 0.981\tAccuracy : 0.586\t\tval-loss: 1.105\tval-Accuracy : 0.575\n",
      "[40, 100]\tloss: 0.836\tAccuracy : 0.605\t\tval-loss: 1.015\tval-Accuracy : 0.650\n",
      "[45, 100]\tloss: 0.760\tAccuracy : 0.611\t\tval-loss: 1.008\tval-Accuracy : 0.700\n",
      "[50, 100]\tloss: 0.677\tAccuracy : 0.654\t\tval-loss: 1.047\tval-Accuracy : 0.725\n",
      "[55, 100]\tloss: 0.561\tAccuracy : 0.753\t\tval-loss: 1.120\tval-Accuracy : 0.725\n",
      "[60, 100]\tloss: 0.421\tAccuracy : 0.833\t\tval-loss: 1.253\tval-Accuracy : 0.800\n",
      "[65, 100]\tloss: 0.301\tAccuracy : 0.895\t\tval-loss: 1.419\tval-Accuracy : 0.750\n",
      "[70, 100]\tloss: 0.212\tAccuracy : 0.944\t\tval-loss: 1.557\tval-Accuracy : 0.825\n",
      "[75, 100]\tloss: 0.145\tAccuracy : 0.969\t\tval-loss: 1.810\tval-Accuracy : 0.875\n",
      "[80, 100]\tloss: 0.096\tAccuracy : 0.981\t\tval-loss: 2.223\tval-Accuracy : 0.875\n",
      "[85, 100]\tloss: 0.063\tAccuracy : 0.994\t\tval-loss: 2.621\tval-Accuracy : 0.875\n",
      "[90, 100]\tloss: 0.043\tAccuracy : 0.994\t\tval-loss: 2.955\tval-Accuracy : 0.900\n",
      "[95, 100]\tloss: 0.031\tAccuracy : 0.994\t\tval-loss: 3.245\tval-Accuracy : 0.900\n",
      "[100, 100]\tloss: 0.023\tAccuracy : 0.994\t\tval-loss: 3.496\tval-Accuracy : 0.900\n",
      "Finished Training \n",
      " loss: 0.023\tAccuracy : 0.994\t\tval-loss: 3.496\tval-Accuracy : 0.900\n"
     ]
    }
   ],
   "source": [
    "print('Begin Training for Patient '+str(choosen_patient))\n",
    "res = TrainTest_Model(Mix, Trainloader, Testloader, n_epoch=60, learning_rate=0.00001, print_epoch=5, opti='Adam')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Pytorch",
   "language": "python",
   "name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}