upload dataset file to repo
Browse files- lisa_data/ade.py +77 -0
lisa_data/ade.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
def singleMask2rle(mask):
|
| 8 |
+
rle = maskUtils.encode(np.array(mask[:, :, None], order='F', dtype="uint8"))[0]
|
| 9 |
+
rle["counts"] = rle["counts"].decode("utf-8")
|
| 10 |
+
return rle
|
| 11 |
+
|
| 12 |
+
def init_ade20k(base_image_dir):
|
| 13 |
+
with open("../ade20k_classes.json", "r") as f:
|
| 14 |
+
ade20k_classes = json.load(f)
|
| 15 |
+
ade20k_classes = np.array(ade20k_classes)
|
| 16 |
+
image_ids = sorted(
|
| 17 |
+
os.listdir(os.path.join(base_image_dir, "ade20k/images", "training"))
|
| 18 |
+
)
|
| 19 |
+
ade20k_image_ids = []
|
| 20 |
+
for x in image_ids:
|
| 21 |
+
if x.endswith(".jpg"):
|
| 22 |
+
ade20k_image_ids.append(x[:-4])
|
| 23 |
+
ade20k_images = []
|
| 24 |
+
for image_id in ade20k_image_ids: # self.descriptions:
|
| 25 |
+
ade20k_images.append(
|
| 26 |
+
os.path.join(
|
| 27 |
+
base_image_dir,
|
| 28 |
+
"ade20k",
|
| 29 |
+
"images",
|
| 30 |
+
"training",
|
| 31 |
+
"{}.jpg".format(image_id),
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
ade20k_labels = [
|
| 35 |
+
x.replace(".jpg", ".png").replace("images", "annotations")
|
| 36 |
+
for x in ade20k_images
|
| 37 |
+
]
|
| 38 |
+
print("ade20k: ", len(ade20k_images))
|
| 39 |
+
return ade20k_classes, ade20k_images, ade20k_labels
|
| 40 |
+
|
| 41 |
+
base_image_dir = '/mnt/workspace/workgroup/yuanyq/code/LISA/dataset'
|
| 42 |
+
classes, images, labels = init_ade20k(base_image_dir)
|
| 43 |
+
final_data = []
|
| 44 |
+
for idx in tqdm(range(len(images))):
|
| 45 |
+
dic = {}
|
| 46 |
+
image_path = images[idx]
|
| 47 |
+
label_path = labels[idx]
|
| 48 |
+
label = Image.open(label_path)
|
| 49 |
+
label = np.array(label)
|
| 50 |
+
|
| 51 |
+
label[label == 0] = 255
|
| 52 |
+
label -= 1
|
| 53 |
+
label[label == 254] = 255
|
| 54 |
+
|
| 55 |
+
unique_label = np.unique(label).tolist()
|
| 56 |
+
if 255 in unique_label:
|
| 57 |
+
unique_label.remove(255)
|
| 58 |
+
if len(unique_label) == 0:
|
| 59 |
+
continue
|
| 60 |
+
|
| 61 |
+
cats = []
|
| 62 |
+
for class_id in unique_label:
|
| 63 |
+
cats.append(classes[class_id])
|
| 64 |
+
masks = []
|
| 65 |
+
for class_id in unique_label:
|
| 66 |
+
msk = label==class_id
|
| 67 |
+
rle = singleMask2rle(msk)
|
| 68 |
+
masks.append(rle)
|
| 69 |
+
|
| 70 |
+
dic['image'] = image_path.replace('/mnt/workspace/workgroup/yuanyq/code/LISA/dataset/', '')
|
| 71 |
+
dic['cat'] = cats
|
| 72 |
+
dic['masks'] = masks
|
| 73 |
+
final_data.append(dic)
|
| 74 |
+
|
| 75 |
+
print(len(final_data))
|
| 76 |
+
with open('ade20k.json', 'w') as f:
|
| 77 |
+
f.write(json.dumps(final_data))
|