Skip to content

An real-time EEG emotive-state classifier trained on DEAP dataset using ML Engine and Cloud Storage provided by Google Cloud Platform

Notifications You must be signed in to change notification settings

hestonst/HappyEEG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

HappyEEG

HappyEEG is a cloud-enabled, mobile-ready, realtime EEG emotion predicting algorithm allowing marketing specialists to see how their material affects their target audience at the subconcious level. Want to know if a poster of a luxury brand spikes the Schadenfreude--responsible for jealousy-- in real-time with real-world stimuli to know how your audience react to your ad? Use HappyEEG!

  • Generalizes to new brains with short training (<20 minutes)
  • State of the art training is performed entirely in the Google ML Engine
  • This allows a completely mobile system to monitor emotional states virtually anywhere

Google Cloud Platform Jupyter Notebooks Google Cloud Platform

Setup

Simply download the DEAP dataset from here and set-up a Google GPU-enabled Jupyter notebook in under 15 minutes per these instructions. Then upload this very notebook to your Google VM to be running accurate valence (emotion-state) classification right away! Serve this model with Google AI Enginer to allow for online learning and connect an internet (MySQL) commercially available EEG cap to monitor your target audience's emotion state and intensity.

Classifing Offline:

First, let's read in a entire minute of EEG data. This is a lot of time to be considered real-time, but it's a good proof-of-concept. Note that our data has already been down-sampled to 126 Hz, and trasnformed into the power domain. Implement these in silico for a real-time system.

from os import listdir
import random
from os.path import isfile, join
import numpy as np
import numpy as np
import datetime
import os
import subprocess
import sys
import xgboost as xgb
import pickle 
---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

<ipython-input-6-5a51e74eb298> in <module>()
      8 import subprocess
      9 import sys
---> 10 import xgboost as xgb
     11 import pickle


ImportError: No module named 'xgboost'
#first, read in the pre-processed data from a folder containing the DEAP .dat files.  

print("Loading EEG files")
validation_fragments = []
validation_truth = []
train_fragments = []
train_truth = []
filenames = [f for f in listdir("DEAP_data/") if (isfile(join("DEAP_data/", f)) and '.dat' in f)]
print("Filenames are ", filenames)

x_test = []
y_test= []
x_train = []
y_train= []
import _pickle as cPickle

import random

for filename in filenames:
    with open("DEAP_data/" + filename, 'rb') as f:
        print(filename)
        array = cPickle.load(f, encoding='latin1')
        #print("array is", np.array(array))
        for datum, label in zip(list(array["data"]), list(array["labels"])):
            if random.uniform(0,1) < .2:
                x_test.append(np.array(datum).flatten())
                y_test.append(label[0])
            else:
                x_train.append(np.array(datum).flatten())
                y_train.append(label[0])


import numpy as np

x_test, y_test, x_train, y_train = np.array(x_test), np.array(y_test), np.array(x_train), np.array(y_train)

# Load data into DMatrix object
print(x_train.shape, " is length")
dtrain = xgb.DMatrix(x_train, label=y_train)
dvalidation = xgb.DMatrix(x_test, label=y_test)

# Train XGBoost model
parameter_dictionary = {}
parameter_dictionary["objective"] = "binary:logitraw"
parameter_dictionary["watchlist"]= dvalidation

clf = xgb.XGBRegressor(n_estimators=10000)
eval_set  = [(x_train,y_train), (x_test,y_test)]
clf.fit(x_train, y_train, eval_set=eval_set, eval_metric="mae")
# Export the classifier to a file
model = 'model.bst'

Classifing Online:

Now that we have some classification working, let us confirm that we can classifiy with only 3 seconds of brain-recording so that we can truly claim a real-time system!

print("Loading EEG files")
validation_fragments = []
validation_truth = []
train_fragments = []
train_truth = []
filenames = [f for f in listdir("DEAP_data/") if (isfile(join("DEAP_data/", f)) and '.dat' in f)]
print("Filenames are ", filenames)

x_test = []
y_test= []
x_train = []
y_train= []
import _pickle as cPickle

import random

for filename in filenames:
    with open("DEAP_data/" + filename, 'rb') as f:
        print(filename)
        array = cPickle.load(f, encoding='latin1')
        #print("array is", np.array(array))
        for datum, label in zip(list(array["data"]), list(array["labels"])):
            numberSegments = (len(datum) % (1751))
            markers = np.array([(1,(i)*(1751),(i+1)*(1751)-1) for i in range(numberSegments-1)]) 
            print("markers are ", markers)
            for stim_code, start, end in markers:
                stim_code, start, end = (int(stim_code), int(start), int(end))
                #note that the Emory lab doesn't use end; all are of length 1
                print("datum size is ", datum.size)
                print("start is ", start)
                fragment = datum[start:end]   
                print("fragment size is ", fragment.size)
                if not len(fragment) == 0:
                    if random.uniform(0,1) < .8:
                        x_test.append(np.array(fragment).flatten())
                        y_test.append(label[0])
                    else:
                        x_train.append(np.array(fragment).flatten())
                        y_train.append(label[0])


import numpy as np



x_test, y_test, x_train, y_train = np.array(x_test), np.array(y_test), np.array(x_train), np.array(y_train)


# Load data into DMatrix object
print(x_train.shape, " is length")
dtrain = xgb.DMatrix(x_train, label=y_train)
dvalidation = xgb.DMatrix(x_test, label=y_test)

# Train XGBoost model
parameter_dictionary = {}
parameter_dictionary["objective"] = "binary:logitraw"
parameter_dictionary["watchlist"]= dvalidation

clf = xgb.XGBRegressor(n_estimators=10000)
#we found XGBoost to be more affective than any keras-implemented CNN we tried
eval_set  = [(x_train,y_train), (x_test,y_test)]
clf.fit(x_train, y_train, eval_set=eval_set, eval_metric="mae")
#we use mean absolute error becuase this is a regression problem 
# Export the classifier to a file
model = 'model.bst'

Et voilà ! A state-of-the-art classifier fit for a novel EEG application, which achieves almost 80% accuracy. Do you think you experience anger, jealousy, or happiness when you see most ads? What makes them more or less effective?

About

An real-time EEG emotive-state classifier trained on DEAP dataset using ML Engine and Cloud Storage provided by Google Cloud Platform

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published