coco_stuff164k.py 5.1 KB

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