text_plain_output_1.png
from tensorflow.keras import layers

import cv2

import numpy as np

import os

import tensorflow as tf

subdir = ['angry', 'notAngry']
target = {'angry': 0, 'notAngry': 1}
dataset = '../input/fer2013new2class/FER2013NEW2CLASS/train/'
X = []
y = []
for emotions in subdir:
    for img_names in os.listdir(dataset + '/' + emotions):
        load_images = cv2.imread(dataset + '/' + emotions + '/' + img_names)
        X.append(load_images)
        y.append(target[emotions])

X, y = (np.array(X), np.array(y))
(X.shape, y.shape)

y = tf.keras.utils.to_categorical(y, num_classes=2)
y.shape

class SEBlock(layers.Layer):
    def __init__(self, channels, reduction=16):
        super(SEBlock, self).__init__()
        self.avg_pool = layers.GlobalAveragePooling2D()
        self.fc1 = layers.Dense(channels // reduction, activation='relu')
        self.fc2 = layers.Dense(channels, activation='sigmoid')
    def call(self, input_tensor):
        bs, c = (input_tensor.shape[0], input_tensor.shape[-1])
        y = self.avg_pool(input_tensor)
        y = self.fc1(y)
        y = self.fc2(y)
        y = tf.reshape(y, [bs, 1, 1, c])
        return input_tensor * y

def create_model():
    model = tf.keras.Sequential([layers.Conv2D(32, (3, 3), activation='relu', input_shape=(48, 48, 3)), SEBlock(32), layers.BatchNormalization(), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), SEBlock(64), layers.BatchNormalization(), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dense(2, activation='sigmoid')])
    return model

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