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_davinci_summary.py 3.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. """ test model train """
  16. import os
  17. import numpy as np
  18. from apply_momentum import ApplyMomentum
  19. import mindspore.context as context
  20. import mindspore.nn as nn
  21. import mindspore.nn as wrap
  22. from mindspore import Tensor, Model
  23. from mindspore.common.api import ms_function
  24. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  25. from mindspore.ops import operations as P
  26. from mindspore.train.summary.summary_record import SummaryRecord
  27. CUR_DIR = os.getcwd()
  28. SUMMARY_DIR = CUR_DIR + "/test_temp_summary_event_file/"
  29. context.set_context(device_target="Ascend")
  30. class MsWrapper(nn.Cell):
  31. def __init__(self, network):
  32. super(MsWrapper, self).__init__(auto_prefix=False)
  33. self._network = network
  34. @ms_function
  35. def construct(self, *args):
  36. return self._network(*args)
  37. def me_train_tensor(net, input_np, label_np, epoch_size=2):
  38. context.set_context(mode=context.GRAPH_MODE)
  39. loss = SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  40. opt = ApplyMomentum(Tensor(np.array([0.1])), Tensor(np.array([0.9])),
  41. filter(lambda x: x.requires_grad, net.get_parameters()))
  42. Model(net, loss, opt)
  43. _network = wrap.WithLossCell(net, loss)
  44. _train_net = MsWrapper(wrap.TrainOneStepCell(_network, opt))
  45. _train_net.set_train()
  46. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_GRAPH", network=_train_net) as summary_writer:
  47. for epoch in range(0, epoch_size):
  48. print(f"epoch %d" % (epoch))
  49. output = _train_net(Tensor(input_np), Tensor(label_np))
  50. summary_writer.record(i)
  51. print("********output***********")
  52. print(output.asnumpy())
  53. def me_infer_tensor(net, input_np):
  54. net.set_train()
  55. net = MsWrapper(net)
  56. output = net(Tensor(input_np))
  57. return output
  58. def test_net():
  59. class Net(nn.Cell):
  60. def __init__(self, cin, cout):
  61. super(Net, self).__init__()
  62. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="same")
  63. self.conv = nn.Conv2d(cin, cin, kernel_size=1, stride=1, padding=0, has_bias=False, pad_mode="same")
  64. self.bn = nn.BatchNorm2d(cin, momentum=0.1, eps=0.0001)
  65. self.add = P.TensorAdd()
  66. self.relu = P.ReLU()
  67. self.mean = P.ReduceMean(keep_dims=True)
  68. self.reshape = P.Reshape()
  69. self.dense = nn.Dense(cin, cout)
  70. def construct(self, input_x):
  71. output = input_x
  72. output = self.maxpool(output)
  73. identity = output
  74. output = self.conv(output)
  75. output = self.bn(output)
  76. output = self.add(output, identity)
  77. output = self.relu(output)
  78. output = self.mean(output, (-2, -1))
  79. output = self.reshape(output, (32, -1))
  80. output = self.dense(output)
  81. return output
  82. net = Net(2048, 1001)
  83. input_np = np.ones([32, 2048, 14, 14]).astype(np.float32) * 0.01
  84. label_np = np.ones([32]).astype(np.int32)
  85. me_train_tensor(net, input_np, label_np)
  86. # me_infer_tensor(net, input_np)