space1 / app /Hackathon_setup /exp_recognition_model.py
suvradip2000's picture
fix
b5e5bf0
Raw
History Blame Contribute Delete
2.52 kB
import torch
import torchvision
import torch.nn as nn
from torchvision import transforms
import torch.nn.functional as F
## 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 Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)
# Define the Fully connected layers
# The output of the second convolution layer will be input to the first fully connected layer
self.fc1 = nn.Linear(128*10*10, 256)
# 256 input features, 128 output features
self.fc2 = nn.Linear(256, 128)
# 128 input features, 64 output features
self.fc3 = nn.Linear(128, 64)
# 64 input features, 7 output features for our 7 defined classes
self.fc4 = nn.Linear(64, 7)
# Max pooling
self.pool = nn.MaxPool2d(kernel_size=2) # Max pooling layer with filter size 2x2
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
# Flatten the image
x = x.view(-1, 128*10*10) # Output shape of convolutional layer is 16*5*5
# Linear layers with RELU activation
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.fc4(x)
x = F.log_softmax(x, dim=1)
return x
# Sample Helper function
def rgb2gray(image):
return image.convert('L')
# Sample Transformation function
#YOUR CODE HERE for changing the Transformation values.
trnscm = transforms.Compose([transforms.Resize((224,224)), transforms.ToTensor()])