text: # -*- coding: utf-8 -*-
  """
  MIT License
  
  Copyright (c) 2018 Ozan Oktay
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
  
  https://github.com/ozan-oktay/Attention-Gated-Networks
  
  """
  
  import torch
  from torch import nn
  from torch.nn import functional as F
  from torch.nn import init
  import functools
  from torch.autograd import Variable
  from torch.optim import lr_scheduler
  import time
  import numpy as np
  import math
  
  def init_weights(net, init_type='normal'):
      #print('initialization method [%s]' % init_type)
      if init_type == 'normal':
          net.apply(weights_init_normal)
      elif init_type == 'xavier':
          net.apply(weights_init_xavier)
      elif init_type == 'kaiming':
          net.apply(weights_init_kaiming)
      else:
        raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
  
  
  def weights_init_kaiming(m):
    classname = m.__class__.__name__
    #print(classname)
    if classname.find('Conv') != -1:
        init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
    elif classname.find('Linear') != -1:
        init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
    elif classname.find('BatchNorm') != -1:
        init.normal(m.weight.data, 1.0, 0.02)
        init.constant(m.bias.data, 0.0)
  
  def weights_init_xavier(m):
      classname = m.__class__.__name__
      #print(classname)
      if classname.find('Conv') != -1:
          init.xavier_normal(m.weight.data, gain=1)
      elif classname.find('Linear') != -1:
          init.xavier_normal(m.weight.data, gain=1)
      elif classname.find('BatchNorm') != -1:
          init.normal(m.weight.data, 1.0, 0.02)
          init.constant(m.bias.data, 0.0)
  
  def weights_init_normal(m):
      classname = m.__class__.__name__
      #print(classname)
      if classname.find('Conv') != -1:
          init.normal(m.weight.data, 0.0, 0.02)
      elif classname.find('Linear') != -1:
          init.normal(m.weight.data, 0.0, 0.02)
      elif classname.find('BatchNorm') != -1:
          init.normal(m.weight.data, 1.0, 0.02)
          init.constant(m.bias.data, 0.0)
  
  class UnetConv3(nn.Module):
      def __init__(self, in_size, out_size, is_batchnorm, kernel_size=(3,3,1), padding_size=(1,1,0), init_stride=(1,1,1)):
          super(UnetConv3, self).__init__()
  
          if is_batchnorm:
              self.conv1 = nn.Sequential(nn.Conv3d(in_size, out_size, kernel_size, init_stride, padding_size),
                                         nn.BatchNorm3d(out_size),
                                         nn.ReLU(inplace=True),)
              self.conv2 = nn.Sequential(nn.Conv3d(out_size, out_size, kernel_size, 1, padding_size),
                                         nn.BatchNorm3d(out_size),
                                         nn.ReLU(inplace=True),)
          else:
              self.conv1 = nn.Sequential(nn.Conv3d(in_size, out_size, kernel_size, init_stride, padding_size),
                                         nn.ReLU(inplace=True),)
              self.conv2 = nn.Sequential(nn.Conv3d(out_size, out_size, kernel_size, 1, padding_size),
                                         nn.ReLU(inplace=True),)
  
          # initialise the blocks
          for m in self.children():
              init_weights(m, init_type='kaiming')
  
      def forward(self, inputs):
          outputs = self.conv1(inputs)
          outputs = self.conv2(outputs)
          return outputs
  
  class UnetUp3(nn.Module):
      def __init__(self, in_size, out_size, is_deconv, is_batchnorm=True):
          super(UnetUp3, self).__init__()
          if is_deconv:
              self.conv = UnetConv3(in_size, out_size, is_batchnorm)
              self.up = nn.ConvTranspose3d(in_size, out_size, kernel_size=(4,4,1), stride=(2,2,1), padding=(1,1,0))
          else:
              self.conv = UnetConv3(in_size+out_size, out_size, is_batchnorm)
              self.up = nn.Upsample(scale_factor=(2, 2, 1), mode='trilinear')
  
          # initialise the blocks
          for m in self.children():
              if m.__class__.__name__.find('UnetConv3') != -1: continue
              init_weights(m, init_type='kaiming')
  
      def forward(self, inputs1, inputs2):
          outputs2 = self.up(inputs2)
          offset = outputs2.size()[2] - inputs1.size()[2]
          padding = 2 * [offset // 2, offset // 2, 0]
          outputs1 = F.pad(inputs1, padding)
          return self.conv(torch.cat([outputs1, outputs2], 1))
  
  class unet_3D(nn.Module):
  
      def __init__(self, feature_scale=1, n_classes=2, is_deconv=True, in_channels=1, is_batchnorm=True):
          super(unet_3D, self).__init__()
          self.is_deconv = is_deconv
          self.in_channels = in_channels
          self.is_batchnorm = is_batchnorm
          self.feature_scale = feature_scale
  
          filters = [64, 128, 256, 512]#, 1024]
          filters = [int(x / self.feature_scale) for x in filters]
  
          # downsampling
          self.conv1 = UnetConv3(self.in_channels, filters[0], self.is_batchnorm)
          self.maxpool1 = nn.MaxPool3d(kernel_size=(2, 2, 1))
  
          self.conv2 = UnetConv3(filters[0], filters[1], self.is_batchnorm)
          self.maxpool2 = nn.MaxPool3d(kernel_size=(2, 2, 1))
  
          self.conv3 = UnetConv3(filters[1], filters[2], self.is_batchnorm)
          self.maxpool3 = nn.MaxPool3d(kernel_size=(2, 2, 1))
  
          #self.conv4 = UnetConv3(filters[2], filters[3], self.is_batchnorm)
          #self.maxpool4 = nn.MaxPool3d(kernel_size=(2, 2, 1))
  
          self.center = UnetConv3(filters[2], filters[3], self.is_batchnorm)
  
          # upsampling
          #self.up_concat4 = UnetUp3(filters[4], filters[3], self.is_deconv, is_batchnorm)
          self.up_concat3 = UnetUp3(filters[3], filters[2], self.is_deconv, is_batchnorm)
          self.up_concat2 = UnetUp3(filters[2], filters[1], self.is_deconv, is_batchnorm)
          self.up_concat1 = UnetUp3(filters[1], filters[0], self.is_deconv, is_batchnorm)
  
          # final conv (without any concat)
          self.final = nn.Conv3d(filters[0], n_classes, 1)
  
          # initialise weights
          for m in self.modules():
              if isinstance(m, nn.Conv3d):
                  init_weights(m, init_type='kaiming')
              elif isinstance(m, nn.BatchNorm3d):
                  init_weights(m, init_type='kaiming')
  
      def forward(self, inputs):
          conv1 = self.conv1(inputs)
          maxpool1 = self.maxpool1(conv1)
  
          conv2 = self.conv2(maxpool1)
          maxpool2 = self.maxpool2(conv2)
  
          conv3 = self.conv3(maxpool2)
          maxpool3 = self.maxpool3(conv3)
  
          #conv4 = self.conv4(maxpool3)
          #maxpool4 = self.maxpool4(conv4)
  
          center = self.center(maxpool3)
          #up4 = self.up_concat4(conv4, center)
          up3 = self.up_concat3(conv3, center)
          up2 = self.up_concat2(conv2, up3)
          up1 = self.up_concat1(conv1, up2)
  
          final = self.final(up1)
  
          return final
  
      @staticmethod
      def apply_argmax_softmax(pred):
          log_p = F.softmax(pred, dim=1)
  
          return log_p
  
  def test():
      
      x = torch.rand((3,1,64,64,64)) ## batch,channel,height,width,depth
      
      model = unet_3D(feature_scale=1, n_classes=2, is_deconv=True, in_channels=1, is_batchnorm=True)
      preds = model(x)
      
      print(preds.shape)
  
  