Week 4 content of face emoji
Hello AI Club members! Welcome to Week 4 of our Face-Emoji project. So far, we've built a solid foundation by detecting faces and facial landmarks. This week, we're taking a step back from facial processing to learn something crucial for our next steps: PyTorch!
Understanding the basics of PyTorch
Building a simple neural network
Training a model on synthetic data
Setting the foundation for emotion detection
File you will need: week4_code.py
Before we start coding, we need to install PyTorch:
Open Anaconda Powershell Prompt
Activate your environment: conda activate face-emoji
Install PyTorch: pip install torch matplotlib
Open Terminal
Install PyTorch using pip: python3.12 -m pip install torch matplotlib
A neural network is a series of mathematical operations inspired by the human brain. At its simplest:
Input Layer: Takes in our data (like facial landmark coordinates)
Hidden Layers: Process the data through weights and activation functions
Output Layer: Produces the final prediction (like emotion classifications)
Each connection between neurons has a "weight" that gets adjusted during training to make better predictions.
Tensors are the fundamental data structure in PyTorch - similar to arrays or matrices. They can have different dimensions:
1D tensor: A vector (like a list of numbers)
2D tensor: A matrix (like a table of numbers)
3D+ tensor: Higher dimensional data (like images or video)
# Creating tensors
import torch
# From Python list
x = torch.tensor([1, 2, 3, 4])
# All zeros
zeros = torch.zeros(3, 4) # 3x4 matrix of zeros
# Random values
random = torch.rand(2, 2) # 2x2 matrix of random values
PyTorch makes it easy to build neural networks using nn.Module:
import torch.nn as nn
# Linear layer (fully connected)
linear = nn.Linear(in_features=10, out_features=5)
# Activation functions
relu = nn.ReLU() sigmoid = nn.Sigmoid()
Training a neural network involves:
Forward Pass: Data flows through the network to make predictions
Loss Calculation: Comparing predictions to actual values
Backward Pass: Computing gradients to see how to adjust weights
Optimization: Updating weights to improve predictions
The provided code includes:
Data Generation: Creates a simple synthetic dataset where points are classified based on their position
Model Definition: A simple neural network with one hidden layer
Training Function: A skeleton for you to implement the training loop
Visualization: Code to view the data and decision boundary
class SimpleClassifier(nn.Module):
def init(self, input_size=2, hidden_size=8, output_size=1):
super(SimpleClassifier, self).__init__()
# Define the layers
self.layer1 = nn.Linear(input_size, hidden_size)
self.activation = nn.ReLU()
self.layer2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.layer1(x)
x = self.activation(x)
x = self.layer2(x)
return x
This neural network:
Takes 2 input features
Has 8 neurons in the hidden layer with ReLU activation
Outputs a single value (for binary classification)
The forward method defines how data flows through the network.
We've provided the model architecture and data, but you need to implement the training function. You should ONLY update the train function. Here's what you'll need to do:
Initialize the Loss Function: For binary classification, use nn.BCEWithLogitsLoss()
Initialize the Optimizer: Use torch.optim.SGD or torch.optim.Adam
Forward Pass: Feed the data through the model
Compute Loss: Calculate how far off the predictions are
Backward Pass: Compute gradients with loss.backward()
Optimization Step: Update weights with optimizer.step()
The code has specific TODO sections for each of these parts.
For binary classification, we use Binary Cross Entropy loss:
loss_fn = nn.BCEWithLogitsLoss()
Adam is a good default optimizer:
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
# Forward pass
outputs = model(X)
# Compute loss loss = loss_fn(outputs, y)
# Backward pass and optimization
optimizer.zero_grad()
# Clear previous gradients
loss.backward()
# Compute gradients
optimizer.step()
# Update weights
Our synthetic dataset is deliberately simple:
Each point has 2 features (x and y coordinates)
Points are classified as 0 or 1 based on whether x1 + x2 > 0
We add some noise to make it more realistic
This simple dataset lets you focus on understanding PyTorch without the complexity of facial landmarks.
To understand PyTorch better, check out:
PyTorch Official Documentation: https://pytorch.org/docs/stable/index.html
PyTorch Tutorials: https://pytorch.org/tutorials/beginner/basics/intro.html
Neural Network Visualization: https://playground.tensorflow.org/
[Recommended watching]: https://www.youtube.com/watch?v=tHL5STNJKag&pp=ygUYY3JlYXRpbmcgbm4gd2l0aCBweXRvcmNo
Next week, we'll apply what we've learned to facial landmarks. Instead of classifying points in 2D space, we'll use the landmarks we extracted from faces to classify emotions!
Happy coding! ðŸ§