<jupyter_start><jupyter_text>Convolutional Neural Network  Importing the libraries<jupyter_code>import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
tf.__version__<jupyter_output><empty_output><jupyter_text>Part 1 - Data Preprocessing  Preprocessing the Training set<jupyter_code>train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        'emotion/train',
        target_size=(64, 64),
        batch_size=128,
        class_mode='categorical')<jupyter_output>Found 28709 images belonging to 7 classes.<jupyter_text>Preprocessing the Test set<jupyter_code>test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        'emotion/test',
        target_size=(64, 64),
        batch_size=128,
        class_mode='categorical')<jupyter_output>Found 7178 images belonging to 7 classes.<jupyter_text>Part 2 - Building the CNN  Initialising the CNN<jupyter_code>cnn = tf.keras.models.Sequential()<jupyter_output><empty_output><jupyter_text>Step 1 - Convolution<jupyter_code>cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3,activation='relu', input_shape=[64,64,3]))<jupyter_output><empty_output><jupyter_text>Step 2 - Pooling<jupyter_code>cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))<jupyter_output><empty_output><jupyter_text>Adding a second convolutional layer<jupyter_code>cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3,activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))<jupyter_output><empty_output><jupyter_text>Step 3 - Flattening<jupyter_code>cnn.add(tf.keras.layers.Flatten())<jupyter_output><empty_output><jupyter_text>Step 4 - Full Connection<jupyter_code>cnn.add(tf.keras.layers.Dense(units=256, activation='relu'))<jupyter_output><empty_output><jupyter_text>Step 5 - Output Layer<jupyter_code>cnn.add(tf.keras.layers.Dense(units=7, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Part 3 - Training the CNN  Compiling the CNN<jupyter_code>cnn.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Training the CNN on the Training set and evaluating it on the Test set<jupyter_code>cnn.fit_generator(training_set, validation_data = test_set, epochs = 40, steps_per_epoch = 28709//16, validation_steps = 300)<jupyter_output>Epoch 1/40
1794/1794 [==============================] - 354s 197ms/step - loss: 1.4225 - acc: 0.4520 - val_loss: 1.2823 - val_acc: 0.5041
Epoch 2/40
1794/1794 [==============================] - 303s 169ms/step - loss: 1.1635 - acc: 0.5600 - val_loss: 1.2215 - val_acc: 0.5441
Epoch 3/40
1794/1794 [==============================] - 308s 172ms/step - loss: 1.0171 - acc: 0.6199 - val_loss: 1.2553 - val_acc: 0.5455
Epoch 4/40
1794/1794 [==============================] - 292s 163ms/step - loss: 0.9065 - acc: 0.6646 - val_loss: 1.3074 - val_acc: 0.5456
Epoch 5/40
1794/1794 [==============================] - 268s 150ms/step - loss: 0.8169 - acc: 0.7000 - val_loss: 1.3568 - val_acc: 0.5600
Epoch 6/40
1794/1794 [==============================] - 261s 146ms/step - loss: 0.7379 - acc: 0.7305 - val_loss: 1.4225 - val_acc: 0.5647
Epoch 7/40
1794/1794 [==============================] - 259s 144ms/step - loss: 0.6730 - acc: 0.7545 - val_loss: 1.5042 - val_acc: 0.5594
Epoch 8/40
1794/1794 [============[...]<jupyter_text>Part 4 - Making a single prediction<jupyter_code>import numpy as np
from keras.preprocessing import image
test_image = image.load_img("Faces/single_prediction/happy.jpg", target_size = (64,64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0]==1:
    prediction='angry'
elif result[0][1]==1:
    prediction='disgust'
elif result[0][2]==1:
    prediction='fear'
elif result[0][3]==1:
    prediction='happy'
elif result[0][4]==1:
    prediction='neutral'
elif result[0][5]==1:
    prediction='sad'
else:
    prediction='suprise'
print(prediction)
# run this after 40 epochs are done
cnn.save('best_model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17746478873239438, 'fraction_numerical': 0.08849765258215962, 'mean_word_length': 5.806709265175719, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '363511', 'license': 'MIT', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1576, 'n_words': 269, 'path': 'Facial Emotion Recognition/main.ipynb', 'repo_name': 'The-Technocrats-NSUT/Emotion-Recognition', 'size': '4260'}