text: # Xception71
  # Code Adapted from:
  # https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/modeling/backbone/xception.py
  import math
  import torch
  import torch.nn as nn
  import torch.nn.functional as F
  
  from extern.nvidia_segment.config import cfg
  from network.mynn import Norm2d
  from apex.parallel import SyncBatchNorm
  from runx.logx import logx
  
  
  def fixed_padding(inputs, kernel_size, dilation):
      kernel_size_effective = kernel_size + (kernel_size - 1) * (dilation - 1)
      pad_total = kernel_size_effective - 1
      pad_beg = pad_total // 2
      pad_end = pad_total - pad_beg
      padded_inputs = F.pad(inputs, (pad_beg, pad_end, pad_beg, pad_end))
      return padded_inputs
  
  
  class SeparableConv2d(nn.Module):
      def __init__(self, inplanes, planes, kernel_size=3, stride=1, dilation=1,
                   bias=False, BatchNorm=None):
          super(SeparableConv2d, self).__init__()
  
          self.conv1 = nn.Conv2d(inplanes, inplanes, kernel_size, stride, 0,
                                 dilation, groups=inplanes, bias=bias)
          self.bn = BatchNorm(inplanes)
          self.pointwise = nn.Conv2d(inplanes, planes, 1, 1, 0, 1, 1, bias=bias)
  
      def forward(self, x):
          x = fixed_padding(x, self.conv1.kernel_size[0],
                            dilation=self.conv1.dilation[0])
          x = self.conv1(x)
          x = self.bn(x)
          x = self.pointwise(x)
          return x
  
  
  class Block(nn.Module):
      def __init__(self, inplanes, planes, reps, stride=1, dilation=1,
                   BatchNorm=None, start_with_relu=True, grow_first=True,
                   is_last=False):
          super(Block, self).__init__()
  
          if planes != inplanes or stride != 1:
              self.skip = nn.Conv2d(inplanes, planes, 1, stride=stride,
                                    bias=False)
              self.skipbn = BatchNorm(planes)
          else:
              self.skip = None
  
          self.relu = nn.ReLU(inplace=True)
          rep = []
  
          filters = inplanes
          if grow_first:
              rep.append(self.relu)
              rep.append(SeparableConv2d(inplanes, planes, 3, 1, dilation,
                                         BatchNorm=BatchNorm))
              rep.append(BatchNorm(planes))
              filters = planes
  
          for i in range(reps - 1):
              rep.append(self.relu)
              rep.append(SeparableConv2d(filters, filters, 3, 1, dilation,
                                         BatchNorm=BatchNorm))
              rep.append(BatchNorm(filters))
  
          if not grow_first:
              rep.append(self.relu)
              rep.append(SeparableConv2d(inplanes, planes, 3, 1, dilation,
                                         BatchNorm=BatchNorm))
              rep.append(BatchNorm(planes))
  
          if stride != 1:
              rep.append(self.relu)
              rep.append(SeparableConv2d(planes, planes, 3, 2,
                                         BatchNorm=BatchNorm))
              rep.append(BatchNorm(planes))
  
          if stride == 1 and is_last:
              rep.append(self.relu)
              rep.append(SeparableConv2d(planes, planes, 3, 1,
                                         BatchNorm=BatchNorm))
              rep.append(BatchNorm(planes))
  
          if not start_with_relu:
              rep = rep[1:]
  
          self.rep = nn.Sequential(*rep)
  
      def forward(self, inp):
          x = self.rep(inp)
  
          if self.skip is not None:
              skip = self.skip(inp)
              skip = self.skipbn(skip)
          else:
              skip = inp
  
          x = x + skip
  
          return x
  
  
  class xception71(nn.Module):
      """
      Modified Alighed Xception
      """
      def __init__(self, output_stride, BatchNorm,
                   pretrained=True):
          super(xception71, self).__init__()
  
          self.output_stride = output_stride
          if self.output_stride == 16:
              middle_block_dilation = 1
              exit_block_dilations = (1, 2)
              exit_stride = 2
          elif self.output_stride == 8:
              middle_block_dilation = 2
              exit_block_dilations = (2, 4)
              exit_stride = 1
          else:
              raise NotImplementedError
  
          # Entry flow
          self.conv1 = nn.Conv2d(3, 32, 3, stride=2, padding=1, bias=False)
          self.bn1 = BatchNorm(32)
          self.relu = nn.ReLU(inplace=True)
  
          self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1, bias=False)
          self.bn2 = BatchNorm(64)
  
          self.block1 = Block(64, 128, reps=2, stride=2, BatchNorm=BatchNorm, start_with_relu=False)
          # stride4
  
          self.block2 = Block(128, 256, reps=2, stride=1, BatchNorm=BatchNorm, start_with_relu=False,
                              grow_first=True)
          self.block3 = Block(256, 728, reps=2, stride=2, BatchNorm=BatchNorm,
                              start_with_relu=True, grow_first=True, is_last=True)
          # stride8
  
          # Middle flow
          self.block4  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block5  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block6  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block7  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block8  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block9  = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block10 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block11 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block12 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block13 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block14 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block15 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block16 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block17 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block18 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
          self.block19 = Block(728, 728, reps=3, stride=1, dilation=middle_block_dilation,
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=True)
  
          # Exit flow
          self.block20 = Block(728, 1024, reps=2, stride=exit_stride, dilation=exit_block_dilations[0],
                               BatchNorm=BatchNorm, start_with_relu=True, grow_first=False, is_last=True)
  
          self.conv3 = SeparableConv2d(1024, 1536, 3, stride=1, dilation=exit_block_dilations[1], BatchNorm=BatchNorm)
          self.bn3 = BatchNorm(1536)
  
          self.conv4 = SeparableConv2d(1536, 1536, 3, stride=1, dilation=exit_block_dilations[1], BatchNorm=BatchNorm)
          self.bn4 = BatchNorm(1536)
  
          self.conv5 = SeparableConv2d(1536, 2048, 3, stride=1, dilation=exit_block_dilations[1], BatchNorm=BatchNorm)
          self.bn5 = BatchNorm(2048)
  
          # Init weights
          self._init_weight()
  
          # Load pretrained model
          if pretrained:
              self._load_pretrained_model()
  
      def forward(self, x):
          # Entry flow
          x = self.conv1(x)
          x = self.bn1(x)
          x = self.relu(x)
  
          x = self.conv2(x)
          x = self.bn2(x)
          str2 = self.relu(x)
          # s2
          str4 = self.block1(str2)
          str4 = self.relu(str4)
          # s4
          x = self.block2(str4)
          str8 = self.block3(x)
          # s8
  
          if self.output_stride == 8:
              low_level_feat, high_level_feat = str2, str4
          else:
              low_level_feat, high_level_feat = str4, str8
  
          # Middle flow
          x = self.block4(str8)
          x = self.block5(x)
          x = self.block6(x)
          x = self.block7(x)
          x = self.block8(x)
          x = self.block9(x)
          x = self.block10(x)
          x = self.block11(x)
          x = self.block12(x)
          x = self.block13(x)
          x = self.block14(x)
          x = self.block15(x)
          x = self.block16(x)
          x = self.block17(x)
          x = self.block18(x)
          x = self.block19(x)
  
          # Exit flow
          x = self.block20(x)
          x = self.relu(x)
          x = self.conv3(x)
          x = self.bn3(x)
          x = self.relu(x)
  
          x = self.conv4(x)
          x = self.bn4(x)
          x = self.relu(x)
  
          x = self.conv5(x)
          x = self.bn5(x)
          x = self.relu(x)
  
          return low_level_feat, high_level_feat, x
  
      def _init_weight(self):
          for m in self.modules():
              if isinstance(m, nn.Conv2d):
                  n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                  m.weight.data.normal_(0, math.sqrt(2. / n))
              elif isinstance(m, SyncBatchNorm):
                  m.weight.data.fill_(1)
                  m.bias.data.zero_()
              elif isinstance(m, nn.BatchNorm2d):
                  m.weight.data.fill_(1)
                  m.bias.data.zero_()
  
      def _load_pretrained_model(self):
          pretrained_model = cfg.MODEL.X71_CHECKPOINT
          ckpt = torch.load(pretrained_model, map_location='cpu')
          model_dict = {k.replace('module.', ''): v for k, v in
                        ckpt['model_dict'].items()}
          state_dict = self.state_dict()
          state_dict.update(model_dict)
          self.load_state_dict(state_dict, strict=False)
          del ckpt
          # logx.msg('Loaded {} weights'.format(pretrained_model))
  
  
  if __name__ == "__main__":
      model = xception71(BatchNorm=Norm2d, pretrained=True,
                         output_stride=16)
      input = torch.rand(1, 3, 512, 512)
      output, low_level_feat = model(input)
      print(output.size())
      print(low_level_feat.size())
  