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.

test_gpu_alexnet.py 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import pytest
  19. import numpy as np
  20. import mindspore.nn as nn
  21. from mindspore.nn.optim import Momentum
  22. from mindspore.ops import operations as P
  23. from mindspore.nn import TrainOneStepCell, WithLossCell
  24. from mindspore import Tensor
  25. from mindspore.common.initializer import initializer
  26. import mindspore.context as context
  27. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  28. class AlexNet(nn.Cell):
  29. def __init__(self, num_classes=10):
  30. super(AlexNet, self).__init__()
  31. self.batch_size = 32
  32. self.conv1 = nn.Conv2d(3, 96, 11, stride=4, pad_mode="valid")
  33. self.conv2 = nn.Conv2d(96, 256, 5, stride=1, pad_mode="same")
  34. self.conv3 = nn.Conv2d(256, 384, 3, stride=1, pad_mode="same")
  35. self.conv4 = nn.Conv2d(384, 384, 3, stride=1, pad_mode="same")
  36. self.conv5 = nn.Conv2d(384, 256, 3, stride=1, pad_mode="same")
  37. self.relu = nn.ReLU()
  38. self.max_pool2d = nn.MaxPool2d(kernel_size=3, stride=2,pad_mode="valid",padding=0)
  39. self.flatten = nn.Flatten()
  40. self.fc1 = nn.Dense(6*6*256, 4096)
  41. self.fc2 = nn.Dense(4096, 4096)
  42. self.fc3 = nn.Dense(4096, num_classes)
  43. def construct(self, x):
  44. x = self.conv1(x)
  45. x = self.relu(x)
  46. x = self.max_pool2d(x)
  47. x = self.conv2(x)
  48. x = self.relu(x)
  49. x = self.max_pool2d(x)
  50. x = self.conv3(x)
  51. x = self.relu(x)
  52. x = self.conv4(x)
  53. x = self.relu(x)
  54. x = self.conv5(x)
  55. x = self.relu(x)
  56. x = self.max_pool2d(x)
  57. x = self.flatten(x)
  58. x = self.fc1(x)
  59. x = self.relu(x)
  60. x = self.fc2(x)
  61. x = self.relu(x)
  62. x = self.fc3(x)
  63. return x
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_trainTensor(num_classes=10, epoch=15, batch_size=32):
  68. net = AlexNet(num_classes)
  69. lr = 0.1
  70. momentum = 0.9
  71. optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), lr, momentum, weight_decay = 0.0001)
  72. criterion = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  73. net_with_criterion = WithLossCell(net, criterion)
  74. train_network = TrainOneStepCell(net_with_criterion, optimizer)
  75. train_network.set_train()
  76. losses=[]
  77. for i in range(0, epoch):
  78. data = Tensor(np.ones([batch_size, 3 ,227, 227]).astype(np.float32) * 0.01)
  79. label = Tensor(np.ones([batch_size]).astype(np.int32))
  80. loss = train_network(data, label)
  81. losses.append(loss)
  82. assert(losses[-1].asnumpy() < 0.01)