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.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.ops import operations as P
  18. def conv(in_channels, out_channels, kernel_size, stride=1, padding=0, pad_mode="valid", has_bias=True):
  19. return nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding,
  20. has_bias=has_bias, pad_mode=pad_mode)
  21. def fc_with_initialize(input_channels, out_channels, has_bias=True):
  22. return nn.Dense(input_channels, out_channels, has_bias=has_bias)
  23. class AlexNet(nn.Cell):
  24. """
  25. Alexnet
  26. """
  27. def __init__(self, num_classes=10, channel=3, phase='train', include_top=True):
  28. super(AlexNet, self).__init__()
  29. self.conv1 = conv(channel, 64, 11, stride=4, pad_mode="same", has_bias=True)
  30. self.conv2 = conv(64, 128, 5, pad_mode="same", has_bias=True)
  31. self.conv3 = conv(128, 192, 3, pad_mode="same", has_bias=True)
  32. self.conv4 = conv(192, 256, 3, pad_mode="same", has_bias=True)
  33. self.conv5 = conv(256, 256, 3, pad_mode="same", has_bias=True)
  34. self.relu = P.ReLU()
  35. self.max_pool2d = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='valid')
  36. self.include_top = include_top
  37. if self.include_top:
  38. dropout_ratio = 0.65
  39. if phase == 'test':
  40. dropout_ratio = 1.0
  41. self.flatten = nn.Flatten()
  42. self.fc1 = fc_with_initialize(6 * 6 * 256, 4096)
  43. self.fc2 = fc_with_initialize(4096, 4096)
  44. self.fc3 = fc_with_initialize(4096, num_classes)
  45. self.dropout = nn.Dropout(dropout_ratio)
  46. def construct(self, x):
  47. """define network"""
  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. if not self.include_top:
  62. return x
  63. x = self.flatten(x)
  64. x = self.fc1(x)
  65. x = self.relu(x)
  66. x = self.dropout(x)
  67. x = self.fc2(x)
  68. x = self.relu(x)
  69. x = self.dropout(x)
  70. x = self.fc3(x)
  71. return x