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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright 2019 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. import mindspore.nn as nn
  16. from mindspore.ops import operations as P
  17. class AlexNet(nn.Cell):
  18. def __init__(self, num_classes=10):
  19. super(AlexNet, self).__init__()
  20. self.batch_size = 32
  21. self.conv1 = nn.Conv2d(3, 96, 11, stride=4, pad_mode="valid")
  22. self.conv2 = nn.Conv2d(96, 256, 5, stride=1, pad_mode="same")
  23. self.conv3 = nn.Conv2d(256, 384, 3, stride=1, pad_mode="same")
  24. self.conv4 = nn.Conv2d(384, 384, 3, stride=1, pad_mode="same")
  25. self.conv5 = nn.Conv2d(384, 256, 3, stride=1, pad_mode="same")
  26. self.relu = P.ReLU()
  27. self.max_pool2d = nn.MaxPool2d(kernel_size=3, stride=2)
  28. self.flatten = nn.Flatten()
  29. self.fc1 = nn.Dense(66256, 4096)
  30. self.fc2 = nn.Dense(4096, 4096)
  31. self.fc3 = nn.Dense(4096, num_classes)
  32. def construct(self, x):
  33. x = self.conv1(x)
  34. x = self.relu(x)
  35. x = self.max_pool2d(x)
  36. x = self.conv2(x)
  37. x = self.relu(x)
  38. x = self.max_pool2d(x)
  39. x = self.conv3(x)
  40. x = self.relu(x)
  41. x = self.conv4(x)
  42. x = self.relu(x)
  43. x = self.conv5(x)
  44. x = self.relu(x)
  45. x = self.max_pool2d(x)
  46. x = self.flatten(x)
  47. x = self.fc1(x)
  48. x = self.relu(x)
  49. x = self.fc2(x)
  50. x = self.relu(x)
  51. x = self.fc3(x)
  52. return x