• 提供了2183张图片,其中训练集1693张,验证集245,测试集245张。
    • 包含7种昆虫,分别是Boerner、Leconte、Linnaeus、acuminatus、armandi、coleoptera和linnaeus。
    • 包含了图片和标注,请读者先将数据解压,并存放在insects目录下。

    将数据解压之后,可以看到insects目录下的结构如下所示。

    1. |---train
    2. | |---annotations
    3. | | |---xmls
    4. | | |---100.xml
    5. | | |---101.xml
    6. | | |---...
    7. | |
    8. | |---images
    9. | |---100.jpeg
    10. | |---101.jpeg
    11. | |---...
    12. |
    13. |---val
    14. | |---annotations
    15. | | |---xmls
    16. | | |---1221.xml
    17. | | |---1277.xml
    18. | | |---...
    19. | |
    20. | |---images
    21. | |---1221.jpeg
    22. | |---1277.jpeg
    23. | |---...
    24. |
    25. |---test
    26. |---images
    27. |---1833.jpeg
    28. |---1838.jpeg
    29. |---...

    insects包含train、val和test三个文件夹。train/annotations/xmls目录下存放着图片的标注。每个xml文件是对一张图片的说明,包括图片尺寸、包含的昆虫名称、在图片上出现的位置等信息。

    1. <annotation>
    2. <folder>刘霏霏</folder>
    3. <filename>100.jpeg</filename>
    4. <path>/home/fion/桌面/刘霏霏/100.jpeg</path>
    5. <source>
    6. <database>Unknown</database>
    7. </source>
    8. <size>
    9. <width>1336</width>
    10. <height>1336</height>
    11. <depth>3</depth>
    12. </size>
    13. <segmented>0</segmented>
    14. <object>
    15. <name>Boerner</name>
    16. <pose>Unspecified</pose>
    17. <truncated>0</truncated>
    18. <difficult>0</difficult>
    19. <bndbox>
    20. <xmin>500</xmin>
    21. <ymin>893</ymin>
    22. <xmax>656</xmax>
    23. <ymax>966</ymax>
    24. </bndbox>
    25. </object>
    26. <object>
    27. <name>Leconte</name>
    28. <pose>Unspecified</pose>
    29. <truncated>0</truncated>
    30. <difficult>0</difficult>
    31. <bndbox>
    32. <xmin>622</xmin>
    33. <ymin>490</ymin>
    34. <ymax>610</ymax>
    35. </bndbox>
    36. </object>
    37. <object>
    38. <name>armandi</name>
    39. <truncated>0</truncated>
    40. <difficult>0</difficult>
    41. <bndbox>
    42. <xmin>432</xmin>
    43. <ymin>663</ymin>
    44. <xmax>517</xmax>
    45. <ymax>729</ymax>
    46. </bndbox>
    47. </object>
    48. <object>
    49. <name>coleoptera</name>
    50. <pose>Unspecified</pose>
    51. <truncated>0</truncated>
    52. <difficult>0</difficult>
    53. <bndbox>
    54. <xmin>624</xmin>
    55. <ymin>685</ymin>
    56. <xmax>697</xmax>
    57. <ymax>771</ymax>
    58. </bndbox>
    59. </object>
    60. <object>
    61. <name>linnaeus</name>
    62. <pose>Unspecified</pose>
    63. <truncated>0</truncated>
    64. <difficult>0</difficult>
    65. <bndbox>
    66. <xmin>783</xmin>
    67. <ymin>700</ymin>
    68. <xmax>856</xmax>
    69. <ymax>802</ymax>
    70. </bndbox>
    71. </object>
    72. </annotation>

    -size:图片尺寸

    -object:图片中包含的物体,一张图片可能中包含多个物体

    • name:昆虫名称
    • bndbox:物体真实框
    • difficult:识别是否困难
    1. INSECT_NAMES = ['Boerner', 'Leconte', 'Linnaeus',
    2. 'acuminatus', 'armandi', 'coleoptera', 'linnaeus']
    3. def get_insect_names():
    4. """
    5. return a dict, as following,
    6. {'Boerner': 0,
    7. 'Leconte': 1,
    8. 'Linnaeus': 2,
    9. 'acuminatus': 3,
    10. 'armandi': 4,
    11. 'coleoptera': 5,
    12. 'linnaeus': 6
    13. }
    14. It can map the insect name into an integer label.
    15. """
    16. insect_category2id = {}
    17. for i, item in enumerate(INSECT_NAMES):
    18. insect_category2id[item] = i
    19. return insect_category2id
    1. {'Boerner': 0,
    2. 'Leconte': 1,
    3. 'Linnaeus': 2,
    4. 'acuminatus': 3,
    5. 'armandi': 4,
    6. 'coleoptera': 5,
    7. 'linnaeus': 6}

    调用get_insect_names函数返回一个dict,其键-值对描述了昆虫名称-数字类别之间的映射关系。

    下面的程序从annotations/xml目录下面读取所有文件标注信息。

    1. import os
    2. import xml.etree.ElementTree as ET
    3. def get_annotations(cname2cid, datadir):
    4. filenames = os.listdir(os.path.join(datadir, 'annotations', 'xmls'))
    5. records = []
    6. for fname in filenames:
    7. fid = fname.split('.')[0]
    8. fpath = os.path.join(datadir, 'annotations', 'xmls', fname)
    9. img_file = os.path.join(datadir, 'images', fid + '.jpeg')
    10. tree = ET.parse(fpath)
    11. if tree.find('id') is None:
    12. im_id = np.array([ct])
    13. else:
    14. im_id = np.array([int(tree.find('id').text)])
    15. objs = tree.findall('object')
    16. im_w = float(tree.find('size').find('width').text)
    17. im_h = float(tree.find('size').find('height').text)
    18. gt_bbox = np.zeros((len(objs), 4), dtype=np.float32)
    19. gt_class = np.zeros((len(objs), ), dtype=np.int32)
    20. is_crowd = np.zeros((len(objs), ), dtype=np.int32)
    21. difficult = np.zeros((len(objs), ), dtype=np.int32)
    22. for i, obj in enumerate(objs):
    23. cname = obj.find('name').text
    24. gt_class[i] = cname2cid[cname]
    25. _difficult = int(obj.find('difficult').text)
    26. x1 = float(obj.find('bndbox').find('xmin').text)
    27. y1 = float(obj.find('bndbox').find('ymin').text)
    28. x2 = float(obj.find('bndbox').find('xmax').text)
    29. y2 = float(obj.find('bndbox').find('ymax').text)
    30. x1 = max(0, x1)
    31. y1 = max(0, y1)
    32. x2 = min(im_w - 1, x2)
    33. y2 = min(im_h - 1, y2)
    34. # 这里使用xywh格式来表示目标物体真实框
    35. gt_bbox[i] = [(x1+x2)/2.0 , (y1+y2)/2.0, x2-x1+1., y2-y1+1.]
    36. is_crowd[i] = 0
    37. difficult[i] = _difficult
    38. voc_rec = {
    39. 'im_file': img_file,
    40. 'im_id': im_id,
    41. 'h': im_h,
    42. 'w': im_w,
    43. 'is_crowd': is_crowd,
    44. 'gt_class': gt_class,
    45. 'gt_bbox': gt_bbox,
    46. 'gt_poly': [],
    47. 'difficult': difficult
    48. }
    49. if len(objs) != 0:
    50. records.append(voc_rec)
    51. ct += 1
    52. return records
    1. TRAINDIR = '/home/aistudio/work/insects/train'
    2. TESTDIR = '/home/aistudio/work/insects/test'
    3. VALIDDIR = '/home/aistudio/work/insects/val'
    4. cname2cid = get_insect_names()
    5. records = get_annotations(cname2cid, TRAINDIR)
    1. 1693
    1. records[0]
    1. {'im_file': '/home/aistudio/work/insects/train/images/1915.jpeg',
    2. 'im_id': array([0]),
    3. 'h': 1268.0,
    4. 'w': 1268.0,
    5. 'is_crowd': array([0, 0, 0, 0, 0, 0, 0], dtype=int32),
    6. 'gt_class': array([1, 0, 2, 3, 4, 5, 5], dtype=int32),
    7. 'gt_bbox': array([[411.5, 583. , 142. , 199. ],
    8. [654.5, 418.5, 128. , 132. ],
    9. [678.5, 736.5, 70. , 90. ],
    10. [758. , 647.5, 53. , 76. ],
    11. [843. , 538.5, 69. , 96. ],
    12. [559.5, 788. , 68. , 83. ],
    13. [831.5, 754.5, 56. , 56. ]], dtype=float32),
    14. 'gt_poly': [],
    15. 'difficult': array([0, 0, 0, 0, 0, 0, 0], dtype=int32)}