forked from onlytailei/carla_cil_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
carla_loader.py
133 lines (119 loc) · 4.4 KB
/
carla_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
#!/usr/bin/env python
# coding=utf-8
'''
Author:Tai Lei
Date:Thu Nov 22 12:09:27 2018
Info:
'''
import glob
import numpy as np
import h5py
import torch
from torchvision import transforms
from torch.utils.data import Dataset
from imgaug import augmenters as iaa
from helper import RandomTransWrapper
class CarlaH5Data():
def __init__(self,
train_folder,
eval_folder,
batch_size=4, num_workers=4, distributed=False):
self.loaders = {
"train": torch.utils.data.DataLoader(
CarlaH5Dataset(
data_dir=train_folder,
train_eval_flag="train"),
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
shuffle=True
),
"eval": torch.utils.data.DataLoader(
CarlaH5Dataset(
data_dir=eval_folder,
train_eval_flag="eval"),
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
shuffle=False
)}
class CarlaH5Dataset(Dataset):
def __init__(self, data_dir,
train_eval_flag="train", sequence_len=200):
self.data_dir = data_dir
self.data_list = glob.glob(data_dir+'*.h5')
self.data_list.sort()
self.sequnece_len = sequence_len
self.train_eval_flag = train_eval_flag
self.build_transform()
def build_transform(self):
if self.train_eval_flag == "train":
self.transform = transforms.Compose([
transforms.RandomOrder([
RandomTransWrapper(
seq=iaa.GaussianBlur(
(0, 1.5)),
p=0.09),
RandomTransWrapper(
seq=iaa.AdditiveGaussianNoise(
loc=0,
scale=(0.0, 0.05),
per_channel=0.5),
p=0.09),
RandomTransWrapper(
seq=iaa.Dropout(
(0.0, 0.10),
per_channel=0.5),
p=0.3),
RandomTransWrapper(
seq=iaa.CoarseDropout(
(0.0, 0.10),
size_percent=(0.08, 0.2),
per_channel=0.5),
p=0.3),
RandomTransWrapper(
seq=iaa.Add(
(-20, 20),
per_channel=0.5),
p=0.3),
RandomTransWrapper(
seq=iaa.Multiply(
(0.9, 1.1),
per_channel=0.2),
p=0.4),
RandomTransWrapper(
seq=iaa.ContrastNormalization(
(0.8, 1.2),
per_channel=0.5),
p=0.09),
]),
transforms.ToTensor()])
else:
self.transform = transforms.Compose([
transforms.ToTensor(),
])
def __len__(self):
return self.sequnece_len * len(self.data_list)
def __getitem__(self, idx):
data_idx = idx // self.sequnece_len
file_idx = idx % self.sequnece_len
file_name = self.data_list[data_idx]
with h5py.File(file_name, 'r') as h5_file:
img = np.array(h5_file['rgb'])[file_idx]
img = self.transform(img)
target = np.array(h5_file['targets'])[file_idx]
target = target.astype(np.float32)
# 2 Follow lane, 3 Left, 4 Right, 5 Straight
# -> 0 Follow lane, 1 Left, 2 Right, 3 Straight
command = int(target[24])-2
# Steer, Gas, Brake (0,1, focus on steer loss)
target_vec = np.zeros((4, 3), dtype=np.float32)
target_vec[command, :] = target[:3]
# in km/h, <90
speed = np.array([target[10]/90, ]).astype(np.float32)
mask_vec = np.zeros((4, 3), dtype=np.float32)
mask_vec[command, :] = 1
# TODO
# add preprocess
return img, speed, target_vec.reshape(-1), \
mask_vec.reshape(-1),