# Copyright 2017 Division of Medical Image Computing, German Cancer Research Center (DKFZ)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import glob
from os.path import join
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adamax
import torch.optim.lr_scheduler as lr_scheduler
from torch.autograd import Variable

from tractseg.libs.PytorchUtils import PytorchUtils
from tractseg.libs.ExpUtils import ExpUtils
from tractseg.models.BaseModel import BaseModel
from tractseg.libs.MetricUtils import MetricUtils
from tractseg.libs.PytorchUtils import conv2d
from tractseg.libs.PytorchUtils import deconv2d


class UNet_Pytorch_Regression(torch.nn.Module):
    def __init__(self, n_input_channels=3, n_classes=7, n_filt=64, batchnorm=False, dropout=False):
        super(UNet_Pytorch_Regression, self).__init__()
        self.in_channel = n_input_channels
        self.n_classes = n_classes

        self.contr_1_1 = conv2d(n_input_channels, n_filt)
        self.contr_1_2 = conv2d(n_filt, n_filt)
        self.pool_1 = nn.MaxPool2d((2, 2))

        self.contr_2_1 = conv2d(n_filt, n_filt * 2)
        self.contr_2_2 = conv2d(n_filt * 2, n_filt * 2)
        self.pool_2 = nn.MaxPool2d((2, 2))

        self.contr_3_1 = conv2d(n_filt * 2, n_filt * 4)
        self.contr_3_2 = conv2d(n_filt * 4, n_filt * 4)
        self.pool_3 = nn.MaxPool2d((2, 2))

        self.contr_4_1 = conv2d(n_filt * 4, n_filt * 8)
        self.contr_4_2 = conv2d(n_filt * 8, n_filt * 8)
        self.pool_4 = nn.MaxPool2d((2, 2))

        self.dropout = nn.Dropout(p=0.4)

        self.encode_1 = conv2d(n_filt * 8, n_filt * 16)
        self.encode_2 = conv2d(n_filt * 16, n_filt * 16)
        self.deconv_1 = deconv2d(n_filt * 16, n_filt * 16, kernel_size=2, stride=2)
        # self.deconv_1 = nn.Upsample(scale_factor=2)     #does only upscale width and height  #Similar results to deconv2d

        self.expand_1_1 = conv2d(n_filt * 8 + n_filt * 16, n_filt * 8)
        self.expand_1_2 = conv2d(n_filt * 8, n_filt * 8)
        self.deconv_2 = deconv2d(n_filt * 8, n_filt * 8, kernel_size=2, stride=2)
        # self.deconv_2 = nn.Upsample(scale_factor=2)

        self.expand_2_1 = conv2d(n_filt * 4 + n_filt * 8, n_filt * 4, stride=1)
        self.expand_2_2 = conv2d(n_filt * 4, n_filt * 4, stride=1)
        self.deconv_3 = deconv2d(n_filt * 4, n_filt * 4, kernel_size=2, stride=2)
        # self.deconv_3 = nn.Upsample(scale_factor=2)

        self.expand_3_1 = conv2d(n_filt * 2 + n_filt * 4, n_filt * 2, stride=1)
        self.expand_3_2 = conv2d(n_filt * 2, n_filt * 2, stride=1)
        self.deconv_4 = deconv2d(n_filt * 2, n_filt * 2, kernel_size=2, stride=2)
        # self.deconv_4 = nn.Upsample(scale_factor=2)

        self.expand_4_1 = conv2d(n_filt + n_filt * 2, n_filt, stride=1)
        self.expand_4_2 = conv2d(n_filt, n_filt, stride=1)

        self.conv_5 = nn.Conv2d(n_filt, n_classes, kernel_size=1, stride=1, padding=0, bias=True)  # no activation function, because is in LossFunction (...WithLogits)

    def forward(self, inpt):
        contr_1_1 = self.contr_1_1(inpt)
        contr_1_2 = self.contr_1_2(contr_1_1)
        pool_1 = self.pool_1(contr_1_2)

        contr_2_1 = self.contr_2_1(pool_1)
        contr_2_2 = self.contr_2_2(contr_2_1)
        pool_2 = self.pool_2(contr_2_2)

        contr_3_1 = self.contr_3_1(pool_2)
        contr_3_2 = self.contr_3_2(contr_3_1)
        pool_3 = self.pool_3(contr_3_2)

        contr_4_1 = self.contr_4_1(pool_3)
        contr_4_2 = self.contr_4_2(contr_4_1)
        pool_4 = self.pool_4(contr_4_2)

        pool_4 = self.dropout(pool_4)

        encode_1 = self.encode_1(pool_4)
        encode_2 = self.encode_2(encode_1)
        deconv_1 = self.deconv_1(encode_2)

        concat1 = torch.cat([deconv_1, contr_4_2], 1)
        expand_1_1 = self.expand_1_1(concat1)
        expand_1_2 = self.expand_1_2(expand_1_1)
        deconv_2 = self.deconv_2(expand_1_2)

        concat2 = torch.cat([deconv_2, contr_3_2], 1)
        expand_2_1 = self.expand_2_1(concat2)
        expand_2_2 = self.expand_2_2(expand_2_1)
        deconv_3 = self.deconv_3(expand_2_2)

        concat3 = torch.cat([deconv_3, contr_2_2], 1)
        expand_3_1 = self.expand_3_1(concat3)
        expand_3_2 = self.expand_3_2(expand_3_1)
        deconv_4 = self.deconv_4(expand_3_2)

        concat4 = torch.cat([deconv_4, contr_1_2], 1)
        expand_4_1 = self.expand_4_1(concat4)
        expand_4_2 = self.expand_4_2(expand_4_1)

        conv_5 = self.conv_5(expand_4_2)
        return conv_5, None

