3
3
import json
4
4
import logging
5
5
import multiprocessing as mp
6
+ import numpy as np
6
7
import os
7
8
from itertools import chain
8
-
9
- import numpy as np
10
9
import pycocotools .mask as mask_util
10
+ from PIL import Image
11
11
12
12
from detectron2 .structures import BoxMode
13
13
from detectron2 .utils .comm import get_world_size
14
14
from detectron2 .utils .file_io import PathManager
15
15
from detectron2 .utils .logger import setup_logger
16
- from PIL import Image
17
16
18
17
try :
19
18
import cv2 # noqa
@@ -40,9 +39,7 @@ def _get_cityscapes_files(image_dir, gt_dir):
40
39
assert basename .endswith (suffix ), basename
41
40
basename = basename [: - len (suffix )]
42
41
43
- instance_file = os .path .join (
44
- city_gt_dir , basename + "gtFine_instanceIds.png"
45
- )
42
+ instance_file = os .path .join (city_gt_dir , basename + "gtFine_instanceIds.png" )
46
43
label_file = os .path .join (city_gt_dir , basename + "gtFine_labelIds.png" )
47
44
json_file = os .path .join (city_gt_dir , basename + "gtFine_polygons.json" )
48
45
@@ -79,9 +76,7 @@ def load_cityscapes_instances(image_dir, gt_dir, from_json=True, to_polygons=Tru
79
76
pool = mp .Pool (processes = max (mp .cpu_count () // get_world_size () // 2 , 4 ))
80
77
81
78
ret = pool .map (
82
- functools .partial (
83
- _cityscapes_files_to_dict , from_json = from_json , to_polygons = to_polygons
84
- ),
79
+ functools .partial (_cityscapes_files_to_dict , from_json = from_json , to_polygons = to_polygons ),
85
80
files ,
86
81
)
87
82
logger .info ("Loaded {} images from {}" .format (len (ret ), image_dir ))
@@ -110,9 +105,7 @@ def load_cityscapes_semantic(image_dir, gt_dir):
110
105
ret = []
111
106
# gt_dir is small and contain many small files. make sense to fetch to local first
112
107
gt_dir = PathManager .get_local_path (gt_dir )
113
- for image_file , _ , label_file , json_file in _get_cityscapes_files (
114
- image_dir , gt_dir
115
- ):
108
+ for image_file , _ , label_file , json_file in _get_cityscapes_files (image_dir , gt_dir ):
116
109
label_file = label_file .replace ("labelIds" , "labelTrainIds" )
117
110
118
111
with PathManager .open (json_file , "r" ) as f :
@@ -216,9 +209,7 @@ def _cityscapes_files_to_dict(files, from_json, to_polygons):
216
209
elif isinstance (poly_wo_overlaps , MultiPolygon ):
217
210
poly_list = poly_wo_overlaps .geoms
218
211
else :
219
- raise NotImplementedError (
220
- "Unknown geometric structure {}" .format (poly_wo_overlaps )
221
- )
212
+ raise NotImplementedError ("Unknown geometric structure {}" .format (poly_wo_overlaps ))
222
213
223
214
poly_coord = []
224
215
for poly_el in poly_list :
@@ -272,9 +263,9 @@ def _cityscapes_files_to_dict(files, from_json, to_polygons):
272
263
if to_polygons :
273
264
# This conversion comes from D4809743 and D5171122,
274
265
# when Mask-RCNN was first developed.
275
- contours = cv2 .findContours (
276
- mask . copy (), cv2 . RETR_EXTERNAL , cv2 . CHAIN_APPROX_NONE
277
- )[ - 2 ]
266
+ contours = cv2 .findContours (mask . copy (), cv2 . RETR_EXTERNAL , cv2 . CHAIN_APPROX_NONE )[
267
+ - 2
268
+ ]
278
269
polygons = [c .reshape (- 1 ).tolist () for c in contours if len (c ) >= 3 ]
279
270
# opencv's can produce invalid polygons
280
271
if len (polygons ) == 0 :
@@ -318,9 +309,7 @@ def main() -> None:
318
309
)
319
310
logger .info ("Done loading {} samples." .format (len (dicts )))
320
311
321
- thing_classes = [
322
- k .name for k in labels if k .hasInstances and not k .ignoreInEval
323
- ]
312
+ thing_classes = [k .name for k in labels if k .hasInstances and not k .ignoreInEval ]
324
313
meta = Metadata ().set (thing_classes = thing_classes )
325
314
326
315
else :
0 commit comments