prepare_data.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import argparse
  19. from convert import load_dota_infos, data_to_coco
  20. from slicebase import SliceBase
  21. wordname_15 = [
  22. 'plane', 'baseball-diamond', 'bridge', 'ground-track-field',
  23. 'small-vehicle', 'large-vehicle', 'ship', 'tennis-court',
  24. 'basketball-court', 'storage-tank', 'soccer-ball-field', 'roundabout',
  25. 'harbor', 'swimming-pool', 'helicopter'
  26. ]
  27. wordname_16 = wordname_15 + ['container-crane']
  28. wordname_18 = wordname_16 + ['airport', 'helipad']
  29. DATA_CLASSES = {
  30. 'dota10': wordname_15,
  31. 'dota15': wordname_16,
  32. 'dota20': wordname_18
  33. }
  34. def parse_args():
  35. parser = argparse.ArgumentParser('prepare data for training')
  36. parser.add_argument(
  37. '--input_dirs',
  38. nargs='+',
  39. type=str,
  40. default=None,
  41. help='input dirs which contain image and labelTxt dir')
  42. parser.add_argument(
  43. '--output_dir',
  44. type=str,
  45. default=None,
  46. help='output dirs which contain image and labelTxt dir and coco style json file'
  47. )
  48. parser.add_argument(
  49. '--coco_json_file',
  50. type=str,
  51. default='',
  52. help='coco json annotation files')
  53. parser.add_argument('--subsize', type=int, default=1024, help='patch size')
  54. parser.add_argument('--gap', type=int, default=200, help='step size')
  55. parser.add_argument(
  56. '--data_type', type=str, default='dota10', help='data type')
  57. parser.add_argument(
  58. '--rates',
  59. nargs='+',
  60. type=float,
  61. default=[1.],
  62. help='scales for multi-sclace training')
  63. parser.add_argument(
  64. '--nproc', type=int, default=8, help='the processor number')
  65. parser.add_argument(
  66. '--iof_thr',
  67. type=float,
  68. default=0.5,
  69. help='the minimal iof between a object and a window')
  70. parser.add_argument(
  71. '--image_only',
  72. action='store_true',
  73. default=False,
  74. help='only processing image')
  75. args = parser.parse_args()
  76. return args
  77. def load_dataset(input_dir, nproc, data_type):
  78. if 'dota' in data_type.lower():
  79. infos = load_dota_infos(input_dir, nproc)
  80. else:
  81. raise ValueError('only dota dataset is supported now')
  82. return infos
  83. def main():
  84. args = parse_args()
  85. infos = []
  86. for input_dir in args.input_dirs:
  87. infos += load_dataset(input_dir, args.nproc, args.data_type)
  88. slicer = SliceBase(
  89. args.gap,
  90. args.subsize,
  91. args.iof_thr,
  92. num_process=args.nproc,
  93. image_only=args.image_only)
  94. slicer.slice_data(infos, args.rates, args.output_dir)
  95. if args.coco_json_file:
  96. infos = load_dota_infos(args.output_dir, args.nproc)
  97. coco_json_file = os.path.join(args.output_dir, args.coco_json_file)
  98. class_names = DATA_CLASSES[args.data_type]
  99. data_to_coco(infos, coco_json_file, class_names, args.nproc)
  100. if __name__ == '__main__':
  101. main()