def rot90(image, k=1, name=None): """ tf.image.rot90 (Khanh Remix) Rotate an image counter-clockwise by 90 degrees. Args: image: A 3-D tensor of shape `[height, width, channels]`. k: A scalar integer. The number of times the image is rotated by 90 degrees. name: A name for this operation (optional). Returns: A rotated 3-D tensor of the same type and shape as `image`. """ with ops.name_scope(name, 'rot90', [image, k]) as scope: ret = image k = k % 4 if k == 1: ret = array_ops.transpose(array_ops.reverse_v2(image, [1]), [1, 0, 2], name=scope) elif k == 2: ret = array_ops.reverse_v2(image, [0, 1], name=scope) elif k == 3: ret = array_ops.reverse_v2(array_ops.transpose(image, [1, 0, 2]), [1], name=scope) ret.set_shape([None, None, image.get_shape()[2]]) return ret
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with variable_scope.variable_scope(scope, "SeqLstm", [inputs]): lstm_cell = rnn_cell.BasicLSTMCell(noutput) if reverse: inputs = array_ops.reverse_v2(inputs, [0]) outputs, _ = rnn.dynamic_rnn(lstm_cell, inputs, time_major=True, dtype=inputs.dtype) if reverse: outputs = array_ops.reverse_v2(outputs, [0]) return outputs
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with variable_scope.variable_scope(scope, "SeqLstm", [inputs]): # TODO(tmb) make batch size, sequence_length dynamic # example: sequence_length = tf.shape(inputs)[0] _, batch_size, _ = _shape(inputs) lstm_cell = core_rnn_cell_impl.BasicLSTMCell(noutput, state_is_tuple=False) state = array_ops.zeros([batch_size, lstm_cell.state_size]) sequence_length = int(inputs.get_shape()[0]) sequence_lengths = math_ops.to_int64( array_ops.fill([batch_size], sequence_length)) if reverse: inputs = array_ops.reverse_v2(inputs, [0]) outputs, _ = rnn.dynamic_rnn( lstm_cell, inputs, sequence_lengths, state, time_major=True) if reverse: outputs = array_ops.reverse_v2(outputs, [0]) return outputs
def _reverse2DimAuto(self, np_dtype): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np_dtype) for use_gpu in [False, True]: with self.test_session(use_gpu=use_gpu): x_tf_1 = array_ops.reverse_v2(x_np, [0]).eval() x_tf_2 = array_ops.reverse_v2(x_np, [-2]).eval() x_tf_3 = array_ops.reverse_v2(x_np, [1]).eval() x_tf_4 = array_ops.reverse_v2(x_np, [-1]).eval() x_tf_5 = array_ops.reverse_v2(x_np, [1, 0]).eval() self.assertAllEqual(x_tf_1, np.asarray(x_np)[::-1, :]) self.assertAllEqual(x_tf_2, np.asarray(x_np)[::-1, :]) self.assertAllEqual(x_tf_3, np.asarray(x_np)[:, ::-1]) self.assertAllEqual(x_tf_4, np.asarray(x_np)[:, ::-1]) self.assertAllEqual(x_tf_5, np.asarray(x_np)[::-1, ::-1])
def random_flip_left_right(image, bboxes, seed=None): """Random flip left-right of an image and its bounding boxes. """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack([bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('random_flip_left_right'): image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. result = control_flow_ops.cond(mirror_cond, lambda: array_ops.reverse_v2(image, [1]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) return fix_image_flip_shape(image, result), bboxes
def random_flip_left_right(image, bboxes, seed=None): """Random flip left-right of an image and its bounding boxes. """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack( [bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('random_flip_left_right'): image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. result = control_flow_ops.cond( mirror_cond, lambda: array_ops.reverse_v2(image, [1]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) return fix_image_flip_shape(image, result), bboxes
def _reverse1DimAuto(self, np_dtype): x_np = np.array([1, 200, 3, 40, 5], dtype=np_dtype) for use_gpu in [False, True]: with self.test_session(use_gpu=use_gpu): x_tf = array_ops.reverse_v2(x_np, [0]).eval() self.assertAllEqual(x_tf, np.asarray(x_np)[::-1])
def image_flip_left_right(image,bboxes): def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack([bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('lip_left_right'): image = ops.convert_to_tensor(image, name='image') # _Check3DImage(image, require_static=False) # Flip image. image_flip = array_ops.reverse_v2(image, [1]) # Flip bboxes. bboxes = flip_bboxes(bboxes) def fix_image_flip_shape(image, result): """Set the shape to 3 dimensional if we don't know anything else. Args: image: original image size result: flipped or transformed image Returns: An image whose shape is at least None,None,None. """ image_shape = image.get_shape() if image_shape == tensor_shape.unknown_shape(): result.set_shape([None, None, None]) else: result.set_shape(image_shape) return result return fix_image_flip_shape(image, image_flip), bboxes
def ndlstm_base_dynamic(inputs, noutputs, scope=None, reverse=False): with variable_scope.variable_scope(scope, "Sequence_LSTM", [inputs]): _, batch_size, _ = _shape(inputs) lstm_cell = rnn_cell_impl.BasicLSTMCell(noutputs, state_is_tuple=True) lstm_cell.zero_state(batch_size, tf.float32) sequence_length = int(inputs.get_shape()[0]) sequence_lengths = math_ops.to_int64( array_ops.fill([batch_size], sequence_length)) if reverse: inputs = array_ops.reverse_v2(inputs, [0]) outputs, _ = rnn.dynamic_rnn(lstm_cell, inputs, sequence_lengths, dtype=tf.float32, time_major=True) if reverse: outputs = array_ops.reverse_v2(outputs, [0]) return outputs
def _reverse1DimAuto(self, np_dtype): x_np = np.array([1, 200, 3, 40, 5], dtype=np_dtype) for use_gpu in [False, True]: for axis_dtype in [dtypes.int32, dtypes.int64]: with self.test_session(use_gpu=use_gpu): x_tf = array_ops.reverse_v2(x_np, constant_op.constant([0], dtype=axis_dtype)).eval() self.assertAllEqual(x_tf, np.asarray(x_np)[::-1])
def _auc_convert_hist_to_auc(hist_true_acc, hist_false_acc, nbins): """Convert histograms to auc. Args: hist_true_acc: `Tensor` holding accumulated histogram of scores for records that were `True`. hist_false_acc: `Tensor` holding accumulated histogram of scores for records that were `False`. nbins: Integer number of bins in the histograms. Returns: Scalar `Tensor` estimating AUC. """ # Note that this follows the "Approximating AUC" section in: # Efficient AUC learning curve calculation, R. R. Bouckaert, # AI'06 Proceedings of the 19th Australian joint conference on Artificial # Intelligence: advances in Artificial Intelligence # Pages 181-191. # Note that the above paper has an error, and we need to re-order our bins to # go from high to low score. # Normalize histogram so we get fraction in each bin. normed_hist_true = math_ops.truediv(hist_true_acc, math_ops.reduce_sum(hist_true_acc)) normed_hist_false = math_ops.truediv(hist_false_acc, math_ops.reduce_sum(hist_false_acc)) # These become delta x, delta y from the paper. delta_y_t = array_ops.reverse_v2(normed_hist_true, [0], name='delta_y_t') delta_x_t = array_ops.reverse_v2(normed_hist_false, [0], name='delta_x_t') # strict_1d_cumsum requires float32 args. delta_y_t = math_ops.cast(delta_y_t, dtypes.float32) delta_x_t = math_ops.cast(delta_x_t, dtypes.float32) # Trapezoidal integration, \int_0^1 0.5 * (y_t + y_{t-1}) dx_t y_t = _strict_1d_cumsum(delta_y_t, nbins) first_trap = delta_x_t[0] * y_t[0] / 2.0 other_traps = delta_x_t[1:] * (y_t[1:] + y_t[:nbins - 1]) / 2.0 return math_ops.add(first_trap, math_ops.reduce_sum(other_traps), name='auc')
def random_flip_left_right(image, bboxes, xs, ys, seed=None): """Random flip left-right of an image and its bounding boxes. """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack([bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes def flip_xs(xs): """Flip xs coordinates """ # xs_temp = tf.ones(xs.shape) # xs_temp[:, 0] = 1 - xs[:, 1] # xs_temp[:, 1] = 1 - xs[:, 0] # xs_temp[:, 2] = 1 - xs[:, 3] # xs_temp[:, 3] = 1 - xs[:, 2] xs = tf.stack([1 - xs[:, 1], 1 - xs[:, 0], 1 - xs[ :, 3], 1 - xs[ :, 2]], axis=-1) return xs def flip_ys(ys): """Flip ys coordinates """ # ys_temp = tf.ones(ys.shape) # ys_temp[:, 0] = ys[: ,1] # ys_temp[:, 1] = ys[:, 0] # ys_temp[:, 2] = ys[:, 3] # ys_temp[:, 3] = ys[:, 2] # return ys_temp ys = tf.stack([ys[:, 1], ys[: ,0], ys[: ,3], ys[:, 2]], axis=-1) return ys # Random flip. Tensorflow implementation. with tf.compat.v1.name_scope('random_flip_left_right'): image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. result = control_flow_ops.cond(mirror_cond, lambda: array_ops.reverse_v2(image, [1]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) xs = control_flow_ops.cond(mirror_cond, lambda: flip_xs(xs), lambda: xs) ys = control_flow_ops.cond(mirror_cond, lambda: flip_ys(ys), lambda: ys) return fix_image_flip_shape(image, result), bboxes, xs, ys
def flip_image_bounding_boxes(self, image, bboxes, seed=None): image = ops.convert_to_tensor(image) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. flipped_image = control_flow_ops.cond( mirror_cond, lambda: array_ops.reverse_v2(image, [1]), lambda: image) flipped_image.set_shape(image.get_shape()) # Flip bboxes. flipped_bboxes = control_flow_ops.cond( mirror_cond, lambda: self.flip_bboxes(bboxes), lambda: bboxes) return flipped_image, flipped_bboxes
def testInvalid(self): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) with self.test_session(): with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, [-30]).eval() with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, [2]).eval() with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, "axis 0 specified more than once"): array_ops.reverse_v2(x_np, [0, -2]).eval()
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with variable_scope.variable_scope(scope, "SeqLstm", [inputs]): lstm_cell = rnn_cell.BasicLSTMCell(noutput) if reverse: inputs = array_ops.reverse_v2(inputs, [0]) outputs, _ = rnn.dynamic_rnn( lstm_cell, inputs, time_major=True, dtype=inputs.dtype) if reverse: outputs = array_ops.reverse_v2(outputs, [0]) return outputs
def testInvalidAxis(self): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) with self.assertRaisesRegexp(ValueError, "is out of valid range"): array_ops.reverse_v2(x_np, [-30]) with self.assertRaisesRegexp(ValueError, "is out of valid range"): array_ops.reverse_v2(x_np, [2]) with self.assertRaisesRegexp(ValueError, "axis 0 specified more than once"): array_ops.reverse_v2(x_np, [0, -2])
def testInvalid(self): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) with self.test_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, [-30]).eval() with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, [2]).eval() with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "axis 0 specified more than once"): array_ops.reverse_v2(x_np, [0, -2]).eval()
def testInvalid(self): x_np = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32) axis = array_ops.placeholder(dtypes.int32) with self.test_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, axis).eval(feed_dict={axis: [-30]}) with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "is out of valid range"): array_ops.reverse_v2(x_np, axis).eval(feed_dict={axis: [2]}) with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "axis 0 specified more than once"): array_ops.reverse_v2(x_np, axis).eval(feed_dict={axis: [0, -2]})
def _reverse(self, t, lengths): """Time reverse the provided tensor or list of tensors. Assumes the top dimension is the time dimension. Args: t: 3D tensor or list of 2D tensors to be reversed lengths: 1D tensor of lengths, or `None` Returns: A reversed tensor or list of tensors """ if isinstance(t, list): return list(reversed(t)) else: if lengths is None: return array_ops.reverse_v2(t, [0]) else: return array_ops.reverse_sequence(t, lengths, 0, 1)
def flip_up_down(image): """Flip an image horizontally (upside down). Outputs the contents of `image` flipped along the first dimension, which is `height`. See also `reverse()`. Args: image: A 3-D tensor of shape `[height, width, channels].` Returns: A 3-D tensor of the same type and shape as `image`. Raises: ValueError: if the shape of `image` not supported. """ image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) return fix_image_flip_shape(image, array_ops.reverse_v2(image, [0]))
def random_flip_left_right(image, bboxes, seed=None): """Random flip left-right of an image and its bounding boxes. """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack([bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('random_flip_left_right'): image = ops.convert_to_tensor(image, name='image') # _Check3DImage(image, require_static=False) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. result = control_flow_ops.cond(mirror_cond, lambda: array_ops.reverse_v2(image, [1]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) def fix_image_flip_shape(image, result): """Set the shape to 3 dimensional if we don't know anything else. Args: image: original image size result: flipped or transformed image Returns: An image whose shape is at least None,None,None. """ image_shape = image.get_shape() if image_shape == tensor_shape.unknown_shape(): result.set_shape([None, None, None]) else: result.set_shape(image_shape) return result return fix_image_flip_shape(image, result), bboxes
def random_flip_up_down(image, bboxes, seed=None): """Randomly flips an image vertically (upside down). With a 1 in 2 chance, outputs the contents of `image` flipped along the first dimension, which is `height`. Otherwise output the image as-is. Args: image: A 3-D tensor of shape `[height, width, channels].` seed: A Python integer. Used to create a random seed. See @{tf.set_random_seed} for behavior. Returns: A 3-D tensor of the same type and shape as `image`. Raises: ValueError: if the shape of `image` not supported. """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack( [1 - bboxes[:, 2], bboxes[:, 1], 1 - bboxes[:, 0], bboxes[:, 3]], axis=-1) return bboxes image = ops.convert_to_tensor(image, name='image') image = control_flow_ops.with_dependencies( _Check3DImage(image, require_static=False), image) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) result = control_flow_ops.cond(mirror_cond, lambda: array_ops.reverse_v2(image, [0]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) return fix_image_flip_shape(image, result), bboxes
def random_rot90(image, bboxes, seed=None): def _rot_bboxes(bboxes): bboxes = tf.stack( [1 - bboxes[:, 3], bboxes[:, 0], 1 - bboxes[:, 1], bboxes[:, 2]], axis=-1) return bboxes # Random rotate 90 degrees. Tensorflow impelmentation. with tf.name_scope('random_rot90'): image = ops.convert_to_tensor(image, name='image') _Check3DImage(image, require_static=False) uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) mirror_cond = math_ops.less(uniform_random, .5) # Flip image. result = control_flow_ops.cond( mirror_cond, lambda: array_ops.transpose( array_ops.reverse_v2(image, [1]), [1, 0, 2]), lambda: image) # Flip bboxes. bboxes = control_flow_ops.cond(mirror_cond, lambda: _rot_bboxes(bboxes), lambda: bboxes) return fix_image_flip_shape(image, result), bboxes
def _ReverseV2Grad(op, grad): axis = op.inputs[1] return array_ops.reverse_v2(grad, axis), None
def testReverse0DimAuto(self): x_np = 4 for use_gpu in [False, True]: with self.test_session(use_gpu=use_gpu): x_tf = array_ops.reverse_v2(x_np, []).eval() self.assertAllEqual(x_tf, x_np)
def _event_shape(self): return array_ops.reverse_v2(array_ops.shape(self.alpha), [0])[0]
def rand_flip_left_right(images): return tf.image.random_flip_left_right(images) B, H, W, C = images.shape.as_list() toss = tf.random_uniform([B]) < p flipped = array_ops.reverse_v2(x, [2]) return tf.where(toss, flipped, images)
def rot90_5d_batch(images): """ When the tensor is in shape: [batch_idx, #parallel_envs, h, w, channels], This function rotate images counter clock 90 degrees """ return array_ops.transpose(array_ops.reverse_v2(images, [3]), [0, 1, 3, 2, 4])
def _rot90(): return array_ops.transpose(array_ops.reverse_v2(image, [1]), [1, 0, 2]), _rot_bboxes90(bboxes)
def _FFTSizeForGrad(grad, rank): return math_ops.reduce_prod( array_ops.slice(array_ops.reverse_v2(array_ops.shape(grad), [0]), (0, ), (rank, )))
def _rot180(): return array_ops.reverse_v2(image, [0, 1]), _rot_bboxes180(bboxes)
def _rot180(): return array_ops.reverse_v2(image, [0, 1])
def _rot270(): return array_ops.reverse_v2(array_ops.transpose(image, [1, 0, 2]), [1])
def _FFTSizeForGrad(grad, rank): return math_ops.reduce_prod( array_ops.slice( array_ops.reverse_v2(array_ops.shape(grad), [0]), (0,), (rank,)))