-
Notifications
You must be signed in to change notification settings - Fork 512
/
pascal_voc.py
167 lines (149 loc) · 5.67 KB
/
pascal_voc.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import os
from typing import List
import numpy as np
from corenet.data.datasets import DATASET_REGISTRY
from corenet.data.datasets.segmentation.base_segmentation import (
BaseImageSegmentationDataset,
)
@DATASET_REGISTRY.register(name="pascal", type="segmentation")
class PascalVOCDataset(BaseImageSegmentationDataset):
"""Dataset class for the PASCAL VOC 2012 dataset
The structure of PASCAL VOC dataset should be like this: ::
pascal_voc/VOCdevkit/VOC2012/Annotations
pascal_voc/VOCdevkit/VOC2012/JPEGImages
pascal_voc/VOCdevkit/VOC2012/SegmentationClass
pascal_voc/VOCdevkit/VOC2012/SegmentationClassAug_Visualization
pascal_voc/VOCdevkit/VOC2012/ImageSets
pascal_voc/VOCdevkit/VOC2012/list
pascal_voc/VOCdevkit/VOC2012/SegmentationClassAug
pascal_voc/VOCdevkit/VOC2012/SegmentationObject
Args:
opts: Command-line arguments
"""
def __init__(self, opts: argparse.Namespace, *args, **kwargs) -> None:
super().__init__(opts=opts, *args, **kwargs)
use_coco_data = getattr(opts, "dataset.pascal.use_coco_data")
coco_root_dir = getattr(opts, "dataset.pascal.coco_root_dir")
root = self.root
voc_root_dir = os.path.join(root, "VOC2012")
voc_list_dir = os.path.join(voc_root_dir, "list")
coco_data_file = None
if self.is_training:
# use the PASCAL VOC 2012 train data with augmented data
data_file = os.path.join(voc_list_dir, "train_aug.txt")
if use_coco_data and coco_root_dir is not None:
coco_data_file = os.path.join(coco_root_dir, "train_2017.txt")
assert os.path.isfile(
coco_data_file
), "COCO data file does not exist at: {}".format(coco_root_dir)
else:
data_file = os.path.join(voc_list_dir, "val.txt")
self.images = []
self.masks = []
with open(data_file, "r") as lines:
for line in lines:
line_split = line.split(" ")
rgb_img_loc = voc_root_dir + os.sep + line_split[0].strip()
mask_img_loc = voc_root_dir + os.sep + line_split[1].strip()
assert os.path.isfile(
rgb_img_loc
), "RGB file does not exist at: {}".format(rgb_img_loc)
assert os.path.isfile(
mask_img_loc
), "Mask image does not exist at: {}".format(rgb_img_loc)
self.images.append(rgb_img_loc)
self.masks.append(mask_img_loc)
# if COCO data (mapped in PASCAL VOC format) needs to be used during training
if self.is_training and coco_data_file is not None:
with open(coco_data_file, "r") as lines:
for line in lines:
line_split = line.split(" ")
rgb_img_loc = coco_root_dir + os.sep + line_split[0].rstrip()
mask_img_loc = coco_root_dir + os.sep + line_split[1].rstrip()
assert os.path.isfile(rgb_img_loc)
assert os.path.isfile(mask_img_loc)
self.images.append(rgb_img_loc)
self.masks.append(mask_img_loc)
self.use_coco_data = use_coco_data
self.ignore_label = 255
self.background_idx = 0
self.check_dataset()
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if cls != PascalVOCDataset:
# Don't re-register arguments in subclasses that don't override `add_arguments()`.
return parser
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--dataset.pascal.use-coco-data",
action="store_true",
default=False,
help="Use MS-COCO data for training with PASCAL VOC dataset. Defaults to False.",
)
group.add_argument(
"--dataset.pascal.coco-root-dir",
type=str,
default=None,
help="Location of MS-COCO data. Defaults to None.",
)
return parser
@staticmethod
def color_palette() -> List[int]:
"""Class index to RGB color mapping. The list index corresponds to class id.
Note that the color list is flattened."""
color_codes = [
[0, 0, 0],
[128, 0, 0],
[0, 128, 0],
[128, 128, 0],
[0, 0, 128],
[128, 0, 128],
[0, 128, 128],
[128, 128, 128],
[64, 0, 0],
[192, 0, 0],
[64, 128, 0],
[192, 128, 0],
[64, 0, 128],
[192, 0, 128],
[64, 128, 128],
[192, 128, 128],
[0, 64, 0],
[128, 64, 0],
[0, 192, 0],
[128, 192, 0],
[0, 64, 128],
]
color_codes = np.asarray(color_codes).flatten()
return list(color_codes)
@staticmethod
def class_names() -> List[str]:
"""Class index to class name mapping. Class index corresponds to list index"""
return [
"background",
"aeroplane",
"bicycle",
"bird",
"boat",
"bottle",
"bus",
"car",
"cat",
"chair",
"cow",
"diningtable",
"dog",
"horse",
"motorbike",
"person",
"potted_plant",
"sheep",
"sofa",
"train",
"tv_monitor",
]