prepare_coco_semantic_annos_from_panoptic_annos.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. # Modified from https://github.com/facebookresearch/Mask2Former/blob/main/datasets/prepare_coco_semantic_annos_from_panoptic_annos.py # noqa
  3. import argparse
  4. import functools
  5. import json
  6. import multiprocessing as mp
  7. import os
  8. import time
  9. import numpy as np
  10. from panopticapi.utils import rgb2id
  11. from PIL import Image
  12. COCO_CATEGORIES = [
  13. {
  14. 'color': [220, 20, 60],
  15. 'isthing': 1,
  16. 'id': 1,
  17. 'name': 'person'
  18. },
  19. {
  20. 'color': [119, 11, 32],
  21. 'isthing': 1,
  22. 'id': 2,
  23. 'name': 'bicycle'
  24. },
  25. {
  26. 'color': [0, 0, 142],
  27. 'isthing': 1,
  28. 'id': 3,
  29. 'name': 'car'
  30. },
  31. {
  32. 'color': [0, 0, 230],
  33. 'isthing': 1,
  34. 'id': 4,
  35. 'name': 'motorcycle'
  36. },
  37. {
  38. 'color': [106, 0, 228],
  39. 'isthing': 1,
  40. 'id': 5,
  41. 'name': 'airplane'
  42. },
  43. {
  44. 'color': [0, 60, 100],
  45. 'isthing': 1,
  46. 'id': 6,
  47. 'name': 'bus'
  48. },
  49. {
  50. 'color': [0, 80, 100],
  51. 'isthing': 1,
  52. 'id': 7,
  53. 'name': 'train'
  54. },
  55. {
  56. 'color': [0, 0, 70],
  57. 'isthing': 1,
  58. 'id': 8,
  59. 'name': 'truck'
  60. },
  61. {
  62. 'color': [0, 0, 192],
  63. 'isthing': 1,
  64. 'id': 9,
  65. 'name': 'boat'
  66. },
  67. {
  68. 'color': [250, 170, 30],
  69. 'isthing': 1,
  70. 'id': 10,
  71. 'name': 'traffic light'
  72. },
  73. {
  74. 'color': [100, 170, 30],
  75. 'isthing': 1,
  76. 'id': 11,
  77. 'name': 'fire hydrant'
  78. },
  79. {
  80. 'color': [220, 220, 0],
  81. 'isthing': 1,
  82. 'id': 13,
  83. 'name': 'stop sign'
  84. },
  85. {
  86. 'color': [175, 116, 175],
  87. 'isthing': 1,
  88. 'id': 14,
  89. 'name': 'parking meter'
  90. },
  91. {
  92. 'color': [250, 0, 30],
  93. 'isthing': 1,
  94. 'id': 15,
  95. 'name': 'bench'
  96. },
  97. {
  98. 'color': [165, 42, 42],
  99. 'isthing': 1,
  100. 'id': 16,
  101. 'name': 'bird'
  102. },
  103. {
  104. 'color': [255, 77, 255],
  105. 'isthing': 1,
  106. 'id': 17,
  107. 'name': 'cat'
  108. },
  109. {
  110. 'color': [0, 226, 252],
  111. 'isthing': 1,
  112. 'id': 18,
  113. 'name': 'dog'
  114. },
  115. {
  116. 'color': [182, 182, 255],
  117. 'isthing': 1,
  118. 'id': 19,
  119. 'name': 'horse'
  120. },
  121. {
  122. 'color': [0, 82, 0],
  123. 'isthing': 1,
  124. 'id': 20,
  125. 'name': 'sheep'
  126. },
  127. {
  128. 'color': [120, 166, 157],
  129. 'isthing': 1,
  130. 'id': 21,
  131. 'name': 'cow'
  132. },
  133. {
  134. 'color': [110, 76, 0],
  135. 'isthing': 1,
  136. 'id': 22,
  137. 'name': 'elephant'
  138. },
  139. {
  140. 'color': [174, 57, 255],
  141. 'isthing': 1,
  142. 'id': 23,
  143. 'name': 'bear'
  144. },
  145. {
  146. 'color': [199, 100, 0],
  147. 'isthing': 1,
  148. 'id': 24,
  149. 'name': 'zebra'
  150. },
  151. {
  152. 'color': [72, 0, 118],
  153. 'isthing': 1,
  154. 'id': 25,
  155. 'name': 'giraffe'
  156. },
  157. {
  158. 'color': [255, 179, 240],
  159. 'isthing': 1,
  160. 'id': 27,
  161. 'name': 'backpack'
  162. },
  163. {
  164. 'color': [0, 125, 92],
  165. 'isthing': 1,
  166. 'id': 28,
  167. 'name': 'umbrella'
  168. },
  169. {
  170. 'color': [209, 0, 151],
  171. 'isthing': 1,
  172. 'id': 31,
  173. 'name': 'handbag'
  174. },
  175. {
  176. 'color': [188, 208, 182],
  177. 'isthing': 1,
  178. 'id': 32,
  179. 'name': 'tie'
  180. },
  181. {
  182. 'color': [0, 220, 176],
  183. 'isthing': 1,
  184. 'id': 33,
  185. 'name': 'suitcase'
  186. },
  187. {
  188. 'color': [255, 99, 164],
  189. 'isthing': 1,
  190. 'id': 34,
  191. 'name': 'frisbee'
  192. },
  193. {
  194. 'color': [92, 0, 73],
  195. 'isthing': 1,
  196. 'id': 35,
  197. 'name': 'skis'
  198. },
  199. {
  200. 'color': [133, 129, 255],
  201. 'isthing': 1,
  202. 'id': 36,
  203. 'name': 'snowboard'
  204. },
  205. {
  206. 'color': [78, 180, 255],
  207. 'isthing': 1,
  208. 'id': 37,
  209. 'name': 'sports ball'
  210. },
  211. {
  212. 'color': [0, 228, 0],
  213. 'isthing': 1,
  214. 'id': 38,
  215. 'name': 'kite'
  216. },
  217. {
  218. 'color': [174, 255, 243],
  219. 'isthing': 1,
  220. 'id': 39,
  221. 'name': 'baseball bat'
  222. },
  223. {
  224. 'color': [45, 89, 255],
  225. 'isthing': 1,
  226. 'id': 40,
  227. 'name': 'baseball glove'
  228. },
  229. {
  230. 'color': [134, 134, 103],
  231. 'isthing': 1,
  232. 'id': 41,
  233. 'name': 'skateboard'
  234. },
  235. {
  236. 'color': [145, 148, 174],
  237. 'isthing': 1,
  238. 'id': 42,
  239. 'name': 'surfboard'
  240. },
  241. {
  242. 'color': [255, 208, 186],
  243. 'isthing': 1,
  244. 'id': 43,
  245. 'name': 'tennis racket'
  246. },
  247. {
  248. 'color': [197, 226, 255],
  249. 'isthing': 1,
  250. 'id': 44,
  251. 'name': 'bottle'
  252. },
  253. {
  254. 'color': [171, 134, 1],
  255. 'isthing': 1,
  256. 'id': 46,
  257. 'name': 'wine glass'
  258. },
  259. {
  260. 'color': [109, 63, 54],
  261. 'isthing': 1,
  262. 'id': 47,
  263. 'name': 'cup'
  264. },
  265. {
  266. 'color': [207, 138, 255],
  267. 'isthing': 1,
  268. 'id': 48,
  269. 'name': 'fork'
  270. },
  271. {
  272. 'color': [151, 0, 95],
  273. 'isthing': 1,
  274. 'id': 49,
  275. 'name': 'knife'
  276. },
  277. {
  278. 'color': [9, 80, 61],
  279. 'isthing': 1,
  280. 'id': 50,
  281. 'name': 'spoon'
  282. },
  283. {
  284. 'color': [84, 105, 51],
  285. 'isthing': 1,
  286. 'id': 51,
  287. 'name': 'bowl'
  288. },
  289. {
  290. 'color': [74, 65, 105],
  291. 'isthing': 1,
  292. 'id': 52,
  293. 'name': 'banana'
  294. },
  295. {
  296. 'color': [166, 196, 102],
  297. 'isthing': 1,
  298. 'id': 53,
  299. 'name': 'apple'
  300. },
  301. {
  302. 'color': [208, 195, 210],
  303. 'isthing': 1,
  304. 'id': 54,
  305. 'name': 'sandwich'
  306. },
  307. {
  308. 'color': [255, 109, 65],
  309. 'isthing': 1,
  310. 'id': 55,
  311. 'name': 'orange'
  312. },
  313. {
  314. 'color': [0, 143, 149],
  315. 'isthing': 1,
  316. 'id': 56,
  317. 'name': 'broccoli'
  318. },
  319. {
  320. 'color': [179, 0, 194],
  321. 'isthing': 1,
  322. 'id': 57,
  323. 'name': 'carrot'
  324. },
  325. {
  326. 'color': [209, 99, 106],
  327. 'isthing': 1,
  328. 'id': 58,
  329. 'name': 'hot dog'
  330. },
  331. {
  332. 'color': [5, 121, 0],
  333. 'isthing': 1,
  334. 'id': 59,
  335. 'name': 'pizza'
  336. },
  337. {
  338. 'color': [227, 255, 205],
  339. 'isthing': 1,
  340. 'id': 60,
  341. 'name': 'donut'
  342. },
  343. {
  344. 'color': [147, 186, 208],
  345. 'isthing': 1,
  346. 'id': 61,
  347. 'name': 'cake'
  348. },
  349. {
  350. 'color': [153, 69, 1],
  351. 'isthing': 1,
  352. 'id': 62,
  353. 'name': 'chair'
  354. },
  355. {
  356. 'color': [3, 95, 161],
  357. 'isthing': 1,
  358. 'id': 63,
  359. 'name': 'couch'
  360. },
  361. {
  362. 'color': [163, 255, 0],
  363. 'isthing': 1,
  364. 'id': 64,
  365. 'name': 'potted plant'
  366. },
  367. {
  368. 'color': [119, 0, 170],
  369. 'isthing': 1,
  370. 'id': 65,
  371. 'name': 'bed'
  372. },
  373. {
  374. 'color': [0, 182, 199],
  375. 'isthing': 1,
  376. 'id': 67,
  377. 'name': 'dining table'
  378. },
  379. {
  380. 'color': [0, 165, 120],
  381. 'isthing': 1,
  382. 'id': 70,
  383. 'name': 'toilet'
  384. },
  385. {
  386. 'color': [183, 130, 88],
  387. 'isthing': 1,
  388. 'id': 72,
  389. 'name': 'tv'
  390. },
  391. {
  392. 'color': [95, 32, 0],
  393. 'isthing': 1,
  394. 'id': 73,
  395. 'name': 'laptop'
  396. },
  397. {
  398. 'color': [130, 114, 135],
  399. 'isthing': 1,
  400. 'id': 74,
  401. 'name': 'mouse'
  402. },
  403. {
  404. 'color': [110, 129, 133],
  405. 'isthing': 1,
  406. 'id': 75,
  407. 'name': 'remote'
  408. },
  409. {
  410. 'color': [166, 74, 118],
  411. 'isthing': 1,
  412. 'id': 76,
  413. 'name': 'keyboard'
  414. },
  415. {
  416. 'color': [219, 142, 185],
  417. 'isthing': 1,
  418. 'id': 77,
  419. 'name': 'cell phone'
  420. },
  421. {
  422. 'color': [79, 210, 114],
  423. 'isthing': 1,
  424. 'id': 78,
  425. 'name': 'microwave'
  426. },
  427. {
  428. 'color': [178, 90, 62],
  429. 'isthing': 1,
  430. 'id': 79,
  431. 'name': 'oven'
  432. },
  433. {
  434. 'color': [65, 70, 15],
  435. 'isthing': 1,
  436. 'id': 80,
  437. 'name': 'toaster'
  438. },
  439. {
  440. 'color': [127, 167, 115],
  441. 'isthing': 1,
  442. 'id': 81,
  443. 'name': 'sink'
  444. },
  445. {
  446. 'color': [59, 105, 106],
  447. 'isthing': 1,
  448. 'id': 82,
  449. 'name': 'refrigerator'
  450. },
  451. {
  452. 'color': [142, 108, 45],
  453. 'isthing': 1,
  454. 'id': 84,
  455. 'name': 'book'
  456. },
  457. {
  458. 'color': [196, 172, 0],
  459. 'isthing': 1,
  460. 'id': 85,
  461. 'name': 'clock'
  462. },
  463. {
  464. 'color': [95, 54, 80],
  465. 'isthing': 1,
  466. 'id': 86,
  467. 'name': 'vase'
  468. },
  469. {
  470. 'color': [128, 76, 255],
  471. 'isthing': 1,
  472. 'id': 87,
  473. 'name': 'scissors'
  474. },
  475. {
  476. 'color': [201, 57, 1],
  477. 'isthing': 1,
  478. 'id': 88,
  479. 'name': 'teddy bear'
  480. },
  481. {
  482. 'color': [246, 0, 122],
  483. 'isthing': 1,
  484. 'id': 89,
  485. 'name': 'hair drier'
  486. },
  487. {
  488. 'color': [191, 162, 208],
  489. 'isthing': 1,
  490. 'id': 90,
  491. 'name': 'toothbrush'
  492. },
  493. {
  494. 'color': [255, 255, 128],
  495. 'isthing': 0,
  496. 'id': 92,
  497. 'name': 'banner'
  498. },
  499. {
  500. 'color': [147, 211, 203],
  501. 'isthing': 0,
  502. 'id': 93,
  503. 'name': 'blanket'
  504. },
  505. {
  506. 'color': [150, 100, 100],
  507. 'isthing': 0,
  508. 'id': 95,
  509. 'name': 'bridge'
  510. },
  511. {
  512. 'color': [168, 171, 172],
  513. 'isthing': 0,
  514. 'id': 100,
  515. 'name': 'cardboard'
  516. },
  517. {
  518. 'color': [146, 112, 198],
  519. 'isthing': 0,
  520. 'id': 107,
  521. 'name': 'counter'
  522. },
  523. {
  524. 'color': [210, 170, 100],
  525. 'isthing': 0,
  526. 'id': 109,
  527. 'name': 'curtain'
  528. },
  529. {
  530. 'color': [92, 136, 89],
  531. 'isthing': 0,
  532. 'id': 112,
  533. 'name': 'door-stuff'
  534. },
  535. {
  536. 'color': [218, 88, 184],
  537. 'isthing': 0,
  538. 'id': 118,
  539. 'name': 'floor-wood'
  540. },
  541. {
  542. 'color': [241, 129, 0],
  543. 'isthing': 0,
  544. 'id': 119,
  545. 'name': 'flower'
  546. },
  547. {
  548. 'color': [217, 17, 255],
  549. 'isthing': 0,
  550. 'id': 122,
  551. 'name': 'fruit'
  552. },
  553. {
  554. 'color': [124, 74, 181],
  555. 'isthing': 0,
  556. 'id': 125,
  557. 'name': 'gravel'
  558. },
  559. {
  560. 'color': [70, 70, 70],
  561. 'isthing': 0,
  562. 'id': 128,
  563. 'name': 'house'
  564. },
  565. {
  566. 'color': [255, 228, 255],
  567. 'isthing': 0,
  568. 'id': 130,
  569. 'name': 'light'
  570. },
  571. {
  572. 'color': [154, 208, 0],
  573. 'isthing': 0,
  574. 'id': 133,
  575. 'name': 'mirror-stuff'
  576. },
  577. {
  578. 'color': [193, 0, 92],
  579. 'isthing': 0,
  580. 'id': 138,
  581. 'name': 'net'
  582. },
  583. {
  584. 'color': [76, 91, 113],
  585. 'isthing': 0,
  586. 'id': 141,
  587. 'name': 'pillow'
  588. },
  589. {
  590. 'color': [255, 180, 195],
  591. 'isthing': 0,
  592. 'id': 144,
  593. 'name': 'platform'
  594. },
  595. {
  596. 'color': [106, 154, 176],
  597. 'isthing': 0,
  598. 'id': 145,
  599. 'name': 'playingfield'
  600. },
  601. {
  602. 'color': [230, 150, 140],
  603. 'isthing': 0,
  604. 'id': 147,
  605. 'name': 'railroad'
  606. },
  607. {
  608. 'color': [60, 143, 255],
  609. 'isthing': 0,
  610. 'id': 148,
  611. 'name': 'river'
  612. },
  613. {
  614. 'color': [128, 64, 128],
  615. 'isthing': 0,
  616. 'id': 149,
  617. 'name': 'road'
  618. },
  619. {
  620. 'color': [92, 82, 55],
  621. 'isthing': 0,
  622. 'id': 151,
  623. 'name': 'roof'
  624. },
  625. {
  626. 'color': [254, 212, 124],
  627. 'isthing': 0,
  628. 'id': 154,
  629. 'name': 'sand'
  630. },
  631. {
  632. 'color': [73, 77, 174],
  633. 'isthing': 0,
  634. 'id': 155,
  635. 'name': 'sea'
  636. },
  637. {
  638. 'color': [255, 160, 98],
  639. 'isthing': 0,
  640. 'id': 156,
  641. 'name': 'shelf'
  642. },
  643. {
  644. 'color': [255, 255, 255],
  645. 'isthing': 0,
  646. 'id': 159,
  647. 'name': 'snow'
  648. },
  649. {
  650. 'color': [104, 84, 109],
  651. 'isthing': 0,
  652. 'id': 161,
  653. 'name': 'stairs'
  654. },
  655. {
  656. 'color': [169, 164, 131],
  657. 'isthing': 0,
  658. 'id': 166,
  659. 'name': 'tent'
  660. },
  661. {
  662. 'color': [225, 199, 255],
  663. 'isthing': 0,
  664. 'id': 168,
  665. 'name': 'towel'
  666. },
  667. {
  668. 'color': [137, 54, 74],
  669. 'isthing': 0,
  670. 'id': 171,
  671. 'name': 'wall-brick'
  672. },
  673. {
  674. 'color': [135, 158, 223],
  675. 'isthing': 0,
  676. 'id': 175,
  677. 'name': 'wall-stone'
  678. },
  679. {
  680. 'color': [7, 246, 231],
  681. 'isthing': 0,
  682. 'id': 176,
  683. 'name': 'wall-tile'
  684. },
  685. {
  686. 'color': [107, 255, 200],
  687. 'isthing': 0,
  688. 'id': 177,
  689. 'name': 'wall-wood'
  690. },
  691. {
  692. 'color': [58, 41, 149],
  693. 'isthing': 0,
  694. 'id': 178,
  695. 'name': 'water-other'
  696. },
  697. {
  698. 'color': [183, 121, 142],
  699. 'isthing': 0,
  700. 'id': 180,
  701. 'name': 'window-blind'
  702. },
  703. {
  704. 'color': [255, 73, 97],
  705. 'isthing': 0,
  706. 'id': 181,
  707. 'name': 'window-other'
  708. },
  709. {
  710. 'color': [107, 142, 35],
  711. 'isthing': 0,
  712. 'id': 184,
  713. 'name': 'tree-merged'
  714. },
  715. {
  716. 'color': [190, 153, 153],
  717. 'isthing': 0,
  718. 'id': 185,
  719. 'name': 'fence-merged'
  720. },
  721. {
  722. 'color': [146, 139, 141],
  723. 'isthing': 0,
  724. 'id': 186,
  725. 'name': 'ceiling-merged'
  726. },
  727. {
  728. 'color': [70, 130, 180],
  729. 'isthing': 0,
  730. 'id': 187,
  731. 'name': 'sky-other-merged'
  732. },
  733. {
  734. 'color': [134, 199, 156],
  735. 'isthing': 0,
  736. 'id': 188,
  737. 'name': 'cabinet-merged'
  738. },
  739. {
  740. 'color': [209, 226, 140],
  741. 'isthing': 0,
  742. 'id': 189,
  743. 'name': 'table-merged'
  744. },
  745. {
  746. 'color': [96, 36, 108],
  747. 'isthing': 0,
  748. 'id': 190,
  749. 'name': 'floor-other-merged'
  750. },
  751. {
  752. 'color': [96, 96, 96],
  753. 'isthing': 0,
  754. 'id': 191,
  755. 'name': 'pavement-merged'
  756. },
  757. {
  758. 'color': [64, 170, 64],
  759. 'isthing': 0,
  760. 'id': 192,
  761. 'name': 'mountain-merged'
  762. },
  763. {
  764. 'color': [152, 251, 152],
  765. 'isthing': 0,
  766. 'id': 193,
  767. 'name': 'grass-merged'
  768. },
  769. {
  770. 'color': [208, 229, 228],
  771. 'isthing': 0,
  772. 'id': 194,
  773. 'name': 'dirt-merged'
  774. },
  775. {
  776. 'color': [206, 186, 171],
  777. 'isthing': 0,
  778. 'id': 195,
  779. 'name': 'paper-merged'
  780. },
  781. {
  782. 'color': [152, 161, 64],
  783. 'isthing': 0,
  784. 'id': 196,
  785. 'name': 'food-other-merged'
  786. },
  787. {
  788. 'color': [116, 112, 0],
  789. 'isthing': 0,
  790. 'id': 197,
  791. 'name': 'building-other-merged'
  792. },
  793. {
  794. 'color': [0, 114, 143],
  795. 'isthing': 0,
  796. 'id': 198,
  797. 'name': 'rock-merged'
  798. },
  799. {
  800. 'color': [102, 102, 156],
  801. 'isthing': 0,
  802. 'id': 199,
  803. 'name': 'wall-other-merged'
  804. },
  805. {
  806. 'color': [250, 141, 255],
  807. 'isthing': 0,
  808. 'id': 200,
  809. 'name': 'rug-merged'
  810. },
  811. ]
  812. def _process_panoptic_to_semantic(input_panoptic, output_semantic, segments,
  813. id_map):
  814. panoptic = np.asarray(Image.open(input_panoptic), dtype=np.uint32)
  815. panoptic = rgb2id(panoptic)
  816. output = np.zeros_like(panoptic, dtype=np.uint8) + 255
  817. for seg in segments:
  818. cat_id = seg['category_id']
  819. new_cat_id = id_map[cat_id]
  820. output[panoptic == seg['id']] = new_cat_id
  821. Image.fromarray(output).save(output_semantic)
  822. def separate_coco_semantic_from_panoptic(panoptic_json, panoptic_root,
  823. sem_seg_root, categories):
  824. """Create semantic segmentation annotations from panoptic segmentation
  825. annotations, to be used by PanopticFPN.
  826. It maps all thing categories to class 0, and maps all
  827. unlabeled pixels to class 255.
  828. It maps all stuff categories to contiguous ids starting from 1.
  829. Args:
  830. panoptic_json (str): path to the panoptic json file, in COCO's format.
  831. panoptic_root (str): a directory with panoptic annotation files, in
  832. COCO's format.
  833. sem_seg_root (str): a directory to output semantic annotation files
  834. categories (list[dict]): category metadata. Each dict needs to have:
  835. "id": corresponds to the "category_id" in the json annotations
  836. "isthing": 0 or 1
  837. """
  838. os.makedirs(sem_seg_root, exist_ok=True)
  839. id_map = {} # map from category id to id in the output semantic annotation
  840. assert len(categories) <= 254
  841. for i, k in enumerate(categories):
  842. id_map[k['id']] = i
  843. # what is id = 0?
  844. # id_map[0] = 255
  845. print(id_map)
  846. with open(panoptic_json) as f:
  847. obj = json.load(f)
  848. pool = mp.Pool(processes=max(mp.cpu_count() // 2, 4))
  849. def iter_annotations():
  850. for anno in obj['annotations']:
  851. file_name = anno['file_name']
  852. segments = anno['segments_info']
  853. input = os.path.join(panoptic_root, file_name)
  854. output = os.path.join(sem_seg_root, file_name)
  855. yield input, output, segments
  856. print('Start writing to {} ...'.format(sem_seg_root))
  857. start = time.time()
  858. pool.starmap(
  859. functools.partial(_process_panoptic_to_semantic, id_map=id_map),
  860. iter_annotations(),
  861. chunksize=100,
  862. )
  863. print('Finished. time: {:.2f}s'.format(time.time() - start))
  864. def parse_args():
  865. parser = argparse.ArgumentParser(
  866. description=\
  867. 'Convert COCO Stuff 164k annotations to mmdet format') # noqa
  868. parser.add_argument('coco_path', help='coco stuff path')
  869. args = parser.parse_args()
  870. return args
  871. if __name__ == '__main__':
  872. args = parse_args()
  873. dataset_dir = args.coco_path
  874. for s in ['val2017', 'train2017']:
  875. separate_coco_semantic_from_panoptic(
  876. os.path.join(dataset_dir,
  877. 'annotations/panoptic_{}.json'.format(s)),
  878. os.path.join(dataset_dir, 'annotations/panoptic_{}'.format(s)),
  879. os.path.join(dataset_dir,
  880. 'annotations/panoptic_semseg_{}'.format(s)),
  881. COCO_CATEGORIES,
  882. )