fusion.py 904 B

1234567891011121314151617181920212223242526272829303132
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import torch
  3. import torch.nn as nn
  4. class AFF(nn.Module):
  5. def __init__(self, channels=64, r=4):
  6. super(AFF, self).__init__()
  7. inter_channels = int(channels // r)
  8. self.local_att = nn.Sequential(
  9. nn.Conv2d(
  10. channels * 2,
  11. inter_channels,
  12. kernel_size=1,
  13. stride=1,
  14. padding=0),
  15. nn.BatchNorm2d(inter_channels),
  16. nn.SiLU(inplace=True),
  17. nn.Conv2d(
  18. inter_channels, channels, kernel_size=1, stride=1, padding=0),
  19. nn.BatchNorm2d(channels),
  20. )
  21. def forward(self, x, ds_y):
  22. xa = torch.cat((x, ds_y), dim=1)
  23. x_att = self.local_att(xa)
  24. x_att = 1.0 + torch.tanh(x_att)
  25. xo = torch.mul(x, x_att) + torch.mul(ds_y, 2.0 - x_att)
  26. return xo