What I learned by doing emotion detection in Python

Things learned while working on the machine learning project.

John Paul Ada
Programmers — Developers
3 min readSep 28, 2017

--

Photo by Pietro Jeng

I was asked to give a talk on practical machine learning as part of the Tech Caravan held in Bataan on September 23–24, 2017. I decided to create an emotion detection program, like Microsoft’s Emotion API. It accepts an image with a face on it, guesses the emotion on the face, and prints it on the console. I was able to get ~75% accuracy with what I built.

In this story, I will be talking about things I learned while building the emotion detection program so I won’t go into detail about how I built it. Check out the links to the slides and the code at the bottom instead.

If someone did it before, it’s doable

When I first agreed to doing the talk, I had no idea how to build an emotion detection program, but I thought that if someone was able to do it, then I probably can. It may not be as good, but it’s doable, and that’s exactly what I did. I got an accuracy of around 75%, which is pretty okay.

Find good features

I think I could have tried to find better features for this problem aside from the line lengths and angles of the eyes, eyebrows, and lips, but I was constrained by time and I didn’t have time to extract better features.

It’s all about the data

Your machine learning algorithms are pointless if your data sucks. You should get a lot of data (volume) that are different from each other (variety). This way, the model doesn’t overfit and can predict on data it has never seen before.

Data should also be processed properly. Removing irrelevant features (dimensionality reduction) and normalizing data boosted the accuracy of the model built.

Getting the raw data, extracting the features, cleaning, and processing the resulting data is all the biggest and hardest part of the process of building this program. It’s all about the data.

Pick the right algorithm

There are cheatsheets out there that can help you pick the algorithm you need, like these ones:

If those algorithms don’t quite work out, try adding more volume and variety to the data. Also, you can try the other algorithms related to that algorithm. For example, I initially used Random Forests and AdaBoost as the ensemble learning algorithms, but found out that Extremely Randomized Trees performed better for this problem.

Honestly though, these algorithms are suboptimal for this problem, given the 75% accuracy. I could have used Convolutional Neural Networks, which are a better fit for tasks that require learning from images. I don’t think I had enough processing power so I didn’t use it.

Functional Programming works for Machine Learning

Some would argue that it’s a given that it would work because I’m working on data, but I had to see it for myself. I built a Maybe in Python for this reason. It’s so fulfilling to read Python code — which is already very readable — made even more readable. It’s also easy to see where there is a problem because it looks like a pipeline now. Making it work in parallel would be even better but it needed some more restructuring because I didn’t plan for it. But because it’s functional, it is very possible to make things work in parallel with less effort. With this experience, I’m convinced that Functional Programming works with Machine Learning and I will continue to do just that in the future.

Slides and Code

Code speaks louder than words.

If you want to know how I built it, check out my slides here:

http://jpa-emotion-detection-slides.surge.sh/#slide=1

and you can look at the code here:

If you liked this story, hit the clap button as much as you like!
If you have any comments, write them below!
Thank you so much!

--

--