code: #!/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),
  
apis: numpy.array numpy.zeros