You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

childnet.py 4.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from ...utils.builder_util import *
  2. from ..builders.build_childnet import *
  3. from timm.models.layers import SelectAdaptivePool2d
  4. from timm.models.layers.activations import hard_sigmoid
  5. class ChildNet(nn.Module):
  6. def __init__(
  7. self,
  8. block_args,
  9. num_classes=1000,
  10. in_chans=3,
  11. stem_size=16,
  12. num_features=1280,
  13. head_bias=True,
  14. channel_multiplier=1.0,
  15. pad_type='',
  16. act_layer=nn.ReLU,
  17. drop_rate=0.,
  18. drop_path_rate=0.,
  19. se_kwargs=None,
  20. norm_layer=nn.BatchNorm2d,
  21. norm_kwargs=None,
  22. global_pool='avg',
  23. logger=None,
  24. verbose=False):
  25. super(ChildNet, self).__init__()
  26. self.num_classes = num_classes
  27. self.num_features = num_features
  28. self.drop_rate = drop_rate
  29. self._in_chs = in_chans
  30. self.logger = logger
  31. # Stem
  32. stem_size = round_channels(stem_size, channel_multiplier)
  33. self.conv_stem = create_conv2d(
  34. self._in_chs, stem_size, 3, stride=2, padding=pad_type)
  35. self.bn1 = norm_layer(stem_size, **norm_kwargs)
  36. self.act1 = act_layer(inplace=True)
  37. self._in_chs = stem_size
  38. # Middle stages (IR/ER/DS Blocks)
  39. builder = ChildNetBuilder(
  40. channel_multiplier, 8, None, 32, pad_type, act_layer, se_kwargs,
  41. norm_layer, norm_kwargs, drop_path_rate, verbose=verbose)
  42. self.blocks = nn.Sequential(*builder(self._in_chs, block_args))
  43. # self.blocks = builder(self._in_chs, block_args)
  44. self._in_chs = builder.in_chs
  45. # Head + Pooling
  46. self.global_pool = SelectAdaptivePool2d(pool_type=global_pool)
  47. self.conv_head = create_conv2d(
  48. self._in_chs,
  49. self.num_features,
  50. 1,
  51. padding=pad_type,
  52. bias=head_bias)
  53. self.act2 = act_layer(inplace=True)
  54. # Classifier
  55. self.classifier = nn.Linear(
  56. self.num_features *
  57. self.global_pool.feat_mult(),
  58. self.num_classes)
  59. efficientnet_init_weights(self)
  60. def get_classifier(self):
  61. return self.classifier
  62. def reset_classifier(self, num_classes, global_pool='avg'):
  63. self.global_pool = SelectAdaptivePool2d(pool_type=global_pool)
  64. self.num_classes = num_classes
  65. self.classifier = nn.Linear(
  66. self.num_features * self.global_pool.feat_mult(),
  67. num_classes) if self.num_classes else None
  68. def forward_features(self, x):
  69. # architecture = [[0], [], [], [], [], [0]]
  70. x = self.conv_stem(x)
  71. x = self.bn1(x)
  72. x = self.act1(x)
  73. x = self.blocks(x)
  74. x = self.global_pool(x)
  75. x = self.conv_head(x)
  76. x = self.act2(x)
  77. return x
  78. def forward(self, x):
  79. x = self.forward_features(x)
  80. x = x.flatten(1)
  81. if self.drop_rate > 0.:
  82. x = F.dropout(x, p=self.drop_rate, training=self.training)
  83. x = self.classifier(x)
  84. return x
  85. def gen_childnet(arch_list, arch_def, **kwargs):
  86. # arch_list = [[0], [], [], [], [], [0]]
  87. choices = {'kernel_size': [3, 5, 7], 'exp_ratio': [4, 6]}
  88. choices_list = [[x, y] for x in choices['kernel_size']
  89. for y in choices['exp_ratio']]
  90. num_features = 1280
  91. # act_layer = HardSwish
  92. act_layer = Swish
  93. new_arch = []
  94. # change to child arch_def
  95. for i, (layer_choice, layer_arch) in enumerate(zip(arch_list, arch_def)):
  96. if len(layer_arch) == 1:
  97. new_arch.append(layer_arch)
  98. continue
  99. else:
  100. new_layer = []
  101. for j, (block_choice, block_arch) in enumerate(
  102. zip(layer_choice, layer_arch)):
  103. kernel_size, exp_ratio = choices_list[block_choice]
  104. elements = block_arch.split('_')
  105. block_arch = block_arch.replace(
  106. elements[2], 'k{}'.format(str(kernel_size)))
  107. block_arch = block_arch.replace(
  108. elements[4], 'e{}'.format(str(exp_ratio)))
  109. new_layer.append(block_arch)
  110. new_arch.append(new_layer)
  111. model_kwargs = dict(
  112. block_args=decode_arch_def(new_arch),
  113. num_features=num_features,
  114. stem_size=16,
  115. norm_kwargs=resolve_bn_args(kwargs),
  116. act_layer=act_layer,
  117. se_kwargs=dict(
  118. act_layer=nn.ReLU,
  119. gate_fn=hard_sigmoid,
  120. reduce_mid=True,
  121. divisor=8),
  122. **kwargs,
  123. )
  124. model = ChildNet(**model_kwargs)
  125. return model

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能