## TODO: define the convolutional neural network architecture

import torch
import torch.nn as nn
import torch.nn.functional as F
# can use the below import should you choose to initialize the weights of your Net
import torch.nn.init as I


class Net(nn.Module):
    #Using NameishNet Architecture
    
    def __init__(self):
        super(Net, self).__init__()
         
        #input image: 1 x 224 x 224 (grayscale squared image)
        
        
        ## TODO: Define all the layers of this CNN, the only requirements are:
        ## 1. This network takes in a square (same width and height), grayscale image as input
        ## 2. It ends with a linear layer that represents the keypoints
        ## it's suggested that you make this last layer output 136 values, 2 for each of the 68 keypoint (x, y) pairs
        
       
        # 1 input image channel (grayscale), 32 output channels/feature maps, 4x4 square convolution kernel
        self.conv1 = nn.Conv2d(1, 32, 4)
        self.bn1 = nn.BatchNorm2d(32)
        self.pool1 = nn.MaxPool2d(2,2)
        self.dropout1 = nn.Dropout(p=0.1)
        
        self.conv2 = nn.Conv2d(32,64,3)
        self.bn2 = nn.BatchNorm2d(64)
        self.pool2 = nn.MaxPool2d(2,2)
        self.dropout2 = nn.Dropout(p=0.2)
        
        self.conv3 = nn.Conv2d(64,128,2)
        self.bn3 = nn.BatchNorm2d(128)
        self.pool3 = nn.MaxPool2d(2,2)
        self.dropout3 = nn.Dropout(p=0.3)
        
        self.conv4 = nn.Conv2d(128,256,1)
        self.bn4 = nn.BatchNorm2d(256)
        self.pool4 = nn.MaxPool2d(2,2)
        self.dropout4 = nn.Dropout(p=0.4)
        
        self.fc1 = nn.Linear(43264, 1000)
        self.fc2 = nn.Linear(1000,1000)
        self.fc3 = nn.Linear(1000, 136)
        
        self.bn5 = nn.BatchNorm1d(1000)
        self.bn6 = nn.BatchNorm1d(1000)
        self.dropout5 = nn.Dropout(p=0.5)
        self.dropout6 = nn.Dropout(p=0.6)
        
        I.xavier_uniform(self.fc1.weight.data)
        I.xavier_uniform(self.fc2.weight.data)
        I.xavier_uniform(self.fc3.weight.data)
    
        
    def forward(self, x):
        ## TODO: Define the feedforward behavior of this model
        ## x is the input image and, as an example, here you may choose to include a pool/conv step:
        ## x = self.pool(F.relu(self.conv1(x)))
        
        
        # a modified x, having gone through all the layers of your model, should be returned
        
        x = self.dropout1(self.pool1(F.elu(self.conv1(x))))
        x = self.dropout2(self.pool2(F.elu(self.conv2(x))))
        x = self.dropout3(self.pool3(F.elu(self.conv3(x))))
        x = self.dropout4(self.pool4(F.elu(self.conv4(x))))
        
        #Flatten the image
        x = x.view(x.size(0), -1)
        
        x = self.dropout5(F.elu(self.bn5(self.fc1(x))))
        x = self.dropout6(F.relu(self.bn6(self.fc2(x))))
        x = self.fc3(x)
        
        return x
