Randomly (50% chance) flip an image along axis `flip_index`. Args: image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor of shape `[height, width, channels]`. flip_index: Dimension along which to flip image. Vertical: 0, Horizontal: 1 seed: A Python integer
(image, flip_index, seed, scope_name)
| 367 | |
| 368 | |
| 369 | def _random_flip(image, flip_index, seed, scope_name): |
| 370 | """Randomly (50% chance) flip an image along axis `flip_index`. |
| 371 | |
| 372 | Args: |
| 373 | image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor |
| 374 | of shape `[height, width, channels]`. |
| 375 | flip_index: Dimension along which to flip image. Vertical: 0, Horizontal: 1 |
| 376 | seed: A Python integer. Used to create a random seed. See |
| 377 | `tf.compat.v1.set_random_seed` for behavior. |
| 378 | scope_name: Name of the scope in which the ops are added. |
| 379 | |
| 380 | Returns: |
| 381 | A tensor of the same type and shape as `image`. |
| 382 | |
| 383 | Raises: |
| 384 | ValueError: if the shape of `image` not supported. |
| 385 | """ |
| 386 | with ops.name_scope(None, scope_name, [image]) as scope: |
| 387 | image = ops.convert_to_tensor(image, name='image') |
| 388 | image = _AssertAtLeast3DImage(image) |
| 389 | shape = image.get_shape() |
| 390 | if shape.ndims == 3 or shape.ndims is None: |
| 391 | uniform_random = random_ops.random_uniform([], 0, 1.0, seed=seed) |
| 392 | mirror_cond = math_ops.less(uniform_random, .5) |
| 393 | result = control_flow_ops.cond( |
| 394 | mirror_cond, |
| 395 | lambda: array_ops.reverse(image, [flip_index]), |
| 396 | lambda: image, |
| 397 | name=scope) |
| 398 | return fix_image_flip_shape(image, result) |
| 399 | elif shape.ndims == 4: |
| 400 | batch_size = array_ops.shape(image)[0] |
| 401 | uniform_random = random_ops.random_uniform([batch_size], |
| 402 | 0, |
| 403 | 1.0, |
| 404 | seed=seed) |
| 405 | flips = math_ops.round( |
| 406 | array_ops.reshape(uniform_random, [batch_size, 1, 1, 1])) |
| 407 | flips = math_ops.cast(flips, image.dtype) |
| 408 | flipped_input = array_ops.reverse(image, [flip_index + 1]) |
| 409 | return flips * flipped_input + (1 - flips) * image |
| 410 | else: |
| 411 | raise ValueError('\'image\' must have either 3 or 4 dimensions.') |
| 412 | |
| 413 | |
| 414 | @tf_export('image.flip_left_right') |
no test coverage detected