convert_coco_object.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # -------------------------------------------------------------------------
  2. # Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  3. #
  4. # NVIDIA CORPORATION & AFFILIATES and its licensors retain all intellectual
  5. # property and proprietary rights in and to this software, related
  6. # documentation and any modifications thereto. Any use, reproduction,
  7. # disclosure or distribution of this software and related documentation
  8. # without an express license agreement from NVIDIA CORPORATION is strictly
  9. # prohibited.
  10. #
  11. # Written by Jiarui Xu
  12. # -------------------------------------------------------------------------
  13. import argparse
  14. import os.path as osp
  15. import shutil
  16. from functools import partial
  17. from glob import glob
  18. import mmcv
  19. import numpy as np
  20. from PIL import Image
  21. COCO_LEN = 123287
  22. clsID_to_trID = {
  23. 0: 0,
  24. 1: 1,
  25. 2: 2,
  26. 3: 3,
  27. 4: 4,
  28. 5: 5,
  29. 6: 6,
  30. 7: 7,
  31. 8: 8,
  32. 9: 9,
  33. 10: 10,
  34. 12: 11,
  35. 13: 12,
  36. 14: 13,
  37. 15: 14,
  38. 16: 15,
  39. 17: 16,
  40. 18: 17,
  41. 19: 18,
  42. 20: 19,
  43. 21: 20,
  44. 22: 21,
  45. 23: 22,
  46. 24: 23,
  47. 26: 24,
  48. 27: 25,
  49. 30: 26,
  50. 31: 27,
  51. 32: 28,
  52. 33: 29,
  53. 34: 30,
  54. 35: 31,
  55. 36: 32,
  56. 37: 33,
  57. 38: 34,
  58. 39: 35,
  59. 40: 36,
  60. 41: 37,
  61. 42: 38,
  62. 43: 39,
  63. 45: 40,
  64. 46: 41,
  65. 47: 42,
  66. 48: 43,
  67. 49: 44,
  68. 50: 45,
  69. 51: 46,
  70. 52: 47,
  71. 53: 48,
  72. 54: 49,
  73. 55: 50,
  74. 56: 51,
  75. 57: 52,
  76. 58: 53,
  77. 59: 54,
  78. 60: 55,
  79. 61: 56,
  80. 62: 57,
  81. 63: 58,
  82. 64: 59,
  83. 66: 60,
  84. 69: 61,
  85. 71: 62,
  86. 72: 63,
  87. 73: 64,
  88. 74: 65,
  89. 75: 66,
  90. 76: 67,
  91. 77: 68,
  92. 78: 69,
  93. 79: 70,
  94. 80: 71,
  95. 81: 72,
  96. 83: 73,
  97. 84: 74,
  98. 85: 75,
  99. 86: 76,
  100. 87: 77,
  101. 88: 78,
  102. 89: 79,
  103. 91: 80,
  104. 92: 81,
  105. 93: 82,
  106. 94: 83,
  107. 95: 84,
  108. 96: 85,
  109. 97: 86,
  110. 98: 87,
  111. 99: 88,
  112. 100: 89,
  113. 101: 90,
  114. 102: 91,
  115. 103: 92,
  116. 104: 93,
  117. 105: 94,
  118. 106: 95,
  119. 107: 96,
  120. 108: 97,
  121. 109: 98,
  122. 110: 99,
  123. 111: 100,
  124. 112: 101,
  125. 113: 102,
  126. 114: 103,
  127. 115: 104,
  128. 116: 105,
  129. 117: 106,
  130. 118: 107,
  131. 119: 108,
  132. 120: 109,
  133. 121: 110,
  134. 122: 111,
  135. 123: 112,
  136. 124: 113,
  137. 125: 114,
  138. 126: 115,
  139. 127: 116,
  140. 128: 117,
  141. 129: 118,
  142. 130: 119,
  143. 131: 120,
  144. 132: 121,
  145. 133: 122,
  146. 134: 123,
  147. 135: 124,
  148. 136: 125,
  149. 137: 126,
  150. 138: 127,
  151. 139: 128,
  152. 140: 129,
  153. 141: 130,
  154. 142: 131,
  155. 143: 132,
  156. 144: 133,
  157. 145: 134,
  158. 146: 135,
  159. 147: 136,
  160. 148: 137,
  161. 149: 138,
  162. 150: 139,
  163. 151: 140,
  164. 152: 141,
  165. 153: 142,
  166. 154: 143,
  167. 155: 144,
  168. 156: 145,
  169. 157: 146,
  170. 158: 147,
  171. 159: 148,
  172. 160: 149,
  173. 161: 150,
  174. 162: 151,
  175. 163: 152,
  176. 164: 153,
  177. 165: 154,
  178. 166: 155,
  179. 167: 156,
  180. 168: 157,
  181. 169: 158,
  182. 170: 159,
  183. 171: 160,
  184. 172: 161,
  185. 173: 162,
  186. 174: 163,
  187. 175: 164,
  188. 176: 165,
  189. 177: 166,
  190. 178: 167,
  191. 179: 168,
  192. 180: 169,
  193. 181: 170,
  194. 255: 255
  195. }
  196. # set to background for cocoobject with first 80 foreground classes
  197. # for k, v in clsID_to_trID.items():
  198. # clsID_to_trID[k] = v + 1
  199. # if k > 90:
  200. # clsID_to_trID[k] = 0
  201. # for all foreground classes
  202. for k, v in clsID_to_trID.items():
  203. clsID_to_trID[k] = v + 1
  204. if k == 255:
  205. clsID_to_trID[k] = 0
  206. def convert_to_trainID(maskpath, out_mask_dir, is_train):
  207. mask = np.array(Image.open(maskpath))
  208. mask_copy = mask.copy()
  209. for clsID, trID in clsID_to_trID.items():
  210. mask_copy[mask == clsID] = trID
  211. seg_filename = osp.join(
  212. out_mask_dir, 'train2017',
  213. osp.basename(maskpath).split('.')[0] +
  214. '_instanceTrainIds.png') if is_train else osp.join(
  215. out_mask_dir, 'val2017',
  216. osp.basename(maskpath).split('.')[0] + '_instanceTrainIds.png')
  217. Image.fromarray(mask_copy).save(seg_filename, 'PNG')
  218. def parse_args():
  219. parser = argparse.ArgumentParser(
  220. description=\
  221. 'Convert COCO Stuff 164k annotations to COCO Objects') # noqa
  222. parser.add_argument('coco_path', help='coco stuff path')
  223. parser.add_argument('-o', '--out_dir', help='output path')
  224. parser.add_argument(
  225. '--nproc', default=16, type=int, help='number of process')
  226. args = parser.parse_args()
  227. return args
  228. def main():
  229. args = parse_args()
  230. coco_path = args.coco_path
  231. nproc = args.nproc
  232. out_dir = args.out_dir or coco_path
  233. out_img_dir = osp.join(out_dir, 'images')
  234. out_mask_dir = osp.join(out_dir, 'annotations')
  235. mmcv.mkdir_or_exist(osp.join(out_mask_dir, 'train2017'))
  236. mmcv.mkdir_or_exist(osp.join(out_mask_dir, 'val2017'))
  237. if out_dir != coco_path:
  238. shutil.copytree(osp.join(coco_path, 'images'), out_img_dir)
  239. train_list = glob(osp.join(coco_path, 'annotations', 'train2017', '*.png'))
  240. train_list = [file for file in train_list if 'TrainIds' not in file]
  241. test_list = glob(osp.join(coco_path, 'annotations', 'val2017', '*.png'))
  242. test_list = [file for file in test_list if 'TrainIds' not in file]
  243. assert (len(train_list) +
  244. len(test_list)) == COCO_LEN, 'Wrong length of list {} & {}'.format(
  245. len(train_list), len(test_list))
  246. if args.nproc > 1:
  247. mmcv.track_parallel_progress(
  248. partial(
  249. convert_to_trainID, out_mask_dir=out_mask_dir, is_train=True),
  250. train_list,
  251. nproc=nproc)
  252. mmcv.track_parallel_progress(
  253. partial(
  254. convert_to_trainID, out_mask_dir=out_mask_dir, is_train=False),
  255. test_list,
  256. nproc=nproc)
  257. else:
  258. mmcv.track_progress(
  259. partial(
  260. convert_to_trainID, out_mask_dir=out_mask_dir, is_train=True),
  261. train_list)
  262. mmcv.track_progress(
  263. partial(
  264. convert_to_trainID, out_mask_dir=out_mask_dir, is_train=False),
  265. test_list)
  266. print('Done!')
  267. if __name__ == '__main__':
  268. main()