import torch import torchvision import torch.nn as nn from torchvision import transforms ## Add more imports if required #################################################################################################################### # Define your model and transform and all necessary helper functions here # # They will be imported to the exp_recognition.py file # #################################################################################################################### # Definition of classes as dictionary classes = {0: 'ANGER', 1: 'DISGUST', 2: 'FEAR', 3: 'HAPPINESS', 4: 'NEUTRAL', 5: 'SADNESS', 6: 'SURPRISE'} # Grayscale-compatible Expression CNN from notebook # CNN model for Expression Recognition - Clean architecture with BatchNorm class ExpressionCNN(nn.Module): def __init__(self, num_classes=7): super(ExpressionCNN, self).__init__() # Conv Block 1 self.conv1 = nn.Conv2d(1, 32, kernel_size=3, padding=1) self.bn1 = nn.BatchNorm2d(32) self.relu1 = nn.ReLU() self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) # Conv Block 2 self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1) self.bn2 = nn.BatchNorm2d(64) self.relu2 = nn.ReLU() self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) # Conv Block 3 self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1) self.bn3 = nn.BatchNorm2d(128) self.relu3 = nn.ReLU() self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) # Fully Connected Layers self.fc1 = nn.Linear(128 * 6 * 6, 512) self.bn4 = nn.BatchNorm1d(512) self.relu4 = nn.ReLU() self.fc2 = nn.Linear(512, 256) self.bn5 = nn.BatchNorm1d(256) self.relu5 = nn.ReLU() self.fc3 = nn.Linear(256, num_classes) def forward(self, x): # Conv Block 1 x = self.conv1(x) x = self.bn1(x) x = self.relu1(x) x = self.pool1(x) # Conv Block 2 x = self.conv2(x) x = self.bn2(x) x = self.relu2(x) x = self.pool2(x) # Conv Block 3 x = self.conv3(x) x = self.bn3(x) x = self.relu3(x) x = self.pool3(x) # Flatten x = x.view(x.size(0), -1) # FC layers x = self.fc1(x) x = self.bn4(x) x = self.relu4(x) x = self.fc2(x) x = self.bn5(x) x = self.relu5(x) x = self.fc3(x) return x # Backward compatibility alias facExpRec = ExpressionCNN # Sample Helper function def rgb2gray(image): return image.convert('L') # Transformation function - normalized grayscale trnscm = transforms.Compose([ rgb2gray, # Convert to grayscale transforms.Resize((48, 48)), # Resize to model input size transforms.ToTensor(), # Convert to tensor transforms.Normalize(mean=[0.5], std=[0.5]) # Normalize grayscale image ])