Learn practical skills, build real-world projects, and advance your career
import os
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
import torchvision.transforms as tt
from torchvision.utils import make_grid
import matplotlib.pyplot as plt
%matplotlib inline

Preparing the Data

# Looking into the directory
data_dir = './dataset'
classes_train = os.listdir(data_dir + "/train")
classes_valid = os.listdir(data_dir + "/validation")
print(f'Train Classes - {classes_train}')
print(f'Validation Classes - {classes_valid}')
['emotion_dataset.zip', 'fer2013.bib', 'fer2013.csv', 'README', 'train', 'validation'] Train Classes - ['Angry', 'Happy', 'Neutral', 'Sad', 'Surprise'] Validation Classes - ['Angry', 'Happy', 'Neutral', 'Sad', 'Surprise']
# Data transforms (Gray Scaling & data augmentation)
train_tfms = tt.Compose([tt.Grayscale(num_output_channels=1),

valid_tfms = tt.Compose([tt.Grayscale(num_output_channels=1), tt.ToTensor()])
# Emotion Detection datasets
train_ds = ImageFolder(data_dir + '/train', train_tfms)
valid_ds = ImageFolder(data_dir + '/validation', valid_tfms)