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_datasets_voc.py 5.8 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 mindspore.dataset as ds
  16. import mindspore.dataset.transforms.vision.c_transforms as vision
  17. DATA_DIR = "../data/dataset/testVOC2012"
  18. IMAGE_SHAPE = [2268, 2268, 2268, 2268, 642, 607, 561, 596, 612, 2268]
  19. TARGET_SHAPE = [680, 680, 680, 680, 642, 607, 561, 596, 612, 680]
  20. def test_voc_segmentation():
  21. data1 = ds.VOCDataset(DATA_DIR, task="Segmentation", mode="train", decode=True, shuffle=False)
  22. num = 0
  23. for item in data1.create_dict_iterator():
  24. assert (item["image"].shape[0] == IMAGE_SHAPE[num])
  25. assert (item["target"].shape[0] == TARGET_SHAPE[num])
  26. num += 1
  27. assert (num == 10)
  28. def test_voc_detection():
  29. data1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True, shuffle=False)
  30. num = 0
  31. count = [0, 0, 0, 0, 0, 0]
  32. for item in data1.create_dict_iterator():
  33. assert (item["image"].shape[0] == IMAGE_SHAPE[num])
  34. for bbox in item["annotation"]:
  35. count[bbox[0]] += 1
  36. num += 1
  37. assert (num == 9)
  38. assert (count == [3, 2, 1, 2, 4, 3])
  39. def test_voc_class_index():
  40. class_index = {'car': 0, 'cat': 1, 'train': 5}
  41. data1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", class_indexing=class_index, decode=True)
  42. class_index1 = data1.get_class_indexing()
  43. assert (class_index1 == {'car': 0, 'cat': 1, 'train': 5})
  44. data1 = data1.shuffle(4)
  45. class_index2 = data1.get_class_indexing()
  46. assert (class_index2 == {'car': 0, 'cat': 1, 'train': 5})
  47. num = 0
  48. count = [0, 0, 0, 0, 0, 0]
  49. for item in data1.create_dict_iterator():
  50. for bbox in item["annotation"]:
  51. assert (bbox[0] == 0 or bbox[0] == 1 or bbox[0] == 5)
  52. count[bbox[0]] += 1
  53. num += 1
  54. assert (num == 6)
  55. assert (count == [3, 2, 0, 0, 0, 3])
  56. def test_voc_get_class_indexing():
  57. data1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True)
  58. class_index1 = data1.get_class_indexing()
  59. assert (class_index1 == {'car': 0, 'cat': 1, 'chair': 2, 'dog': 3, 'person': 4, 'train': 5})
  60. data1 = data1.shuffle(4)
  61. class_index2 = data1.get_class_indexing()
  62. assert (class_index2 == {'car': 0, 'cat': 1, 'chair': 2, 'dog': 3, 'person': 4, 'train': 5})
  63. num = 0
  64. count = [0, 0, 0, 0, 0, 0]
  65. for item in data1.create_dict_iterator():
  66. for bbox in item["annotation"]:
  67. assert (bbox[0] == 0 or bbox[0] == 1 or bbox[0] == 2 or bbox[0] == 3 or bbox[0] == 4 or bbox[0] == 5)
  68. count[bbox[0]] += 1
  69. num += 1
  70. assert (num == 9)
  71. assert (count == [3, 2, 1, 2, 4, 3])
  72. def test_case_0():
  73. data1 = ds.VOCDataset(DATA_DIR, task="Segmentation", mode="train", decode=True)
  74. resize_op = vision.Resize((224, 224))
  75. data1 = data1.map(input_columns=["image"], operations=resize_op)
  76. data1 = data1.map(input_columns=["target"], operations=resize_op)
  77. repeat_num = 4
  78. data1 = data1.repeat(repeat_num)
  79. batch_size = 2
  80. data1 = data1.batch(batch_size, drop_remainder=True)
  81. num = 0
  82. for item in data1.create_dict_iterator():
  83. num += 1
  84. assert (num == 20)
  85. def test_case_1():
  86. data1 = ds.VOCDataset(DATA_DIR, task="Detection", mode="train", decode=True)
  87. resize_op = vision.Resize((224, 224))
  88. data1 = data1.map(input_columns=["image"], operations=resize_op)
  89. repeat_num = 4
  90. data1 = data1.repeat(repeat_num)
  91. batch_size = 2
  92. data1 = data1.batch(batch_size, drop_remainder=True, pad_info={})
  93. num = 0
  94. for item in data1.create_dict_iterator():
  95. num += 1
  96. assert (num == 18)
  97. def test_voc_exception():
  98. try:
  99. data1 = ds.VOCDataset(DATA_DIR, task="InvalidTask", mode="train", decode=True)
  100. for _ in data1.create_dict_iterator():
  101. pass
  102. assert False
  103. except ValueError:
  104. pass
  105. try:
  106. data2 = ds.VOCDataset(DATA_DIR, task="Segmentation", mode="train", class_indexing={"cat": 0}, decode=True)
  107. for _ in data2.create_dict_iterator():
  108. pass
  109. assert False
  110. except ValueError:
  111. pass
  112. try:
  113. data3 = ds.VOCDataset(DATA_DIR, task="Detection", mode="notexist", decode=True)
  114. for _ in data3.create_dict_iterator():
  115. pass
  116. assert False
  117. except ValueError:
  118. pass
  119. try:
  120. data4 = ds.VOCDataset(DATA_DIR, task="Detection", mode="xmlnotexist", decode=True)
  121. for _ in data4.create_dict_iterator():
  122. pass
  123. assert False
  124. except RuntimeError:
  125. pass
  126. try:
  127. data5 = ds.VOCDataset(DATA_DIR, task="Detection", mode="invalidxml", decode=True)
  128. for _ in data5.create_dict_iterator():
  129. pass
  130. assert False
  131. except RuntimeError:
  132. pass
  133. try:
  134. data6 = ds.VOCDataset(DATA_DIR, task="Detection", mode="xmlnoobject", decode=True)
  135. for _ in data6.create_dict_iterator():
  136. pass
  137. assert False
  138. except RuntimeError:
  139. pass
  140. if __name__ == '__main__':
  141. test_voc_segmentation()
  142. test_voc_detection()
  143. test_voc_class_index()
  144. test_voc_get_class_indexing()
  145. test_case_0()
  146. test_case_1()
  147. test_voc_exception()