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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. def create_cv_mindrecord(files_num):
  22. """tutorial for cv dataset writer."""
  23. os.remove(CV_FILE_NAME) if os.path.exists(CV_FILE_NAME) else None
  24. os.remove("{}.db".format(CV_FILE_NAME)) if os.path.exists("{}.db".format(CV_FILE_NAME)) else None
  25. writer = FileWriter(CV_FILE_NAME, files_num)
  26. cv_schema_json = {"file_name": {"type": "string"}, "label": {"type": "int32"}, "data": {"type": "bytes"}}
  27. data = [{"file_name": "001.jpg", "label": 43, "data": bytes('0xffsafdafda', encoding='utf-8')}]
  28. writer.add_schema(cv_schema_json, "img_schema")
  29. writer.add_index(["file_name", "label"])
  30. writer.write_raw_data(data)
  31. writer.commit()
  32. def test_cv_lack_json():
  33. """tutorial for cv minderdataset."""
  34. create_cv_mindrecord(1)
  35. columns_list = ["data", "file_name", "label"]
  36. num_readers = 4
  37. with pytest.raises(Exception) as err:
  38. data_set = ds.MindDataset(CV_FILE_NAME, "no_exist.json", columns_list, num_readers)
  39. os.remove(CV_FILE_NAME)
  40. os.remove("{}.db".format(CV_FILE_NAME))
  41. def test_cv_lack_mindrecord():
  42. """tutorial for cv minderdataset."""
  43. columns_list = ["data", "file_name", "label"]
  44. num_readers = 4
  45. with pytest.raises(Exception, match="does not exist or permission denied"):
  46. data_set = ds.MindDataset("no_exist.mindrecord", columns_list, num_readers)
  47. def test_invalid_mindrecord():
  48. with open('dummy.mindrecord', 'w') as f:
  49. f.write('just for test')
  50. columns_list = ["data", "file_name", "label"]
  51. num_readers = 4
  52. with pytest.raises(Exception, match="MindRecordOp init failed"):
  53. data_set = ds.MindDataset('dummy.mindrecord', columns_list, num_readers)
  54. num_iter = 0
  55. for item in data_set.create_dict_iterator():
  56. num_iter += 1
  57. assert num_iter == 0
  58. os.remove('dummy.mindrecord')
  59. def test_minddataset_lack_db():
  60. create_cv_mindrecord(1)
  61. os.remove("{}.db".format(CV_FILE_NAME))
  62. columns_list = ["data", "file_name", "label"]
  63. num_readers = 4
  64. with pytest.raises(Exception, match="MindRecordOp init failed"):
  65. data_set = ds.MindDataset(CV_FILE_NAME, columns_list, num_readers)
  66. num_iter = 0
  67. for item in data_set.create_dict_iterator():
  68. num_iter += 1
  69. assert num_iter == 0
  70. os.remove(CV_FILE_NAME)