def step(i, probs, counts, images): # Sample image patch H = sample_homography(shape, **config['homographies']) H_inv = invert_homography(H) warped = H_transform(image, H, interpolation='BILINEAR') count = H_transform(tf.expand_dims(tf.ones(tf.shape(image)[:3]), -1), H_inv, interpolation='NEAREST')[..., 0] # Predict detection probabilities warped_shape = tf.to_int32( tf.to_float(shape) * config['homographies']['patch_ratio']) input_warped = tf.image.resize_images(warped, warped_shape) prob = net(input_warped)['prob'] prob = tf.image.resize_images(tf.expand_dims(prob, axis=-1), shape)[..., 0] prob_proj = H_transform(tf.expand_dims(prob, -1), H_inv, interpolation='BILINEAR')[..., 0] probs = tf.concat([probs, tf.expand_dims(prob_proj, -1)], axis=-1) counts = tf.concat([counts, tf.expand_dims(count, -1)], axis=-1) images = tf.concat([images, tf.expand_dims(warped, -1)], axis=-1) return i + 1, probs, counts, images
def compute_valid_mask(image_shape, homography, erosion_radius=0): """ Compute a boolean mask of the valid pixels resulting from an homography applied to an image of a given shape. Pixels that are False correspond to bordering artifacts. A margin can be discarded using erosion. Arguments: input_shape: Tensor of rank 2 representing the image shape, i.e. `[H, W]`. homography: Tensor of shape (B, 8) or (8,), where B is the batch size. erosion_radius: radius of the margin to be discarded. Returns: a Tensor of type `tf.int32` and shape (H, W). """ mask = H_transform(tf.ones(image_shape), homography, interpolation='NEAREST') if erosion_radius > 0: kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (erosion_radius*2,)*2) mask = tf.nn.erosion2d( mask[tf.newaxis, ..., tf.newaxis], tf.to_float(tf.constant(kernel)[..., tf.newaxis]), [1, 1, 1, 1], [1, 1, 1, 1], 'SAME')[0, ..., 0] + 1. return tf.to_int32(mask)
def step(i, probs, counts, images): #Sample image patch H = sample_homography(shape, **config['homographies']) H_inv = invert_homography(H) ############################################# H_ = shape[0] W = shape[1] row_c = tf.random_uniform(shape=[], minval=0, maxval=tf.cast(H_, tf.float32), dtype=tf.float32) col_c = tf.random_uniform(shape=[], minval=0, maxval=tf.cast(W, tf.float32), dtype=tf.float32) lambda_ = tf.constant(0.000006) ############################################# #apply the homography warped = H_transform(image, H, interpolation='BILINEAR') ############################################# #apply the radial distortion warped = distort(warped, lambda_, (row_c, col_c)) #count = warp_points_dist(tf.expand_dims(tf.ones(tf.shape(image)[:3]),-1), lambda_, (row_c,col_c), inverse=True) count = undistort(tf.expand_dims(tf.ones(tf.shape(image)[:3]), -1), lambda_, (row_c, col_c)) #count = tf.round(count) count = H_transform(count, H_inv, interpolation='NEAREST') mask = H_transform(tf.expand_dims(tf.ones(tf.shape(image)[:3]), -1), H, interpolation='NEAREST') mask = distort(mask, lambda_, (row_c, col_c)) ############################################# # Ignore the detections too close to the border to avoid artifacts if config['valid_border_margin']: kernel = cv.getStructuringElement( cv.MORPH_ELLIPSE, (config['valid_border_margin'] * 2, ) * 2) with tf.device('/cpu:0'): count = tf.nn.erosion2d( count, tf.to_float(tf.constant(kernel)[..., tf.newaxis]), [1, 1, 1, 1], [1, 1, 1, 1], 'SAME')[..., 0] + 1. mask = tf.nn.erosion2d( mask, tf.to_float(tf.constant(kernel)[..., tf.newaxis]), [1, 1, 1, 1], [1, 1, 1, 1], 'SAME')[..., 0] + 1. # Predict detection probabilities prob = net(warped)['prob'] prob = prob * mask prob_proj = undistort(tf.expand_dims(prob, -1), lambda_, (row_c, col_c)) prob_proj = H_transform(prob_proj, H_inv, interpolation='BILINEAR')[..., 0] prob_proj = prob_proj * count probs = tf.concat([probs, tf.expand_dims(prob_proj, -1)], axis=-1) counts = tf.concat([counts, tf.expand_dims(count, -1)], axis=-1) images = tf.concat([images, tf.expand_dims(warped, -1)], axis=-1) return i + 1, probs, counts, images