forked from ikergarcia1996/Self-Driving-Car-in-Video-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.py
210 lines (171 loc) · 6.47 KB
/
Dataset.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from __future__ import print_function, division
import os
import torch
from skimage import io
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import glob
from typing import List
class RemoveMinimap(object):
"""Remove minimap (black square) from the sequence"""
def __init__(self, hide_map_prob):
"""
Init
Input:
-hide_map_prob: Probability for removing the minimap (black square)
from the sequence of images (0<=hide_map_prob<=1)
"""
self.hide_map_prob = hide_map_prob
def __call__(self, sample):
image, y = (
sample["image"],
sample["y"],
)
width: int = int(image.shape[1] / 5)
if torch.rand(1)[0] <= self.hide_map_prob:
for j in range(0, 5):
image[215:, j * width : (j * width) + 80] = np.zeros(
(55, 80, 3), dtype=image.dtype
)
return {
"image": image,
"y": y,
}
class RemoveImage(object):
"""Remove images (black square) from the sequence"""
def __init__(self, dropout_images_prob):
"""
Init
Input:
- dropout_images_prob List of 5 floats or None, probability for removing each input image during training
(black image) from a training example (0<=dropout_images_prob<=1)
"""
self.dropout_images_prob = dropout_images_prob
def __call__(self, sample):
image, y = (
sample["image"],
sample["y"],
)
width: int = int(image.shape[1] / 5)
for j in range(0, 5):
if torch.rand(1)[0] <= self.dropout_images_prob[j]:
image[:, j * width : (j + 1) * width] = np.zeros(
(image.shape[0], width, image.shape[2]), dtype=image.dtype
)
return {
"image": image,
"y": y,
}
class SplitImages(object):
"""Splits the sequence into 5 images"""
def __call__(self, sample):
image, y = sample["image"], sample["y"]
width: int = int(image.shape[1] / 5)
return {
"image1": image[:, 0:width],
"image2": image[:, width : width * 2],
"image3": image[:, width * 2 : width * 3],
"image4": image[:, width * 3 : width * 4],
"image5": image[:, width * 4 : width * 5],
"y": y,
}
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
image1, image2, image3, image4, image5, y = (
sample["image1"],
sample["image2"],
sample["image3"],
sample["image4"],
sample["image5"],
sample["y"],
)
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image1 = image1.transpose((2, 0, 1))
image2 = image2.transpose((2, 0, 1))
image3 = image3.transpose((2, 0, 1))
image4 = image4.transpose((2, 0, 1))
image5 = image5.transpose((2, 0, 1))
return {
"image1": torch.from_numpy(image1),
"image2": torch.from_numpy(image2),
"image3": torch.from_numpy(image3),
"image4": torch.from_numpy(image4),
"image5": torch.from_numpy(image5),
"y": torch.tensor(y).long(),
}
class Normalize(object):
"""Normalize image"""
transform = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
def __call__(self, sample):
image1, image2, image3, image4, image5, y = (
sample["image1"],
sample["image2"],
sample["image3"],
sample["image4"],
sample["image5"],
sample["y"],
)
return {
"image1": self.transform(image1 / 255.0),
"image2": self.transform(image2 / 255.0),
"image3": self.transform(image3 / 255.0),
"image4": self.transform(image4 / 255.0),
"image5": self.transform(image5 / 255.0),
"y": y,
}
class Tedd1104Dataset(Dataset):
"""TEDD1104 dataset."""
def __init__(
self, dataset_dir: str, hide_map_prob: float, dropout_images_prob: List[float]
):
"""
Init
Input:
-dataset_dir: Directory containing the dataset files
-hide_map_prob: Probability for removing the minimap (black square)
from the sequence of images (0<=hide_map_prob<=1)
- dropout_images_prob List of 5 floats or None, probability for removing each input image during training
(black image) from a training example (0<=dropout_images_prob<=1)
"""
assert 0 <= hide_map_prob <= 1.0, (
f"hide_map_prob not in 0 <= hide_map_prob <= 1.0 range. "
f"hide_map_prob: {hide_map_prob}"
)
assert len(dropout_images_prob) == 5, (
f"dropout_images_prob must have 5 probabilities, one for each image in the sequence. "
f"dropout_images_prob len: {len(dropout_images_prob)}"
)
for dropout_image_prob in dropout_images_prob:
assert 0 <= dropout_image_prob <= 1.0, (
f"All probabilities in dropout_image_prob must be in the range 0 <= dropout_image_prob <= 1.0. "
f"dropout_images_prob: {dropout_images_prob}"
)
self.dataset_dir = dataset_dir
self.hide_map_prob = hide_map_prob
self.dropout_images_prob = dropout_images_prob
self.transform = transforms.Compose(
[
RemoveMinimap(hide_map_prob=hide_map_prob),
RemoveImage(dropout_images_prob=dropout_images_prob),
SplitImages(),
ToTensor(),
Normalize(),
]
)
self.dataset_files = glob.glob(os.path.join(dataset_dir, "*.jpeg"))
def __len__(self):
return len(self.dataset_files)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = self.dataset_files[idx]
image = io.imread(img_name)
y = int(os.path.basename(img_name)[-6])
sample = {"image": image, "y": y}
return self.transform(sample)