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.

alexnet.py 2.6 kB

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. """Alexnet."""
  16. import mindspore.nn as nn
  17. from mindspore.common.initializer import TruncatedNormal
  18. from mindspore.ops import operations as P
  19. def conv(in_channels, out_channels, kernel_size, stride=1, padding=0, pad_mode="valid"):
  20. weight = weight_variable()
  21. return nn.Conv2d(in_channels, out_channels,
  22. kernel_size=kernel_size, stride=stride, padding=padding,
  23. weight_init=weight, has_bias=False, pad_mode=pad_mode)
  24. def fc_with_initialize(input_channels, out_channels):
  25. weight = weight_variable()
  26. bias = weight_variable()
  27. return nn.Dense(input_channels, out_channels, weight, bias)
  28. def weight_variable():
  29. return TruncatedNormal(0.02) # 0.02
  30. class AlexNet(nn.Cell):
  31. """
  32. Alexnet
  33. """
  34. def __init__(self, num_classes=10, channel=3):
  35. super(AlexNet, self).__init__()
  36. self.conv1 = conv(channel, 96, 11, stride=4)
  37. self.conv2 = conv(96, 256, 5, pad_mode="same")
  38. self.conv3 = conv(256, 384, 3, pad_mode="same")
  39. self.conv4 = conv(384, 384, 3, pad_mode="same")
  40. self.conv5 = conv(384, 256, 3, pad_mode="same")
  41. self.relu = nn.ReLU()
  42. self.max_pool2d = P.MaxPool(ksize=3, strides=2)
  43. self.flatten = nn.Flatten()
  44. self.fc1 = fc_with_initialize(6*6*256, 4096)
  45. self.fc2 = fc_with_initialize(4096, 4096)
  46. self.fc3 = fc_with_initialize(4096, num_classes)
  47. def construct(self, x):
  48. x = self.conv1(x)
  49. x = self.relu(x)
  50. x = self.max_pool2d(x)
  51. x = self.conv2(x)
  52. x = self.relu(x)
  53. x = self.max_pool2d(x)
  54. x = self.conv3(x)
  55. x = self.relu(x)
  56. x = self.conv4(x)
  57. x = self.relu(x)
  58. x = self.conv5(x)
  59. x = self.relu(x)
  60. x = self.max_pool2d(x)
  61. x = self.flatten(x)
  62. x = self.fc1(x)
  63. x = self.relu(x)
  64. x = self.fc2(x)
  65. x = self.relu(x)
  66. x = self.fc3(x)
  67. return x