aspp.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from models.modules.deform_conv import DeformableConv2d
  5. from config import Config
  6. config = Config()
  7. class _ASPPModule(nn.Module):
  8. def __init__(self, in_channels, planes, kernel_size, padding, dilation):
  9. super(_ASPPModule, self).__init__()
  10. self.atrous_conv = nn.Conv2d(in_channels, planes, kernel_size=kernel_size,
  11. stride=1, padding=padding, dilation=dilation, bias=False)
  12. self.bn = nn.BatchNorm2d(planes) if config.batch_size > 1 else nn.Identity()
  13. self.relu = nn.ReLU(inplace=True)
  14. def forward(self, x):
  15. x = self.atrous_conv(x)
  16. x = self.bn(x)
  17. return self.relu(x)
  18. class ASPP(nn.Module):
  19. def __init__(self, in_channels=64, out_channels=None, output_stride=16):
  20. super(ASPP, self).__init__()
  21. self.down_scale = 1
  22. if out_channels is None:
  23. out_channels = in_channels
  24. self.in_channelster = 256 // self.down_scale
  25. if output_stride == 16:
  26. dilations = [1, 6, 12, 18]
  27. elif output_stride == 8:
  28. dilations = [1, 12, 24, 36]
  29. else:
  30. raise NotImplementedError
  31. self.aspp1 = _ASPPModule(in_channels, self.in_channelster, 1, padding=0, dilation=dilations[0])
  32. self.aspp2 = _ASPPModule(in_channels, self.in_channelster, 3, padding=dilations[1], dilation=dilations[1])
  33. self.aspp3 = _ASPPModule(in_channels, self.in_channelster, 3, padding=dilations[2], dilation=dilations[2])
  34. self.aspp4 = _ASPPModule(in_channels, self.in_channelster, 3, padding=dilations[3], dilation=dilations[3])
  35. self.global_avg_pool = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)),
  36. nn.Conv2d(in_channels, self.in_channelster, 1, stride=1, bias=False),
  37. nn.BatchNorm2d(self.in_channelster) if config.batch_size > 1 else nn.Identity(),
  38. nn.ReLU(inplace=True))
  39. self.conv1 = nn.Conv2d(self.in_channelster * 5, out_channels, 1, bias=False)
  40. self.bn1 = nn.BatchNorm2d(out_channels) if config.batch_size > 1 else nn.Identity()
  41. self.relu = nn.ReLU(inplace=True)
  42. self.dropout = nn.Dropout(0.5)
  43. def forward(self, x):
  44. x1 = self.aspp1(x)
  45. x2 = self.aspp2(x)
  46. x3 = self.aspp3(x)
  47. x4 = self.aspp4(x)
  48. x5 = self.global_avg_pool(x)
  49. x5 = F.interpolate(x5, size=x1.size()[2:], mode='bilinear', align_corners=True)
  50. x = torch.cat((x1, x2, x3, x4, x5), dim=1)
  51. x = self.conv1(x)
  52. x = self.bn1(x)
  53. x = self.relu(x)
  54. return self.dropout(x)
  55. ##################### Deformable
  56. class _ASPPModuleDeformable(nn.Module):
  57. def __init__(self, in_channels, planes, kernel_size, padding):
  58. super(_ASPPModuleDeformable, self).__init__()
  59. self.atrous_conv = DeformableConv2d(in_channels, planes, kernel_size=kernel_size,
  60. stride=1, padding=padding, bias=False)
  61. self.bn = nn.BatchNorm2d(planes) if config.batch_size > 1 else nn.Identity()
  62. self.relu = nn.ReLU(inplace=True)
  63. def forward(self, x):
  64. x = self.atrous_conv(x)
  65. x = self.bn(x)
  66. return self.relu(x)
  67. class ASPPDeformable(nn.Module):
  68. def __init__(self, in_channels, out_channels=None, parallel_block_sizes=[1, 3, 7]):
  69. super(ASPPDeformable, self).__init__()
  70. self.down_scale = 1
  71. if out_channels is None:
  72. out_channels = in_channels
  73. self.in_channelster = 256 // self.down_scale
  74. self.aspp1 = _ASPPModuleDeformable(in_channels, self.in_channelster, 1, padding=0)
  75. self.aspp_deforms = nn.ModuleList([
  76. _ASPPModuleDeformable(in_channels, self.in_channelster, conv_size, padding=int(conv_size//2)) for conv_size in parallel_block_sizes
  77. ])
  78. self.global_avg_pool = nn.Sequential(nn.AdaptiveAvgPool2d((1, 1)),
  79. nn.Conv2d(in_channels, self.in_channelster, 1, stride=1, bias=False),
  80. nn.BatchNorm2d(self.in_channelster) if config.batch_size > 1 else nn.Identity(),
  81. nn.ReLU(inplace=True))
  82. self.conv1 = nn.Conv2d(self.in_channelster * (2 + len(self.aspp_deforms)), out_channels, 1, bias=False)
  83. self.bn1 = nn.BatchNorm2d(out_channels) if config.batch_size > 1 else nn.Identity()
  84. self.relu = nn.ReLU(inplace=True)
  85. self.dropout = nn.Dropout(0.5)
  86. def forward(self, x):
  87. x1 = self.aspp1(x)
  88. x_aspp_deforms = [aspp_deform(x) for aspp_deform in self.aspp_deforms]
  89. x5 = self.global_avg_pool(x)
  90. x5 = F.interpolate(x5, size=x1.size()[2:], mode='bilinear', align_corners=True)
  91. x = torch.cat((x1, *x_aspp_deforms, x5), dim=1)
  92. x = self.conv1(x)
  93. x = self.bn1(x)
  94. x = self.relu(x)
  95. return self.dropout(x)