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

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