1 from __future__ 
import division
 
   15     class_names = np.array([
 
   19     class_names.setflags(write=0)
 
   28     mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
 
   32     def __init__(self, root_dir, split, aug=False):
 
   34         assert split 
in [
'train', 
'test']
 
   40         for date_dir 
in sorted(os.listdir(self.
root_dir)):
 
   41             date_dir = osp.join(self.
root_dir, date_dir)
 
   42             split_dir = osp.join(date_dir, split)
 
   43             for files_dir 
in sorted(os.listdir(split_dir)):
 
   44                 files_dir = osp.join(split_dir, files_dir)
 
   45                 files = set(os.listdir(files_dir))
 
   46                 assert self.
_files.issubset(files), (
 
   47                     'In {}: File set does not match.\n' 
   48                     'Expected: {}\nActual: {}' 
   59         image_file = osp.join(files_dir, 
'image.png')
 
   60         image = skimage.io.imread(image_file)
 
   61         assert image.dtype == np.uint8
 
   62         assert image.ndim == 3
 
   64         depth_file = osp.join(files_dir, 
'depth.npz')
 
   65         depth = np.load(depth_file)[
'arr_0']
 
   66         depth[depth == 0] = np.nan
 
   67         if depth.dtype == np.uint16:
 
   68             depth = depth.astype(np.float32)
 
   70         depth_keep = ~np.isnan(depth)
 
   71         depth[depth_keep] = np.maximum(depth[depth_keep], self.
min_value)
 
   72         depth[depth_keep] = np.minimum(depth[depth_keep], self.
max_value)
 
   73         assert depth.dtype == np.float32
 
   74         assert depth.ndim == 2
 
   76         label_file = osp.join(files_dir, 
'label.png')
 
   77         with open(label_file, 
'r') 
as f:
 
   78             label = np.asarray(PIL.Image.open(f)).astype(np.int32)
 
   79         assert label.dtype == np.int32
 
   80         assert label.ndim == 2
 
   82         depth_gt_file = osp.join(files_dir, 
'depth_gt.npz')
 
   83         depth_gt = np.load(depth_gt_file)[
'arr_0']
 
   84         depth_gt[depth_gt == 0] = np.nan
 
   85         if depth_gt.dtype == np.uint16:
 
   86             depth_gt = depth_gt.astype(np.float32)
 
   88         depth_gt_keep = ~np.isnan(depth_gt)
 
   89         depth_gt[depth_gt_keep] = np.maximum(
 
   91         depth_gt[depth_gt_keep] = np.minimum(
 
   93         assert depth_gt.dtype == np.float32
 
   94         assert depth_gt.ndim == 2
 
  102             if np.random.uniform() < proba_color:
 
  103                 image = image.astype(np.float64)
 
  104                 image[:, :, 0] += np.random.uniform() * 100 - 50
 
  105                 image[:, :, 1] += np.random.uniform() * 100 - 50
 
  106                 image[:, :, 2] += np.random.uniform() * 100 - 50
 
  107                 image = np.clip(image, 0, 255)
 
  108                 image = image.astype(np.uint8)
 
  110             if np.random.uniform() < proba_color:
 
  111                 image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
 
  112                 image = image.astype(np.float64)
 
  113                 image[:, :, 1] *= np.random.uniform() * 1.5 + 0.5
 
  114                 image[:, :, 2] *= np.random.uniform() * 1.5 + 0.5
 
  115                 image = np.clip(image, 0, 255)
 
  116                 image = image.astype(np.uint8)
 
  117                 image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
 
  119             if np.random.uniform() < proba_color:
 
  120                 image = cv2.GaussianBlur(image, (5, 5), np.random.uniform())
 
  122             if np.random.uniform() < proba_color:
 
  123                 image = image.astype(np.float64)
 
  126                 image[:, :, 0] += np.random.normal(
 
  127                     0, np.random.uniform() * 0.1 * 255, (h, w))
 
  128                 image[:, :, 1] += np.random.normal(
 
  129                     0, np.random.uniform() * 0.1 * 255, (h, w))
 
  130                 image[:, :, 2] += np.random.normal(
 
  131                     0, np.random.uniform() * 0.1 * 255, (h, w))
 
  132                 image = np.clip(image, 0, 255)
 
  133                 image = image.astype(np.uint8)
 
  136             if np.random.uniform() < 0.3:
 
  137                 noise_rate = np.random.uniform() * 0.25 + 0.05
 
  139                     np.random.rand(depth.shape[0], depth.shape[1]) < noise_rate
 
  143             if np.random.uniform() < 0.5:
 
  144                 image = np.fliplr(image)
 
  145                 depth = np.fliplr(depth)
 
  146                 label = np.fliplr(label)
 
  147                 depth_gt = np.fliplr(depth_gt)
 
  148             if np.random.uniform() < 0.5:
 
  149                 image = np.flipud(image)
 
  150                 depth = np.flipud(depth)
 
  151                 label = np.flipud(label)
 
  152                 depth_gt = np.flipud(depth_gt)
 
  153             if np.random.uniform() < 0.5:
 
  154                 angle = (np.random.uniform() * 180) - 90
 
  155                 image = self.
rotate_image(image, angle, cv2.INTER_LINEAR)
 
  157                 label = self.
rotate_image(label, angle, cv2.INTER_NEAREST)
 
  159                     depth_gt, angle, cv2.INTER_LINEAR)
 
  161         return image, depth, label, depth_gt, idx
 
  164         rot_mat = cv2.getRotationMatrix2D(
 
  165             center=(in_img.shape[1] / 2, in_img.shape[0] / 2),
 
  166             angle=angle, scale=1)
 
  167         rot_img = cv2.warpAffine(
 
  168             src=in_img, M=rot_mat,
 
  169             dsize=(in_img.shape[1], in_img.shape[0]), flags=flags)
 
  173         rot_mat = cv2.getRotationMatrix2D(
 
  174             center=(in_img.shape[1] / 2, in_img.shape[0] / 2),
 
  175             angle=angle, scale=1)
 
  176         ones = np.ones(in_img.shape, dtype=np.int32)
 
  177         rot_keep = cv2.warpAffine(
 
  179             dsize=(in_img.shape[1], in_img.shape[0]),
 
  180             flags=cv2.INTER_NEAREST)
 
  181         rot_keep = rot_keep.astype(np.bool)
 
  182         rot_img = cv2.warpAffine(
 
  183             src=in_img, M=rot_mat,
 
  184             dsize=(in_img.shape[1], in_img.shape[0]), flags=flags)
 
  185         rot_img[rot_keep == 
False] = np.nan