Happy-Sad-Image-Classifier

Overview

For this final project, I'll work on an image classification model for determining happy and sad images. Similar to the cancer classification, images can be classified with 0 and 1 for happy and sad. I will use the Keras Sequential model with convolutional 2D layers. A model like this could be used for sentiment analysis or determining emotions in real-time. This will be a fairly simplified model with only binary output but could be expanded for other emotions such as angry, scared, surprised, etc.

Most of the images I retrieved from Google searches and some were from a Kaggle dataset for emotions.

Observing the Data

image

image

After preprocessing the data to rescale, the images are classified as 1 for Sad and 0 for Happy and each image is 256 x 256, lets look at 12 rescaled and classified images

0 = Happy 1 = Sad

image

Model Architecture

For this first Sequential model, I'll use 3 Convolutional 2D layers each with (3x3) filter size and stride of 1. The input shape is (256, 256, 3). 'relu' activation converts negative values from output to 0 and positive stays the same. MaxPooling2D layer takes max value after relu activation and returns that value to condense the info.

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type)                     Output Shape                  Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D)                 │ (None, 256, 256, 16)   │           448 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d (MaxPooling2D)    │ (None, 128, 128, 16)   │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_1 (Conv2D)               │ (None, 126, 126, 32)   │         4,640 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_1 (MaxPooling2D)  │ (None, 63, 63, 32)     │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_2 (Conv2D)               │ (None, 61, 61, 16)     │         4,624 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_2 (MaxPooling2D)  │ (None, 30, 30, 16)     │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ flatten (Flatten)               │ (None, 14400)          │             0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense (Dense)                   │ (None, 256)            │     3,686,656 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_1 (Dense)                 │ (None, 1)              │           257 │
└─────────────────────────────────┴────────────────────────┴───────────────┘

Plot Performance

image

image

Precision: 1.0, Recall: 1.0, Acc: 1.0

Initial Results from Training

As we see above, the model performed very well with 100% accuracy. This could be from some possible duplicates in the data and only 256 images isn't much for a true model.

Testing the model

Predicted class is sad image

Predicted is happy image

Predicted values vs True labels on Test batch

image

Conclusion

With the initial model being trained on 192 images with 32 used for validation data and ran on only 20 epochs, the model performed extremely well. The validation accuracy hit 1.0 after only 10 epochs. With the test images above, the model also classified them all correctly. I'm very happy with the results and a model like this could be used for emotion detection in images or video frames. Expanding to other facial expressions and emotions would make it even more useful.