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.

dataset.py 7.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. """
  16. Data operations, will be used in run_pretrain.py
  17. """
  18. import os
  19. import mindspore.common.dtype as mstype
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.c_transforms as C
  22. from mindspore import log as logger
  23. from .config import cfg
  24. def create_bert_dataset(device_num=1, rank=0, do_shuffle="true", data_dir=None, schema_dir=None):
  25. """create train dataset"""
  26. # apply repeat operations
  27. files = os.listdir(data_dir)
  28. data_files = []
  29. for file_name in files:
  30. if "tfrecord" in file_name:
  31. data_files.append(os.path.join(data_dir, file_name))
  32. data_set = ds.TFRecordDataset(data_files, schema_dir if schema_dir != "" else None,
  33. columns_list=["input_ids", "input_mask", "segment_ids", "next_sentence_labels",
  34. "masked_lm_positions", "masked_lm_ids", "masked_lm_weights"],
  35. shuffle=ds.Shuffle.FILES if do_shuffle == "true" else False,
  36. num_shards=device_num, shard_id=rank, shard_equal_rows=True)
  37. ori_dataset_size = data_set.get_dataset_size()
  38. print('origin dataset size: ', ori_dataset_size)
  39. type_cast_op = C.TypeCast(mstype.int32)
  40. data_set = data_set.map(operations=type_cast_op, input_columns="masked_lm_ids")
  41. data_set = data_set.map(operations=type_cast_op, input_columns="masked_lm_positions")
  42. data_set = data_set.map(operations=type_cast_op, input_columns="next_sentence_labels")
  43. data_set = data_set.map(operations=type_cast_op, input_columns="segment_ids")
  44. data_set = data_set.map(operations=type_cast_op, input_columns="input_mask")
  45. data_set = data_set.map(operations=type_cast_op, input_columns="input_ids")
  46. # apply batch operations
  47. data_set = data_set.batch(cfg.batch_size, drop_remainder=True)
  48. logger.info("data size: {}".format(data_set.get_dataset_size()))
  49. logger.info("repeat count: {}".format(data_set.get_repeat_count()))
  50. return data_set
  51. def create_ner_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy", data_file_path=None,
  52. dataset_format="mindrecord", schema_file_path=None, do_shuffle=True):
  53. """create finetune or evaluation dataset"""
  54. type_cast_op = C.TypeCast(mstype.int32)
  55. if dataset_format == "mindrecord":
  56. dataset = ds.MindDataset([data_file_path],
  57. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"],
  58. shuffle=do_shuffle)
  59. else:
  60. dataset = ds.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  61. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"],
  62. shuffle=do_shuffle)
  63. if assessment_method == "Spearman_correlation":
  64. type_cast_op_float = C.TypeCast(mstype.float32)
  65. dataset = dataset.map(operations=type_cast_op_float, input_columns="label_ids")
  66. else:
  67. dataset = dataset.map(operations=type_cast_op, input_columns="label_ids")
  68. dataset = dataset.map(operations=type_cast_op, input_columns="segment_ids")
  69. dataset = dataset.map(operations=type_cast_op, input_columns="input_mask")
  70. dataset = dataset.map(operations=type_cast_op, input_columns="input_ids")
  71. dataset = dataset.repeat(repeat_count)
  72. # apply batch operations
  73. dataset = dataset.batch(batch_size, drop_remainder=True)
  74. return dataset
  75. def create_classification_dataset(batch_size=1, repeat_count=1, assessment_method="accuracy",
  76. data_file_path=None, schema_file_path=None, do_shuffle=True):
  77. """create finetune or evaluation dataset"""
  78. type_cast_op = C.TypeCast(mstype.int32)
  79. data_set = ds.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  80. columns_list=["input_ids", "input_mask", "segment_ids", "label_ids"],
  81. shuffle=do_shuffle)
  82. if assessment_method == "Spearman_correlation":
  83. type_cast_op_float = C.TypeCast(mstype.float32)
  84. data_set = data_set.map(operations=type_cast_op_float, input_columns="label_ids")
  85. else:
  86. data_set = data_set.map(operations=type_cast_op, input_columns="label_ids")
  87. data_set = data_set.map(operations=type_cast_op, input_columns="segment_ids")
  88. data_set = data_set.map(operations=type_cast_op, input_columns="input_mask")
  89. data_set = data_set.map(operations=type_cast_op, input_columns="input_ids")
  90. data_set = data_set.repeat(repeat_count)
  91. # apply batch operations
  92. data_set = data_set.batch(batch_size, drop_remainder=True)
  93. return data_set
  94. def generator_squad(data_features):
  95. for feature in data_features:
  96. yield (feature.input_ids, feature.input_mask, feature.segment_ids, feature.unique_id)
  97. def create_squad_dataset(batch_size=1, repeat_count=1, data_file_path=None, schema_file_path=None,
  98. is_training=True, do_shuffle=True):
  99. """create finetune or evaluation dataset"""
  100. type_cast_op = C.TypeCast(mstype.int32)
  101. if is_training:
  102. data_set = ds.TFRecordDataset([data_file_path], schema_file_path if schema_file_path != "" else None,
  103. columns_list=["input_ids", "input_mask", "segment_ids", "start_positions",
  104. "end_positions", "unique_ids", "is_impossible"],
  105. shuffle=do_shuffle)
  106. data_set = data_set.map(operations=type_cast_op, input_columns="start_positions")
  107. data_set = data_set.map(operations=type_cast_op, input_columns="end_positions")
  108. else:
  109. data_set = ds.GeneratorDataset(generator_squad(data_file_path), shuffle=do_shuffle,
  110. column_names=["input_ids", "input_mask", "segment_ids", "unique_ids"])
  111. data_set = data_set.map(operations=type_cast_op, input_columns="segment_ids")
  112. data_set = data_set.map(operations=type_cast_op, input_columns="input_mask")
  113. data_set = data_set.map(operations=type_cast_op, input_columns="input_ids")
  114. data_set = data_set.map(operations=type_cast_op, input_columns="unique_ids")
  115. data_set = data_set.repeat(repeat_count)
  116. # apply batch operations
  117. data_set = data_set.batch(batch_size, drop_remainder=True)
  118. return data_set