How to Detect Facial Expression with Transfer Learning and PyTorch 🔥

Model Output

What is Transfer Learning?

Our Approach

  1. We make a change to the first hidden layer of the Resnet18 model to accomodate a 1 channel image instead of a 3 channel image
  2. We transform the 1 channel grayscale image to a 3 channels RGB image

Training Model with PyTorch 🔥

  1. First, we load the inputs along with its labels.
  2. Next, we make a prediction with the model. [outputs = model(inputs))]
  3. Afterward, we determine how wrong the predictions are using a pre-defined loss function. [loss = criterion(outputs, labels)]
  4. Then, we calculate the gradients by backpropagating through the entire neural network. [loss.backward()]
  5. Then, we use the calculated gradients in combination with the learning rate of the optimizer to produce new weights and biases to be used to test against a fresh set of data. [optimizer.step()]
  6. The process repeats over and over again until we reached the end of our dataset and epochs.


Model Metrics
Kaggle Top Performers
  1. The model was trained on 4 categories instead of 7. I believe this gave me an edge as some categories of the given labeled dataset were unbalanced and would have led to inaccurate predictions.
  2. It is validated upon a lower number of datapoints compared to the Kaggle test set. I split the given labeled dataset 80/20, and used the latter as a validation set which gave me 4210 datapoints to validate the model against and the Kaggle competition was tested on 7179 datapoints.
  3. I believe the hyperparameter tuning of the model played an important role into getting a high accuracy score. The decision to go with a learning rate scheduler was crucial, as it allows the model to get out of local minima issues and keep searching for gradients that would lead to a lower loss.

Using the Model





SDE2 @ Amazon

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[Review of paper] Application of Genetic Algorithm in Mammography

Research Papers to read on Doc2vec(Natural Language Processing)

Is Interpreting ML Models a Dead-End?

Snapshot Ensemble model for Multi-class Classification in Deep Learning

Open Source Ethics: Confronting Bias in Machine Learning

a complete beginner’s intro to machine learning

Research Papers based on developments in Quantum Machine Learning


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Khem Sok

Khem Sok

SDE2 @ Amazon

More from Medium

A magical day in Disney with Machine Learning — Part 11 Support Vector Machines for Classification…

Dog Breed Classifier using CNN

What is Algorithmic Bias?

Metric Learning for Steel Surface Classification — Part 1

Coils of hot-rolled stainless steel after rolling.