nets.py 3.7 KB

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