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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. os.remove(CV_FILE_NAME) if os.path.exists(CV_FILE_NAME) else None
  25. os.remove("{}.db".format(CV_FILE_NAME)) if os.path.exists("{}.db".format(CV_FILE_NAME)) else None
  26. writer = FileWriter(CV_FILE_NAME, files_num)
  27. cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  28. data = [{"file_name": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  29. writer.add_schema(cv_schema_json, "img_schema")
  30. writer.add_index(["file_name", "label"])
  31. writer.write_raw_data(data)
  32. writer.commit()
  33. def create_diff_schema_cv_mindrecord(files_num):
  34. """tutorial for cv dataset writer."""
  35. os.remove(CV1_FILE_NAME) if os.path.exists(CV1_FILE_NAME) else None
  36. os.remove("{}.db".format(CV1_FILE_NAME)) if os.path.exists("{}.db".format(CV1_FILE_NAME)) else None
  37. writer = FileWriter(CV1_FILE_NAME, files_num)
  38. cv_schema_json = {"file_name_1": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  39. data = [{"file_name_1": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  40. writer.add_schema(cv_schema_json, "img_schema")
  41. writer.add_index(["file_name_1", "label"])
  42. writer.write_raw_data(data)
  43. writer.commit()
  44. def create_diff_page_size_cv_mindrecord(files_num):
  45. """tutorial for cv dataset writer."""
  46. os.remove(CV1_FILE_NAME) if os.path.exists(CV1_FILE_NAME) else None
  47. os.remove("{}.db".format(CV1_FILE_NAME)) if os.path.exists("{}.db".format(CV1_FILE_NAME)) else None
  48. writer = FileWriter(CV1_FILE_NAME, files_num)
  49. writer.set_page_size(1<< 26) #64MB
  50. cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  51. data = [{"file_name": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  52. writer.add_schema(cv_schema_json, "img_schema")
  53. writer.add_index(["file_name", "label"])
  54. writer.write_raw_data(data)
  55. writer.commit()
  56. def test_cv_lack_json():
  57. """tutorial for cv minderdataset."""
  58. create_cv_mindrecord(1)
  59. columns_list = ["data", "file_name", "label"]
  60. num_readers = 4
  61. with pytest.raises(Exception) as err:
  62. data_set = ds.MindDataset(CV_FILE_NAME, "no_exist.json", columns_list, num_readers)
  63. os.remove(CV_FILE_NAME)
  64. os.remove("{}.db".format(CV_FILE_NAME))
  65. def test_cv_lack_mindrecord():
  66. """tutorial for cv minderdataset."""
  67. columns_list = ["data", "file_name", "label"]
  68. num_readers = 4
  69. with pytest.raises(Exception, match="does not exist or permission denied"):
  70. data_set = ds.MindDataset("no_exist.mindrecord", columns_list, num_readers)
  71. def test_invalid_mindrecord():
  72. with open('dummy.mindrecord', 'w') as f:
  73. f.write('just for test')
  74. columns_list = ["data", "file_name", "label"]
  75. num_readers = 4
  76. with pytest.raises(Exception, match="MindRecordOp init failed"):
  77. data_set = ds.MindDataset('dummy.mindrecord', columns_list, num_readers)
  78. num_iter = 0
  79. for item in data_set.create_dict_iterator():
  80. num_iter += 1
  81. assert num_iter == 0
  82. os.remove('dummy.mindrecord')
  83. def test_minddataset_lack_db():
  84. create_cv_mindrecord(1)
  85. os.remove("{}.db".format(CV_FILE_NAME))
  86. columns_list = ["data", "file_name", "label"]
  87. num_readers = 4
  88. with pytest.raises(Exception, match="MindRecordOp init failed"):
  89. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers)
  90. num_iter = 0
  91. for item in data_set.create_dict_iterator():
  92. num_iter += 1
  93. assert num_iter == 0
  94. os.remove(CV_FILE_NAME)
  95. def test_cv_minddataset_pk_sample_error_class_column():
  96. create_cv_mindrecord(1)
  97. columns_list = ["data", "file_name", "label"]
  98. num_readers = 4
  99. sampler = ds.PKSampler(5, None, True, 'no_exsit_column')
  100. with pytest.raises(Exception, match="MindRecordOp launch failed"):
  101. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, sampler=sampler)
  102. num_iter = 0
  103. for item in data_set.create_dict_iterator():
  104. num_iter += 1
  105. os.remove(CV_FILE_NAME)
  106. os.remove("{}.db".format(CV_FILE_NAME))
  107. def test_cv_minddataset_pk_sample_exclusive_shuffle():
  108. create_cv_mindrecord(1)
  109. columns_list = ["data", "file_name", "label"]
  110. num_readers = 4
  111. sampler = ds.PKSampler(2)
  112. with pytest.raises(Exception, match="sampler and shuffle cannot be specified at the same time."):
  113. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers,
  114. sampler=sampler, shuffle=False)
  115. num_iter = 0
  116. for item in data_set.create_dict_iterator():
  117. num_iter += 1
  118. os.remove(CV_FILE_NAME)
  119. os.remove("{}.db".format(CV_FILE_NAME))
  120. def test_cv_minddataset_reader_different_schema():
  121. create_cv_mindrecord(1)
  122. create_diff_schema_cv_mindrecord(1)
  123. columns_list = ["data", "label"]
  124. num_readers = 4
  125. with pytest.raises(Exception, match="MindRecordOp init failed"):
  126. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  127. num_readers)
  128. num_iter = 0
  129. for item in data_set.create_dict_iterator():
  130. num_iter += 1
  131. os.remove(CV_FILE_NAME)
  132. os.remove("{}.db".format(CV_FILE_NAME))
  133. os.remove(CV1_FILE_NAME)
  134. os.remove("{}.db".format(CV1_FILE_NAME))
  135. def test_cv_minddataset_reader_different_page_size():
  136. create_cv_mindrecord(1)
  137. create_diff_page_size_cv_mindrecord(1)
  138. columns_list = ["data", "label"]
  139. num_readers = 4
  140. with pytest.raises(Exception, match="MindRecordOp init failed"):
  141. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  142. num_readers)
  143. num_iter = 0
  144. for item in data_set.create_dict_iterator():
  145. num_iter += 1
  146. os.remove(CV_FILE_NAME)
  147. os.remove("{}.db".format(CV_FILE_NAME))
  148. os.remove(CV1_FILE_NAME)
  149. os.remove("{}.db".format(CV1_FILE_NAME))
  150. def test_minddataset_invalidate_num_shards():
  151. create_cv_mindrecord(1)
  152. columns_list = ["data", "label"]
  153. num_readers = 4
  154. with pytest.raises(Exception, match="shard_id is invalid, "):
  155. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 0, 1)
  156. num_iter = 0
  157. for item in data_set.create_dict_iterator():
  158. num_iter += 1
  159. os.remove(CV_FILE_NAME)
  160. os.remove("{}.db".format(CV_FILE_NAME))
  161. def test_minddataset_invalidate_shard_id():
  162. create_cv_mindrecord(1)
  163. columns_list = ["data", "label"]
  164. num_readers = 4
  165. with pytest.raises(Exception, match="shard_id is invalid, "):
  166. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 1, -1)
  167. num_iter = 0
  168. for item in data_set.create_dict_iterator():
  169. num_iter += 1
  170. os.remove(CV_FILE_NAME)
  171. os.remove("{}.db".format(CV_FILE_NAME))
  172. def test_minddataset_shard_id_bigger_than_num_shard():
  173. create_cv_mindrecord(1)
  174. columns_list = ["data", "label"]
  175. num_readers = 4
  176. with pytest.raises(Exception, match="shard_id is invalid, "):
  177. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 2)
  178. num_iter = 0
  179. for item in data_set.create_dict_iterator():
  180. num_iter += 1
  181. with pytest.raises(Exception, match="shard_id is invalid, "):
  182. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 5)
  183. num_iter = 0
  184. for item in data_set.create_dict_iterator():
  185. num_iter += 1
  186. os.remove(CV_FILE_NAME)
  187. os.remove("{}.db".format(CV_FILE_NAME))