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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 train.py and eval.py
  17. """
  18. import os
  19. import numpy as np
  20. from imdb import ImdbParser
  21. import mindspore.dataset as ds
  22. from mindspore.mindrecord import FileWriter
  23. def create_dataset(data_home, batch_size, repeat_num=1, training=True):
  24. """Data operations."""
  25. ds.config.set_seed(1)
  26. data_dir = os.path.join(data_home, "aclImdb_train.mindrecord0")
  27. if not training:
  28. data_dir = os.path.join(data_home, "aclImdb_test.mindrecord0")
  29. data_set = ds.MindDataset(data_dir, columns_list=["feature", "label"], num_parallel_workers=4)
  30. # apply map operations on images
  31. data_set = data_set.shuffle(buffer_size=data_set.get_dataset_size())
  32. data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
  33. data_set = data_set.repeat(count=repeat_num)
  34. return data_set
  35. def _convert_to_mindrecord(data_home, features, labels, weight_np=None, training=True):
  36. """
  37. convert imdb dataset to mindrecoed dataset
  38. """
  39. if weight_np is not None:
  40. np.savetxt(os.path.join(data_home, 'weight.txt'), weight_np)
  41. # write mindrecord
  42. schema_json = {"id": {"type": "int32"},
  43. "label": {"type": "int32"},
  44. "feature": {"type": "int32", "shape": [-1]}}
  45. data_dir = os.path.join(data_home, "aclImdb_train.mindrecord")
  46. if not training:
  47. data_dir = os.path.join(data_home, "aclImdb_test.mindrecord")
  48. def get_imdb_data(features, labels):
  49. data_list = []
  50. for i, (label, feature) in enumerate(zip(labels, features)):
  51. data_json = {"id": i,
  52. "label": int(label),
  53. "feature": feature.reshape(-1)}
  54. data_list.append(data_json)
  55. return data_list
  56. writer = FileWriter(data_dir, shard_num=4)
  57. data = get_imdb_data(features, labels)
  58. writer.add_schema(schema_json, "nlp_schema")
  59. writer.add_index(["id", "label"])
  60. writer.write_raw_data(data)
  61. writer.commit()
  62. def convert_to_mindrecord(embed_size, aclimdb_path, preprocess_path, glove_path):
  63. """
  64. convert imdb dataset to mindrecoed dataset
  65. """
  66. parser = ImdbParser(aclimdb_path, glove_path, embed_size)
  67. parser.parse()
  68. if not os.path.exists(preprocess_path):
  69. print(f"preprocess path {preprocess_path} is not exist")
  70. os.makedirs(preprocess_path)
  71. train_features, train_labels, train_weight_np = parser.get_datas('train')
  72. _convert_to_mindrecord(preprocess_path, train_features, train_labels, train_weight_np)
  73. test_features, test_labels, _ = parser.get_datas('test')
  74. _convert_to_mindrecord(preprocess_path, test_features, test_labels, training=False)