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_cifarop.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright 2019 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 os
  16. import numpy as np
  17. import mindspore.dataset as ds
  18. from mindspore import log as logger
  19. # Data for CIFAR and MNIST are not part of build tree
  20. # They need to be downloaded directly
  21. # prep_data.py can be executed or code below
  22. # import sys
  23. # sys.path.insert(0,"../../data")
  24. # import prep_data
  25. # prep_data.download_all_for_test("../../data")
  26. DATA_DIR_10 = "../data/dataset/testCifar10Data"
  27. DATA_DIR_100 = "../data/dataset/testCifar100Data"
  28. def load_cifar(path):
  29. raw = np.empty(0, dtype=np.uint8)
  30. for file_name in os.listdir(path):
  31. if file_name.endswith(".bin"):
  32. with open(os.path.join(path, file_name), mode='rb') as file:
  33. raw = np.append(raw, np.fromfile(file, dtype=np.uint8), axis=0)
  34. raw = raw.reshape(-1, 3073)
  35. labels = raw[:, 0]
  36. images = raw[:, 1:]
  37. images = images.reshape(-1, 3, 32, 32)
  38. images = images.transpose(0, 2, 3, 1)
  39. return images, labels
  40. def test_case_dataset_cifar10():
  41. """
  42. dataset parameter
  43. """
  44. logger.info("Test dataset parameter")
  45. # apply dataset operations
  46. data1 = ds.Cifar10Dataset(DATA_DIR_10, 100)
  47. num_iter = 0
  48. for _ in data1.create_dict_iterator():
  49. # in this example, each dictionary has keys "image" and "label"
  50. num_iter += 1
  51. assert num_iter == 100
  52. def test_case_dataset_cifar100():
  53. """
  54. dataset parameter
  55. """
  56. logger.info("Test dataset parameter")
  57. # apply dataset operations
  58. data1 = ds.Cifar100Dataset(DATA_DIR_100, 100)
  59. num_iter = 0
  60. for _ in data1.create_dict_iterator():
  61. # in this example, each dictionary has keys "image" and "label"
  62. num_iter += 1
  63. assert num_iter == 100
  64. def test_reading_cifar10():
  65. """
  66. Validate CIFAR10 image readings
  67. """
  68. data1 = ds.Cifar10Dataset(DATA_DIR_10, 100, shuffle=False)
  69. images, labels = load_cifar(DATA_DIR_10)
  70. for i, d in enumerate(data1.create_dict_iterator()):
  71. np.testing.assert_array_equal(d["image"], images[i])
  72. np.testing.assert_array_equal(d["label"], labels[i])
  73. if __name__ == '__main__':
  74. test_case_dataset_cifar10()
  75. test_case_dataset_cifar100()
  76. test_reading_cifar10()