Spaces:
Sleeping
Sleeping
| 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'} | |
| # Example Network | |
| class facExpRec(nn.Module): | |
| def __init__(self, num_classes): | |
| super(facExpRec, self).__init__() | |
| self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1) | |
| self.relu1 = nn.ReLU() | |
| self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) | |
| self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1) | |
| self.relu2 = nn.ReLU() | |
| self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) | |
| self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1) | |
| self.relu3 = nn.ReLU() | |
| self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2) | |
| # Calculate the size of the flattened layer after convolutional and pooling layers | |
| # Assuming input image size is 48x48 | |
| self.fc1 = nn.Linear(128 * 6 * 6, 512) # 48 / 2 / 2 / 2 = 6 | |
| self.relu4 = nn.ReLU() | |
| self.dropout = nn.Dropout(0.5) | |
| self.fc2 = nn.Linear(512, num_classes) | |
| def forward(self, x): | |
| x = self.pool1(self.relu1(self.conv1(x))) | |
| x = self.pool2(self.relu2(self.conv2(x))) | |
| x = self.pool3(self.relu3(self.conv3(x))) | |
| x = x.view(-1, 128 * 6 * 6) # Flatten the output for the fully connected layers | |
| x = self.dropout(self.relu4(self.fc1(x))) | |
| x = self.fc2(x) | |
| return x | |
| # Sample Helper function | |
| def rgb2gray(image): | |
| return image.convert('L') | |
| # Enhanced Transformation function with augmentations | |
| trnscm = transforms.Compose([ | |
| rgb2gray, # Convert to grayscale | |
| transforms.Resize((48, 48)), # Resize to model input size | |
| transforms.RandomHorizontalFlip(p=0.3), # Random horizontal flip | |
| transforms.RandomRotation(10), # Slight random rotation | |
| transforms.ColorJitter( # Adjust image properties | |
| brightness=0.2, | |
| contrast=0.2 | |
| ), | |
| transforms.ToTensor(), # Convert to tensor | |
| transforms.Normalize(mean=[0.485], # Normalize the grayscale image | |
| std=[0.229]) | |
| ]) |