MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _random_flip

Function _random_flip

tensorflow/python/ops/image_ops_impl.py:369–411  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

367
368
369def _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')

Callers 2

random_flip_up_downFunction · 0.85
random_flip_left_rightFunction · 0.85

Calls 10

_AssertAtLeast3DImageFunction · 0.85
fix_image_flip_shapeFunction · 0.85
random_uniformMethod · 0.80
reshapeMethod · 0.80
name_scopeMethod · 0.45
get_shapeMethod · 0.45
condMethod · 0.45
reverseMethod · 0.45
shapeMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected