text_plain_output_1.png
from keras import callbacks

from keras.layers import Dense , Activation , Dropout ,Flatten

from keras.layers.convolutional import Conv2D

from keras.layers.convolutional import MaxPooling2D

from keras.layers.normalization import BatchNormalization

from keras.models import Sequential

from keras.preprocessing.image import ImageDataGenerator

import cv2

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import os

data_path = '../input/ckplus/CK+48/'
for dir1 in os.listdir(data_path):
    count = 0
    for f in os.listdir(data_path + dir1):
        count += 1

plt.imread('../input/ckplus/CK+48/fear/S091_001_00000013.png').shape

data_path = '../input/ckplus/CK+48'
data_dir_list = os.listdir(data_path)
img_rows = 256
img_cols = 256
num_channel = 1
num_epoch = 10
img_data_list = []
for dataset in data_dir_list:
    img_list = os.listdir(data_path + '/' + dataset)
    for img in img_list:
        input_img = cv2.imread(data_path + '/' + dataset + '/' + img)
        input_img_resize = cv2.resize(input_img, (48, 48))
        img_data_list.append(input_img_resize)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data = img_data / 255
img_data.shape

num_classes = 7
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,), dtype='int64')
labels[0:134] = 0
labels[135:188] = 1
labels[189:365] = 2
labels[366:440] = 3
labels[441:647] = 4
labels[648:731] = 5
labels[732:980] = 6
names = ['anger', 'contempt', 'disgust', 'fear', 'happy', 'sadness', 'surprise']
def getLabel(id):
    return ['anger', 'contempt', 'disgust', 'fear', 'happy', 'sadness', 'surprise'][id]

trainAug = ImageDataGenerator(rotation_range=30, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode='nearest')
valAug = ImageDataGenerator()

input_shape = (48, 48, 3)
"\nmodel = Sequential()\nmodel.add(Conv2D(6, (5, 5), input_shape=input_shape, padding='same', activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(16, (5, 5), padding='same', activation = 'relu'))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(7, activation = 'softmax'))\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'],optimizer='adam')\n"
def build_cnn(input_shape, show_arch=True):
    net = Sequential(name='DCNN')
    net.add(Conv2D(filters=64, kernel_size=(3, 3), input_shape=input_shape, activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_1'))
    net.add(BatchNormalization(name='batchnorm_1'))
    net.add(Conv2D(filters=64, kernel_size=(3, 3), activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_2'))
    net.add(BatchNormalization(name='batchnorm_2'))
    net.add(MaxPooling2D(pool_size=(2, 2), name='maxpool2d_1'))
    net.add(Dropout(0.45, name='dropout_1'))
    net.add(Conv2D(filters=128, kernel_size=(3, 3), activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_3'))
    net.add(BatchNormalization(name='batchnorm_3'))
    net.add(Conv2D(filters=128, kernel_size=(3, 3), activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_4'))
    net.add(BatchNormalization(name='batchnorm_4'))
    net.add(MaxPooling2D(pool_size=(2, 2), name='maxpool2d_2'))
    net.add(Dropout(0.45, name='dropout_2'))
    net.add(Conv2D(filters=256, kernel_size=(3, 3), activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_5'))
    net.add(BatchNormalization(name='batchnorm_5'))
    net.add(Conv2D(filters=256, kernel_size=(3, 3), activation='elu', padding='same', kernel_initializer='he_normal', name='conv2d_6'))
    net.add(BatchNormalization(name='batchnorm_6'))
    net.add(MaxPooling2D(pool_size=(2, 2), name='maxpool2d_3'))
    net.add(Dropout(0.5, name='dropout_3'))
    net.add(Flatten(name='flatten'))
    net.add(Dense(128, activation='elu', kernel_initializer='he_normal', name='dense_1'))
    net.add(BatchNormalization(name='batchnorm_7'))
    net.add(Dropout(0.6, name='dropout_4'))
    net.add(Dense(num_classes, activation='softmax', name='out_layer'))
    net.summary()
    return net

model = build_cnn(input_shape)
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.summary()

from keras import callbacks
filename = 'model_train_new.csv'
filepath = 'Best-weights-my_model-{epoch:03d}-{loss:.4f}-{acc:.4f}.hdf5'
csv_log = callbacks.CSVLogger(filename, separator=',', append=False)
checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [csv_log, checkpoint]
callbacks_list = [csv_log]

hist = model.fit_generator(trainAug.flow(X_train, y_train, batch_size=7), steps_per_epoch=len(X_train) // 7, validation_data=valAug.flow(X_test, y_test), validation_steps=len(X_test) // 7, epochs=50, callbacks=callbacks_list)

score = model.evaluate(X_test, y_test, verbose=0)

np.argmax(y_test[9])

test_image = X_test[0:1]
print(test_image.shape)
print(model.predict(test_image))
print(model.predict_classes(test_image))
print(y_test[0:1])
res = model.predict_classes(X_test[9:18])
plt.figure(figsize=(10, 10))
for i in range(0, 9):
    plt.subplot(330 + 1 + i)
    plt.imshow(x_test[i], cmap=plt.get_cmap('gray'))
    plt.gca().get_xaxis().set_ticks([])
    plt.gca().get_yaxis().set_ticks([])
    plt.xlabel('true = %s' % getLabel(np.argmax(y_test[i + 9])))
    plt.ylabel('prediction = %s' % getLabel(res[i]), fontsize=14)
plt.show()