-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_loader.py
221 lines (166 loc) · 6.98 KB
/
dataset_loader.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
211
212
213
214
215
216
217
218
219
220
221
from typing import Tuple
from torch.utils.data import Dataset, DataLoader, ConcatDataset, random_split
from config import config
import pandas as pd
import numpy as np
import os
import torch
import cv2
def add_random_shadow_bgr(img):
# Convert the image to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Define a random shadow intensity and region
intensity = 0.4
x1, x2 = np.random.randint(0, img.shape[1], size=2)
if x1 > x2:
x1, x2 = x2, x1
# Apply the shadow
hsv[:, x1:x2, 2] = hsv[:, x1:x2, 2] * intensity
# Convert the image back to RGB
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def add_random_brightness_bgr(img):
# Convert the image to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Generate a random brightness offset
offset = np.random.randint(-50, 50)
# Add the offset to the V channel
hsv[:, :, 2] = np.clip(hsv[:, :, 2] + offset, 0, 255)
# Convert the image back to RGB
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def convert_opencv_image_to_torch(image):
# Convert the image from OpenCV numpy format to PyTorch format
# From (H x W x C) to (C x H x W) and convert to float
image = np.transpose(image, (2, 0, 1))
image = torch.from_numpy(image).float()
# Normalize the image
image = (image / 127.5) - 1.0
return image
class UdacitySimulatorDataset(Dataset):
def __init__(self, csv_file="driving_log.csv", root_dir="datasets/udacity_sim_data_2"):
self.dataset_folder = root_dir
self.data = pd.read_csv(os.path.join(root_dir, csv_file))
def __len__(self):
return len(self.data) * 3 * 3 # Each row contains three images
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_idx = idx // 9 # Get the corresponding row in the CSV file
img_type = (idx // 3) % 3 # Get the type of image (0=center, 1=left, 2=right)
augmentation_type = idx % 3 # Get the type of augmentation (0=none, 1=flip, 2=brightness)
if img_type == 0:
img_file = self.data.iloc[img_idx]['center']
angle = round(float(self.data.iloc[img_idx]['steering']), 4)
elif img_type == 1:
img_file = self.data.iloc[img_idx]['left']
angle = round(float(self.data.iloc[img_idx]['steering']) + 0.20, 4) # Adjust steering angle
else: # img_type == 2
img_file = self.data.iloc[img_idx]['right']
angle = round(float(self.data.iloc[img_idx]['steering']) - 0.20, 4) # Adjust steering angle
img_name = os.path.join(self.dataset_folder, 'IMG', os.path.basename(img_file))
image = cv2.imread(img_name)
# Augmentation. Apply selected augmentation
if augmentation_type == 1:
image = add_random_brightness_bgr(image)
elif augmentation_type == 2:
image = cv2.flip(image, 1) # 1 for horizontal flipping
angle = angle * -1.0
else:
pass # No augmentation
# Convert the image to YUV
image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
# resize image to 200x66
image = cv2.resize(image, (200, 66))
torch_image = convert_opencv_image_to_torch(image)
return torch_image, angle
class CarlaSimulatorDataset(Dataset):
def __init__(self, csv_file="steering_data.csv", root_dir="dataset/dataset_carla_001_town04"):
self.dataset_folder = root_dir
self.data = pd.read_csv(os.path.join(root_dir, csv_file))
def __len__(self):
return len(self.data) * 3 # Triple the dataset size
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_idx = idx // 3 # Corrected from 9 to 3
img_name = os.path.join(os.path.join(self.dataset_folder, 'images'), self.data.iloc[img_idx]['frame_name'])
image = cv2.imread(img_name)
angle = round(float(self.data.iloc[img_idx]['steering_angle']), 4)
# Get the type of augmentation (0=none, 1=flip, 2=shadows)
augmentation_type = idx % 3
# Apply selected augmentation
if augmentation_type == 1: # Changed from 0 to 1
image = add_random_brightness_bgr(image)
elif augmentation_type == 2: # Changed from 1 to 2
image = cv2.flip(image, 1) # 1 for horizontal flipping
angle = angle * -1.0
else:
pass # No augmentation
# Convert the image to YUV
image = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
# resize image to 200x66
image = cv2.resize(image, (200, 66))
torch_image = convert_opencv_image_to_torch(image)
return torch_image, angle
def get_inference_dataset(dataset_type='carla_001'):
if dataset_type == 'carla_001':
return CarlaSimulatorDataset(
root_dir="datasets/dataset_carla_001_town04"
)
elif dataset_type == 'carla_002':
return CarlaSimulatorDataset(
root_dir="datasets/dataset_carla_002_town02_small"
)
elif dataset_type == 'carla_003':
return CarlaSimulatorDataset(
root_dir="datasets/dataset_carla_003_town01_small"
)
elif dataset_type == 'carla_004':
return CarlaSimulatorDataset(
root_dir="datasets/dataset_carla_004_town04_2"
)
elif dataset_type == 'udacity_sim_1':
return UdacitySimulatorDataset(
root_dir="datasets/udacity_sim_data_1"
)
elif dataset_type == 'udacity_sim_2':
return UdacitySimulatorDataset(
root_dir="datasets/udacity_sim_data_2"
)
else:
raise ValueError("Invalid dataset type")
def get_datasets(dataset_types=['carla_001']) -> Dataset:
datasets_list = []
for dataset_type in dataset_types:
dataset = get_inference_dataset(dataset_type)
datasets_list.append(dataset)
dataset_concatenated = ConcatDataset(datasets_list)
return dataset_concatenated
def get_data_subsets_loaders(dataset_types=['udacity_sim_2'], batch_size=config.batch_size) -> Tuple[DataLoader, DataLoader]:
loades_datasets = []
for dataset_type in dataset_types:
dataset = get_inference_dataset(dataset_type)
loades_datasets.append(dataset)
merged_dataset = ConcatDataset(loades_datasets)
train_set, val_set = random_split(merged_dataset, [config.train_split_size, config.test_split_size])
train_subset_loader = DataLoader(
train_set,
batch_size=batch_size,
shuffle=config.shuffle,
num_workers=config.num_workers
)
val_subset_loader = DataLoader(
val_set,
batch_size=batch_size,
shuffle=config.shuffle,
num_workers=config.num_workers
)
return train_subset_loader, val_subset_loader
def get_full_dataset_loader(dataset_type='carla_001') -> DataLoader:
dataset = get_inference_dataset(dataset_type)
full_dataset_loader = DataLoader(
dataset,
batch_size=1,
shuffle=False,
num_workers=1
)
return full_dataset_loader