├── README.md └── model_deXpression.py /README.md: -------------------------------------------------------------------------------- 1 | # DeXpression 2 | TFLearn Implementation of [DeXpression architecture](https://arxiv.org/abs/1509.05371). 3 | 4 | Batch normalization is used instead of LRN. 5 | 6 | Gives a precision of 99.3 percent, recall of 99.2 percent and f1-score of 99.2 percent on CKPlus Dataset for human emotion recognition from frontal facial images. 7 | 8 | Please refer to this GitHub [Issue #1](https://github.com/arundasan91/DeXpression/issues/1) for info regarding the dataset creation. 9 | -------------------------------------------------------------------------------- /model_deXpression.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tflearn 3 | import tflearn.activations as activations 4 | # Data loading and preprocessing 5 | from tflearn.activations import relu 6 | from tflearn.data_utils import shuffle, to_categorical 7 | from tflearn.layers.conv import avg_pool_2d, conv_2d, max_pool_2d 8 | from tflearn.layers.core import dropout, flatten, fully_connected, input_data 9 | from tflearn.layers.merge_ops import merge 10 | from tflearn.layers.normalization import local_response_normalization 11 | from tflearn.layers.normalization import batch_normalization 12 | 13 | # Give a run ID here. Change it to flags (arguments) in version 2. 14 | ID = '4_1' 15 | RUNID = 'DeXpression_run_' + ID 16 | 17 | # Give a dropout if required (change to True and define the dropout percentage). 18 | dropout = False 19 | dropout_keep_prob=0.5 20 | 21 | # Load data 22 | X = np.load('CKP_X.npy') 23 | Y = np.load('CKP_Y.npy') 24 | 25 | # Define number of output classes. 26 | num_classes = 7 27 | 28 | # Define padding scheme. 29 | padding = 'VALID' 30 | 31 | # Model Architecture 32 | network = input_data(shape=[None, 224, 224, 1]) 33 | conv_1 = relu(conv_2d(network, 64, 7, strides=2, bias=True, padding=padding, activation=None, name='Conv2d_1')) 34 | maxpool_1 = batch_normalization(max_pool_2d(conv_1, 3, strides=2, padding=padding, name='MaxPool_1')) 35 | #LRN_1 = local_response_normalization(maxpool_1, name='LRN_1') 36 | # FeatEX-1 37 | conv_2a = relu(conv_2d(maxpool_1, 96, 1, strides=1, padding=padding, name='Conv_2a_FX1')) 38 | maxpool_2a = max_pool_2d(maxpool_1, 3, strides=1, padding=padding, name='MaxPool_2a_FX1') 39 | conv_2b = relu(conv_2d(conv_2a, 208, 3, strides=1, padding=padding, name='Conv_2b_FX1')) 40 | conv_2c = relu(conv_2d(maxpool_2a, 64, 1, strides=1, padding=padding, name='Conv_2c_FX1')) 41 | FX1_out = merge([conv_2b, conv_2c], mode='concat', axis=3, name='FX1_out') 42 | # FeatEX-2 43 | conv_3a = relu(conv_2d(FX1_out, 96, 1, strides=1, padding=padding, name='Conv_3a_FX2')) 44 | maxpool_3a = max_pool_2d(FX1_out, 3, strides=1, padding=padding, name='MaxPool_3a_FX2') 45 | conv_3b = relu(conv_2d(conv_3a, 208, 3, strides=1, padding=padding, name='Conv_3b_FX2')) 46 | conv_3c = relu(conv_2d(maxpool_3a, 64, 1, strides=1, padding=padding, name='Conv_3c_FX2')) 47 | FX2_out = merge([conv_3b, conv_3c], mode='concat', axis=3, name='FX2_out') 48 | net = flatten(FX2_out) 49 | if dropout: 50 | net = dropout(net, dropout_keep_prob) 51 | loss = fully_connected(net, num_classes,activation='softmax') 52 | 53 | # Compile the model and define the hyperparameters 54 | network = tflearn.regression(loss, optimizer='Adam', 55 | loss='categorical_crossentropy', 56 | learning_rate=0.0001) 57 | 58 | # Final definition of model checkpoints and other configurations 59 | model = tflearn.DNN(network, checkpoint_path='/home/cc/DeXpression/DeXpression_checkpoints', 60 | max_checkpoints=1, tensorboard_verbose=2, tensorboard_dir="./tflearn_logs/") 61 | 62 | # Fit the model, train for 20 epochs. (Change all parameters to flags (arguments) on version 2.) 63 | model.fit(X, Y, n_epoch=20, validation_set=0.1, shuffle=True, show_metric=True, batch_size=350, snapshot_step=2000,snapshot_epoch=True, run_id=RUNID) 64 | 65 | # Save the model 66 | model.save('./DeXpression_checkpoints/' + RUNID + '.model') 67 | 68 | # Load the model if required, later. 69 | #model.load('./DeXpression_checkpoints/' + RUNID + '.model') 70 | --------------------------------------------------------------------------------