-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathade.py
79 lines (71 loc) · 3.35 KB
/
ade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright (c) 2021 PPViT Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import numpy as np
from PIL import Image
from src.datasets import Dataset
from src.transforms import Compose
import src.transforms.functional as F
class ADE20K(Dataset):
"""ADE20K
It is a densely annotated dataset with the instances of stuff, objects,
and parts, covering a diverse set of visual concepts in scenes. The
annotated images cover the scene categories from the SUN and Places database.
Args:
transforms (list): A list of image transformations.
dataset_root (str, optional): The ADK20K dataset directory. Default: None.
mode (str, optional): A subset of the entire dataset.
It should be one of ('train', 'val'). Default: 'train'.
num_classes (int): the number of classes
"""
def __init__(self, transforms, dataset_root=None, mode='train', num_classes=150):
super(ADE20K, self).__init__(transforms=transforms, num_classes=num_classes,
dataset_root=dataset_root, mode=mode)
self.dataset_root = dataset_root
self.transforms = Compose(transforms)
mode = mode.lower()
self.mode = mode
self.file_list = list()
self.num_classes = num_classes
self.ignore_index = 255
if mode not in ['train', 'val']:
raise ValueError("`mode` should be one of ('train', 'val') in"
"ADE20K dataset, but got {}.".format(mode))
if mode == 'train':
img_dir = os.path.join(self.dataset_root, 'images/training')
label_dir = os.path.join(self.dataset_root, 'annotations/training')
elif mode == 'val':
img_dir = os.path.join(self.dataset_root, 'images/validation')
label_dir = os.path.join(self.dataset_root,'annotations/validation')
img_files = os.listdir(img_dir)
label_files = [i.replace('.jpg', '.png') for i in img_files]
for i in range(len(img_files)):
img_path = os.path.join(img_dir, img_files[i])
label_path = os.path.join(label_dir, label_files[i])
self.file_list.append([img_path, label_path])
def __getitem__(self, idx):
image_path, label_path = self.file_list[idx]
if self.mode == 'val':
img, label = self.transforms(img=image_path, label=label_path)
# The class 0 is ignored. And it will equal to 255 after
# subtracted 1, because the dtype of label is uint8.
label = label - 1
label = label[np.newaxis, :, :]
return img, label
else:
img, label = self.transforms(img=image_path, label=label_path)
label = label - 1
# Recover the ignore pixels adding by transform
label[label == 254] = 255
return img, label