Views
No views yet
CNN-based facial expression classifier trained to recognize 7 emotion categories from face images with a clean, reproducible pipeline.
| Property | Details |
|---|---|
| 🏗️ Architecture | CNN (custom) |
| 🎯 Task | Image Classification |
| 😀 Classes | 7 emotions |
| ⚙️ Framework | PyTorch |
| 📐 Input size | 48 × 48 px (grayscale) |
| 📜 License | MIT |
| # | Emotion |
|---|---|
| 0 | 😠 Angry |
| 1 | 🤢 Disgust |
| 2 | 😨 Fear |
| 3 | 😄 Happy |
| 4 | 😐 Neutral |
| 5 | 😢 Sad |
| 6 | 😲 Surprise |
1import torch
2import torch.nn.functional as F
3from torchvision import transforms
4from PIL import Image
5
6# Load model
7model = torch.jit.load("model.pt", map_location="cpu")
8model.eval()
9
10# Preprocessing
11transform = transforms.Compose([
12 transforms.Grayscale(),
13 transforms.Resize((48, 48)),
14 transforms.ToTensor(),
15 transforms.Normalize([0.5], [0.5]),
16])
17
18EMOTIONS = ["Angry", "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise"]
19
20# Inference
21image = Image.open("face.jpg")
22tensor = transform(image).unsqueeze(0)
23
24with torch.no_grad():
25 probs = F.softmax(model(tensor), dim=1)[0]
26
27predicted = EMOTIONS[probs.argmax()]
28confidence = probs.max().item()
29
30print(f"Prediction: {predicted} ({confidence:.0%})")