layers_33966KB.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import torch
  2. import torch.nn.functional as F
  3. from torch import nn
  4. from . import spec_utils
  5. class Conv2DBNActiv(nn.Module):
  6. def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
  7. super(Conv2DBNActiv, self).__init__()
  8. self.conv = nn.Sequential(
  9. nn.Conv2d(
  10. nin,
  11. nout,
  12. kernel_size=ksize,
  13. stride=stride,
  14. padding=pad,
  15. dilation=dilation,
  16. bias=False,
  17. ),
  18. nn.BatchNorm2d(nout),
  19. activ(),
  20. )
  21. def __call__(self, x):
  22. return self.conv(x)
  23. class SeperableConv2DBNActiv(nn.Module):
  24. def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
  25. super(SeperableConv2DBNActiv, self).__init__()
  26. self.conv = nn.Sequential(
  27. nn.Conv2d(
  28. nin,
  29. nin,
  30. kernel_size=ksize,
  31. stride=stride,
  32. padding=pad,
  33. dilation=dilation,
  34. groups=nin,
  35. bias=False,
  36. ),
  37. nn.Conv2d(nin, nout, kernel_size=1, bias=False),
  38. nn.BatchNorm2d(nout),
  39. activ(),
  40. )
  41. def __call__(self, x):
  42. return self.conv(x)
  43. class Encoder(nn.Module):
  44. def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
  45. super(Encoder, self).__init__()
  46. self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
  47. self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
  48. def __call__(self, x):
  49. skip = self.conv1(x)
  50. h = self.conv2(skip)
  51. return h, skip
  52. class Decoder(nn.Module):
  53. def __init__(
  54. self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False
  55. ):
  56. super(Decoder, self).__init__()
  57. self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
  58. self.dropout = nn.Dropout2d(0.1) if dropout else None
  59. def __call__(self, x, skip=None):
  60. x = F.interpolate(x, scale_factor=2, mode="bilinear", align_corners=True)
  61. if skip is not None:
  62. skip = spec_utils.crop_center(skip, x)
  63. x = torch.cat([x, skip], dim=1)
  64. h = self.conv(x)
  65. if self.dropout is not None:
  66. h = self.dropout(h)
  67. return h
  68. class ASPPModule(nn.Module):
  69. def __init__(self, nin, nout, dilations=(4, 8, 16, 32, 64), activ=nn.ReLU):
  70. super(ASPPModule, self).__init__()
  71. self.conv1 = nn.Sequential(
  72. nn.AdaptiveAvgPool2d((1, None)),
  73. Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ),
  74. )
  75. self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
  76. self.conv3 = SeperableConv2DBNActiv(
  77. nin, nin, 3, 1, dilations[0], dilations[0], activ=activ
  78. )
  79. self.conv4 = SeperableConv2DBNActiv(
  80. nin, nin, 3, 1, dilations[1], dilations[1], activ=activ
  81. )
  82. self.conv5 = SeperableConv2DBNActiv(
  83. nin, nin, 3, 1, dilations[2], dilations[2], activ=activ
  84. )
  85. self.conv6 = SeperableConv2DBNActiv(
  86. nin, nin, 3, 1, dilations[2], dilations[2], activ=activ
  87. )
  88. self.conv7 = SeperableConv2DBNActiv(
  89. nin, nin, 3, 1, dilations[2], dilations[2], activ=activ
  90. )
  91. self.bottleneck = nn.Sequential(
  92. Conv2DBNActiv(nin * 7, nout, 1, 1, 0, activ=activ), nn.Dropout2d(0.1)
  93. )
  94. def forward(self, x):
  95. _, _, h, w = x.size()
  96. feat1 = F.interpolate(
  97. self.conv1(x), size=(h, w), mode="bilinear", align_corners=True
  98. )
  99. feat2 = self.conv2(x)
  100. feat3 = self.conv3(x)
  101. feat4 = self.conv4(x)
  102. feat5 = self.conv5(x)
  103. feat6 = self.conv6(x)
  104. feat7 = self.conv7(x)
  105. out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
  106. bottle = self.bottleneck(out)
  107. return bottle