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.

mobilenetv2.py 3.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """MobileNetV2"""
  2. from mindspore import nn
  3. from mindspore.ops import operations as P
  4. def make_divisible(input_x, div_by=8):
  5. return int((input_x + div_by) // div_by)
  6. def _conv_bn(in_channel,
  7. out_channel,
  8. ksize,
  9. stride=1):
  10. """Get a conv2d batchnorm and relu layer."""
  11. return nn.SequentialCell(
  12. [nn.Conv2d(in_channel,
  13. out_channel,
  14. kernel_size=ksize,
  15. stride=stride),
  16. nn.BatchNorm2d(out_channel)])
  17. class InvertedResidual(nn.Cell):
  18. def __init__(self, inp, oup, stride, expend_ratio):
  19. super(InvertedResidual, self).__init__()
  20. self.stride = stride
  21. assert stride in [1, 2]
  22. hidden_dim = int(inp * expend_ratio)
  23. self.use_res_connect = self.stride == 1 and inp == oup
  24. if expend_ratio == 1:
  25. self.conv = nn.SequentialCell([
  26. nn.Conv2d(hidden_dim, hidden_dim, 3, stride, group=hidden_dim),
  27. nn.BatchNorm2d(hidden_dim),
  28. nn.ReLU6(),
  29. nn.Conv2d(hidden_dim, oup, 1, 1),
  30. nn.BatchNorm2d(oup)
  31. ])
  32. else:
  33. self.conv = nn.SequentialCell([
  34. nn.Conv2d(inp, hidden_dim, 1, 1),
  35. nn.BatchNorm2d(hidden_dim),
  36. nn.ReLU6(),
  37. nn.Conv2d(hidden_dim, hidden_dim, 3, stride, group=hidden_dim),
  38. nn.BatchNorm2d(hidden_dim),
  39. nn.ReLU6(),
  40. nn.Conv2d(hidden_dim, oup, 1, 1),
  41. nn.BatchNorm2d(oup)
  42. ])
  43. def construct(self, input_x):
  44. out = self.conv(input_x)
  45. if self.use_res_connect:
  46. out = input_x + out
  47. return out
  48. class MobileNetV2(nn.Cell):
  49. def __init__(self, num_class=1000, input_size=224, width_mul=1.):
  50. super(MobileNetV2, self).__init__()
  51. _ = input_size
  52. block = InvertedResidual
  53. input_channel = 32
  54. last_channel = 1280
  55. inverted_residual_setting = [
  56. [1, 16, 1, 1],
  57. [6, 24, 2, 2],
  58. [6, 32, 3, 2],
  59. [6, 64, 4, 2],
  60. [6, 96, 3, 1],
  61. [6, 160, 3, 2],
  62. [6, 230, 1, 1],
  63. ]
  64. if width_mul > 1.0:
  65. last_channel = make_divisible(last_channel * width_mul)
  66. self.last_channel = last_channel
  67. features = [_conv_bn(3, input_channel, 3, 2)]
  68. for t, c, n, s in inverted_residual_setting:
  69. out_channel = make_divisible(c * width_mul) if t > 1 else c
  70. for i in range(n):
  71. if i == 0:
  72. features.append(block(input_channel, out_channel, s, t))
  73. else:
  74. features.append(block(input_channel, out_channel, 1, t))
  75. input_channel = out_channel
  76. features.append(_conv_bn(input_channel, self.last_channel, 1))
  77. self.features = nn.SequentialCell(features)
  78. self.mean = P.ReduceMean(keep_dims=False)
  79. self.classifier = nn.Dense(self.last_channel, num_class)
  80. def construct(self, input_x):
  81. out = input_x
  82. out = self.features(out)
  83. out = self.mean(out, (2, 3))
  84. out = self.classifier(out)
  85. return out