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

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