def random_flip_left_right(images, flow, mask, probability): """Performs a random left/right flip.""" perform_flip = tf.less(tf.random.uniform([]), probability) # apply flip images = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(images, axis=[-2]), false_fn=lambda: images) if flow is not None: flow = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(flow, axis=[-2]), false_fn=lambda: flow) mask = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(mask, axis=[-2]), false_fn=lambda: mask) # correct sign of flow sign_correction = tf.reshape([1.0, -1.0], [1, 1, 2]) flow = tf.cond(pred=perform_flip, true_fn=lambda: flow * sign_correction, false_fn=lambda: flow) return images, flow, mask
def random_flip_up_down(images, flow=None, mask=None): """Performs a random up/down flip.""" # 50/50 chance perform_flip = tf.equal(tf.random.uniform([], maxval=2, dtype=tf.int32), 1) # apply flip images = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(images, axis=[-3]), false_fn=lambda: images) if flow is not None: flow = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(flow, axis=[-3]), false_fn=lambda: flow) mask = tf.cond(pred=perform_flip, true_fn=lambda: tf.reverse(mask, axis=[-3]), false_fn=lambda: mask) # correct sign of flow sign_correction = tf.reshape([-1.0, 1.0], [1, 1, 2]) flow = tf.cond(pred=perform_flip, true_fn=lambda: flow * sign_correction, false_fn=lambda: flow) return images, flow, mask
def _prepare_lidar_points(inputs, lidar_names): """Integrates and returns the lidar points in vehicle coordinate frame.""" points_position = [] points_intensity = [] points_elongation = [] points_normal = [] points_in_image_frame_xy = [] points_in_image_frame_id = [] for lidar_name in lidar_names: lidar_location = tf.reshape( inputs[('lidars/%s/extrinsics/t') % lidar_name], [-1, 3]) inside_no_label_zone = tf.reshape( inputs[('lidars/%s/pointcloud/inside_nlz' % lidar_name)], [-1]) valid_points_mask = tf.math.logical_not(inside_no_label_zone) points_position_current_lidar = tf.boolean_mask( inputs[('lidars/%s/pointcloud/positions' % lidar_name)], valid_points_mask) points_position.append(points_position_current_lidar) points_intensity.append( tf.boolean_mask( inputs[('lidars/%s/pointcloud/intensity' % lidar_name)], valid_points_mask)) points_elongation.append( tf.boolean_mask( inputs[('lidars/%s/pointcloud/elongation' % lidar_name)], valid_points_mask)) points_to_lidar_vectors = lidar_location - points_position_current_lidar points_normal_direction = points_to_lidar_vectors / tf.expand_dims( tf.norm(points_to_lidar_vectors, axis=1), axis=1) points_normal.append(points_normal_direction) points_in_image_frame_xy.append( tf.boolean_mask( inputs['lidars/%s/camera_projections/positions' % lidar_name], valid_points_mask)) points_in_image_frame_id.append( tf.boolean_mask( inputs['lidars/%s/camera_projections/ids' % lidar_name], valid_points_mask)) points_position = tf.concat(points_position, axis=0) points_intensity = tf.concat(points_intensity, axis=0) points_elongation = tf.concat(points_elongation, axis=0) points_normal = tf.concat(points_normal, axis=0) points_in_image_frame_xy = tf.concat(points_in_image_frame_xy, axis=0) points_in_image_frame_id = tf.cast(tf.concat(points_in_image_frame_id, axis=0), dtype=tf.int32) points_in_image_frame_yx = tf.cast(tf.reverse(points_in_image_frame_xy, axis=[-1]), dtype=tf.int32) return (points_position, points_intensity, points_elongation, points_normal, points_in_image_frame_yx, points_in_image_frame_id)
def true_fn(images): r = tf.random.uniform([], maxval=3, dtype=tf.int32) images = tf.roll(images, r, axis=-1) r = tf.equal(tf.random.uniform([], maxval=2, dtype=tf.int32), 1) return tf.reverse(images, axis=[-1])
def photometric_augmentation(images, augment_color_swap=True, augment_hue_shift=True, augment_saturation=False, augment_brightness=False, augment_contrast=False, augment_gaussian_noise=False, augment_brightness_individual=False, augment_contrast_individual=False, max_delta_hue=0.5, min_bound_saturation=0.8, max_bound_saturation=1.2, max_delta_brightness=0.1, min_bound_contrast=0.8, max_bound_contrast=1.2, min_bound_gaussian_noise=0.0, max_bound_gaussian_noise=0.02, max_delta_brightness_individual=0.02, min_bound_contrast_individual=0.95, max_bound_contrast_individual=1.05): """Applies photometric augmentations to an image pair.""" # Randomly permute colors by rolling and reversing. # This covers all permutations. if augment_color_swap: r = tf.random.uniform([], maxval=3, dtype=tf.int32) images = tf.roll(images, r, axis=-1) r = tf.equal(tf.random.uniform([], maxval=2, dtype=tf.int32), 1) images = tf.cond(pred=r, true_fn=lambda: tf.reverse(images, axis=[-1]), false_fn=lambda: images) if augment_hue_shift: images = tf.image.random_hue(images, max_delta_hue) if augment_saturation: images = tf.image.random_saturation( images, min_bound_saturation, max_bound_saturation) if augment_brightness: images = tf.image.random_brightness(images, max_delta_brightness) if augment_contrast: images = tf.image.random_contrast( images, min_bound_contrast, max_bound_contrast) if augment_gaussian_noise: sigma = tf.random.uniform([], minval=min_bound_gaussian_noise, maxval=max_bound_gaussian_noise, dtype=tf.float32) noise = tf.random.normal( tf.shape(input=images), stddev=sigma, dtype=tf.float32) images = images + noise # perform relative photometric augmentation (individually per image) image_1, image_2 = tf.unstack(images) if augment_brightness_individual: image_1 = tf.image.random_contrast( image_1, min_bound_contrast_individual, max_bound_contrast_individual) image_2 = tf.image.random_contrast( image_2, min_bound_contrast_individual, max_bound_contrast_individual) if augment_contrast_individual: image_1 = tf.image.random_brightness( image_1, max_delta_brightness_individual) image_2 = tf.image.random_brightness( image_2, max_delta_brightness_individual) # crop values to ensure values in [0,1] (some augmentations can violate this) image_1 = tf.clip_by_value(image_1, 0.0, 1.0) image_2 = tf.clip_by_value(image_2, 0.0, 1.0) return tf.stack([image_1, image_2])