upload dataset file to repo
Browse files- lisa_data/mapillary.py +66 -0
lisa_data/mapillary.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from pycocotools import mask as maskUtils
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
import glob
|
| 8 |
+
|
| 9 |
+
def singleMask2rle(mask):
|
| 10 |
+
rle = maskUtils.encode(np.array(mask[:, :, None], order='F', dtype="uint8"))[0]
|
| 11 |
+
rle["counts"] = rle["counts"].decode("utf-8")
|
| 12 |
+
return rle
|
| 13 |
+
|
| 14 |
+
def init_mapillary(base_image_dir):
|
| 15 |
+
mapillary_data_root = os.path.join(base_image_dir, "mapillary")
|
| 16 |
+
with open(os.path.join(mapillary_data_root, "config_v2.0.json")) as f:
|
| 17 |
+
mapillary_classes = json.load(f)["labels"]
|
| 18 |
+
mapillary_classes = [x["readable"].lower() for x in mapillary_classes]
|
| 19 |
+
mapillary_classes = np.array(mapillary_classes)
|
| 20 |
+
mapillary_labels = sorted(
|
| 21 |
+
glob.glob(
|
| 22 |
+
os.path.join(mapillary_data_root, "training", "v2.0", "labels", "*.png")
|
| 23 |
+
)
|
| 24 |
+
)
|
| 25 |
+
mapillary_images = [
|
| 26 |
+
x.replace(".png", ".jpg").replace("v2.0/labels", "images")
|
| 27 |
+
for x in mapillary_labels
|
| 28 |
+
]
|
| 29 |
+
print("mapillary: ", len(mapillary_images))
|
| 30 |
+
return mapillary_classes, mapillary_images, mapillary_labels
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
base_image_dir = '/mnt/workspace/workgroup/yuanyq/code/LISA/dataset'
|
| 35 |
+
classes, images, labels = init_mapillary(base_image_dir)
|
| 36 |
+
final_data = []
|
| 37 |
+
for idx in tqdm(range(len(images))):
|
| 38 |
+
dic = {}
|
| 39 |
+
image_path = images[idx]
|
| 40 |
+
label_path = labels[idx]
|
| 41 |
+
label = Image.open(label_path)
|
| 42 |
+
label = np.array(label)
|
| 43 |
+
|
| 44 |
+
unique_label = np.unique(label).tolist()
|
| 45 |
+
if 255 in unique_label:
|
| 46 |
+
unique_label.remove(255)
|
| 47 |
+
if len(unique_label) == 0:
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
cats = []
|
| 51 |
+
for class_id in unique_label:
|
| 52 |
+
cats.append(classes[class_id])
|
| 53 |
+
masks = []
|
| 54 |
+
for class_id in unique_label:
|
| 55 |
+
msk = label==class_id
|
| 56 |
+
rle = singleMask2rle(msk)
|
| 57 |
+
masks.append(rle)
|
| 58 |
+
|
| 59 |
+
dic['image'] = image_path.replace('/mnt/workspace/workgroup/yuanyq/code/LISA/dataset/', '')
|
| 60 |
+
dic['cat'] = cats
|
| 61 |
+
dic['masks'] = masks
|
| 62 |
+
final_data.append(dic)
|
| 63 |
+
|
| 64 |
+
print(len(final_data))
|
| 65 |
+
with open('mapillary.json', 'w') as f:
|
| 66 |
+
f.write(json.dumps(final_data))
|