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 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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(num_epochs=1, output_numpy=True):
  86. num_iter += 1
  87. try:
  88. assert num_iter == 0
  89. except Exception as error:
  90. os.remove('dummy.mindrecord')
  91. raise error
  92. else:
  93. os.remove('dummy.mindrecord')
  94. def test_minddataset_lack_db():
  95. create_cv_mindrecord(1)
  96. os.remove("{}.db".format(CV_FILE_NAME))
  97. columns_list = ["data", "file_name", "label"]
  98. num_readers = 4
  99. with pytest.raises(Exception, match="MindRecordOp init failed"):
  100. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers)
  101. num_iter = 0
  102. for _ in data_set.create_dict_iterator(num_epochs=1, output_numpy=True):
  103. num_iter += 1
  104. try:
  105. assert num_iter == 0
  106. except Exception as error:
  107. os.remove(CV_FILE_NAME)
  108. raise error
  109. else:
  110. os.remove(CV_FILE_NAME)
  111. def test_cv_minddataset_pk_sample_error_class_column():
  112. create_cv_mindrecord(1)
  113. columns_list = ["data", "file_name", "label"]
  114. num_readers = 4
  115. sampler = ds.PKSampler(5, None, True, 'no_exist_column')
  116. with pytest.raises(Exception, match="MindRecordOp launch failed"):
  117. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, sampler=sampler)
  118. num_iter = 0
  119. for _ in data_set.create_dict_iterator(num_epochs=1, output_numpy=True):
  120. num_iter += 1
  121. os.remove(CV_FILE_NAME)
  122. os.remove("{}.db".format(CV_FILE_NAME))
  123. def test_cv_minddataset_pk_sample_exclusive_shuffle():
  124. create_cv_mindrecord(1)
  125. columns_list = ["data", "file_name", "label"]
  126. num_readers = 4
  127. sampler = ds.PKSampler(2)
  128. with pytest.raises(Exception, match="sampler and shuffle cannot be specified at the same time."):
  129. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers,
  130. sampler=sampler, shuffle=False)
  131. num_iter = 0
  132. for _ in data_set.create_dict_iterator(num_epochs=1, output_numpy=True):
  133. num_iter += 1
  134. os.remove(CV_FILE_NAME)
  135. os.remove("{}.db".format(CV_FILE_NAME))
  136. def test_cv_minddataset_reader_different_schema():
  137. create_cv_mindrecord(1)
  138. create_diff_schema_cv_mindrecord(1)
  139. columns_list = ["data", "label"]
  140. num_readers = 4
  141. with pytest.raises(Exception, match="MindRecordOp init failed"):
  142. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  143. num_readers)
  144. num_iter = 0
  145. for _ in data_set.create_dict_iterator(num_epochs=1):
  146. num_iter += 1
  147. os.remove(CV_FILE_NAME)
  148. os.remove("{}.db".format(CV_FILE_NAME))
  149. os.remove(CV1_FILE_NAME)
  150. os.remove("{}.db".format(CV1_FILE_NAME))
  151. def test_cv_minddataset_reader_different_page_size():
  152. create_cv_mindrecord(1)
  153. create_diff_page_size_cv_mindrecord(1)
  154. columns_list = ["data", "label"]
  155. num_readers = 4
  156. with pytest.raises(Exception, match="MindRecordOp init failed"):
  157. data_set = ds.MindDataset([CV_FILE_NAME, CV1_FILE_NAME], columns_list,
  158. num_readers)
  159. num_iter = 0
  160. for _ in data_set.create_dict_iterator(num_epochs=1):
  161. num_iter += 1
  162. os.remove(CV_FILE_NAME)
  163. os.remove("{}.db".format(CV_FILE_NAME))
  164. os.remove(CV1_FILE_NAME)
  165. os.remove("{}.db".format(CV1_FILE_NAME))
  166. def test_minddataset_invalidate_num_shards():
  167. create_cv_mindrecord(1)
  168. columns_list = ["data", "label"]
  169. num_readers = 4
  170. with pytest.raises(Exception) as error_info:
  171. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 1, 2)
  172. num_iter = 0
  173. for _ in data_set.create_dict_iterator(num_epochs=1):
  174. num_iter += 1
  175. try:
  176. assert 'Input shard_id is not within the required interval of (0 to 0).' in str(error_info.value)
  177. except Exception as error:
  178. os.remove(CV_FILE_NAME)
  179. os.remove("{}.db".format(CV_FILE_NAME))
  180. raise error
  181. else:
  182. os.remove(CV_FILE_NAME)
  183. os.remove("{}.db".format(CV_FILE_NAME))
  184. def test_minddataset_invalidate_shard_id():
  185. create_cv_mindrecord(1)
  186. columns_list = ["data", "label"]
  187. num_readers = 4
  188. with pytest.raises(Exception) as error_info:
  189. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 1, -1)
  190. num_iter = 0
  191. for _ in data_set.create_dict_iterator(num_epochs=1):
  192. num_iter += 1
  193. try:
  194. assert 'Input shard_id is not within the required interval of (0 to 0).' in str(error_info.value)
  195. except Exception as error:
  196. os.remove(CV_FILE_NAME)
  197. os.remove("{}.db".format(CV_FILE_NAME))
  198. raise error
  199. else:
  200. os.remove(CV_FILE_NAME)
  201. os.remove("{}.db".format(CV_FILE_NAME))
  202. def test_minddataset_shard_id_bigger_than_num_shard():
  203. create_cv_mindrecord(1)
  204. columns_list = ["data", "label"]
  205. num_readers = 4
  206. with pytest.raises(Exception) as error_info:
  207. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 2)
  208. num_iter = 0
  209. for _ in data_set.create_dict_iterator(num_epochs=1):
  210. num_iter += 1
  211. try:
  212. assert 'Input shard_id is not within the required interval of (0 to 1).' in str(error_info.value)
  213. except Exception as error:
  214. os.remove(CV_FILE_NAME)
  215. os.remove("{}.db".format(CV_FILE_NAME))
  216. raise error
  217. with pytest.raises(Exception) as error_info:
  218. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers, True, 2, 5)
  219. num_iter = 0
  220. for _ in data_set.create_dict_iterator(num_epochs=1):
  221. num_iter += 1
  222. try:
  223. assert 'Input shard_id is not within the required interval of (0 to 1).' in str(error_info.value)
  224. except Exception as error:
  225. os.remove(CV_FILE_NAME)
  226. os.remove("{}.db".format(CV_FILE_NAME))
  227. raise error
  228. else:
  229. os.remove(CV_FILE_NAME)
  230. os.remove("{}.db".format(CV_FILE_NAME))
  231. def test_cv_minddataset_partition_num_samples_equals_0():
  232. """tutorial for cv minddataset."""
  233. create_cv_mindrecord(1)
  234. columns_list = ["data", "label"]
  235. num_readers = 4
  236. def partitions(num_shards):
  237. for partition_id in range(num_shards):
  238. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers,
  239. num_shards=num_shards,
  240. shard_id=partition_id, num_samples=0)
  241. num_iter = 0
  242. for _ in data_set.create_dict_iterator(num_epochs=1):
  243. num_iter += 1
  244. with pytest.raises(Exception) as error_info:
  245. partitions(5)
  246. try:
  247. assert 'num_samples should be a positive integer value, but got num_samples: 0.' in str(error_info.value)
  248. except Exception as error:
  249. os.remove(CV_FILE_NAME)
  250. os.remove("{}.db".format(CV_FILE_NAME))
  251. raise error
  252. else:
  253. os.remove(CV_FILE_NAME)
  254. os.remove("{}.db".format(CV_FILE_NAME))
  255. if __name__ == '__main__':
  256. test_cv_lack_json()
  257. test_cv_lack_mindrecord()
  258. test_invalid_mindrecord()
  259. test_minddataset_lack_db()
  260. test_cv_minddataset_pk_sample_error_class_column()
  261. test_cv_minddataset_pk_sample_exclusive_shuffle()
  262. test_cv_minddataset_reader_different_schema()
  263. test_cv_minddataset_reader_different_page_size()
  264. test_minddataset_invalidate_num_shards()
  265. test_minddataset_invalidate_shard_id()
  266. test_minddataset_shard_id_bigger_than_num_shard()
  267. test_cv_minddataset_partition_num_samples_equals_0()