Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 6f65af2

Browse files
Joeper214ruimashita
authored andcommitted
lmnet: Add cityscapes loader (#300)
1 parent d24cd75 commit 6f65af2

File tree

22 files changed

+3013
-0
lines changed

22 files changed

+3013
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2019 The Blueoil Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# =============================================================================
16+
from easydict import EasyDict
17+
import tensorflow as tf
18+
19+
from lmnet.common import Tasks
20+
from lmnet.networks.segmentation.lm_segnet_quantize import LmSegnetQuantize
21+
from lmnet.datasets.cityscapes import Cityscapes
22+
from lmnet.data_processor import Sequence
23+
from lmnet.pre_processor import (
24+
DivideBy255,
25+
Resize
26+
)
27+
from lmnet.data_augmentor import (
28+
Brightness,
29+
Color,
30+
Contrast,
31+
FlipLeftRight,
32+
Hue,
33+
)
34+
from lmnet.quantizations import (
35+
binary_mean_scaling_quantizer,
36+
linear_mid_tread_half_quantizer,
37+
)
38+
39+
IS_DEBUG = False
40+
41+
NETWORK_CLASS = LmSegnetQuantize
42+
DATASET_CLASS = Cityscapes
43+
44+
IMAGE_SIZE = [160, 320]
45+
BATCH_SIZE = 8
46+
DATA_FORMAT = "NHWC"
47+
TASK = Tasks.SEMANTIC_SEGMENTATION
48+
CLASSES = DATASET_CLASS.classes
49+
50+
MAX_STEPS = 150000
51+
SAVE_STEPS = 3000
52+
TEST_STEPS = 1000
53+
SUMMARISE_STEPS = 1000
54+
55+
# distributed training
56+
IS_DISTRIBUTION = False
57+
58+
# pretrain
59+
IS_PRETRAIN = False
60+
PRETRAIN_VARS = []
61+
PRETRAIN_DIR = ""
62+
PRETRAIN_FILE = ""
63+
64+
# for debug
65+
# BATCH_SIZE = 2
66+
# SUMMARISE_STEPS = 1
67+
# IS_DEBUG = True
68+
69+
PRE_PROCESSOR = Sequence([
70+
Resize(size=IMAGE_SIZE),
71+
DivideBy255()
72+
])
73+
POST_PROCESSOR = None
74+
75+
NETWORK = EasyDict()
76+
NETWORK.OPTIMIZER_CLASS = tf.train.AdamOptimizer
77+
NETWORK.OPTIMIZER_KWARGS = {"learning_rate": 0.001}
78+
NETWORK.IMAGE_SIZE = IMAGE_SIZE
79+
NETWORK.BATCH_SIZE = BATCH_SIZE
80+
NETWORK.DATA_FORMAT = DATA_FORMAT
81+
NETWORK.ACTIVATION_QUANTIZER = linear_mid_tread_half_quantizer
82+
NETWORK.ACTIVATION_QUANTIZER_KWARGS = {
83+
'bit': 2,
84+
'max_value': 2
85+
}
86+
NETWORK.WEIGHT_QUANTIZER = binary_mean_scaling_quantizer
87+
NETWORK.WEIGHT_QUANTIZER_KWARGS = {}
88+
89+
DATASET = EasyDict()
90+
DATASET.BATCH_SIZE = BATCH_SIZE
91+
DATASET.DATA_FORMAT = DATA_FORMAT
92+
DATASET.PRE_PROCESSOR = PRE_PROCESSOR
93+
DATASET.AUGMENTOR = Sequence([
94+
Brightness((0.75, 1.25)),
95+
Color((0.75, 1.25)),
96+
Contrast((0.75, 1.25)),
97+
FlipLeftRight(),
98+
Hue((-10, 10)),
99+
])
100+
DATASET.ENABLE_PREFETCH = True

lmnet/lmnet/datasets/cityscapes.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2019 The Blueoil Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# =============================================================================
16+
# Support for cityscapes dataset
17+
# https://www.cityscapes-dataset.com/
18+
19+
import functools
20+
import glob
21+
import os.path
22+
import numpy as np
23+
24+
from PIL import Image
25+
26+
from lmnet.datasets.base import SegmentationBase
27+
28+
29+
class Cityscapes(SegmentationBase):
30+
available_subsets = ["train", "validation", "test"]
31+
extend_dir = "cityscapes"
32+
classes = [
33+
"unlabeled",
34+
"ego vehicle",
35+
"rectification boarder",
36+
"out of roi",
37+
"static",
38+
"dynamic",
39+
"ground",
40+
"road",
41+
"sidewalk",
42+
"parking",
43+
"rail track",
44+
"building",
45+
"wall",
46+
"fence",
47+
"guard rail",
48+
"bridge",
49+
"tunnel",
50+
"pole",
51+
"polegroup",
52+
"traffic light",
53+
"traffic sign",
54+
"vegetation",
55+
"terrain",
56+
"sky",
57+
"person",
58+
"rider",
59+
"car",
60+
"truck",
61+
"bus",
62+
"caravan",
63+
"trailer",
64+
"train",
65+
"motorcycle",
66+
"bicycle",
67+
]
68+
num_classes = len(classes)
69+
70+
def __init__(self, batch_size=10, *args, **kwargs):
71+
super().__init__(batch_size=batch_size, *args, **kwargs)
72+
73+
@property
74+
def label_colors(self):
75+
unlabeled = [0, 0, 0]
76+
ego_vehicle = [0, 0, 0]
77+
rectification_boarder = [0, 0, 0]
78+
out_of_roi = [0, 0, 0]
79+
static = [0, 0, 0]
80+
dynamic = [111, 74, 0]
81+
ground = [81, 0, 81]
82+
road = [128, 64, 128]
83+
sidewalk = [244, 35, 232]
84+
parking = [250, 170, 160]
85+
rail_track = [230, 150, 140]
86+
building = [70, 70, 70]
87+
wall = [102, 102, 156]
88+
fence = [190, 153, 153]
89+
guard_rail = [180, 165, 180]
90+
bridge = [150, 100, 100]
91+
tunnel = [150, 120, 90]
92+
pole = [153, 153, 153]
93+
polegroup = [153, 153, 153]
94+
traffic_light = [250, 170, 30]
95+
traffic_sign = [220, 220, 0]
96+
vegetation = [107, 142, 35]
97+
terrain = [152, 251, 152]
98+
sky = [70, 130, 180]
99+
person = [220, 20, 60]
100+
rider = [255, 0, 0]
101+
car = [0, 0, 142]
102+
truck = [0, 0, 70]
103+
bus = [0, 60, 100]
104+
caravan = [0, 0, 90]
105+
trailer = [0, 0, 110]
106+
train = [0, 80, 100]
107+
motorcycle = [0, 0, 230]
108+
bicycle = [119, 11, 32]
109+
110+
return np.array([
111+
unlabeled, ego_vehicle, rectification_boarder, out_of_roi, static,
112+
dynamic, ground, road, sidewalk, parking, rail_track, building,
113+
wall, fence, guard_rail, bridge, tunnel, pole, polegroup,
114+
traffic_light, traffic_sign, vegetation, terrain, sky, person,
115+
rider, car, truck, bus, caravan, trailer, train, motorcycle,
116+
bicycle])
117+
118+
@functools.lru_cache(maxsize=None)
119+
def files_and_annotations(self):
120+
split = "train"
121+
if self.subset == "validation":
122+
split = "val"
123+
elif self.subset == "test":
124+
split = "test"
125+
polygons_json = glob.glob(os.path.join(self.data_dir, "gtFine", split, "*", "*_gt*_polygons.json"))
126+
polygons_json.sort()
127+
128+
labelIds = [i.replace("_polygons.json", "_labelIds.png") for i in polygons_json]
129+
leftImg8bit = [i.replace(
130+
os.path.join(self.data_dir, "gtFine"),
131+
os.path.join(self.data_dir, "leftImg8bit")
132+
).replace("_gtFine_polygons.json", "_leftImg8bit.png") for i in polygons_json]
133+
134+
return leftImg8bit, labelIds
135+
136+
def __getitem__(self, i):
137+
imgs, labels = self.files_and_annotations()
138+
img = Image.open(imgs[i])
139+
label = Image.open(labels[i])
140+
141+
return np.array(img), np.array(label)
142+
143+
def __len__(self):
144+
return len(self.files_and_annotations()[0])
145+
146+
@property
147+
def num_per_epoch(self):
148+
return len(self.files_and_annotations()[0])

lmnet/tests/check_dataset_storage.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from lmnet.datasets.caltech101 import Caltech101
3838
from lmnet.datasets.cifar10 import Cifar10
3939
from lmnet.datasets.camvid import Camvid
40+
from lmnet.datasets.cityscapes import Cityscapes
4041
from lmnet.datasets.ilsvrc_2012 import Ilsvrc2012
4142
from lmnet.datasets.lm_things_on_a_table import LmThingsOnATable
4243
from lmnet.datasets.mscoco import (
@@ -558,6 +559,38 @@ def test_camvid():
558559
assert labels.shape[2] == image_size[1]
559560

560561

562+
def test_cityscapes():
563+
batch_size = 3
564+
image_size = [256, 512]
565+
dataset = Cityscapes(
566+
batch_size=batch_size,
567+
pre_processor=Resize(image_size))
568+
dataset = DatasetIterator(dataset)
569+
570+
val_dataset = Cityscapes(
571+
subset="validation",
572+
batch_size=batch_size,
573+
pre_precessor=Resize(image_size)
574+
)
575+
576+
assert dataset.num_classes == 34
577+
assert dataset.num_per_epoch == 2975
578+
assert val_dataset.num_per_epoch == 500
579+
580+
for _ in range(STEP_SIZE):
581+
images, labels = dataset.feed()
582+
assert isinstance(images, np.ndarray)
583+
assert images.shape[0] == batch_size
584+
assert images.shape[1] == image_size[0]
585+
assert images.shape[2] == image_size[1]
586+
assert images.shape[3] == 3
587+
588+
assert isinstance(labels, np.ndarray)
589+
assert labels.shape[0] == batch_size
590+
assert labels.shape[1] == image_size[0]
591+
assert labels.shape[2] == image_size[1]
592+
593+
561594
def test_lm_things_of_a_table():
562595
batch_size = 3
563596
image_size = [256, 512]
@@ -862,6 +895,7 @@ def test_bdd100k():
862895
test_caltech101()
863896
test_cifar10()
864897
test_camvid()
898+
test_cityscapes()
865899
test_pascalvoc_2007()
866900
test_pascalvoc_2007_not_skip_difficult()
867901
test_pascalvoc_2007_with_target_classes()
16.5 KB
Loading
11.7 KB
Loading

0 commit comments

Comments
 (0)