nets_123812KB.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import torch
  2. import torch.nn.functional as F
  3. from torch import nn
  4. from . import layers_123821KB as layers
  5. class BaseASPPNet(nn.Module):
  6. def __init__(self, nin, ch, dilations=(4, 8, 16)):
  7. super(BaseASPPNet, self).__init__()
  8. self.enc1 = layers.Encoder(nin, ch, 3, 2, 1)
  9. self.enc2 = layers.Encoder(ch, ch * 2, 3, 2, 1)
  10. self.enc3 = layers.Encoder(ch * 2, ch * 4, 3, 2, 1)
  11. self.enc4 = layers.Encoder(ch * 4, ch * 8, 3, 2, 1)
  12. self.aspp = layers.ASPPModule(ch * 8, ch * 16, dilations)
  13. self.dec4 = layers.Decoder(ch * (8 + 16), ch * 8, 3, 1, 1)
  14. self.dec3 = layers.Decoder(ch * (4 + 8), ch * 4, 3, 1, 1)
  15. self.dec2 = layers.Decoder(ch * (2 + 4), ch * 2, 3, 1, 1)
  16. self.dec1 = layers.Decoder(ch * (1 + 2), ch, 3, 1, 1)
  17. def __call__(self, x):
  18. h, e1 = self.enc1(x)
  19. h, e2 = self.enc2(h)
  20. h, e3 = self.enc3(h)
  21. h, e4 = self.enc4(h)
  22. h = self.aspp(h)
  23. h = self.dec4(h, e4)
  24. h = self.dec3(h, e3)
  25. h = self.dec2(h, e2)
  26. h = self.dec1(h, e1)
  27. return h
  28. class CascadedASPPNet(nn.Module):
  29. def __init__(self, n_fft):
  30. super(CascadedASPPNet, self).__init__()
  31. self.stg1_low_band_net = BaseASPPNet(2, 32)
  32. self.stg1_high_band_net = BaseASPPNet(2, 32)
  33. self.stg2_bridge = layers.Conv2DBNActiv(34, 16, 1, 1, 0)
  34. self.stg2_full_band_net = BaseASPPNet(16, 32)
  35. self.stg3_bridge = layers.Conv2DBNActiv(66, 32, 1, 1, 0)
  36. self.stg3_full_band_net = BaseASPPNet(32, 64)
  37. self.out = nn.Conv2d(64, 2, 1, bias=False)
  38. self.aux1_out = nn.Conv2d(32, 2, 1, bias=False)
  39. self.aux2_out = nn.Conv2d(32, 2, 1, bias=False)
  40. self.max_bin = n_fft // 2
  41. self.output_bin = n_fft // 2 + 1
  42. self.offset = 128
  43. def forward(self, x, aggressiveness=None):
  44. mix = x.detach()
  45. x = x.clone()
  46. x = x[:, :, : self.max_bin]
  47. bandw = x.size()[2] // 2
  48. aux1 = torch.cat(
  49. [
  50. self.stg1_low_band_net(x[:, :, :bandw]),
  51. self.stg1_high_band_net(x[:, :, bandw:]),
  52. ],
  53. dim=2,
  54. )
  55. h = torch.cat([x, aux1], dim=1)
  56. aux2 = self.stg2_full_band_net(self.stg2_bridge(h))
  57. h = torch.cat([x, aux1, aux2], dim=1)
  58. h = self.stg3_full_band_net(self.stg3_bridge(h))
  59. mask = torch.sigmoid(self.out(h))
  60. mask = F.pad(
  61. input=mask,
  62. pad=(0, 0, 0, self.output_bin - mask.size()[2]),
  63. mode="replicate",
  64. )
  65. if self.training:
  66. aux1 = torch.sigmoid(self.aux1_out(aux1))
  67. aux1 = F.pad(
  68. input=aux1,
  69. pad=(0, 0, 0, self.output_bin - aux1.size()[2]),
  70. mode="replicate",
  71. )
  72. aux2 = torch.sigmoid(self.aux2_out(aux2))
  73. aux2 = F.pad(
  74. input=aux2,
  75. pad=(0, 0, 0, self.output_bin - aux2.size()[2]),
  76. mode="replicate",
  77. )
  78. return mask * mix, aux1 * mix, aux2 * mix
  79. else:
  80. if aggressiveness:
  81. mask[:, :, : aggressiveness["split_bin"]] = torch.pow(
  82. mask[:, :, : aggressiveness["split_bin"]],
  83. 1 + aggressiveness["value"] / 3,
  84. )
  85. mask[:, :, aggressiveness["split_bin"] :] = torch.pow(
  86. mask[:, :, aggressiveness["split_bin"] :],
  87. 1 + aggressiveness["value"],
  88. )
  89. return mask * mix
  90. def predict(self, x_mag, aggressiveness=None):
  91. h = self.forward(x_mag, aggressiveness)
  92. if self.offset > 0:
  93. h = h[:, :, :, self.offset : -self.offset]
  94. assert h.size()[3] > 0
  95. return h