import torch
import torch.nn as nn
import copy

from torch.nn.functional import normalize

try:
    from torch.hub import load_state_dict_from_url
except ImportError:
    from torch.utils.model_zoo import load_url as load_state_dict_from_url

__all__ = ['AlexNet', 'alexnet']


model_urls = {
    'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
}


class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

class MiniAlexNet(nn.Module):

    def __init__(self, num_classes=1000, n_channels=3, mi=None, noise=1e-4):
        super(MiniAlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(n_channels, 200, kernel_size=5, stride=2, padding=1),
            nn.BatchNorm2d(200),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=1),

            nn.Conv2d(200, 200, kernel_size=5, stride=2, padding=1),
            nn.BatchNorm2d(200),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=1),

        )
        # self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Linear(200 * 3 * 3, 384),
            nn.BatchNorm1d(384),
            nn.ReLU(inplace=True),

            nn.Linear(384, 192),
            nn.BatchNorm1d(192),
            nn.ReLU(inplace=True),
            
            nn.Linear(192, num_classes),
        )

        self.mi = mi
        self.noise = noise

    def forward(self, x, get_res, get_activations = False):
        mi = {}
        j_entropy = {}
        gram_m = {'input':{}, 'layer':{}}
        results = []
        cov = None
        activations = []
        for i, elem in enumerate(self.features):
            x = elem(x)
            if isinstance(elem, nn.Conv2d):
                if get_res['gram'] and self.mi:
                    gram_m['layer'][i] = self.mi.get_gram_matrix(elem.weight.clone().unsqueeze(0)).mean(dim=0).cpu() #entropy

            if isinstance(elem, nn.ReLU):
                x = x + torch.rand_like(x)*self.noise
                if self.mi:
                    if get_res['mi']:
                        mi[i] = self.mi.tc(torch.flatten(x.clone().permute(1,0,2,3),1).permute(1,0).cpu().numpy())

                    if get_res['gram']:
                        gram_m['input'][i] = self.mi.get_gram_matrix(x.clone()).mean(dim=0).cpu() #entropy
                if get_activations:
                    activations.append(x)
        
        # print(x.size())
        # x = self.avgpool(x)
        # print(x.size())
        x = torch.flatten(x, 1)
        # print(x.size())

        for j, elem in enumerate(self.classifier):
            x = elem(x)
            if j == 6 and get_activations:
                activations.append(x)
            if isinstance(elem, nn.Linear):
                if get_res['gram'] and self.mi:
                    gram_m['layer'][j+i] = self.mi.get_gram_matrix(elem.weight.clone().unsqueeze(0)).mean(dim=0).cpu() #entropy

            if isinstance(elem, nn.ReLU):
                x = x + torch.rand_like(x)*self.noise
                if get_activations:
                    activations.append(x)
                if self.mi:
                    if get_res['mi']:
                        mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())

                    if get_res['gram']:
                        gram_m['input'][j+i] = self.mi.get_gram_matrix(x.clone()).mean(dim=0).cpu()
                
        if get_res['mi'] and self.mi:
            mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())
        
        if get_activations:
            return x, mi, gram_m, activations
        else:
            
            return x, mi, gram_m#, results

def alexnet(pretrained=False, progress=True, **kwargs):
    r"""AlexNet model architecture from the
    `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    """
    model = AlexNet(**kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls['alexnet'],
                                              progress=progress)
        model.load_state_dict(state_dict)
    return model