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

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