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.

resnet_example.py 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Resnet examples."""
  16. # pylint: disable=missing-docstring, arguments-differ
  17. import mindspore.nn as nn
  18. from mindspore.ops import operations as P
  19. def conv3x3(in_channels, out_channels, stride=1, padding=1, pad_mode='pad'):
  20. """3x3 convolution """
  21. return nn.Conv2d(in_channels, out_channels,
  22. kernel_size=3, stride=stride, padding=padding, pad_mode=pad_mode)
  23. def conv1x1(in_channels, out_channels, stride=1, padding=0, pad_mode='pad'):
  24. """1x1 convolution"""
  25. return nn.Conv2d(in_channels, out_channels,
  26. kernel_size=1, stride=stride, padding=padding, pad_mode=pad_mode)
  27. class ResidualBlock(nn.Cell):
  28. """
  29. residual Block
  30. """
  31. expansion = 4
  32. def __init__(self,
  33. in_channels,
  34. out_channels,
  35. stride=1,
  36. down_sample=False):
  37. super(ResidualBlock, self).__init__()
  38. out_chls = out_channels // self.expansion
  39. self.conv1 = conv1x1(in_channels, out_chls, stride=1, padding=0)
  40. self.bn1 = nn.BatchNorm2d(out_chls)
  41. self.conv2 = conv3x3(out_chls, out_chls, stride=stride, padding=1)
  42. self.bn2 = nn.BatchNorm2d(out_chls)
  43. self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
  44. self.bn3 = nn.BatchNorm2d(out_channels)
  45. self.relu = nn.ReLU()
  46. self.downsample = down_sample
  47. self.conv_down_sample = conv1x1(in_channels, out_channels,
  48. stride=stride, padding=0)
  49. self.bn_down_sample = nn.BatchNorm2d(out_channels)
  50. self.add = P.Add()
  51. def construct(self, x):
  52. """
  53. :param x:
  54. :return:
  55. """
  56. identity = x
  57. out = self.conv1(x)
  58. out = self.bn1(out)
  59. out = self.relu(out)
  60. out = self.conv2(out)
  61. out = self.bn2(out)
  62. out = self.relu(out)
  63. out = self.conv3(out)
  64. out = self.bn3(out)
  65. if self.downsample:
  66. identity = self.conv_down_sample(identity)
  67. identity = self.bn_down_sample(identity)
  68. out = self.add(out, identity)
  69. out = self.relu(out)
  70. return out
  71. class ResNet50(nn.Cell):
  72. """
  73. resnet nn.Cell
  74. """
  75. def __init__(self, block, num_classes=100):
  76. super(ResNet50, self).__init__()
  77. self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, pad_mode='pad')
  78. self.bn1 = nn.BatchNorm2d(64)
  79. self.relu = nn.ReLU()
  80. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='valid')
  81. self.layer1 = self.MakeLayer(
  82. block, 3, in_channels=64, out_channels=256, stride=1)
  83. self.layer2 = self.MakeLayer(
  84. block, 4, in_channels=256, out_channels=512, stride=2)
  85. self.layer3 = self.MakeLayer(
  86. block, 6, in_channels=512, out_channels=1024, stride=2)
  87. self.layer4 = self.MakeLayer(
  88. block, 3, in_channels=1024, out_channels=2048, stride=2)
  89. self.avgpool = nn.AvgPool2d(7, 1)
  90. self.flatten = P.Flatten()
  91. self.fc = nn.Dense(512 * block.expansion, num_classes)
  92. def MakeLayer(self, block, layer_num, in_channels, out_channels, stride):
  93. """
  94. make block layer
  95. :param block:
  96. :param layer_num:
  97. :param in_channels:
  98. :param out_channels:
  99. :param stride:
  100. :return:
  101. """
  102. layers = []
  103. resblk = block(in_channels, out_channels,
  104. stride=stride, down_sample=True)
  105. layers.append(resblk)
  106. for _ in range(1, layer_num):
  107. resblk = block(out_channels, out_channels, stride=1)
  108. layers.append(resblk)
  109. return nn.SequentialCell(layers)
  110. def construct(self, x):
  111. """
  112. :param x:
  113. :return:
  114. """
  115. x = self.conv1(x)
  116. x = self.bn1(x)
  117. x = self.relu(x)
  118. x = self.maxpool(x)
  119. x = self.layer1(x)
  120. x = self.layer2(x)
  121. x = self.layer3(x)
  122. x = self.layer4(x)
  123. x = self.avgpool(x)
  124. x = self.flatten(x)
  125. x = self.fc(x)
  126. return x
  127. def resnet50():
  128. return ResNet50(ResidualBlock, 10)