-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
149 lines (122 loc) · 4.47 KB
/
Copy pathdataset.py
File metadata and controls
149 lines (122 loc) · 4.47 KB
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
from typing import Dict, List, Tuple
import tensorflow as tf
import tensorflow_datasets as tfds
from utils import ImageEnhancements
class KittiDataset:
def __init__(self, image_enhancer: ImageEnhancements, image_enhance: bool = False):
self.input_shape: Tuple[int, int, int] = (224, 224, 3)
self.datatype: tf.DType = tf.float32
self.batch_size: int = 64
self.shuffle_seed: int = 15
self.image_enhance = image_enhance
self.image_enhancer = image_enhancer
self.training_dataset: List[Dict] = None
self.validation_dataset: List[Dict] = None
self.testing_dataset: List[Dict] = None
self.load_dataset()
self.process_datasets()
def load_dataset(self) -> None:
"""
Loads Kitti training and testing datasets.
"""
self.training_dataset = tfds.load("kitti", split="train", as_supervised=False)
self.validation_dataset = tfds.load(
"kitti", split="validation", as_supervised=False
)
self.testing_dataset = tfds.load("kitti", split="test", as_supervised=False)
def _enhance_batch_dataset(self, x, y) -> Dict:
"""
Applies image enhancements to a batch of images using ImageEnhancements class.
"""
enhanced_images = tf.map_fn(
self.image_enhancer.image_enhance_tensor,
x["image"],
fn_output_signature=tf.float32,
)
enhanced_images.set_shape(x["image"].shape)
enhanced_x = {
"image": enhanced_images,
"box_metadata": x["box_metadata"],
}
return enhanced_x, y
def _crop_image_to_single_obj(self, data: Dict) -> tf.data.Dataset:
"""
Crops multi-object images to single objects using bounding box annotations.
"""
# Extract annotations from data
image = tf.cast(data["image"], self.datatype) / 255
boxes = tf.cast(data["objects"]["bbox"], self.datatype)
dimensions_3d = tf.cast(data["objects"]["dimensions"], self.datatype)
# Calculate xy location of bounding boxes in image, helps with actual geometry estimations
ymin = boxes[:, 0]
xmin = boxes[:, 1]
ymax = boxes[:, 2]
xmax = boxes[:, 3]
box_width = xmax - xmin
box_height = ymax - ymin
box_center_x = (xmin + xmax) / 2
box_center_y = (ymin + ymax) / 2
box_xy_metadata = tf.stack(
[
box_width,
box_height,
box_center_x,
box_center_y,
],
axis=1,
)
# Crop image to single 2d object images
num_objects_in_img = tf.shape(boxes)[0]
image_batch = tf.expand_dims(image, axis=0)
box_indices = tf.zeros((num_objects_in_img,), dtype=tf.int32)
crops = tf.image.crop_and_resize(
image=image_batch,
boxes=boxes,
box_indices=box_indices,
crop_size=self.input_shape[:2],
method="bilinear",
)
return tf.data.Dataset.from_tensor_slices(
(
{
"image": crops,
"box_metadata": box_xy_metadata,
},
dimensions_3d,
)
)
def _prepare_dataset(self, dataset, training: bool):
"""
Convert images to single-object image dataset.
"""
dataset = dataset.flat_map(self._crop_image_to_single_obj)
if training:
dataset = dataset.shuffle(
buffer_size=4096,
seed=self.shuffle_seed,
reshuffle_each_iteration=True,
)
dataset = dataset.batch(self.batch_size)
if self.image_enhance:
dataset = dataset.map(
self._enhance_batch_dataset,
num_parallel_calls=tf.data.AUTOTUNE,
)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
return dataset
def process_datasets(self) -> None:
"""
Processes training, validation and testing datasets for object size estimations.
"""
self.training_dataset = self._prepare_dataset(
self.training_dataset,
training=True,
)
self.validation_dataset = self._prepare_dataset(
self.validation_dataset,
training=False,
)
self.testing_dataset = self._prepare_dataset(
self.testing_dataset,
training=False,
)