[((40, 25, 40, 57), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(40, 32, 40, 48): 'n_input_channels', (40, 50, 40, 56): 'n_filt'}, {}), '(n_input_channels, n_filt)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((41, 25, 41, 47), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(41, 32, 41, 38): 'n_filt', (41, 40, 41, 46): 'n_filt'}, {}), '(n_filt, n_filt)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((42, 22, 42, 42), 'torch.nn.MaxPool2d', 'nn.MaxPool2d', ({(42, 35, 42, 41): '(2, 2)'}, {}), '((2, 2))', True, 'import torch.nn as nn\n'), ((44, 25, 44, 51), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(44, 32, 44, 38): 'n_filt', (44, 40, 44, 50): 'n_filt * 2'}, {}), '(n_filt, n_filt * 2)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((45, 25, 45, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(45, 32, 45, 42): 'n_filt * 2', (45, 44, 45, 54): 'n_filt * 2'}, {}), '(n_filt * 2, n_filt * 2)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((46, 22, 46, 42), 'torch.nn.MaxPool2d', 'nn.MaxPool2d', ({(46, 35, 46, 41): '(2, 2)'}, {}), '((2, 2))', True, 'import torch.nn as nn\n'), ((48, 25, 48, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(48, 32, 48, 42): 'n_filt * 2', (48, 44, 48, 54): 'n_filt * 4'}, {}), '(n_filt * 2, n_filt * 4)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((49, 25, 49, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(49, 32, 49, 42): 'n_filt * 4', (49, 44, 49, 54): 'n_filt * 4'}, {}), '(n_filt * 4, n_filt * 4)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((50, 22, 50, 42), 'torch.nn.MaxPool2d', 'nn.MaxPool2d', ({(50, 35, 50, 41): '(2, 2)'}, {}), '((2, 2))', True, 'import torch.nn as nn\n'), ((52, 25, 52, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(52, 32, 52, 42): 'n_filt * 4', (52, 44, 52, 54): 'n_filt * 8'}, {}), '(n_filt * 4, n_filt * 8)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((53, 25, 53, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(53, 32, 53, 42): 'n_filt * 8', (53, 44, 53, 54): 'n_filt * 8'}, {}), '(n_filt * 8, n_filt * 8)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((54, 22, 54, 42), 'torch.nn.MaxPool2d', 'nn.MaxPool2d', ({(54, 35, 54, 41): '(2, 2)'}, {}), '((2, 2))', True, 'import torch.nn as nn\n'), ((56, 23, 56, 40), 'torch.nn.Dropout', 'nn.Dropout', (), '', True, 'import torch.nn as nn\n'), ((58, 24, 58, 55), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(58, 31, 58, 41): 'n_filt * 8', (58, 43, 58, 54): 'n_filt * 16'}, {}), '(n_filt * 8, n_filt * 16)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((59, 24, 59, 56), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(59, 31, 59, 42): 'n_filt * 16', (59, 44, 59, 55): 'n_filt * 16'}, {}), '(n_filt * 16, n_filt * 16)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((60, 24, 60, 83), 'tractseg.libs.PytorchUtils.deconv2d', 'deconv2d', (), '', False, 'from tractseg.libs.PytorchUtils import deconv2d\n'), ((63, 26, 63, 70), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(63, 33, 63, 57): 'n_filt * 8 + n_filt * 16', (63, 59, 63, 69): 'n_filt * 8'}, {}), '(n_filt * 8 + n_filt * 16, n_filt * 8)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((64, 26, 64, 56), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', ({(64, 33, 64, 43): 'n_filt * 8', (64, 45, 64, 55): 'n_filt * 8'}, {}), '(n_filt * 8, n_filt * 8)', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((65, 24, 65, 81), 'tractseg.libs.PytorchUtils.deconv2d', 'deconv2d', (), '', False, 'from tractseg.libs.PytorchUtils import deconv2d\n'), ((68, 26, 68, 79), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((69, 26, 69, 66), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((70, 24, 70, 81), 'tractseg.libs.PytorchUtils.deconv2d', 'deconv2d', (), '', False, 'from tractseg.libs.PytorchUtils import deconv2d\n'), ((73, 26, 73, 79), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((74, 26, 74, 66), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((75, 24, 75, 81), 'tractseg.libs.PytorchUtils.deconv2d', 'deconv2d', (), '', False, 'from tractseg.libs.PytorchUtils import deconv2d\n'), ((78, 26, 78, 71), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((79, 26, 79, 58), 'tractseg.libs.PytorchUtils.conv2d', 'conv2d', (), '', False, 'from tractseg.libs.PytorchUtils import conv2d\n'), ((81, 22, 81, 97), 'torch.nn.Conv2d', 'nn.Conv2d', (), '', True, 'import torch.nn as nn\n'), ((106, 18, 106, 53), 'torch.cat', 'torch.cat', ({(106, 28, 106, 49): '[deconv_1, contr_4_2]', (106, 51, 106, 52): '1'}, {}), '([deconv_1, contr_4_2], 1)', False, 'import torch\n'), ((111, 18, 111, 53), 'torch.cat', 'torch.cat', ({(111, 28, 111, 49): '[deconv_2, contr_3_2]', (111, 51, 111, 52): '1'}, {}), '([deconv_2, contr_3_2], 1)', False, 'import torch\n'), ((116, 18, 116, 53), 'torch.cat', 'torch.cat', ({(116, 28, 116, 49): '[deconv_3, contr_2_2]', (116, 51, 116, 52): '1'}, {}), '([deconv_3, contr_2_2], 1)', False, 'import torch\n'), ((121, 18, 121, 53), 'torch.cat', 'torch.cat', ({(121, 28, 121, 49): '[deconv_4, contr_1_2]', (121, 51, 121, 52): '1'}, {}), '([deconv_4, contr_1_2], 1)', False, 'import torch\n')]