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_minddataset_exception.py 8.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python
  2. # Copyright 2019 Huawei Technologies Co., Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ==============================================================================
  16. import os
  17. import pytest
  18. import mindspore.dataset as ds
  19. from mindspore.mindrecord import FileWriter
  20. CV_FILE_NAME = "./imagenet.mindrecord"
  21. CV1_FILE_NAME = "./imagenet1.mindrecord"
  22. def create_cv_mindrecord(files_num):
  23. """tutorial for cv dataset writer."""
  24. if os.path.exists(CV_FILE_NAME):
  25. os.remove(CV_FILE_NAME)
  26. if os.path.exists("{}.db".format(CV_FILE_NAME)):
  27. os.remove("{}.db".format(CV_FILE_NAME))
  28. writer = FileWriter(CV_FILE_NAME, files_num)
  29. cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  30. data = [{"file_name": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  31. writer.add_schema(cv_schema_json, "img_schema")
  32. writer.add_index(["file_name", "label"])
  33. writer.write_raw_data(data)
  34. writer.commit()
  35. def create_diff_schema_cv_mindrecord(files_num):
  36. """tutorial for cv dataset writer."""
  37. if os.path.exists(CV1_FILE_NAME):
  38. os.remove(CV1_FILE_NAME)
  39. if os.path.exists("{}.db".format(CV1_FILE_NAME)):
  40. os.remove("{}.db".format(CV1_FILE_NAME))
  41. writer = FileWriter(CV1_FILE_NAME, files_num)
  42. cv_schema_json = {"file_name_1": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  43. data = [{"file_name_1": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  44. writer.add_schema(cv_schema_json, "img_schema")
  45. writer.add_index(["file_name_1", "label"])
  46. writer.write_raw_data(data)
  47. writer.commit()
  48. def create_diff_page_size_cv_mindrecord(files_num):
  49. """tutorial for cv dataset writer."""
  50. if os.path.exists(CV1_FILE_NAME):
  51. os.remove(CV1_FILE_NAME)
  52. if os.path.exists("{}.db".format(CV1_FILE_NAME)):
  53. os.remove("{}.db".format(CV1_FILE_NAME))
  54. writer = FileWriter(CV1_FILE_NAME, files_num)
  55. writer.set_page_size(1 << 26) # 64MB
  56. cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  57. data = [{"file_name": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  58. writer.add_schema(cv_schema_json, "img_schema")
  59. writer.add_index(["file_name", "label"])
  60. writer.write_raw_data(data)
  61. writer.commit()
  62. def test_cv_lack_json():
  63. """tutorial for cv minderdataset."""
  64. create_cv_mindrecord(1)
  65. columns_list = ["data", "file_name", "label"]
  66. num_readers = 4
  67. with pytest.raises(Exception):
  68. ds.MindDataset(CV_FILE_NAME, "no_exist.json", columns_list, num_readers)
  69. os.remove(CV_FILE_NAME)
  70. os.remove("{}.db".format(CV_FILE_NAME))
  71. def test_cv_lack_mindrecord():
  72. """tutorial for cv minderdataset."""
  73. columns_list = ["data", "file_name", "label"]
  74. num_readers = 4
  75. with pytest.raises(Exception, match="does not exist or permission denied"):
  76. _ = ds.MindDataset("no_exist.mindrecord", columns_list, num_readers)
  77. def test_invalid_mindrecord():
  78. with open('dummy.mindrecord', 'w') as f:
  79. f.write('just for test')
  80. columns_list = ["data", "file_name", "label"]
  81. num_readers = 4
  82. with pytest.raises(Exception, match="MindRecordOp init failed"):
  83. data_set = ds.MindDataset('dummy.mindrecord', columns_list, num_readers)
  84. num_iter = 0
  85. for _ in data_set.create_dict_iterator():
  86. num_iter += 1
  87. assert num_iter == 0
  88. os.remove('dummy.mindrecord')
  89. def test_minddataset_lack_db():
  90. create_cv_mindrecord(1)
  91. os.remove("{}.db".format(CV_FILE_NAME))
  92. columns_list = ["data", "file_name", "label"]
  93. num_readers = 4
  94. with pytest.raises(Exception, match="MindRecordOp init failed"):
  95. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers)
  96. num_iter = 0
  97. for _ in data_set.create_dict_iterator():
  98. num_iter += 1
  99. assert num_iter == 0
  100. os.remove(CV_FILE_NAME)
  101. def test_cv_minddataset_pk_sample_error_class_column():
  102. create_cv_mindrecord(1)
  103. columns_list = ["data", "file_name", "label"]
  104. num_readers = 4
  105. sampler = ds.PKSampler(5, None, True, 'no_exsit_column')
  106. with pytest.raises(Exception, match="MindRecordOp launch failed"):
  107. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, sampler=sampler)
  108. num_iter = 0
  109. for _ in data_set.create_dict_iterator():
  110. num_iter += 1
  111. os.remove(CV_FILE_NAME)
  112. os.remove("{}.db".format(CV_FILE_NAME))
  113. def test_cv_minddataset_pk_sample_exclusive_shuffle():
  114. create_cv_mindrecord(1)
  115. columns_list = ["data", "file_name", "label"]
  116. num_readers = 4
  117. sampler = ds.PKSampler(2)
  118. with pytest.raises(Exception, match="sampler and shuffle cannot be specified at the same time."):
  119. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers,
  120. sampler=sampler, shuffle=False)
  121. num_iter = 0
  122. for _ in data_set.create_dict_iterator():
  123. num_iter += 1
  124. os.remove(CV_FILE_NAME)
  125. os.remove("{}.db".format(CV_FILE_NAME))
  126. def test_cv_minddataset_reader_different_schema():
  127. create_cv_mindrecord(1)
  128. create_diff_schema_cv_mindrecord(1)
  129. columns_list = ["data", "label"]
  130. num_readers = 4
  131. with pytest.raises(Exception, match="MindRecordOp init failed"):
  132. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  133. num_readers)
  134. num_iter = 0
  135. for _ in data_set.create_dict_iterator():
  136. num_iter += 1
  137. os.remove(CV_FILE_NAME)
  138. os.remove("{}.db".format(CV_FILE_NAME))
  139. os.remove(CV1_FILE_NAME)
  140. os.remove("{}.db".format(CV1_FILE_NAME))
  141. def test_cv_minddataset_reader_different_page_size():
  142. create_cv_mindrecord(1)
  143. create_diff_page_size_cv_mindrecord(1)
  144. columns_list = ["data", "label"]
  145. num_readers = 4
  146. with pytest.raises(Exception, match="MindRecordOp init failed"):
  147. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  148. num_readers)
  149. num_iter = 0
  150. for _ in data_set.create_dict_iterator():
  151. num_iter += 1
  152. os.remove(CV_FILE_NAME)
  153. os.remove("{}.db".format(CV_FILE_NAME))
  154. os.remove(CV1_FILE_NAME)
  155. os.remove("{}.db".format(CV1_FILE_NAME))
  156. def test_minddataset_invalidate_num_shards():
  157. create_cv_mindrecord(1)
  158. columns_list = ["data", "label"]
  159. num_readers = 4
  160. with pytest.raises(Exception, match="shard_id is invalid, "):
  161. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 0, 1)
  162. num_iter = 0
  163. for _ in data_set.create_dict_iterator():
  164. num_iter += 1
  165. os.remove(CV_FILE_NAME)
  166. os.remove("{}.db".format(CV_FILE_NAME))
  167. def test_minddataset_invalidate_shard_id():
  168. create_cv_mindrecord(1)
  169. columns_list = ["data", "label"]
  170. num_readers = 4
  171. with pytest.raises(Exception, match="shard_id is invalid, "):
  172. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 1, -1)
  173. num_iter = 0
  174. for _ in data_set.create_dict_iterator():
  175. num_iter += 1
  176. os.remove(CV_FILE_NAME)
  177. os.remove("{}.db".format(CV_FILE_NAME))
  178. def test_minddataset_shard_id_bigger_than_num_shard():
  179. create_cv_mindrecord(1)
  180. columns_list = ["data", "label"]
  181. num_readers = 4
  182. with pytest.raises(Exception, match="shard_id is invalid, "):
  183. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 2)
  184. num_iter = 0
  185. for _ in data_set.create_dict_iterator():
  186. num_iter += 1
  187. with pytest.raises(Exception, match="shard_id is invalid, "):
  188. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 5)
  189. num_iter = 0
  190. for _ in data_set.create_dict_iterator():
  191. num_iter += 1
  192. os.remove(CV_FILE_NAME)
  193. os.remove("{}.db".format(CV_FILE_NAME))