ctrnet_encoder.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import torch
  2. import torch.nn as nn
  3. try:
  4. from .ctrnet_basic import Conv, RTCBlock
  5. except:
  6. from ctrnet_basic import Conv, RTCBlock
  7. # ---------------------------- Basic functions ----------------------------
  8. ## Real-time Convolutional Backbone
  9. class CTREncoder(nn.Module):
  10. def __init__(self, width=1.0, depth=1.0, act_type='silu', norm_type='BN', depthwise=False):
  11. super(CTREncoder, self).__init__()
  12. # ---------------- Basic parameters ----------------
  13. self.width_factor = width
  14. self.depth_factor = depth
  15. self.feat_dims = [round(64 * width), round(128 * width), round(256 * width), round(512 * width), round(1024 * width)]
  16. # ---------------- Network parameters ----------------
  17. ## P1/2
  18. self.layer_1 = Conv(3, self.feat_dims[0], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type)
  19. ## P2/4
  20. self.layer_2 = nn.Sequential(
  21. Conv(self.feat_dims[0], self.feat_dims[1], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  22. RTCBlock(in_dim = self.feat_dims[1],
  23. out_dim = self.feat_dims[1],
  24. num_blocks = round(3*depth),
  25. shortcut = True,
  26. act_type = act_type,
  27. norm_type = norm_type,
  28. depthwise = depthwise)
  29. )
  30. ## P3/8
  31. self.layer_3 = nn.Sequential(
  32. Conv(self.feat_dims[1], self.feat_dims[2], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  33. RTCBlock(in_dim = self.feat_dims[2],
  34. out_dim = self.feat_dims[2],
  35. num_blocks = round(6*depth),
  36. shortcut = True,
  37. act_type = act_type,
  38. norm_type = norm_type,
  39. depthwise = depthwise)
  40. )
  41. ## P4/16
  42. self.layer_4 = nn.Sequential(
  43. Conv(self.feat_dims[2], self.feat_dims[3], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  44. RTCBlock(in_dim = self.feat_dims[3],
  45. out_dim = self.feat_dims[3],
  46. num_blocks = round(6*depth),
  47. shortcut = True,
  48. act_type = act_type,
  49. norm_type = norm_type,
  50. depthwise = depthwise)
  51. )
  52. ## P5/32
  53. self.layer_5 = nn.Sequential(
  54. Conv(self.feat_dims[3], self.feat_dims[4], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
  55. RTCBlock(in_dim = self.feat_dims[4],
  56. out_dim = self.feat_dims[4],
  57. num_blocks = round(3*depth),
  58. shortcut = True,
  59. act_type = act_type,
  60. norm_type = norm_type,
  61. depthwise = depthwise)
  62. )
  63. def forward(self, x):
  64. c1 = self.layer_1(x)
  65. c2 = self.layer_2(c1)
  66. c3 = self.layer_3(c2)
  67. c4 = self.layer_4(c3)
  68. c5 = self.layer_5(c4)
  69. outputs = [c3, c4, c5]
  70. return outputs
  71. # ---------------------------- Functions ----------------------------
  72. ## build Backbone
  73. def build_encoder(cfg):
  74. # build backbone model
  75. backbone = CTREncoder(width=cfg['width'],
  76. depth=cfg['depth'],
  77. act_type=cfg['bk_act'],
  78. norm_type=cfg['bk_norm'],
  79. depthwise=cfg['bk_depthwise']
  80. )
  81. feat_dims = backbone.feat_dims[-3:]
  82. return backbone, feat_dims
  83. if __name__ == '__main__':
  84. import time
  85. from thop import profile
  86. cfg = {
  87. 'bk_act': 'silu',
  88. 'bk_norm': 'BN',
  89. 'bk_depthwise': False,
  90. 'width': 1.0,
  91. 'depth': 1.0,
  92. 'ratio': 1.0,
  93. }
  94. model, feats = build_encoder(cfg)
  95. x = torch.randn(1, 3, 640, 640)
  96. t0 = time.time()
  97. outputs = model(x)
  98. t1 = time.time()
  99. print('Time: ', t1 - t0)
  100. for out in outputs:
  101. print(out.shape)
  102. x = torch.randn(1, 3, 640, 640)
  103. print('==============================')
  104. flops, params = profile(model, inputs=(x, ), verbose=False)
  105. print('==============================')
  106. print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
  107. print('Params : {:.2f} M'.format(params / 1e6))