16th May 2019

Machine Learning Can Predict Your Emotions!

Machine Learning

Written By, Ayaz Saiyed


Hey Techies, we all learned a few aspects of machine learning in our previous blog but did you know that a machine can predict your emotions too?


And on a prediction basis, it overlays funny emoji of that emotion/mood on your face!
Do you think that is possible with Machine Learning? 😲

machine learning_image2

Yes, it is possible. So hold on and let’s see how!
So first, let’s clear out on a few things that how machine learning works if you are new to machine learning. I recommend you to refer my previous blog on machine learning concepts to get a clear understanding.

Overview of how ML works

In Machine Learning the dataset is divided into 2 parts.

  1. Training dataset
  2. Test dataset


Size of the training dataset is always higher as we have to first train the model on the training dataset for predicting true and accurate results.

Now you guys might be wondering, how this exactly works?

machine learning_image4

So let’s begin, but again before we move ahead, I am assuming that you have the basic knowledge of Machine Learning.
Don’t have? Don’t worry something is there for you here https://www.yudiz.com/lets-dive-in-the-world-of-machine-learning/

How Emotion Detector Works?

Basically, as we know in machine learning, firstly we have to train the model, so in the same way here we have to provide the user with some images with different moods as the training dataset to train the model.
For ex:
python face_capture.py –emotion_name smile –number_of_images 200

This command will open the camera, and will capture the 200 images of the faces of the user, and will label it as smile dataset.

In the same way for different moods,
python face_capture.py –emotion_name sad –number_of_images 200

So by doing the above process, you have given the machine the dataset of images with different moods from which it can differentiate the emotions.

machine learning_image5

As you can see it starts detecting the face and upon detection, it captures the image and saves it in the folder of defined emotion name. (This process is done through OpenCV library in python, that will detect the face from the frame and will capture the image of the face) After capturing the images of different moods it’s time to create a different dataset on the basis of results obtained.

machine learning_image6

So let’s run the script to create a dataset, python dataset_creator.py
The dataset_creator.py includes some functions to reduce the size and change the dimensions of the stored images, to apply the train_test_splitter (dividing the train and test dataset), and then generating the dataset with labels.

machine learning_image7

Now we have the train and test dataset, which will be saved in a particular folder. So now it’s time to train our model on the dataset to make accurate predictions.

machine learning_image8

So, let’s start training the model. The training process includes some activation functions like,

  • Linear function
  • Sigmoid
  • Tanh
  • ReLU
  • Leaky ReLU, etc.

Basically, activation function decides whether the neuron should be activated or not on the basis of the weights and bias. The Neural Network without the activation function is just a linear regression model.

Now let’s run the script to train the dataset, python trainer.py In an ideal case, the use of the sigmoid activation function would be efficient but here in the trainer.py, I have used Relu and Softmax function as they support multiple classes.

How A Model gets trained?

The process of model training is similar to how we get trained for our exams. Everything in the training depends on Neurons!
Neurons?? 😒

machine learning_image9

Don’t worry, we are softmax, etc not moving into these neurons.

But just for the sake of Understanding, keep in mind that multiple neurons create one Neural Network. Soon I will be writing a blog on neural networks.

In the process of training, each neuron has its own weight(w) and bias(b), and all the neurons produce some results. The main goal is to find the ideal W and B values for all neurons or W and B vectors for a layer. And In the final run, given a test data vector(set) X, we calculate the w*X+b value for subsequent layers and get a final predictive result.

Every time the weights and bias get updated in the training process through the forward and backward propagation until the model is trained perfectly.

machine learning_image10

Such W and B, are then fitted into functions like sigmoid, softmax, etc to get the predicted result.

Yayyy!! Now we have a trained model that can predict human emotions.

machine learning_image11

Now, let’s run the trained model

machine learning_image12

Here we go – Emoji Predictor

machine learning_image13


So, now I am pretty sure that you understood the importance and the usage of machine learning through this blog.
Please share it with your friends to alert them for the new era of machine learning and its power, and share your reviews as it will not burn your calories 😄

Written By,

Python Developer at Yudiz Solutions Pvt. Ltd