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_getnext_dynamic_pipeline.py 3.1 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2021 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. import numpy as np
  16. import pytest
  17. from mindspore import nn, context
  18. from mindspore import ops as P
  19. from mindspore.train import DatasetHelper, connect_network_with_dataset
  20. import mindspore.dataset as ds
  21. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  22. def _exec_preprocess(network, is_train, dataset, dataset_sink_mode, sink_size=-1, epoch_num=1, dataset_helper=None):
  23. if dataset_sink_mode and not is_train:
  24. dataset.__loop_size__ = 1
  25. if dataset_helper is None:
  26. dataset_helper = DatasetHelper(dataset, dataset_sink_mode, sink_size, epoch_num)
  27. if dataset_sink_mode:
  28. network = connect_network_with_dataset(network, dataset_helper)
  29. network.set_train(is_train)
  30. return dataset_helper, network
  31. def _eval_dataset_sink_process(network, valid_dataset):
  32. dataset_helper, eval_network = _exec_preprocess(network, is_train=False, dataset=valid_dataset,
  33. dataset_sink_mode=True)
  34. for inputs1, inputs2 in zip(dataset_helper, valid_dataset.create_dict_iterator()):
  35. outputs = eval_network(*inputs1)
  36. for elem1, (_, elem2) in zip(outputs, inputs2.items()):
  37. assert elem1.shape == elem2.shape
  38. def dataset_generator():
  39. for i in range(1, 10):
  40. yield (
  41. np.ones((32, i), dtype=np.float32), np.zeros((32, i, i, 3), dtype=np.int32),
  42. np.ones((32,), dtype=np.float32),
  43. np.ones((32, i, 8), dtype=np.float32), np.ones((32, 8, 8), dtype=np.float32))
  44. class Net(nn.Cell):
  45. def __init__(self):
  46. super(Net, self).__init__()
  47. self.relu = P.ReLU()
  48. def construct(self, x1, x2, x3, x4, x5):
  49. x1 = self.relu(x1)
  50. x1 = self.relu(x1)
  51. x2 = self.relu(x2)
  52. x3 = self.relu(x3)
  53. x3 = self.relu(x3)
  54. x4 = self.relu(x4)
  55. x5 = self.relu(x5)
  56. return x1, x2, x3, x4, x5
  57. @pytest.mark.level0
  58. @pytest.mark.platform_arm_ascend_training
  59. @pytest.mark.platform_x86_ascend_training
  60. @pytest.mark.env_onecard
  61. def test_getnext_dynamic_pipeline():
  62. network = Net()
  63. dataset = ds.GeneratorDataset(dataset_generator, ["data1", "data2", "data3", "data4", "data5"])
  64. dataset.set_dynamic_columns(columns={"data1": [32, None], "data2": [32, None, None, 3],
  65. "data3": [32], "data4": [32, None, 8], "data5": [32, 8, 8]})
  66. _eval_dataset_sink_process(network, dataset)