lraspp.py 896 B

1234567891011121314151617181920212223242526272829
  1. from torch import nn
  2. class LRASPP(nn.Module):
  3. def __init__(self, in_channels, out_channels):
  4. super().__init__()
  5. self.aspp1 = nn.Sequential(
  6. nn.Conv2d(in_channels, out_channels, 1, bias=False),
  7. nn.BatchNorm2d(out_channels),
  8. nn.ReLU(True)
  9. )
  10. self.aspp2 = nn.Sequential(
  11. nn.AdaptiveAvgPool2d(1),
  12. nn.Conv2d(in_channels, out_channels, 1, bias=False),
  13. nn.Sigmoid()
  14. )
  15. def forward_single_frame(self, x):
  16. return self.aspp1(x) * self.aspp2(x)
  17. def forward_time_series(self, x):
  18. B, T = x.shape[:2]
  19. x = self.forward_single_frame(x.flatten(0, 1)).unflatten(0, (B, T))
  20. return x
  21. def forward(self, x):
  22. if x.ndim == 5:
  23. return self.forward_time_series(x)
  24. else:
  25. return self.forward_single_frame(x)