## 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):

    def __init__(self):
        super(Net, self).__init__()
        
        ## 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
        
        # As an example, you've been given a convolutional layer, which you may (but don't have to) change:
        # 1 input image channel (grayscale), 32 output channels/feature maps, 5x5 square convolution kernel
        self.conv1 = nn.Conv2d(1,32,5) # (32,92,92) output tensor # (W-F)/S + 1 = (96-5)/1 + 1 = 92
        # the output Tensor for one image, will have the dimensions: (10, 110, 110)
        # after one pool layer, this becomes (10, 55, 55)
        # maxpool layer
        # pool with kernel_size=2, stride=2
        self.pool = nn.MaxPool2d(2, 2) #this becomes (10, 55, 55)

        # second conv layer: 10 inputs, 20 outputs, 3x3 conv
        ## output size = (W-F)/S +1 = (55-3)/1+1 = 26
        # the output tensor will have dimensions: (20, 26, 26)
        # after another pool layer this becomes (20, 13, 13); 
        self.conv2 = nn.Conv2d(32,64,5) # (64,44,44) output tensor # (W-F)/S + 1 = (46-5)/1 + 1 = 42
        
        # third conv layer: 20 inputs, 40 outputs, 3x3 conv
        ## output size = (W-F)/S +1 = (13-3)/1+1 = 5
        # the output tensor will have dimensions: (40, 5, 5)
        # after another pool layer this becomes (40, 2, 2); 
       # self.conv3 = nn.Conv2d(20, 40, 3)
        
        
        self.fc1 = nn.Linear(64*21*21, 1000)
        self.fc1 = nn.Linear(1000, 500)
        self.fc1 = nn.Linear(500, 136)
        
        
        # dropout with p=0.4
        self.drop1 = nn.Dropout(p=0.4)
        
        # finally, create 10 output channels (for the 10 classes)
       # self.fc2 = nn.Linear(422, 136)       
        ## Note that among the layers to add, consider including:
        # maxpooling layers, multiple conv layers, fully-connected layers, and other layers (such as dropout or batch normalization) to avoid overfitting
        

        
    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)))
       # x.view(x.size(0), -1)
      #  x = self.pool(F.relu(self.conv1(x)))
      #  x = self.pool(F.relu(self.conv2(x)))
        
        # Flatten and continue with dense layers
       # x = x.view(x.size(0), -1)
       # x = F.relu(self.fc1(x))
       # x = self.fc1_drop(x)
       # x = F.relu(self.fc2(x))
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        
        x = F.relu(self.conv2(x))
        x = self.pool(x)
        x = self.drop1(x)
        
        # Flatten and continue with dense layers
        x = x.view(x.size(0), -1)
        x = F.selu(self.fc1(x))
        x = self.drop1(x)
        x = F.selu(self.fc2(x))
        x = self.drop1(x)
        x = self.fc3(x)
        #x = self.fc1_drop(x)
      
        
 
        # a modified x, having gone through all the layers of your model, should be returned
        return x
