model.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import torch
  2. from torch import Tensor
  3. from torch import nn
  4. from torch.nn import functional as F
  5. from typing import Optional, List
  6. from .mobilenetv3 import MobileNetV3LargeEncoder
  7. from .resnet import ResNet50Encoder
  8. from .lraspp import LRASPP
  9. from .decoder import RecurrentDecoder, Projection
  10. from .fast_guided_filter import FastGuidedFilterRefiner
  11. from .deep_guided_filter import DeepGuidedFilterRefiner
  12. from .onnx_helper import CustomOnnxResizeByFactorOp
  13. class MattingNetwork(nn.Module):
  14. def __init__(self,
  15. variant: str = 'mobilenetv3',
  16. refiner: str = 'deep_guided_filter',
  17. pretrained_backbone: bool = False):
  18. super().__init__()
  19. assert variant in ['mobilenetv3', 'resnet50']
  20. assert refiner in ['fast_guided_filter', 'deep_guided_filter']
  21. if variant == 'mobilenetv3':
  22. self.backbone = MobileNetV3LargeEncoder(pretrained_backbone)
  23. self.aspp = LRASPP(960, 128)
  24. self.decoder = RecurrentDecoder([16, 24, 40, 128], [80, 40, 32, 16])
  25. else:
  26. self.backbone = ResNet50Encoder(pretrained_backbone)
  27. self.aspp = LRASPP(2048, 256)
  28. self.decoder = RecurrentDecoder([64, 256, 512, 256], [128, 64, 32, 16])
  29. self.project_mat = Projection(16, 4)
  30. self.project_seg = Projection(16, 1)
  31. if refiner == 'deep_guided_filter':
  32. self.refiner = DeepGuidedFilterRefiner()
  33. else:
  34. self.refiner = FastGuidedFilterRefiner()
  35. def forward(self, src, r1, r2, r3, r4,
  36. downsample_ratio: float = 1,
  37. segmentation_pass: bool = False):
  38. if torch.onnx.is_in_onnx_export():
  39. src_sm = CustomOnnxResizeByFactorOp.apply(src, downsample_ratio)
  40. elif downsample_ratio != 1:
  41. src_sm = self._interpolate(src, scale_factor=downsample_ratio)
  42. else:
  43. src_sm = src
  44. f1, f2, f3, f4 = self.backbone(src_sm)
  45. f4 = self.aspp(f4)
  46. hid, *rec = self.decoder(src_sm, f1, f2, f3, f4, r1, r2, r3, r4)
  47. if not segmentation_pass:
  48. fgr_residual, pha = self.project_mat(hid).split([3, 1], dim=-3)
  49. if torch.onnx.is_in_onnx_export() or downsample_ratio != 1:
  50. fgr_residual, pha = self.refiner(src, src_sm, fgr_residual, pha, hid)
  51. fgr = fgr_residual + src
  52. fgr = fgr.clamp(0., 1.)
  53. pha = pha.clamp(0., 1.)
  54. return [fgr, pha, *rec]
  55. else:
  56. seg = self.project_seg(hid)
  57. return [seg, *rec]
  58. def _interpolate(self, x: Tensor, scale_factor: float):
  59. if x.ndim == 5:
  60. B, T = x.shape[:2]
  61. x = F.interpolate(x.flatten(0, 1), scale_factor=scale_factor,
  62. mode='bilinear', align_corners=False, recompute_scale_factor=False)
  63. x = x.reshape(B, T, x.size(1), x.size(2), x.size(3))
  64. else:
  65. x = F.interpolate(x, scale_factor=scale_factor,
  66. mode='bilinear', align_corners=False, recompute_scale_factor=False)
  67. return x