group14_hackathon / app /Hackathon_setup /face_recognition_model.py
jeevana's picture
new MLP classifier
2c6e2c1
import math
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
# Add more imports if required
# Sample Transformation function
# YOUR CODE HERE for changing the Transformation values.
trnscm = transforms.Compose([transforms.Resize((100,100)), transforms.ToTensor()])
##Example Network
class Siamese(torch.nn.Module):
def __init__(self):
super(Siamese, self).__init__()
#YOUR CODE HERE
self.cnn1 = nn.Sequential(
nn.ReflectionPad2d(1), #Pads the input tensor using the reflection of the input boundary, it similar to the padding.
nn.Conv2d(1, 4, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(4),
nn.ReflectionPad2d(1),
nn.Conv2d(4, 8, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
nn.ReflectionPad2d(1),
nn.Conv2d(8, 8, kernel_size=3),
nn.ReLU(inplace=True),
nn.BatchNorm2d(8),
)
self.fc1 = nn.Sequential(
nn.Linear(8*100*100, 500),
nn.ReLU(inplace=True),
nn.Linear(500, 500),
nn.ReLU(inplace=True),
nn.Linear(500, 5))
# forward_once is for one image. This can be used while classifying the face images
def forward_once(self, x):
output = self.cnn1(x)
output = output.view(output.size()[0], -1)
output = self.fc1(output)
return output
def forward(self, input1, input2):
output1 = self.forward_once(input1)
output2 = self.forward_once(input2)
return output1, output2
# def forward(self, x):
# pass # remove 'pass' once you have written your code
#YOUR CODE HERE
##########################################################################################################
## Sample classification network (Specify if you are using a pytorch classifier during the training) ##
## classifier = nn.Sequential(nn.Linear(64, 64), nn.BatchNorm1d(64), nn.ReLU(), nn.Linear...) ##
##########################################################################################################
# YOUR CODE HERE for pytorch classifier
classes = ['person1','person2','person6','person7']
num_of_classes = len(classes)
classifier = nn.Sequential(nn.Linear(256, 64), nn.BatchNorm1d(64), nn.ReLU(),
nn.Linear(64, 32), nn.BatchNorm1d(32), nn.ReLU(),
nn.Linear(32, num_of_classes))
#classifier = classifier.to(device) -error thrown
print(classifier)
class MLPClassifier(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(MLPClassifier, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu1 = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, 2048)
self.relu2 = nn.ReLU()
self.fc3 = nn.Linear(2048, 1024)
self.relu3 = nn.ReLU()
self.fc4 = nn.Linear(1024, num_classes)
def forward(self, x):
x = self.fc1(x)
x = self.relu1(x)
x = self.fc2(x)
x = self.relu2(x)
x = self.fc3(x)
x = self.relu2(x)
x = self.fc4(x)
return x