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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. """DSCNN dataset."""
  16. import os
  17. import numpy as np
  18. import mindspore.dataset as de
  19. class NpyDataset():
  20. '''Dataset from numpy.'''
  21. def __init__(self, data_dir, data_type, h, w):
  22. super(NpyDataset, self).__init__()
  23. self.data = np.load(os.path.join(data_dir, '{}_data.npy'.format(data_type)))
  24. self.data = np.reshape(self.data, (-1, 1, h, w))
  25. self.label = np.load(os.path.join(data_dir, '{}_label.npy'.format(data_type)))
  26. def __len__(self):
  27. return self.data.shape[0]
  28. def __getitem__(self, item):
  29. data = self.data[item]
  30. label = self.label[item]
  31. # return data, label
  32. return data.astype(np.float32), label.astype(np.int32)
  33. def audio_dataset(data_dir, data_type, h, w, batch_size):
  34. if 'testing' in data_dir:
  35. shuffle = False
  36. else:
  37. shuffle = True
  38. dataset = NpyDataset(data_dir, data_type, h, w)
  39. de_dataset = de.GeneratorDataset(dataset, ["feats", "labels"], shuffle=shuffle)
  40. de_dataset = de_dataset.batch(batch_size, drop_remainder=False)
  41. return de_dataset