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 4.0 kB

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