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_graph_summary.py 4.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2020 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_graph_summary """
  16. import logging
  17. import numpy as np
  18. import os
  19. import mindspore.nn as nn
  20. from mindspore import Model, context
  21. from mindspore.nn.optim import Momentum
  22. from mindspore.train.callback import SummaryStep
  23. from mindspore.train.summary.summary_record import SummaryRecord
  24. from .....dataset_mock import MindData
  25. CUR_DIR = os.getcwd()
  26. SUMMARY_DIR = CUR_DIR + "/test_temp_summary_event_file/"
  27. GRAPH_TEMP = CUR_DIR + "/ms_output-resnet50.pb"
  28. log = logging.getLogger("test")
  29. log.setLevel(level=logging.ERROR)
  30. class Net(nn.Cell):
  31. """ Net definition """
  32. def __init__(self):
  33. super(Net, self).__init__()
  34. self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal', pad_mode='valid')
  35. self.bn = nn.BatchNorm2d(64)
  36. self.relu = nn.ReLU()
  37. self.flatten = nn.Flatten()
  38. self.fc = nn.Dense(64 * 222 * 222, 3) # padding=0
  39. def construct(self, x):
  40. x = self.conv(x)
  41. x = self.bn(x)
  42. x = self.relu(x)
  43. x = self.flatten(x)
  44. out = self.fc(x)
  45. return out
  46. class LossNet(nn.Cell):
  47. """ LossNet definition """
  48. def __init__(self):
  49. super(LossNet, self).__init__()
  50. self.conv = nn.Conv2d(3, 64, 3, has_bias=False, weight_init='normal', pad_mode='valid')
  51. self.bn = nn.BatchNorm2d(64)
  52. self.relu = nn.ReLU()
  53. self.flatten = nn.Flatten()
  54. self.fc = nn.Dense(64 * 222 * 222, 3) # padding=0
  55. self.loss = nn.SoftmaxCrossEntropyWithLogits()
  56. def construct(self, x, y):
  57. x = self.conv(x)
  58. x = self.bn(x)
  59. x = self.relu(x)
  60. x = self.flatten(x)
  61. x = self.fc(x)
  62. out = self.loss(x, y)
  63. return out
  64. def get_model():
  65. """ get_model """
  66. net = Net()
  67. loss = nn.SoftmaxCrossEntropyWithLogits()
  68. optim = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  69. model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  70. return model
  71. def get_dataset():
  72. """ get_datasetdataset """
  73. dataset_types = (np.float32, np.float32)
  74. dataset_shapes = ((2, 3, 224, 224), (2, 3))
  75. dataset = MindData(size=2, batch_size=2,
  76. np_types=dataset_types,
  77. output_shapes=dataset_shapes,
  78. input_indexs=(0, 1))
  79. return dataset
  80. # Test 1: summary sample of graph
  81. def test_graph_summary_sample():
  82. """ test_graph_summary_sample """
  83. log.debug("begin test_graph_summary_sample")
  84. dataset = get_dataset()
  85. net = Net()
  86. loss = nn.SoftmaxCrossEntropyWithLogits()
  87. optim = Momentum(net.trainable_params(), 0.1, 0.9)
  88. context.set_context(mode=context.GRAPH_MODE)
  89. model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  90. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_GRAPH", network=model._train_network) as test_writer:
  91. model.train(2, dataset)
  92. # step 2: create the Event
  93. for i in range(1, 5):
  94. test_writer.record(i)
  95. # step 3: send the event to mq
  96. # step 4: accept the event and write the file
  97. log.debug("finished test_graph_summary_sample")
  98. def test_graph_summary_callback():
  99. dataset = get_dataset()
  100. net = Net()
  101. loss = nn.SoftmaxCrossEntropyWithLogits()
  102. optim = Momentum(net.trainable_params(), 0.1, 0.9)
  103. context.set_context(mode=context.GRAPH_MODE)
  104. model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  105. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_GRAPH", network=model._train_network) as test_writer:
  106. summary_cb = SummaryStep(test_writer, 1)
  107. model.train(2, dataset, callbacks=summary_cb)
  108. def test_graph_summary_callback2():
  109. dataset = get_dataset()
  110. net = Net()
  111. loss = nn.SoftmaxCrossEntropyWithLogits()
  112. optim = Momentum(net.trainable_params(), 0.1, 0.9)
  113. context.set_context(mode=context.GRAPH_MODE)
  114. model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  115. with SummaryRecord(SUMMARY_DIR, file_suffix="_MS_GRAPH", network=net) as test_writer:
  116. summary_cb = SummaryStep(test_writer, 1)
  117. model.train(2, dataset, callbacks=summary_cb)