text: ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ## Created by: RainbowSecret
  ## Microsoft Research
  ## yuyua@microsoft.com
  ## Copyright (c) 2018
  ##
  ## This source code is licensed under the MIT-style license found in the
  ## LICENSE file in the root directory of this source tree 
  ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
  import torch.nn as nn
  from torch.nn import functional as F
  import math
  import torch.utils.model_zoo as model_zoo
  import torch
  import os
  import sys
  import pdb
  import numpy as np
  from torch.autograd import Variable
  import functools
  
  affine_par = True
  
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  sys.path.append(BASE_DIR)
  sys.path.append(os.path.join(BASE_DIR, '../utils'))
  from resnet_block import conv3x3, Bottleneck
  
  sys.path.append(os.path.join(BASE_DIR, '../oc_module'))
  from aa import topk_PAM_Module, SE_CAM_Module2, selective_aggregation_ASPP_Module2
  
  torch_ver = torch.__version__[:3]
  
  if torch_ver == '0.4':
      sys.path.append(os.path.join(BASE_DIR, '../inplace_abn'))
      from bn import InPlaceABNSync
  
      BatchNorm2d = functools.partial(InPlaceABNSync, activation='none')
  
  elif torch_ver == '0.3':
      sys.path.append(os.path.join(BASE_DIR, '../inplace_abn_03'))
      from modules import InPlaceABNSync
  
      BatchNorm2d = functools.partial(InPlaceABNSync, activation='none')
  
  
  class ResNet(nn.Module):
      def __init__(self, block, layers, num_classes):
          self.inplanes = 128
          super(ResNet, self).__init__()
  
          # self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
          #                        bias=False)
          # self.bn1 = BatchNorm2d(64)
          # self.relu = nn.ReLU(inplace=True)
          # self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
          #
  
          self.conv1 = conv3x3(3, 64, stride=2)
          self.bn1 = BatchNorm2d(64)
          self.relu1 = nn.ReLU(inplace=False)
          self.conv2 = conv3x3(64, 64)
          self.bn2 = BatchNorm2d(64)
          self.relu2 = nn.ReLU(inplace=False)
          self.conv3 = conv3x3(64, 128)
          self.bn3 = BatchNorm2d(128)
          self.relu3 = nn.ReLU(inplace=False)
  
          self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
          self.relu = nn.ReLU(inplace=False)
          self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=True)  # change
          self.layer1 = self._make_layer(block, 64, layers[0])
          self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
          self.layer3 = self._make_layer(block, 256, layers[2], stride=1, dilation=2)
          self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4,
                                         multi_grid=(1, 1, 1))  # we do not apply multi-grid method here
  
  
  
  
          self.context = AANetHead(2048, num_classes, InPlaceABNSync)
          # extra added layers
          self.cls = nn.Conv2d(512, num_classes, kernel_size=1, stride=1, padding=0, bias=True)
  
          self.dsn = nn.Sequential(
              nn.Conv2d(1024, 512, kernel_size=3, stride=1, padding=1),
              InPlaceABNSync(512),
              nn.Dropout2d(0.05),
              nn.Conv2d(512, num_classes, kernel_size=1, stride=1, padding=0, bias=True)
          )
  
  
  
  
      def _make_layer(self, block, planes, blocks, stride=1, dilation=1, multi_grid=1):
          downsample = None
          if stride != 1 or self.inplanes != planes * block.expansion:
              downsample = nn.Sequential(
                  nn.Conv2d(self.inplanes, planes * block.expansion,
                            kernel_size=1, stride=stride, bias=False),
                  BatchNorm2d(planes * block.expansion, affine=affine_par))
  
          layers = []
          generate_multi_grid = lambda index, grids: grids[index % len(grids)] if isinstance(grids, tuple) else 1
          layers.append(block(self.inplanes, planes, stride, dilation=dilation, downsample=downsample,
                              multi_grid=generate_multi_grid(0, multi_grid)))
          self.inplanes = planes * block.expansion
          for i in range(1, blocks):
              layers.append(
                  block(self.inplanes, planes, dilation=dilation, multi_grid=generate_multi_grid(i, multi_grid)))
          return nn.Sequential(*layers)
  
      def forward(self, x):
          # x = self.conv1(x)
          # x = self.bn1(x)
          # x = self.relu(x)
          # x = self.maxpool(x)
  
          x = self.relu1(self.bn1(self.conv1(x)))
          x = self.relu2(self.bn2(self.conv2(x)))
          x = self.relu3(self.bn3(self.conv3(x)))
          x = self.maxpool(x)
  
          x = self.layer1(x)
          x = self.layer2(x)
          x = self.layer3(x)
          x_dsn = self.dsn(x)
          x = self.layer4(x)
  
          x = self.context(x)
  
          # x = self.cls(x)
          return [x_dsn, x]
  
  
  def get_resnet101_aa_dsn(num_classes=21):
      model = ResNet(Bottleneck, [3, 4, 23, 3], num_classes)
      return model
  
  
  class AANetHead(nn.Module):
      def __init__(self, in_channels, out_channels, norm_layer):
          super(AANetHead, self).__init__()
          inter_channels = in_channels // 4
  
  
          self.conv5a = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
                                      norm_layer(inter_channels))
          self.conv5as = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
                                       norm_layer(inter_channels))
          self.sa = topk_PAM_Module(inter_channels, 256, inter_channels, 4)
          self.conv51 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, padding=1, bias=False),
                                      norm_layer(inter_channels))
  
  
          self.conv5s = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 1, padding=0, bias=False),
                                      norm_layer(inter_channels))
          self.aspp = selective_aggregation_ASPP_Module2(inter_channels, inner_features=256, out_features=256,
                                                        dilations=(12, 24, 36))
          self.conv52 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, padding=1, bias=False),
                                      norm_layer(inter_channels))
  
  
          self.conv5c = nn.Sequential(nn.Conv2d(in_channels, inter_channels, 3, padding=1, bias=False),
                                      norm_layer(inter_channels))
          self.sc = SE_CAM_Module2(inter_channels)
          self.conv53 = nn.Sequential(nn.Conv2d(inter_channels, inter_channels, 3, padding=1, bias=False),
                                      norm_layer(inter_channels))
  
          # self.conv5 = nn.Sequential(nn.Dropout2d(0.1), nn.Conv2d(512, out_channels, 1))
          # self.conv6 = nn.Sequential(nn.Dropout2d(0.1), nn.Conv2d(512, out_channels, 1))
          # self.conv7 = nn.Sequential(nn.Dropout2d(0.1), nn.Conv2d(512, out_channels, 1))
          self.conv8 = nn.Sequential(nn.Dropout2d(0.1), nn.Conv2d(512, out_channels, 1))
  
  
      def forward(self, x):
          #ssa
          feat1 = self.conv5a(x)
          sa_feat = self.sa(feat1)
          sa_conv = self.conv51(sa_feat)
  
          # aspp_output = self.conv5(sa_conv)
          #aaspp
  
          feat_as = self.conv5as(x)
          aspp_feat = self.aspp(feat_as)
          aspp_conv = self.conv52(aspp_feat)
  
          # sa_output = self.conv6(aspp_conv)
  
          #sec
          feat2 = self.conv5c(x)
          sc_feat = self.sc(feat2)
          sc_conv = self.conv53(sc_feat)
  
          # sc_output = self.conv7(sc_conv)
  
          #fuse
          feat_sum = aspp_conv + sc_conv + sa_conv
          sasc_output = self.conv8(feat_sum)
  
          return sasc_output
          # output = [sasc_output]
          # output.append(sa_output)
          # output.append(sc_output)
          # output.append(aspp_output)
  
          # return tuple(output)
  