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_network_main.py 2.9 kB

6 years ago
5 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. """
  16. Function:
  17. test network
  18. Usage:
  19. python test_network_main.py --net lenet --target Ascend
  20. """
  21. import argparse
  22. import numpy as np
  23. from models.alexnet import AlexNet
  24. from models.lenet import LeNet
  25. from models.resnetv1_5 import resnet50
  26. import mindspore.context as context
  27. import mindspore.nn as nn
  28. from mindspore import Tensor
  29. from mindspore.nn import TrainOneStepCell, WithLossCell
  30. from mindspore.nn.optim import Momentum
  31. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  32. def train(net, data, label):
  33. learning_rate = 0.01
  34. momentum = 0.9
  35. optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), learning_rate, momentum)
  36. criterion = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
  37. net_with_criterion = WithLossCell(net, criterion)
  38. train_network = TrainOneStepCell(net_with_criterion, optimizer) # optimizer
  39. train_network.set_train()
  40. res = train_network(data, label)
  41. print(res)
  42. assert res
  43. def test_resnet50():
  44. data = Tensor(np.ones([32, 3, 224, 224]).astype(np.float32) * 0.01)
  45. label = Tensor(np.ones([32]).astype(np.int32))
  46. net = resnet50(32, 10)
  47. train(net, data, label)
  48. def test_lenet():
  49. data = Tensor(np.ones([32, 1, 32, 32]).astype(np.float32) * 0.01)
  50. label = Tensor(np.ones([32]).astype(np.int32))
  51. net = LeNet()
  52. train(net, data, label)
  53. def test_alexnet():
  54. data = Tensor(np.ones([32, 3, 227, 227]).astype(np.float32) * 0.01)
  55. label = Tensor(np.ones([32]).astype(np.int32))
  56. net = AlexNet()
  57. train(net, data, label)
  58. parser = argparse.ArgumentParser(description='MindSpore Testing Network')
  59. parser.add_argument('--net', default='resnet50', type=str, help='net name')
  60. parser.add_argument('--device', default='Ascend', type=str, help='device target')
  61. if __name__ == "__main__":
  62. args = parser.parse_args()
  63. context.set_context(device_target=args.device)
  64. if args.net == 'resnet50':
  65. test_resnet50()
  66. elif args.net == 'lenet':
  67. test_lenet()
  68. elif args.net == 'alexnet':
  69. test_alexnet()
  70. else:
  71. print("Please add net name like --net lenet")