
    AVhϔ             	       @   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddl%m&Z& dd l%m'Z' dd!l%m(Z( dd"l)m*Z*  ejV                  d#        ejV                  d$        ejV                  d%        ejV                  d&        ejV                  d'        ejV                  d(        ejV                  d)        ejV                  d*        ejV                  d+        ejV                  d,       d- Z,d. Z-d/ Z.dd1Z/d2 Z0d3 Z1dd4Z2d5 Z3dd6Z4d7 Z5 e*d8      e'jl                  dd9              Z7 e*d:      e'jl                  dd;              Z8 e*d<g =      e'jl                  d>               Z9 e*d?g =      e'jl                  d@               Z:dA Z; e*dB      e'jl                  dC               Z< e*dD      e'jl                  dE               Z=dF Z> e*dG      e'jl                  ddH              Z?dI Z@dJ ZA e*dKdKdLg=      e'jl                  ddM              ZB e*dN      e'jl                  dO               ZC e*dP      e'jl                  dQ               ZDdR ZE e*dS      e'jl                  dT               ZF e*dUdUdVg=      e'jl                  dW               ZG e*dXg=       G dY dZ             ZH e*dXg =       G d[ d\             ZId] ZJ e*d^d_g=      e'jl                  eHj                  d`d`dfda              ZL e*d_g =      e'jl                  eIj                  d`d`dfdb              ZMdc ZN e*ddg=      e'jl                  eHj                  d`fde              ZO e*dfg =      e'jl                  eIj                  d`fdg              ZP e*dh      e'jl                  di               ZQ e*dj      e'j                  e'jl                  ddk                     ZS e*dlg =      e'j                  e'jl                  dm                      ZT e*dn      e'jl                  ddo              ZU e*dpg =      e'jl                  dq               ZV e*dr      e'j                  e'jl                  ds                      ZW e*dt      e'jl                  du               ZX e*dv      e'j                  e'jl                  ddw                     ZY e*dx      e'j                  e'jl                  d	dy                     ZZ e*dz      e'jl                  dd{              Z[ e*d|      e'jl                  dd}              Z\ e*d~      e'jl                  dd              Z] e*dg =      e'jl                  d               Z^ e*d      e'jl                  dd              Z_ e*d      e'jl                  dd              Z` e*dg =      e'jl                  d               Za e*d      e'jl                  d
d              Zb e*d      e'jl                  dd              Zc e*dg =      e'jl                  dd              Zd e*d      e'jl                  dd              Ze e*ddddg=      dd       ZfddZg  e*ddddg=       e'jl                  ej                              Zh  e*ddddg=       e'jl                  ej                              Zi  e*ddddg=       e'jl                  ej                              Zj  e*ddddg=       e'jl                  ej                              Zk  e*ddddg=       e'jl                  ej                              Zl  e*ddddg=       e'jl                  ej                              Zm  e*ddddg=       e'jl                  ej                              Zn e*dd      e'jl                  dd              Zo e*ddddg=      e'jl                  de
j                  dd0fd              Zq e*d      e'jl                  dd              Zr e*dg =      e'jl                  	 	 	 	 	 	 	 dd              Zs e*dg =      e'jl                  	 	 	 	 	 	 dd              Zt e*dg=      e'jl                   e&j                  dd      	 	 	 	 	 	 	 	 dd                     Zv e*d      e'jl                  d ewd      dfd              Zx e*d      e'jl                  d ewd      ddfd              Zy e*d      e'jl                  d ewd      dfd              Zzg dg dg dgZ{ e*d      e'jl                  d               Z|g dg dg dgZ} e*d«      e'jl                  dÄ               Z~g dĢg dŢg dƢgZ e*dǫ      e'jl                  dȄ               Zg dg dɢg dʢgZ e*d˫      e'jl                  d̄               Zd̈́ Z e*dΫ      e'jl                  ddτ              Zdd҄Zdӄ Z	 	 	 	 	 	 ddքZ e*d׫      e'jl                  	 	 	 	 	 dd؄              ZdZ e*dګ      e'jl                  eddddfdۄ              Z e*dܫ      e'jl                  d݄               Z e*dޫ      e'jl                  d߄               Z e*dg=      e'jl                   e&j                  ddᬯ      	 	 	 dd                     Z e*dg=      e'jl                   e&j                  dd䬯      	 	 	 dd                     Z e*dg=      e'jl                   e&j                  dd笯      	 	 	 dd                     Z e&j                  dd鬯      Z  e*dg=       e e'jl                  ej"                                    Z e*dg =      e'jl                  	 	 	 dd              Z e*dg=      e'jl                   e&j&                  ddd      	 	 	 	 	 	 dd                     Zej*                  j                   e_          e*dg=      e'jl                  	 	 	 	 dd              Z e*dg =      e'jl                  	 	 	 	 dd              Z e*d      e'jl                  d ewd      d`d0dfd              Zd Zd Zd Zd Z e*d      e'jl                  d ewd      d`dd`d`dfd              Z ej<                  d      d ewd      d`d`dfd       Zd ewd      d`dfdZ e*d g =      e'jl                  dd              Z e*d g=      e'jl                  dd              Z e*d      e'jl                  	 	 	 	 	 dd              Zy(  zImplementation of image ops.    N)context)def_function)config)constant_op)dtypes)ops)random_seed)tensor)tensor_shape)tensor_util)	array_ops)array_ops_stack)	check_ops)cond)control_flow_assert)control_flow_case)control_flow_ops)gen_image_ops)math_ops)nn_impl)nn_ops)
random_ops)ref_variable)sort_ops)stateless_random_ops)
string_ops)	variables)
while_loop)deprecation)dispatch)numpy_compat)	tf_export
RandomCropHSVToRGBDrawBoundingBoxesSampleDistortedBoundingBoxSampleDistortedBoundingBoxV2ExtractGlimpseNonMaxSuppressionNonMaxSuppressionV2NonMaxSuppressionWithOverlapsGenerateBoundingBoxProposalsc                 `    t        |       rt        j                  | |g      gS | s ||      g S )a  A polymorphic assert, works with tensors and boolean expressions.

  If `cond` is not a tensor, behave like an ordinary assert statement, except
  that a empty list is returned. If `cond` is a tensor, return a list
  containing a single TensorFlow assert op.

  Args:
    cond: Something evaluates to a boolean value. May be a tensor.
    ex_type: The exception class to use.
    msg: The error message.

  Returns:
    A list, containing at most one assert op.
  )
_is_tensorr   Assert)r   ex_typemsgs      T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/image_ops_impl.py_assertr3   H   s6     &&tcU344CLi    c                 V    t        | t        j                  t        j                  f      S )zReturns `True` if `x` is a symbolic tensor-like object.

  Args:
    x: A python object to check.

  Returns:
    `True` if `x` is a `tf.Tensor` or `tf.Variable`, otherwise `False`.
  )
isinstance
tensor_libTensorr   Variable)xs    r2   r.   r.   `   s"     
A
))9+=+=>	??r4   c                 x   | j                         j                         r| j                         j                         S | j                         j                  |      j                         }t	        j
                  t        j                  |       |      }t        ||      D cg c]  \  }}||n| c}}S c c}}w )ah  Returns the dimensions of an image tensor.

  Args:
    image: A rank-D Tensor. For 3-D  of shape: `[height, width, channels]`.
    rank: The expected rank of the image

  Returns:
    A list of corresponding to the dimensions of the
    input image.  Dimensions that are statically known are python integers,
    otherwise, they are integer scalar tensors.
  )		get_shapeis_fully_definedas_list	with_rankr   unstackr   shapezip)imagerankstatic_shapedynamic_shapesds         r2   _ImageDimensionsrI   l   s     __'')??$$&&??$..t4<<>L#++IOOE,BDIM.1,.N&*aQ]!  s   "B6Tc                    	 | j                         j                  d      }|r|j	                         st        d|z        t        d |D              rt        d|z        |j	                         s+t        j                  t        j                  |       dg      gS g S # t        $ r t        d| j                  z        w xY w)a  Assert that we are working with a properly shaped image.

  Args:
    image: 3-D Tensor of shape [height, width, channels]
    require_static: If `True`, requires that all dimensions of `image` are known
      and non-zero.

  Raises:
    ValueError: if `image.shape` is not a 3-vector.

  Returns:
    An empty list, if `image` has fully defined dimensions. Otherwise, a list
    containing an assert op is returned.
     z-'image' (shape %s) must be three-dimensional.z)'image' (shape %s) must be fully defined.c              3   &   K   | ]	  }|d k(    ywr   N .0r:   s     r2   	<genexpr>z _Check3DImage.<locals>.<genexpr>   s     %Aa%   z)all dims of 'image.shape' must be > 0: %sz&all dims of 'image.shape' must be > 0.)	r<   r?   
ValueErrorrA   r=   anyr   assert_positiver   rC   require_staticimage_shapes      r2   _Check3DImagerY      s    "//#--a0K K88:
@;N
OO%%%
@;N
OO		%	%	'!!OOE" 	  I 
 "
D[[! " ""s   B "C c                 D    t        j                  t        | d      |       S )a1  Assert that we are working with a properly shaped image.

  Performs the check statically if possible (i.e. if the shape
  is statically known). Otherwise adds a control dependency
  to an assert op that checks the dynamic shape.

  Args:
    image: 3-D Tensor of shape [height, width, channels]

  Raises:
    ValueError: if `image.shape` is not a 3-vector.

  Returns:
    If the shape of `image` could be verified statically, `image` is
    returned unchanged, otherwise there will be a control dependency
    added that asserts the correct dynamic shape.
  FrW   )r   with_dependenciesrY   rC   s    r2   _Assert3DImager^      s#    $ 
	+	+E%0%
9 9r4   c                 D    t        j                  t        | d      |       S )a6  Assert that we are working with a properly shaped image.

  Performs the check statically if possible (i.e. if the shape
  is statically known). Otherwise adds a control dependency
  to an assert op that checks the dynamic shape.

  Args:
    image: >= 3-D Tensor of size [*, height, width, depth]

  Raises:
    ValueError: if image.shape is not a [>= 3] vector.

  Returns:
    If the shape of `image` could be verified statically, `image` is
    returned unchanged, otherwise there will be a control dependency
    added that asserts the correct dynamic shape.
  Fr[   )r   r\   _CheckAtLeast3DImager]   s    r2   _AssertAtLeast3DImagera      s%    $ 
	+	+57
@ @r4   c                 Z   	 | j                         j                   | j                         j                  d      }n| j                         j                  d      }|r|j                         st	        d      t        d |dd D              rt	        d|z        |dd j                         sXt        j                  t        j
                  |       dd dg      t        j                  t        j                  |       dd	
      gS g S # t        $ r t	        d| j
                  z        w xY w)a  Assert that we are working with a properly shaped image.

  Args:
    image: >= 3-D Tensor of size [*, height, width, depth]
    require_static: If `True`, requires that all dimensions of `image` are known
      and non-zero.

  Raises:
    ValueError: if image.shape is not a [>= 3] vector.

  Returns:
    An empty list, if `image` has fully defined dimensions. Otherwise, a list
    containing an assert op is returned.
  NrK   z6'image' (shape %s) must be at least three-dimensional.'image' must be fully defined.c              3   &   K   | ]	  }|d k(    ywrM   rN   rO   s     r2   rQ   z'_CheckAtLeast3DImage.<locals>.<genexpr>   s     *Aa*rR   z-inner 3 dims of 'image.shape' must be > 0: %sz*inner 3 dims of 'image.shape' must be > 0.z+'image' must be at least three-dimensional.message)r<   ndimsr?   with_rank_at_leastrS   rA   r=   rT   r   rU   r   assert_greater_equalrD   rV   s      r2   r`   r`      s0   "&OO%//2kOO%88;k K88:
7
88*RS)**
F ! " "	RS		*	*	,!!OOE"23' 	 	&&NN5!A	C	 	 I) 
 "
M[[! " ""s   AD "D*c                 D    t        j                  t        | d      |       S )a^  Assert that we are working with a properly shaped grayscale image.

  Performs the check statically if possible (i.e. if the shape
  is statically known). Otherwise adds a control dependency
  to an assert op that checks the dynamic shape.

  Args:
    image: >= 2-D Tensor of size [*, 1]

  Raises:
    ValueError: if image.shape is not a [>= 2] vector or if
              last dimension is not size 1.

  Returns:
    If the shape of `image` could be verified statically, `image` is
    returned unchanged, otherwise there will be a control dependency
    added that asserts the correct dynamic shape.
  Fr[   )r   r\   _CheckGrayscaleImager]   s    r2   _AssertGrayscaleImagerm      s%    & 
	+	+57
@ @r4   c                 V   	 | j                         j                   | j                         j                  d      }n| j                         j                  d      }|r|j                         st	        d      |j                         r|d   dk7  rt	        d      |j                         sYt        j                  t        j
                  |       d   dd      t        j                  t        j                  |       dd	      gS g S # t        $ r t	        d| j
                  z        w xY w)
a  Assert that we are working with properly shaped grayscale image.

  Args:
    image: >= 2-D Tensor of size [*, 1]
    require_static: Boolean, whether static shape is required.

  Raises:
    ValueError: if image.shape is not a [>= 2] vector or if
              last dimension is not size 1.

  Returns:
    An empty list, if `image` has fully defined dimensions. Otherwise, a list
    containing an assert op is returned.
     z>A grayscale image (shape %s) must be at least two-dimensional.rc      z5Last dimension of a grayscale image should be size 1.rf   rK   z3A grayscale image must be at least two-dimensional.)r<   rh   r?   ri   rS   rA   r=   r   assert_equalr   rj   rD   rV   s      r2   rl   rl     s   7&OO%//2kOO%88;k K88:
7
88!!#2!NOO		%	%	'OOE"2&K	M 	&&NN5!I	K	 	 I) 
 7
 (*/++6 7 77s   AD "D(c                     | j                         }|t        j                         k(  r|j                  g d       |S |j                  |       |S )zSet 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).
  )NNN)r<   r   unknown_shape	set_shape)rC   resultrX   s      r2   fix_image_flip_shaperw   >  sL     !+L..00
'( 
- [!	-r4   zimage.random_flip_up_downc                 h    t        j                  t        j                  |      }t	        | d|d      S )a  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.
  When passing a batch of images, each image will be randomly flipped
  independent of other images.

  Example usage:

  >>> image = np.array([[[1], [2]], [[3], [4]]])
  >>> tf.image.random_flip_up_down(image, 3).numpy().tolist()
  [[[3], [4]], [[1], [2]]]

  Randomly flip multiple images.

  >>> images = np.array(
  ... [
  ...     [[[1], [2]], [[3], [4]]],
  ...     [[[5], [6]], [[7], [8]]]
  ... ])
  >>> tf.image.random_flip_up_down(images, 4).numpy().tolist()
  [[[[3], [4]], [[1], [2]]], [[[5], [6]], [[7], [8]]]]

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_flip_up_down`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    seed: A Python integer. Used to create a random seed. See
      `tf.compat.v1.set_random_seed` for behavior.

  Returns:
    A tensor of the same type and shape as `image`.
  Raises:
    ValueError: if the shape of `image` not supported.
  seedr   random_flip_up_down	functoolspartialr   random_uniform_random_fliprC   rz   random_funcs      r2   r{   r{   Q  s/    V !!*";";$G+	eQ-B	CCr4   zimage.random_flip_left_rightc                 h    t        j                  t        j                  |      }t	        | d|d      S )a  Randomly flip an image horizontally (left to right).

  With a 1 in 2 chance, outputs the contents of `image` flipped along the
  second dimension, which is `width`.  Otherwise output the image as-is.
  When passing a batch of images, each image will be randomly flipped
  independent of other images.

  Example usage:

  >>> image = np.array([[[1], [2]], [[3], [4]]])
  >>> tf.image.random_flip_left_right(image, 5).numpy().tolist()
  [[[2], [1]], [[4], [3]]]

  Randomly flip multiple images.

  >>> images = np.array(
  ... [
  ...     [[[1], [2]], [[3], [4]]],
  ...     [[[5], [6]], [[7], [8]]]
  ... ])
  >>> tf.image.random_flip_left_right(images, 6).numpy().tolist()
  [[[[2], [1]], [[4], [3]]], [[[5], [6]], [[7], [8]]]]

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_flip_left_right`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    seed: A Python integer. Used to create a random seed. See
      `tf.compat.v1.set_random_seed` for behavior.

  Returns:
    A tensor of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  ry   rq   random_flip_left_rightr|   r   s      r2   r   r     s/    X !!*";";$G+	eQ-E	FFr4   z&image.stateless_random_flip_left_right)v1c                 h    t        j                  t        j                  |      }t	        | d|d      S )a  Randomly flip an image horizontally (left to right) deterministically.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  Example usage:

  >>> image = np.array([[[1], [2]], [[3], [4]]])
  >>> seed = (2, 3)
  >>> tf.image.stateless_random_flip_left_right(image, seed).numpy().tolist()
  [[[2], [1]], [[4], [3]]]

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Returns:
    A tensor of the same type and shape as `image`.
  ry   rq    stateless_random_flip_left_rightr}   r~   r   stateless_random_uniformr   r   s      r2   r   r     s9    2 !!33$@+	Q?
A Ar4   z#image.stateless_random_flip_up_downc                 h    t        j                  t        j                  |      }t	        | d|d      S )a  Randomly flip an image vertically (upside down) deterministically.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  Example usage:

  >>> image = np.array([[[1], [2]], [[3], [4]]])
  >>> seed = (2, 3)
  >>> tf.image.stateless_random_flip_up_down(image, seed).numpy().tolist()
  [[[3], [4]], [[1], [2]]]

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Returns:
    A tensor of the same type and shape as `image`.
  ry   r   stateless_random_flip_up_downr   r   s      r2   r   r     s7    2 !!33$@+	Q<
> >r4   c                     t        j                  d| g      5 t        j                   d       t                 j	                         } fd} fd}|j
                  It        j                         }t        j                  t        j                  |d      ||      cddd       S |j
                  dk(  r |       cddd       S |j
                  dk(  r |       cddd       S t        d|z        # 1 sw Y   yxY w)	a`  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 the image.
      Vertical is 0, Horizontal is 1.
    random_func: partial function for calling either stateful or stateless
      random ops with `seed` parameter specified.
    scope_name: Name of the scope in which the ops are added.

  Returns:
    A tensor of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  NrC   namec                       g dd      } t        j                  | d      }t        j                  |fdfd      }t	        |      S )Nr         ?rA   minvalmaxval      ?c                  2    t        j                   g      S Nr   reverse
flip_indexrC   s   r2   <lambda>z/_random_flip.<locals>.f_rank3.<locals>.<lambda>
  s    )##EJ<8 r4   c                       S r   rN   r]   s   r2   r   z/_random_flip.<locals>.f_rank3.<locals>.<lambda>  s    % r4   r   )r   lesstf_condr   rw   )uniform_randommirror_condrv   r   rC   r   scopes      r2   f_rank3z_random_flip.<locals>.f_rank3  sJ    "AcBnMM."5k||

8
	f
 "%00r4   c            	      6   t        j                        d   }  | gdd      }t        j                  t        j                  || dddg            }t        j
                  |j                        }t        j                  dz   g      }||z  d|z
  z  z   S )Nr   r   r   rq   )r   rA   r   roundreshapecastdtyper   )
batch_sizer   flipsflipped_inputr   rC   r   s       r2   f_rank4z_random_flip.<locals>.f_rank4  s    ??5)!,j"*aLnnn


NZAq,A
BDemmE5;;/e''
Q/?@m]"a%i5%888r4   rK      6'image' (shape %s) must have either 3 or 4 dimensions.r   
name_scopeconvert_to_tensorra   r<   rh   r   rD   r   r   r   equalrS   )	rC   r   r   
scope_namerA   r   r   rD   r   s	   ```     @r2   r   r     s    $ ~~dJ0 !NE!!%g6E!%(EOOE19 {{^^E"d\\(..q17GD5!N !N6 {{aY9!N !N: 
	Y=!N !N@ 
Du
LN NA!N !Ns   BD 4D D 2D  D	zimage.flip_left_rightc                     t        | dd      S )a  Flip an image horizontally (left to right).

  Outputs the contents of `image` flipped along the width dimension.

  See also `tf.reverse`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.flip_left_right(x)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 4.,  5.,  6.],
          [ 1.,  2.,  3.]],
         [[10., 11., 12.],
          [ 7.,  8.,  9.]]], dtype=float32)>

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.

  Returns:
    A tensor of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  rq   flip_left_right_flipr]   s    r2   r   r   $  s    @ 
ua*	++r4   zimage.flip_up_downc                     t        | dd      S )a  Flip an image vertically (upside down).

  Outputs the contents of `image` flipped along the height dimension.

  See also `reverse()`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.flip_up_down(x)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 7.,  8.,  9.],
          [10., 11., 12.]],
         [[ 1.,  2.,  3.],
          [ 4.,  5.,  6.]]], dtype=float32)>

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.

  Returns:
    A `Tensor` of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  r   flip_up_downr   r]   s    r2   r   r   G  s    @ 
ua	((r4   c                     t        j                  d| g      5  t        j                   d       t                 j	                         } fd} fd}|j
                  It        j                         }t        j                  t        j                  |d      ||      cddd       S |j
                  dk(  r |       cddd       S |j
                  dk(  r |       cddd       S t        d|z        # 1 sw Y   yxY w)	a  Flip an image either horizontally or vertically.

  Outputs the contents of `image` flipped along the dimension `flip_index`.

  See also `reverse()`.

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    flip_index: 0 For vertical, 1 for horizontal.
    scope_name: string, scope name.

  Returns:
    A `Tensor` of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  NrC   r   c                  F    t        t        j                   g            S r   )rw   r   r   r   s   r2   r   z_flip.<locals>.f_rank3  s    !%):):5:,)OPPr4   c                  8    t        j                   dz   g      S )Nrq   r   r   s   r2   r   z_flip.<locals>.f_rank4  s    uzA~&677r4   rK   r   z5'image' (shape %s)must have either 3 or 4 dimensions.r   )rC   r   r   rA   r   r   rD   s   ``     r2   r   r   j  s    & ~~dJ0 M!!%g6E!%(EOOEQ8 {{^^E"d\\(..q17GDM M 
	YM M  
	Y#M M& 
Ce
KM M'M Ms   B
C;/C;C;-C;;Dzimage.rot90c                     t        j                  |d g      5 t        j                   d       t                t        j                  t        j
                  d      j                         j                  d       t        j                  d       j                         }|j                  Wt        j                         } fd	} fd
}t        j                  t        j                  |d      ||      cddd       S |j                  dk(  rt!               cddd       S |j                  dk(  rt#               cddd       S t%        d|z        # 1 sw Y   yxY w)a  Rotate image(s) by 90 degrees.


  For example:

  >>> a=tf.constant([[[1],[2]],
  ...                [[3],[4]]])
  >>> # rotating `a` counter clockwise by 90 degrees
  >>> a_rot=tf.image.rot90(a)
  >>> print(a_rot[...,0].numpy())
  [[2 4]
   [1 3]]
  >>> # rotating `a` counter clockwise by 270 degrees
  >>> a_rot=tf.image.rot90(a, k=3)
  >>> print(a_rot[...,0].numpy())
  [[3 1]
   [4 2]]
  >>> # rotating `a` clockwise by 180 degrees
  >>> a_rot=tf.image.rot90(a, k=-2)
  >>> print(a_rot[...,0].numpy())
  [[4 3]
   [2 1]]

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    k: A scalar integer tensor. The number of times the image(s) are rotated by
      90 degrees.
    name: A name for this operation (optional).

  Returns:
    A rotated tensor of the same type and shape as `image`.

  Raises:
    ValueError: if the shape of `image` not supported.
  rot90rC   r   kr   r   r   r   Nc                      t               S r   )	_rot90_3DrC   r   r   s   r2   r   zrot90.<locals>.f_rank3      5))r4   c                      t               S r   )	_rot90_4Dr   s   r2   r   zrot90.<locals>.f_rank4  r   r4   rK   r   )r   r   r   ra   r   int32r<   assert_has_rankr   modrh   r   rD   r   r   r   r   r   rS   )rC   r   r   rA   rD   r   r   r   s   ``     @r2   r   r     sC   N ~~dGeQZ0 NE!!%g6E!%(Eav||#>AKKM!!!$QAOOE{{^^E"d** \\(..q17GD#N N$ 
	ua''N N( 
	ua'+N N. 
Du
LN N/N Ns   C'E&E&3E&E&&E/c                 6     fd} fd} fd}t        j                  |d      |ft        j                  |d      |ft        j                  |d      |fg}t        j                  | fdd|	      }|j	                  d
d
 j                         d   g       |S )aD  Rotate image counter-clockwise by 90 degrees `k` times.

  Args:
    image: 3-D Tensor of shape `[height, width, channels]`.
    k: A scalar integer. The number of times the image is rotated by 90 degrees.
    name_scope: A valid TensorFlow name scope.

  Returns:
    A 3-D tensor of the same type and shape as `image`.

  c                  ^    t        j                  t        j                   dg      g d      S )Nrq   rq   r   ro   r   	transpose
reverse_v2r]   s   r2   _rot90z_rot90_3D.<locals>._rot90  s%    y33EA3?KKr4   c                  4    t        j                   ddg      S )Nr   rq   r   r   r]   s   r2   _rot180z_rot90_3D.<locals>._rot180  s    1v..r4   c                  ^    t        j                  t        j                   g d      dg      S )Nr   rq   r   r   r   r]   s   r2   _rot270z_rot90_3D.<locals>._rot270  s%    	 3 3E9 EsKKr4   rq   ro   rK   c                       S r   rN   r]   s   r2   r   z_rot90_3D.<locals>.<lambda>  s    U r4   Tdefault	exclusiver   N)r   r   r   caseru   r<   )rC   r   r   r   r   r   casesrv   s   `       r2   r   r     s    L/L NN1a &)HNN1a,@'+JNN1a '*,% !!]dE&D$ 1! 456	-r4   c                 B     fd} fd} fd}t        j                  |d      |ft        j                  |d      |ft        j                  |d      |fg}t        j                  | fdd|	      }|j	                         }|j                  |d
   dd|d   g       |S )aY  Rotate batch of images counter-clockwise by 90 degrees `k` times.

  Args:
    images: 4-D Tensor of shape `[height, width, channels]`.
    k: A scalar integer. The number of times the images are rotated by 90
      degrees.
    name_scope: A valid TensorFlow name scope.

  Returns:
    A 4-D `Tensor` of the same type and shape as `images`.
  c                  ^    t        j                  t        j                   dg      g d      S )Nro   r   ro   rq   rK   r   imagess   r2   r   z_rot90_4D.<locals>._rot90  s%    y33FQC@,OOr4   c                  4    t        j                   ddg      S )Nrq   ro   r   r   s   r2   r   z_rot90_4D.<locals>._rot180  s    A//r4   c                  ^    t        j                  t        j                   g d      dg      S )Nr   ro   r   r   s   r2   r   z_rot90_4D.<locals>._rot270  s%    	 3 3FL IA3OOr4   rq   ro   rK   c                       S r   rN   r   s   r2   r   z_rot90_4D.<locals>.<lambda>  s    V r4   Tr   r   N)r   r   r   r   r<   ru   )	r   r   r   r   r   r   r   rv   rA   s	   `        r2   r   r     s    P0P NN1a &)HNN1a,@'+JNN1a '*,% !!^t*F&



%E!HdD%(34	-r4   zimage.transposezimage.transpose_imagec                 Z    t        j                  d g      5  t        j                   d       t                 j	                         }|j
                  Ut        j                         } fd} fd}t        j                  t        j                  |d      ||      cddd       S |j
                  dk(  r#t        j                   g d      cddd       S |j
                  d	k(  r#t        j                   g d
      cddd       S t        d|z        # 1 sw Y   yxY w)a  Transpose image(s) by swapping the height and width dimension.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.transpose(x)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 1.,  2.,  3.],
          [ 7.,  8.,  9.]],
         [[ 4.,  5.,  6.],
          [10., 11., 12.]]], dtype=float32)>

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    name: A name for this operation (optional).

  Returns:
    If `image` was 4-D, a 4-D float Tensor of shape
   `[batch, width, height, channels]`
    If `image` was 3-D, a 3-D float Tensor of shape
   `[width, height, channels]`

  Raises:
    ValueError: if the shape of `image` not supported.

  Usage Example:

  >>> image = [[[1, 2], [3, 4]],
  ...         [[5, 6], [7, 8]],
  ...         [[9, 10], [11, 12]]]
  >>> image = tf.constant(image)
  >>> tf.image.transpose(image)
  <tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
  array([[[ 1,  2],
         [ 5,  6],
         [ 9, 10]],
        [[ 3,  4],
         [ 7,  8],
         [11, 12]]], dtype=int32)>
  r   rC   r   Nc                  8    t        j                   g d      S )Nr   r   r   r   rC   r   s   r2   r   ztranspose.<locals>.f_rank3K  s    ""5)$??r4   c                  8    t        j                   g d      S )Nr   r   r   r   s   r2   r   ztranspose.<locals>.f_rank4N  s    ""5,TBBr4   rK   r   r   r   r   )r   r   r   ra   r<   rh   r   rD   r   r   r   r   r   rS   )rC   r   rA   rD   r   r   s   ``    r2   r   r     s   ^ ~~dK%1 N!!%g6E!%(EOOE{{^^E"d@C \\(..q17GDN N 
	  	=N N  
	  4@#N N& 
Du
LN N'N Ns   B
D!/(D!!(D!D!!D*zimage.central_cropc           	         t        j                  dd| g      5  t        j                  | d      } t        j                  |      }|%|dk  s|dkD  rt        d      |dk(  rL| cddd       S t        t        j                  |dkD  |dk        t
        d      }t        j                  ||       } t        |        | j                         j                  }|dk7  r|d	k7  rt        d
j                  |            d }|dk(  r, || d      \  }} || d      \  }}	| j                         d   }
n>| j                         d   } || d      \  }} || d      \  }}	| j                         d   }
|xs |du }|	xs |du }	|rtt        j                  |t         j"                        }t        j                  ||t        j                  |t         j"                        z  z
  dz  t         j$                        }nt'        |      }t)        |||z  z
  dz        }|	rtt        j                  |t         j"                        }t        j                  ||t        j                  |t         j"                        z  z
  dz  t         j$                        }nt'        |      }t)        |||z  z
  dz        }||dz  z
  }||dz  z
  }|dk(  r1t+        j,                  ||dg      }t+        j,                  ||dg      }n2t+        j,                  d||dg      }t+        j,                  d||dg      }t/        j0                  | ||      } |dk(  r| j3                  |rdn||	rdn||
g       n| j3                  |rdn||	rdn||
g       | cddd       S # 1 sw Y   yxY w)a  Crop the central region of the image(s).

  Remove the outer parts of an image but retain the central region of the image
  along each dimension. If we specify `central_fraction = 0.5`, this function
  returns the region marked with "X" in the below diagram. The larger the value
  of `central_fraction`, the larger the dimension of the region to be cropped
  and retained.

       --------
      |        |
      |  XXXX  |
      |  XXXX  |
      |        |   where "X" is the central 50% of the image.
       --------

  This function works on either a single image (`image` is a 3-D Tensor), or a
  batch of images (`image` is a 4-D Tensor).

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0],
  ...       [7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]],
  ...     [[13.0, 14.0, 15.0],
  ...       [16.0, 17.0, 18.0],
  ...       [19.0, 20.0, 21.0],
  ...       [22.0, 23.0, 24.0]],
  ...     [[25.0, 26.0, 27.0],
  ...       [28.0, 29.0, 30.0],
  ...       [31.0, 32.0, 33.0],
  ...       [34.0, 35.0, 36.0]],
  ...     [[37.0, 38.0, 39.0],
  ...       [40.0, 41.0, 42.0],
  ...       [43.0, 44.0, 45.0],
  ...       [46.0, 47.0, 48.0]]]
  >>> tf.image.central_crop(x, 0.5)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[16., 17., 18.],
          [19., 20., 21.]],
         [[28., 29., 30.],
          [31., 32., 33.]]], dtype=float32)>

  Args:
    image: Either a 3-D float Tensor of shape [height, width, depth], or a 4-D
      Tensor of shape [batch_size, height, width, depth].
    central_fraction: float (0, 1], fraction of size to crop

  Raises:
    ValueError: if central_crop_fraction is not within (0, 1].

  Returns:
    3-D / 4-D float Tensor, as per the input.
  Ncentral_croprC   r           r   z&central_fraction must be within (0, 1]rK   r   zK`image` should either be a Tensor with rank = 3 or rank = 4. Had rank = {}.c                     | j                         j                  |   j                  }||dfS t        j                  |       |   dfS )NFT)r<   dimsvaluer   rA   )r
   idxrE   s      r2   _get_dimzcentral_crop.<locals>._get_dim  sK    %%',,S177l		!U""__V$S)4//r4   r   rq   ro   rp   )r   r   r   r   constant_valuerS   r3   r   
logical_orr   r\   ra   r<   rh   formatr   r   float64r   floatintr   stackr   sliceru   )rC   central_fractioncentral_fraction_static
assert_opsrD   r   img_h	dynamic_himg_w	dynamic_wimg_dimg_bsimg_hdbbox_h_startimg_wdbbox_w_startbbox_h_sizebbox_w_size
bbox_begin	bbox_sizes                       r2   r   r   [  s   r ~~dNUG4 W!!%g6E)889IJ*	 C	'+BS+HABB	 C	'W W 


.46F#6M
N
>@j 00UCe% ??""DqyTQY 228&,@ @
0 qy!%+eY!%+eYoo"e #f!%+eY!%+eYoo"e>5=I>5=I
 }}UFNN3f]]FX]]+;V^^LLL
\\l U|f&6,C#CCqHIl}}UFNN3f]]FX]]+;V^^LLL
\\l U|f&6,C#CCqHIl,**K,**Kqy"((,a)HIj!''k2(FGi"((!\<)KLj!''[+r(JKiOOE:y9E qyoo$$e 	
 oo
)$$e 	 oW W Ws   A
M<-LM<<Nzimage.pad_to_bounding_boxc                 $    t        | ||||d      S )a  Pad `image` with zeros to the specified `height` and `width`.

  Adds `offset_height` rows of zeros on top, `offset_width` columns of
  zeros on the left, and then pads the image on the bottom and right
  with zeros until it has dimensions `target_height`, `target_width`.

  This op does nothing if `offset_*` is zero and the image already has size
  `target_height` by `target_width`.

  Usage Example:

  >>> x = [[[1., 2., 3.],
  ...       [4., 5., 6.]],
  ...       [[7., 8., 9.],
  ...       [10., 11., 12.]]]
  >>> padded_image = tf.image.pad_to_bounding_box(x, 1, 1, 4, 4)
  >>> padded_image
  <tf.Tensor: shape=(4, 4, 3), dtype=float32, numpy=
  array([[[ 0.,  0.,  0.],
  [ 0.,  0.,  0.],
  [ 0.,  0.,  0.],
  [ 0.,  0.,  0.]],
  [[ 0.,  0.,  0.],
  [ 1.,  2.,  3.],
  [ 4.,  5.,  6.],
  [ 0.,  0.,  0.]],
  [[ 0.,  0.,  0.],
  [ 7.,  8.,  9.],
  [10., 11., 12.],
  [ 0.,  0.,  0.]],
  [[ 0.,  0.,  0.],
  [ 0.,  0.,  0.],
  [ 0.,  0.,  0.],
  [ 0.,  0.,  0.]]], dtype=float32)>

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    offset_height: Number of rows of zeros to add on top.
    offset_width: Number of columns of zeros to add on the left.
    target_height: Height of output image.
    target_width: Width of output image.

  Returns:
    If `image` was 4-D, a 4-D float Tensor of shape
    `[batch, target_height, target_width, channels]`
    If `image` was 3-D, a 3-D float Tensor of shape
    `[target_height, target_width, channels]`

  Raises:
    ValueError: If the shape of `image` is incompatible with the `offset_*` or
      `target_*` arguments, or either `offset_height` or `offset_width` is
      negative.
  T)
check_dims)pad_to_bounding_box_internal)rC   offset_heightoffset_widthtarget_heighttarget_widths        r2   pad_to_bounding_boxr    s$    t 
&
 r4   c                 *   t        j                  dd| g      5  t        j                  | d      } d}| j                         }|j                  dk(  rd}t        j                  | d      } nW|j                  .d}t        j                  | d      } | j                  dgd	z         n|j                  d	k7  rt        d
|z        t        | d	      \  }}	}
}||z
  |
z
  }||z
  |	z
  }|rt        | d      }|t        |dk\  t        d      z  }|t        |dk\  t        d      z  }|t        |dk\  t        d      z  }|t        |dk\  t        d      z  }t        j                  ||       } t        j                  t        j                   dd||||ddg      d	dg      }t        j"                  | |      }||||fD cg c]  }t%        |      rdn| }}|j                  |       |st        j&                  |dg      }|cddd       S c c}w # 1 sw Y   yxY w)a  Pad `image` with zeros to the specified `height` and `width`.

  Adds `offset_height` rows of zeros on top, `offset_width` columns of
  zeros on the left, and then pads the image on the bottom and right
  with zeros until it has dimensions `target_height`, `target_width`.

  This op does nothing if `offset_*` is zero and the image already has size
  `target_height` by `target_width`.

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    offset_height: Number of rows of zeros to add on top.Must be 0-D `Tensor` of
      dtype int32 or int64. Can also a python integer.
    offset_width: Number of columns of zeros to add on the left.Must be 0-D
      `Tensor` of dtype int32 or int64. Can also a python integer.
    target_height: Height of output image.Must be 0-D `Tensor` of dtype int32 or
      int64. Can also a python integer.
    target_width: Width of output image.Must be 0-D `Tensor` of dtype int32 or
      int64. Can also a python integer.
    check_dims: If True, assert that dimensions are non-negative and in range.
      In multi-GPU distributed settings, assertions can cause program slowdown.
      Setting this parameter to `False` avoids this, resulting in faster speed
      in some situations, with the tradeoff being that some error checking is
      not happening.

  Returns:
    If `image` was 4-D, a 4-D float Tensor of shape
    `[batch, target_height, target_width, channels]`
    If `image` was 3-D, a 3-D float Tensor of shape
    `[target_height, target_width, channels]`

  Raises:
    ValueError: If the shape of `image` is incompatible with the `offset_*` or
      `target_*` arguments, or either `offset_height` or `offset_width` is
      negative. Not raised if `check_dims` is `False`.
  Nr  rC   r   TrK   Fr   r   r   rD   r[   zoffset_height must be >= 0zoffset_width must be >= 0z width must be <= target - offsetz!height must be <= target - offsetro   axis)r   r   r   r<   rh   r   expand_dimsru   rS   rI   r`   r3   r   r\   r   r   r   padr.   squeeze)rC   r  r  r  r  r  is_batchrX   batchheightwidthdepthafter_padding_widthafter_padding_heightr   paddingspaddedipadded_shapes                      r2   r  r  4  s_   N ~~d1E7; 4!!%g6EH//#KAh##E1-e				"h##E1-eootfqj!			a	
D
  #35q"AE65%&5=(=86A'eDjGMQ.
8: :jGLA-z79 9jG/14j>@ @jG0A5z?A Aj00UCe   q-!5|A
 	 F	H
 ]]5(+F e< 11$L  \"  qc2fi4 4VW4 4s   FH	6H.H	H		Hzimage.crop_to_bounding_boxc                    t        j                  dd| g      5  t        j                  | d      } d}| j                         }|j                  dk(  rd}t        j                  | d      } nW|j                  .d}t        j                  | d      } | j                  dgd	z         n|j                  d	k7  rt        d
|z        t        | d      }t        | d	      \  }}	}
}|t        |dk\  t        d      z  }|t        |dk\  t        d      z  }|t        |dkD  t        d      z  }|t        |dkD  t        d      z  }|t        |
||z   k\  t        d      z  }|t        |	||z   k\  t        d      z  }t        j                  ||       } t        j                  | t        j                   d||dg      t        j                   t        j"                  |       d   ||t        j"                  |       d   g            }||||fD cg c]  }t%        |      rdn| }}|j                  |       |st        j&                  |dg      }|cddd       S c c}w # 1 sw Y   yxY w)a  Crops an `image` to a specified bounding box.

  This op cuts a rectangular bounding box out of `image`. The top-left corner
  of the bounding box is at `offset_height, offset_width` in `image`, and the
  lower-right corner is at
  `offset_height + target_height, offset_width + target_width`.

  Example Usage:

  >>> image = tf.constant(np.arange(1, 28, dtype=np.float32), shape=[3, 3, 3])
  >>> image[:,:,0] # print the first channel of the 3-D tensor
  <tf.Tensor: shape=(3, 3), dtype=float32, numpy=
  array([[ 1.,  4.,  7.],
         [10., 13., 16.],
         [19., 22., 25.]], dtype=float32)>
  >>> cropped_image = tf.image.crop_to_bounding_box(image, 0, 0, 2, 2)
  >>> cropped_image[:,:,0] # print the first channel of the cropped 3-D tensor
  <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
  array([[ 1.,  4.],
         [10., 13.]], dtype=float32)>

  Args:
    image: 4-D `Tensor` of shape `[batch, height, width, channels]` or 3-D
      `Tensor` of shape `[height, width, channels]`.
    offset_height: Vertical coordinate of the top-left corner of the bounding
      box in `image`. Must be 0-D int32 `Tensor` or python integer.
    offset_width: Horizontal coordinate of the top-left corner of the bounding
      box in `image`. Must be 0-D int32 `Tensor` or python integer.
    target_height: Height of the bounding box. Must be 0-D int32 `Tensor` or
      python integer.
    target_width: Width of the bounding box. Must be 0-D int32 `Tensor` or
      python integer.

  Returns:
    If `image` was 4-D, a 4-D `Tensor` of shape
    `[batch, target_height, target_width, channels]`.
    If `image` was 3-D, a 3-D `Tensor` of shape
    `[target_height, target_width, channels]`.
    It has the same dtype with `image`.

  Raises:
    ValueError: `image` is not a 3-D or 4-D `Tensor`.
    ValueError: `offset_width < 0` or `offset_height < 0`.
    ValueError: `target_width <= 0` or `target_height <= 0`.
    ValueError: `width < offset_width + target_width` or
      `height < offset_height + target_height`.
  Ncrop_to_bounding_boxrC   r   TrK   Fr   r   r   r[   r  zoffset_width must be >= 0.zoffset_height must be >= 0.target_width must be > 0.target_height must be > 0.z!width must be >= target + offset.z"height must be >= target + offset.r  )r   r   r   r<   rh   r   r  ru   rS   r`   rI   r3   r   r\   r   r   r   rA   r.   r  )rC   r  r  r  r  r  rX   r   r  r  r  r  croppedr"  cropped_shapes                  r2   r%  r%    s   f ~~d2UG< 5!!%g6EH//#KAh##E1-e				"h##E1-eootfqj!			a	
D
  &eEBJ"25q"AE65%',!+Z68 8J'-1,j79 9J',*J57 7J'-!+Z68 8J'%L<$?@*=? ?J'&]]%BCZ>@ @J..z5AEooq-qABOOE"1%OOE"1%	' 	()G e< 11$M  m$!!'4gk5 5XY5 5s   G'I I.IIIzimage.resize_with_crop_or_padz#image.resize_image_with_crop_or_padc                    t        j                  dd| g      5  t        j                  | d      } | j                         }d}|j                  dk(  rd}t        j                  | d      } nW|j                  .d}t        j                  | d      } | j                  dgd	z         n|j                  d	k7  rt        d
|z        t        | d      }|t        |dkD  t        d      z  }|t        |dkD  t        d      z  }t        j                  ||       } t        |      rt        j                  ||      }t        |      rt        j                  ||      }d }d }d }t        | d	      \  }	}
}}	||z
  } || dz  d      } ||dz  d      }||
z
  } || dz  d      } ||dz  d      }t        | || |||
       |||            }t!        |||||      }|j                         j                  t        d      t        |d	      \  }	}}}	g }|t         |||      t        d      z  }|t         |||      t        d      z  }t        j                  ||      }|st        j"                  |dg      }|cddd       S # 1 sw Y   yxY w)a  Crops and/or pads an image to a target width and height.

  Resizes an image to a target width and height by either centrally
  cropping the image or padding it evenly with zeros.

  If `width` or `height` is greater than the specified `target_width` or
  `target_height` respectively, this op centrally crops along that dimension.

  For example:

  >>> image = np.arange(75).reshape(5, 5, 3)  # create 3-D image input
  >>> image[:,:,0]  # print first channel just for demo purposes
  array([[ 0,  3,  6,  9, 12],
         [15, 18, 21, 24, 27],
         [30, 33, 36, 39, 42],
         [45, 48, 51, 54, 57],
         [60, 63, 66, 69, 72]])
  >>> image = tf.image.resize_with_crop_or_pad(image, 3, 3)  # crop
  >>> # print first channel for demo purposes; centrally cropped output
  >>> image[:,:,0]
  <tf.Tensor: shape=(3, 3), dtype=int64, numpy=
  array([[18, 21, 24],
         [33, 36, 39],
         [48, 51, 54]])>

  If `width` or `height` is smaller than the specified `target_width` or
  `target_height` respectively, this op centrally pads with 0 along that
  dimension.

  For example:

  >>> image = np.arange(1, 28).reshape(3, 3, 3)  # create 3-D image input
  >>> image[:,:,0]  # print first channel just for demo purposes
  array([[ 1,  4,  7],
         [10, 13, 16],
         [19, 22, 25]])
  >>> image = tf.image.resize_with_crop_or_pad(image, 5, 5)  # pad
  >>> # print first channel for demo purposes; we should see 0 paddings
  >>> image[:,:,0]
  <tf.Tensor: shape=(5, 5), dtype=int64, numpy=
  array([[ 0,  0,  0,  0,  0],
         [ 0,  1,  4,  7,  0],
         [ 0, 10, 13, 16,  0],
         [ 0, 19, 22, 25,  0],
         [ 0,  0,  0,  0,  0]])>

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    target_height: Target height.
    target_width: Target width.

  Raises:
    ValueError: if `target_height` or `target_width` are zero or negative.

  Returns:
    Cropped and/or padded image.
    If `images` was 4-D, a 4-D float Tensor of shape
    `[batch, new_height, new_width, channels]`.
    If `images` was 3-D, a 3-D float Tensor of shape
    `[new_height, new_width, channels]`.
  Nresize_image_with_crop_or_padrC   r   TrK   Fr   r   r   r[   r&  r'  c                 r    t        |       st        |      rt        j                  | |      S t        | |      S r   r.   r   maximummaxr:   ys     r2   max_z+resize_image_with_crop_or_pad.<locals>.max_`  .    	A*Q-1%%1ayr4   c                 r    t        |       st        |      rt        j                  | |      S t        | |      S r   )r.   r   minimumminr0  s     r2   min_z+resize_image_with_crop_or_pad.<locals>.min_f  r3  r4   c                 d    t        |       st        |      rt        j                  | |      S | |k(  S r   )r.   r   r   r0  s     r2   equal_z-resize_image_with_crop_or_pad.<locals>.equal_l  s)    	A*Q-~~a##Avr4   r  ro   zresized contains no shape.zresized height is not correct.zresized width is not correct.r  )r   r   r   r<   rh   r   r  ru   rS   r`   r3   r   r\   r.   rI   r%  r  r  )rC   r  r  rX   r  r   r2  r7  r9  _r  r  
width_diffoffset_crop_widthoffset_pad_widthheight_diffoffset_crop_heightoffset_pad_heightr(  resizedresized_heightresized_widths                         r2   r+  r+    s   F ~~d;eWE W!!%g6E//#KHAh##E1-e				"h##E1-eootfqj!			a	
D
  &eEBJ',*J57 7J'-!+Z68 8J ..z5AE - &88
m%m,%77
8DFl +5q9Avua%Jj[A-q1J!OQ/&(K{la/3[A-q1 #5*<>O#'v#>#'e#<>G
 "'+<>N"/?G   (344*:7*K'A~}aJ'~}-z(* *J '}l+Z') )J 00WEG!!'4goW W Ws   IJJzimage.ResizeMethodc                        e Zd ZdZdZdZdZdZy)ResizeMethodV1z"See `v1.image.resize` for details.r   rq   ro   rK   N)__name__
__module____qualname____doc__BILINEARNEAREST_NEIGHBORBICUBICAREArN   r4   r2   rE  rE    s    *('	
$r4   rE  c                   0    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zy
)ResizeMethodz"See `tf.image.resize` for details.bilinearnearestbicubicarealanczos3lanczos5gaussianmitchellcubicN)rF  rG  rH  rI  rJ  rK  rL  rM  LANCZOS3LANCZOS5GAUSSIANMITCHELLCUBICrN   r4   r2   rO  rO    s.    *('	$(((!-r4   rO  c           
         t        j                  |d| |g      5  t        j                  | d      } | j                         j                  t        d      d}| j                         j                  dk(  rd}t        j                  | d	      } n(| j                         j                  d
k7  rt        d      | j                         j                         \  }}}	}	 t        j                  |t        j                  d      }|j                         j                  dg      st        d      |rt        | d
      \  }}
}}t        j                  |d	   t        j                         t        j                  |
t        j                         z  }t        j                  |d   t        j                         t        j                  |t        j                         z  }t        j"                  ||      }t        j                  t        j$                  |t        j                  |
t        j                         z        t        j                        }t        j                  t        j$                  |t        j                  |t        j                         z        t        j                        }t        j                  ||gt        j                  d      }t'        j(                  |      }t+        j,                  |d	      j.                  }t+        j,                  |d      j.                  }|rEt1        d ||	||fD              r/|	|k(  r*||k(  r%|st        j2                  | d	g      } | cddd       S  || |      } | j5                  d||dg       |st        j2                  | d	g      } | cddd       S # t        t
        f$ r t        d      w xY w# 1 sw Y   yxY w)z2Core functionality for v1 and v2 resize functions.resizer   r   Nz'images' contains no shape.TrK   Fr   r   z,'images' must have either 3 or 4 dimensions.sizez!'size' must be a 1-D int32 Tensorro   z@'size' must be a 1-D Tensor of 2 elements: new_height, new_widthr  rq   c              3   $   K   | ]  }|d u 
 y wr   rN   rO   s     r2   rQ   z(_resize_images_common.<locals>.<genexpr>  s      #E 	
#Es   r  )r   r   r   r<   rh   rS   r   r  r>   r   r   	TypeErroris_compatible_withrI   r   r   float32r5  r   r   constant_value_as_shaper   dimension_at_indexr   allr  ru   )r   
resizer_fnr^  preserve_aspect_ratior   skip_resize_if_samer  r:  r  r  current_heightcurrent_widthscale_factor_heightscale_factor_widthscale_factorscaled_height_constscaled_width_constsize_const_as_shapenew_height_constnew_width_consts                       r2   _resize_images_commonrs    su    ~~dHvtn5 H""69F'677H1$h$$VQ/f					!	!Q	&GHH **,446Avua>""4FCd >>..s3 / 0 0 ,<V!,L)a --Q
0
--
78  --Q
0
--v~~
67  %%&9;MNl$MM
..!~v~~FG H
,, $==
..!}fnnEF G
,, ""$79K#L#)<<(.0d &==dC#667J78::?% "556I6799> 
 s #E!5*:FC#E  E _$3C)C""64}H H@ %F d,otDE  qc2fQH H  z" ><==>!H Hs+   C O&N.I O+9O.O		OOzimage.resize_imageszimage.resizeFc                 4    fd}t        | ||||d      S )a]	  Resize `images` to `size` using the specified `method`.

  Resized images will be distorted if their original aspect ratio is not
  the same as `size`.  To avoid distortions see
  `tf.image.resize_with_pad` or `tf.image.resize_with_crop_or_pad`.

  The `method` can be one of:

  *   <b>`tf.image.ResizeMethod.BILINEAR`</b>: [Bilinear interpolation.](
    https://en.wikipedia.org/wiki/Bilinear_interpolation)
  *   <b>`tf.image.ResizeMethod.NEAREST_NEIGHBOR`</b>: [
    Nearest neighbor interpolation.](
    https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)
  *   <b>`tf.image.ResizeMethod.BICUBIC`</b>: [Bicubic interpolation.](
    https://en.wikipedia.org/wiki/Bicubic_interpolation)
  *   <b>`tf.image.ResizeMethod.AREA`</b>: Area interpolation.

  The return value has the same type as `images` if `method` is
  `tf.image.ResizeMethod.NEAREST_NEIGHBOR`. It will also have the same type
  as `images` if the size of `images` can be statically determined to be the
  same as `size`, because `images` is returned in this case. Otherwise, the
  return value has type `float32`.

  Args:
    images: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    size: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The new
      size for the images.
    method: ResizeMethod.  Defaults to `tf.image.ResizeMethod.BILINEAR`.
    align_corners: bool.  If True, the centers of the 4 corner pixels of the
      input and output tensors are aligned, preserving the values at the corner
      pixels. Defaults to `False`.
    preserve_aspect_ratio: Whether to preserve the aspect ratio. If this is set,
      then `images` will be resized to a size that fits in `size` while
      preserving the aspect ratio of the original image. Scales up the image if
      `size` is bigger than the current size of the `image`. Defaults to False.
    name: A name for this operation (optional).

  Raises:
    ValueError: if the shape of `images` is incompatible with the
      shape arguments to this function
    ValueError: if `size` has invalid shape or type.
    ValueError: if an unsupported resize method is specified.

  Returns:
    If `images` was 4-D, a 4-D float Tensor of shape
    `[batch, new_height, new_width, channels]`.
    If `images` was 3-D, a 3-D float Tensor of shape
    `[new_height, new_width, channels]`.
  c                 (   t         j                  k(  st        j                  k(  rt        j                  | |      S t         j
                  k(  st        j
                  k(  rt        j                  | |      S t         j                  k(  st        j                  k(  rt        j                  | |      S t         j                  k(  st        j                  k(  rt        j                  | |      S t        dj                              )z=Legacy resize core function, passed to _resize_images_common.align_corners$Resize method is not implemented: {})rE  rJ  rO  r   resize_bilinearrK  resize_nearest_neighborrL  resize_bicubicrM  resize_arearS   r   )images_tnew_sizerw  methods     r2   	resize_fnz resize_images.<locals>.resize_fn9  s    (((Fl6K6K,K**
HM; ;
N33
3
L11
122
HM; ;	>))	)V|7K7K-K))
HM; ;	>&&	&&L4E4E*E&&
HM; ; =DDVLMMr4   Trg  r   rh  rs  )r   r^  r  rw  rg  r   r  s     ``   r2   resize_imagesr    s+    vN$ 

1
   r4   c                 4    fd}t        | ||||d      S )ad  Resize `images` to `size` using the specified `method`.

  Resized images will be distorted if their original aspect ratio is not
  the same as `size`.  To avoid distortions see
  `tf.image.resize_with_pad`.

  >>> image = tf.constant([
  ...  [1,0,0,0,0],
  ...  [0,1,0,0,0],
  ...  [0,0,1,0,0],
  ...  [0,0,0,1,0],
  ...  [0,0,0,0,1],
  ... ])
  >>> # Add "batch" and "channels" dimensions
  >>> image = image[tf.newaxis, ..., tf.newaxis]
  >>> image.shape.as_list()  # [batch, height, width, channels]
  [1, 5, 5, 1]
  >>> tf.image.resize(image, [3,5])[0,...,0].numpy()
  array([[0.6666667, 0.3333333, 0.       , 0.       , 0.       ],
         [0.       , 0.       , 1.       , 0.       , 0.       ],
         [0.       , 0.       , 0.       , 0.3333335, 0.6666665]],
        dtype=float32)

  It works equally well with a single image instead of a batch of images:

  >>> tf.image.resize(image[0], [3,5]).shape.as_list()
  [3, 5, 1]

  When `antialias` is true, the sampling filter will anti-alias the input image
  as well as interpolate.  When downsampling an image with [anti-aliasing](
  https://en.wikipedia.org/wiki/Spatial_anti-aliasing) the sampling filter
  kernel is scaled in order to properly anti-alias the input image signal.
  `antialias` has no effect when upsampling an image:

  >>> a = tf.image.resize(image, [5,10])
  >>> b = tf.image.resize(image, [5,10], antialias=True)
  >>> tf.reduce_max(abs(a - b)).numpy()
  0.0

  The `method` argument expects an item from the `image.ResizeMethod` enum, or
  the string equivalent. The options are:

  *   <b>`bilinear`</b>: [Bilinear interpolation.](
    https://en.wikipedia.org/wiki/Bilinear_interpolation) If `antialias` is
    true, becomes a hat/tent filter function with radius 1 when downsampling.
  *   <b>`lanczos3`</b>:  [Lanczos kernel](
    https://en.wikipedia.org/wiki/Lanczos_resampling) with radius 3.
    High-quality practical filter but may have some ringing, especially on
    synthetic images.
  *   <b>`lanczos5`</b>: [Lanczos kernel] (
    https://en.wikipedia.org/wiki/Lanczos_resampling) with radius 5.
    Very-high-quality filter but may have stronger ringing.
  *   <b>`bicubic`</b>: [Cubic interpolant](
    https://en.wikipedia.org/wiki/Bicubic_interpolation) of Keys. Equivalent to
    Catmull-Rom kernel. Reasonably good quality and faster than Lanczos3Kernel,
    particularly when upsampling.
  *   <b>`gaussian`</b>: [Gaussian kernel](
    https://en.wikipedia.org/wiki/Gaussian_filter) with radius 3,
    sigma = 1.5 / 3.0.
  *   <b>`nearest`</b>: [Nearest neighbor interpolation.](
    https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation)
    `antialias` has no effect when used with nearest neighbor interpolation.
  *   <b>`area`</b>: Anti-aliased resampling with area interpolation.
    `antialias` has no effect when used with area interpolation; it
    always anti-aliases.
  *   <b>`mitchellcubic`</b>: Mitchell-Netravali Cubic non-interpolating filter.
    For synthetic images (especially those lacking proper prefiltering), less
    ringing than Keys cubic kernel but less sharp.

  Note: Near image edges the filtering kernel may be partially outside the
  image boundaries. For these pixels, only input pixels inside the image will be
  included in the filter sum, and the output value will be appropriately
  normalized.

  The return value has type `float32`, unless the `method` is
  `ResizeMethod.NEAREST_NEIGHBOR`, then the return dtype is the dtype
  of `images`:

  >>> nn = tf.image.resize(image, [5,7], method='nearest')
  >>> nn[0,...,0].numpy()
  array([[1, 0, 0, 0, 0, 0, 0],
         [0, 1, 1, 0, 0, 0, 0],
         [0, 0, 0, 1, 0, 0, 0],
         [0, 0, 0, 0, 1, 1, 0],
         [0, 0, 0, 0, 0, 0, 1]], dtype=int32)

  With `preserve_aspect_ratio=True`, the aspect ratio is preserved, so `size`
  is the maximum for each dimension:

  >>> max_10_20 = tf.image.resize(image, [10,20], preserve_aspect_ratio=True)
  >>> max_10_20.shape.as_list()
  [1, 10, 10, 1]

  Args:
    images: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    size: A 1-D int32 Tensor of 2 elements: `new_height, new_width`.  The new
      size for the images.
    method: An `image.ResizeMethod`, or string equivalent.  Defaults to
      `bilinear`.
    preserve_aspect_ratio: Whether to preserve the aspect ratio. If this is set,
      then `images` will be resized to a size that fits in `size` while
      preserving the aspect ratio of the original image. Scales up the image if
      `size` is bigger than the current size of the `image`. Defaults to False.
    antialias: Whether to use an anti-aliasing filter when downsampling an
      image.
    name: A name for this operation (optional).

  Raises:
    ValueError: if the shape of `images` is incompatible with the
      shape arguments to this function
    ValueError: if `size` has an invalid shape or type.
    ValueError: if an unsupported resize method is specified.

  Returns:
    If `images` was 4-D, a 4-D float Tensor of shape
    `[batch, new_height, new_width, channels]`.
    If `images` was 3-D, a 3-D float Tensor of shape
    `[new_height, new_width, channels]`.
  c                 Z    t         j                  t         j                  t         j                  t         j                  g} fd}t         j
                  k(  r"r |d      S t        j                   d      S t         j                  k(  rt        j                   d      S t         j                  k(  r"r |d      S t        j                   d      S t         j                  k(  rt        j                         S |v r |      S t        dj                              )z6Resize core function, passed to _resize_images_common.c                 $   t        j                  t        j                        t        j                  t	        j
                        dd t        j                        z  }t        j                  |t	        j                  dg      |       S )Nr   rq   rK   ro   )kernel_type	antialias)	r   r   r   rb  r   rA   r   scale_and_translatezeros)r  scaler  r}  r~  s     r2   resize_with_scale_and_translatezLresize_images_v2.<locals>.resize_fn.<locals>.resize_with_scale_and_translate  st    
--
7
--	1!A6fnn
MN  ..



//1#
 r4   triangleT)half_pixel_centers	keyscubicrx  )rO  rX  rY  rZ  r[  rJ  r   ry  rK  rz  rL  r{  rM  r|  rS   r   )r}  r~  scale_and_translate_methodsr  r  r  s   ``  r2   r  z#resize_images_v2.<locals>.resize_fn  s    	|44l6K6K""#

 &&&	.z::,,h49 	9	<00	022
H7 7	<''	'	.{;;++h49 	9	<$$	$&&x::	.	.,V44=DDVLMMr4   Fr  r  )r   r^  r  rg  r  r   r  s     ` `  r2   resize_images_v2r  T  s,    B'NR 

1
! !r4   c                    t        j                  dd| g      5  t        j                  | d      } | j                         }d}|j                  dk(  rd}t        j                  | d      } nW|j                  .d}t        j                  | d      } | j                  dgd	z         n|j                  d	k7  rt        d
|z        t        | d      }|t        |dkD  t        d      z  }|t        |dkD  t        d      z  }t        j                  ||       } d }t        | d	      \  }}	}
}t        j                  |	t         j"                        }t        j                  |
t         j"                        }t        j                  |t         j"                        }t        j                  |t         j"                        } |||z  ||z        }||z  }||z  }t        j                  t        j$                  |      t         j&                        }t        j                  t        j$                  |      t         j&                        }||z
  dz  }||z
  dz  }t        j$                  |      }t        j$                  |      } |dt        j                  |t         j&                              } |dt        j                  |t         j&                              } || ||g      }t)        |||||      }|j                         j                  t        d      t        |d	       |st        j*                  |dg      }|cddd       S # 1 sw Y   yxY w)zACore functionality for v1 and v2 resize_image_with_pad functions.Nresize_image_with_padrC   r   TrK   Fr   r   r   r[   r&  r'  c                 r    t        |       st        |      rt        j                  | |      S t        | |      S r   r-  r0  s     r2   r2  z+_resize_image_with_pad_common.<locals>.max_"  r3  r4   r  r  ro   zpadded contains no shape.r  )r   r   r   r<   rh   r   r  ru   rS   r`   r3   r   r\   rI   r   r   r   rb  floorr   r  r  )rC   r  r  r  rX   r  r   r2  r:  r  r  f_heightf_widthf_target_heightf_target_widthratioresized_height_floatresized_width_floatrB  rC  padding_heightpadding_widthf_padding_heightf_padding_widthp_heightp_widthrA  r!  s                               r2   _resize_image_with_pad_commonr    s    ~~d3eW= E!!%g6E//#KHAh##E1-e				"h##E1-eootfqj!			a	
D
  &eEBJ',*J57 7J'-!+Z68 8J ..z5AE +5q9Avua }}V6>>:HmmE8GmmMHO]]<v~~FN >)8o+EFE#e+!E/]]+,FLLBNMM*+6<<AM &(<<AN#&99Q>M~~n5nn]3OAx}}%5V\\JKH1hmmO6<<HIG >?G (G]!-/F '233V!$  qc2fKE E Es   L%MMzimage.resize_image_with_padc                 .    fd}t        | |||      S )aE  Resizes and pads an image to a target width and height.

  Resizes an image to a target width and height by keeping
  the aspect ratio the same without distortion. If the target
  dimensions don't match the image dimensions, the image
  is resized and then padded with zeroes to match requested
  dimensions.

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    target_height: Target height.
    target_width: Target width.
    method: Method to use for resizing image. See `resize_images()`
    align_corners: bool.  If True, the centers of the 4 corner pixels of the
      input and output tensors are aligned, preserving the values at the corner
      pixels. Defaults to `False`.

  Raises:
    ValueError: if `target_height` or `target_width` are zero or negative.

  Returns:
    Resized and padded image.
    If `images` was 4-D, a 4-D float Tensor of shape
    `[batch, new_height, new_width, channels]`.
    If `images` was 3-D, a 3-D float Tensor of shape
    `[new_height, new_width, channels]`.
  c                 "    t        | |      S )Nrv  )r  )imr~  rw  r  s     r2   
_resize_fnz,resize_image_with_pad_v1.<locals>._resize_fnv  s    Xv]KKr4   r  )rC   r  r  r  rw  r  s      `` r2   resize_image_with_pad_v1r  R  s"    HL 
'um\'1
3 3r4   zimage.resize_with_padc                 .    fd}t        | |||      S )a  Resizes and pads an image to a target width and height.

  Resizes an image to a target width and height by keeping
  the aspect ratio the same without distortion. If the target
  dimensions don't match the image dimensions, the image
  is resized and then padded with zeroes to match requested
  dimensions.

  Args:
    image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    target_height: Target height.
    target_width: Target width.
    method: Method to use for resizing image. See `image.resize()`
    antialias: Whether to use anti-aliasing when resizing. See 'image.resize()'.

  Raises:
    ValueError: if `target_height` or `target_width` are zero or negative.

  Returns:
    Resized and padded image.
    If `images` was 4-D, a 4-D float Tensor of shape
    `[batch, new_height, new_width, channels]`.
    If `images` was 3-D, a 3-D float Tensor of shape
    `[new_height, new_width, channels]`.
  c                 "    t        | |      S )N)r  )r  )r  r~  r  r  s     r2   r  z,resize_image_with_pad_v2.<locals>._resize_fn  s    B&IFFr4   r  )rC   r  r  r  r  r  s      `` r2   resize_image_with_pad_v2r  }  s"    DG 
'um\'1
3 3r4   zimage.per_image_standardizationc                    t        j                  dd| g      5 }t        j                  | d      } t        |       } t	        j
                  | t        j                        } t	        j                  t        j                  |       dd       }t	        j                  | g dd	      }t	        j                  | g dd	      }t	        j                  t	        j
                  |t        j                              }t	        j                  ||      }| |z  } t	        j                  | ||      } | cddd       S # 1 sw Y   yxY w)
a  Linearly scales each image in `image` to have mean 0 and variance 1.

  For each 3-D image `x` in `image`, computes `(x - mean) / adjusted_stddev`,
  where

  - `mean` is the average of all values in `x`
  - `adjusted_stddev = max(stddev, 1.0/sqrt(N))` is capped away from 0 to
    protect against division by 0 when handling uniform images
    - `N` is the number of elements in `x`
    - `stddev` is the standard deviation of all values in `x`

  Example Usage:

  >>> image = tf.constant(np.arange(1, 13, dtype=np.int32), shape=[2, 2, 3])
  >>> image # 3-D tensor
  <tf.Tensor: shape=(2, 2, 3), dtype=int32, numpy=
  array([[[ 1,  2,  3],
          [ 4,  5,  6]],
         [[ 7,  8,  9],
          [10, 11, 12]]], dtype=int32)>
  >>> new_image = tf.image.per_image_standardization(image)
  >>> new_image # 3-D tensor with mean ~= 0 and variance ~= 1
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[-1.593255  , -1.3035723 , -1.0138896 ],
          [-0.7242068 , -0.4345241 , -0.14484136]],
         [[ 0.14484136,  0.4345241 ,  0.7242068 ],
          [ 1.0138896 ,  1.3035723 ,  1.593255  ]]], dtype=float32)>

  Args:
    image: An n-D `Tensor` with at least 3 dimensions, the last 3 of which are
      the dimensions of each image.

  Returns:
    A `Tensor` with the same shape as `image` and its dtype is `float32`.

  Raises:
    ValueError: The shape of `image` has fewer than 3 dimensions.
  Nper_image_standardizationrC   r   r  re   )rp   re   T)r  keepdims)r   r   r   ra   r   r   r   rb  reduce_prodr   rA   reduce_mean
reduce_stdrsqrtr.  divide)rC   r   
num_pixels
image_meanstddev
min_stddevadjusted_stddevs          r2   r  r    s    R ~~d7%A U!!%g6E!%(EMM%v~~6E%%iooe&<RS&ABJ%%e,NJ   \DIFj&.. IJJ&&vz:O	ZEOOE??E  s   DD55D>zimage.random_brightnessc                 n    |dk  rt        d      t        j                  g | ||      }t        | |      S )a  Adjust the brightness of images by a random factor.

  Equivalent to `adjust_brightness()` using a `delta` randomly picked in the
  interval `[-max_delta, max_delta)`.

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_brightness`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: An image or images to adjust.
    max_delta: float, must be non-negative. This parameter controls the maximum
      relative change in brightness.
    seed: A Python integer. Used to create a random seed. See
      `tf.compat.v1.set_random_seed` for behavior.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...      [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.random_brightness(x, 0.2)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=...>

  Returns:
    The brightness-adjusted image(s).

  Raises:
    ValueError: if `max_delta` is negative.
  r   max_delta must be non-negative.ry   )rS   r   r   adjust_brightnessrC   	max_deltarz   deltas       r2   random_brightnessr    s=    J ]
6
77

#
#B
ID
I%	5%	((r4   z!image.stateless_random_brightnessc                 n    |dk  rt        d      t        j                  g | ||      }t        | |      S )a  Adjust the brightness of images by a random factor deterministically.

  Equivalent to `adjust_brightness()` using a `delta` randomly picked in the
  interval `[-max_delta, max_delta)`.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...      [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> seed = (1, 2)
  >>> tf.image.stateless_random_brightness(x, 0.2, seed)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 1.1376241,  2.1376243,  3.1376243],
          [ 4.1376243,  5.1376243,  6.1376243]],
         [[ 7.1376243,  8.137624 ,  9.137624 ],
          [10.137624 , 11.137624 , 12.137624 ]]], dtype=float32)>

  Args:
    image: An image or images to adjust.
    max_delta: float, must be non-negative.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Returns:
    The brightness-adjusted image(s).

  Raises:
    ValueError: if `max_delta` is negative.
  r   r  rA   r   r   rz   )rS   r   r   r  r  s       r2   stateless_random_brightnessr    sA    N ]
6
77

7
7	z)$@%	5%	((r4   zimage.random_contrastc                     ||k  rt        d      |dk  rt        d      t        j                  g |||      }t        | |      S )a  Adjust the contrast of an image or images by a random factor.

  Equivalent to `adjust_contrast()` but uses a `contrast_factor` randomly
  picked in the interval `[lower, upper)`.

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_contrast`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: An image tensor with 3 or more dimensions.
    lower: float.  Lower bound for the random contrast factor.
    upper: float.  Upper bound for the random contrast factor.
    seed: A Python integer. Used to create a random seed. See
      `tf.compat.v1.set_random_seed` for behavior.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.random_contrast(x, 0.2, 0.5)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=...>

  Returns:
    The contrast-adjusted image(s).

  Raises:
    ValueError: if `upper <= lower` or if `lower < 0`.
  upper must be > lower.r   lower must be non-negative.ry   )rS   r   r   adjust_contrastrC   lowerupperrz   contrast_factors        r2   random_contrastr  <  sN    H e^
-
..
QY
2
33--b%TJ/		00r4   zimage.stateless_random_contrastc                     ||k  rt        d      |dk  rt        d      t        j                  g |||      }t        | |      S )a  Adjust the contrast of images by a random factor deterministically.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  Args:
    image: An image tensor with 3 or more dimensions.
    lower: float.  Lower bound for the random contrast factor.
    upper: float.  Upper bound for the random contrast factor.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...      [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> seed = (1, 2)
  >>> tf.image.stateless_random_contrast(x, 0.2, 0.5, seed)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[3.4605184, 4.4605184, 5.4605184],
          [4.820173 , 5.820173 , 6.820173 ]],
         [[6.179827 , 7.179827 , 8.179828 ],
          [7.5394816, 8.539482 , 9.539482 ]]], dtype=float32)>

  Returns:
    The contrast-adjusted image(s).

  Raises:
    ValueError: if `upper <= lower` or if `lower < 0`.
  r  r   r  r  )rS   r   r   r  r  s        r2   stateless_random_contrastr  j  sQ    H e^
-
..
QY
2
33(AAuU7/		00r4   zimage.adjust_brightnessc                    t        j                  dd| |g      5 }t        j                  | d      } | j                  }|t        j
                  t        j                  fv r| }nt        | t        j                        }t        j                  |t        j                  ||j                        |      }t        ||d      cddd       S # 1 sw Y   yxY w)a  Adjust the brightness of RGB or Grayscale images.

  This is a convenience method that converts RGB images to float
  representation, adjusts their brightness, and then converts them back to the
  original data type. If several adjustments are chained, it is advisable to
  minimize the number of redundant conversions.

  The value `delta` is added to all components of the tensor `image`. `image` is
  converted to `float` and scaled appropriately if it is in fixed-point
  representation, and `delta` is converted to the same data type. For regular
  images, `delta` should be in the range `(-1,1)`, as it is added to the image
  in floating point representation, where pixel values are in the `[0,1)` range.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_brightness(x, delta=0.1)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 1.1,  2.1,  3.1],
          [ 4.1,  5.1,  6.1]],
         [[ 7.1,  8.1,  9.1],
          [10.1, 11.1, 12.1]]], dtype=float32)>

  Args:
    image: RGB image or images to adjust.
    delta: A scalar. Amount to add to the pixel values.

  Returns:
    A brightness-adjusted tensor of the same shape and type as `image`.
  Nr  rC   r   Tsaturate)r   r   r   r   r   float16rb  convert_image_dtyper   addr   )rC   r  r   
orig_dtype	flt_imageadjusteds         r2   r  r    s    J ~~d/%@ DD!!%g6EJfnnfnn55i%eV^^<i||8==	8tEH xdCD D Ds   B&C

Czimage.adjust_contrastc                 p   t        j                  dd| |g      5 }t        j                  | d      } | j                  }|t        j
                  t        j                  fv r| }nt        | t        j                        }t        j                  |||      }t        ||d      cddd       S # 1 sw Y   yxY w)ar  Adjust contrast of RGB or grayscale images.

  This is a convenience method that converts RGB images to float
  representation, adjusts their contrast, and then converts them back to the
  original data type. If several adjustments are chained, it is advisable to
  minimize the number of redundant conversions.

  `images` is a tensor of at least 3 dimensions.  The last 3 dimensions are
  interpreted as `[height, width, channels]`.  The other dimensions only
  represent a collection of images, such as `[batch, height, width, channels].`

  Contrast is adjusted independently for each channel of each image.

  For each channel, this Op computes the mean of the image pixels in the
  channel and then adjusts each component `x` of each pixel to
  `(x - mean) * contrast_factor + mean`.

  `contrast_factor` must be in the interval `(-inf, inf)`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_contrast(x, 2.)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[-3.5, -2.5, -1.5],
          [ 2.5,  3.5,  4.5]],
         [[ 8.5,  9.5, 10.5],
          [14.5, 15.5, 16.5]]], dtype=float32)>

  Args:
    images: Images to adjust.  At least 3-D.
    contrast_factor: A float multiplier for adjusting contrast.

  Returns:
    The contrast-adjusted image or images.
  Nr  r   r   )r  r   Tr  )
r   r   r   r   r   r  rb  r  r   adjust_contrastv2)r   r  r   r  
flt_imagesr  s         r2   r  r    s    T ~~d-/1 D48""69FJfnnfnn55j&vv~~>j..O$@H xdCD D Ds   BB,,B5zimage.adjust_gammac                    t        j                  dd| ||g      5 }t        j                  | d      } | j                  }|t        j
                  t        j                  fv r| }nt        | t        j                        }t        |dk\  t        d      }|rt        j                  ||      }|||z  z  }t        ||d      cddd       S # 1 sw Y   yxY w)	a  Performs [Gamma Correction](http://en.wikipedia.org/wiki/Gamma_correction).

  on the input image.

  Also known as Power Law Transform. This function converts the
  input images at first to float representation, then transforms them
  pixelwise according to the equation `Out = gain * In**gamma`,
  and then converts the back to the original data type.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_gamma(x, 0.2)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[1.       , 1.1486983, 1.2457309],
          [1.319508 , 1.3797297, 1.4309691]],
         [[1.4757731, 1.5157166, 1.5518456],
          [1.5848932, 1.6153942, 1.6437519]]], dtype=float32)>

  Args:
    image : RGB image or images to adjust.
    gamma : A scalar or tensor. Non-negative real number.
    gain  : A scalar or tensor. The constant multiplier.

  Returns:
    A Tensor. A Gamma-adjusted tensor of the same shape and type as `image`.

  Raises:
    ValueError: If gamma is negative.
  Notes:
    For gamma greater than 1, the histogram will shift towards left and
    the output image will be darker than the input image.
    For gamma less than 1, the histogram will shift towards right and
    the output image will be brighter than the input image.
  References:
    [Wikipedia](http://en.wikipedia.org/wiki/Gamma_correction)
  Nadjust_gammarC   r   r   z+Gamma should be a non-negative real number.Tr  )r   r   r   r   r   r  rb  r  r3   rS   r   r\   )rC   gammagainr   r  r  	assert_opadjusted_imgs           r2   r  r  		  s    Z ~~dNUE4,@A HT!!%g6EJfnnfnn55i%eV^^<i
JEGI00EBe )U**L|Z$G%H H Hs   B$C		Czimage.convert_image_dtypec                    t        j                  | d      } t        j                  |      }|j                  s|j
                  st        d      | j                  j                  s!| j                  j
                  st        d      || j                  k(  rt        j                  | |      S t        j                  |d| g      5 }| j                  j
                  r|j
                  r| j                  j                  }|j                  }||kD  re|dz   |dz   z  }t        j                  | |      }|r!t        j                  |||      cddd       S t        j                  |||      cddd       S |rt        j                  | |      }nt        j                  | |      }|dz   |dz   z  }t        j                   |||      cddd       S | j                  j                  r-|j                  r!t        j                  | ||      cddd       S | j                  j
                  rPt        j                  | |      }d| j                  j                  z  }t        j                   |||      cddd       S |j                  d	z   }t        j                   | |      }|r!t        j                  |||      cddd       S t        j                  |||      cddd       S # 1 sw Y   yxY w)
a   Convert `image` to `dtype`, scaling its values if needed.

  The operation supports data types (for `image` and `dtype`) of
  `uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,
  `float16`, `float32`, `float64`, `bfloat16`.

  Images that are represented using floating point values are expected to have
  values in the range [0,1). Image data stored in integer data types are
  expected to have values in the range `[0,MAX]`, where `MAX` is the largest
  positive representable number for the data type.

  This op converts between data types, scaling the values appropriately before
  casting.

  Usage Example:

  >>> x = [[[1, 2, 3], [4, 5, 6]],
  ...      [[7, 8, 9], [10, 11, 12]]]
  >>> x_int8 = tf.convert_to_tensor(x, dtype=tf.int8)
  >>> tf.image.convert_image_dtype(x_int8, dtype=tf.float16, saturate=False)
  <tf.Tensor: shape=(2, 2, 3), dtype=float16, numpy=
  array([[[0.00787, 0.01575, 0.02362],
          [0.0315 , 0.03937, 0.04724]],
         [[0.0551 , 0.063  , 0.07086],
          [0.07874, 0.0866 , 0.0945 ]]], dtype=float16)>

  Converting integer types to floating point types returns normalized floating
  point values in the range [0, 1); the values are normalized by the `MAX` value
  of the input dtype. Consider the following two examples:

  >>> a = [[[1], [2]], [[3], [4]]]
  >>> a_int8 = tf.convert_to_tensor(a, dtype=tf.int8)
  >>> tf.image.convert_image_dtype(a_int8, dtype=tf.float32)
  <tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
  array([[[0.00787402],
          [0.01574803]],
         [[0.02362205],
          [0.03149606]]], dtype=float32)>

  >>> a_int32 = tf.convert_to_tensor(a, dtype=tf.int32)
  >>> tf.image.convert_image_dtype(a_int32, dtype=tf.float32)
  <tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
  array([[[4.6566129e-10],
          [9.3132257e-10]],
         [[1.3969839e-09],
          [1.8626451e-09]]], dtype=float32)>

  Despite having identical values of `a` and output dtype of `float32`, the
  outputs differ due to the different input dtypes (`int8` vs. `int32`). This
  is, again, because the values are normalized by the `MAX` value of the input
  dtype.

  Note that converting floating point values to integer type may lose precision.
  In the example below, an image tensor `b` of dtype `float32` is converted to
  `int8` and back to `float32`. The final output, however, is different from
  the original input `b` due to precision loss.

  >>> b = [[[0.12], [0.34]], [[0.56], [0.78]]]
  >>> b_float32 = tf.convert_to_tensor(b, dtype=tf.float32)
  >>> b_int8 = tf.image.convert_image_dtype(b_float32, dtype=tf.int8)
  >>> tf.image.convert_image_dtype(b_int8, dtype=tf.float32)
  <tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
  array([[[0.11811024],
          [0.33858266]],
         [[0.5590551 ],
          [0.77952754]]], dtype=float32)>

  Scaling up from an integer type (input dtype) to another integer type (output
  dtype) will not map input dtype's `MAX` to output dtype's `MAX` but converting
  back and forth should result in no change. For example, as shown below, the
  `MAX` value of int8 (=127) is not mapped to the `MAX` value of int16 (=32,767)
  but, when scaled back, we get the same, original values of `c`.

  >>> c = [[[1], [2]], [[127], [127]]]
  >>> c_int8 = tf.convert_to_tensor(c, dtype=tf.int8)
  >>> c_int16 = tf.image.convert_image_dtype(c_int8, dtype=tf.int16)
  >>> print(c_int16)
  tf.Tensor(
  [[[  256]
    [  512]]
   [[32512]
    [32512]]], shape=(2, 2, 1), dtype=int16)
  >>> c_int8_back = tf.image.convert_image_dtype(c_int16, dtype=tf.int8)
  >>> print(c_int8_back)
  tf.Tensor(
  [[[  1]
    [  2]]
   [[127]
    [127]]], shape=(2, 2, 1), dtype=int8)

  Scaling down from an integer type to another integer type can be a lossy
  conversion. Notice in the example below that converting `int16` to `uint8` and
  back to `int16` has lost precision.

  >>> d = [[[1000], [2000]], [[3000], [4000]]]
  >>> d_int16 = tf.convert_to_tensor(d, dtype=tf.int16)
  >>> d_uint8 = tf.image.convert_image_dtype(d_int16, dtype=tf.uint8)
  >>> d_int16_back = tf.image.convert_image_dtype(d_uint8, dtype=tf.int16)
  >>> print(d_int16_back)
  tf.Tensor(
  [[[ 896]
    [1920]]
   [[2944]
    [3968]]], shape=(2, 2, 1), dtype=int16)

  Note that converting from floating point inputs to integer types may lead to
  over/underflow problems. Set saturate to `True` to avoid such problem in
  problematic conversions. If enabled, saturation will clip the output into the
  allowed range before performing a potentially dangerous cast (and only before
  performing such a cast, i.e., when casting from a floating point to an integer
  type, and when casting from a signed to an unsigned type; `saturate` has no
  effect on casts between floats, or on casts that increase the type's range).

  Args:
    image: An image.
    dtype: A `DType` to convert `image` to.
    saturate: If `True`, clip the input before casting (if necessary).
    name: A name for this operation (optional).

  Returns:
    `image`, converted to `dtype`.

  Raises:
    AttributeError: Raises an attribute error when dtype is neither
    float nor integer.
  rC   r   z.dtype must be either floating point or integerz4image dtype must be either floating point or integerconvert_imagerq   Nr   r   )r   r   r   as_dtypeis_floating
is_integerAttributeErrorr   r   identityr   r/  r   floordivsaturate_castr   multiply)	rC   r   r  r   scale_in	scale_outr  scaledr   s	            r2   r  r  K	  so   D 

G
4%
//%
 %			5#3#3
I
JJ		 	 )?)?
O
PP
ekke$//
~~dOeW5 +9{{%"2"2h))i	I	 A9q=1""5%0''DA+9 +9 vu48+9 +9& ''u5$ue,$QHqL1  u481+9 +92 
	 	 U%6%6 ]]5%d3;+9 +9> 
		}}UE*U[[__$  u48G+9 +9L 		C""5%0''DAS+9 +9V vu48W+9 +9 +9s3   BK!K!4AK!9K!AK!8>K! K!!K*zimage.rgb_to_grayscalec                 Z   t        j                  |d| g      5 }t        j                  | d      } | j                  }t	        | t
        j                        }g d}t        j                  ||ddg      }t        j                  |d      }t	        |||      cddd       S # 1 sw Y   yxY w)aK  Converts one or more images from RGB to Grayscale.

  Outputs a tensor of the same `DType` and rank as `images`.  The size of the
  last dimension of the output is 1, containing the Grayscale value of the
  pixels.

  >>> original = tf.constant([[[1.0, 2.0, 3.0]]])
  >>> converted = tf.image.rgb_to_grayscale(original)
  >>> print(converted.numpy())
  [[[1.81...]]]

  Args:
    images: The RGB tensor to convert. The last dimension must have size 3 and
      should contain RGB values.
    name: A name for the operation (optional).

  Returns:
    The converted grayscale image(s).
  rgb_to_grayscaler   r   )gŏ1w-!?bX9?v/?rp   N)r   r   r   r   r  r   rb  r   	tensordotr   r  )r   r   r  r  rgb_weights
gray_floats         r2   r  r  
  s    , ~~d.9 BT""69FJ#FFNN;I +K##I{RHEJ&&z26Jz:DAB B Bs   A>B!!B*zimage.grayscale_to_rgbc                 :   t        j                  |d| g      5 }t        |       } t        j                  | d      } t	        j
                  t	        j                  |       dz
  d      }t	        j                  |t        j                        gt	        j
                  dd      gz   }t	        j                  |d      }t	        j                  | ||      }|j                  | j                         dd	 j                  dg             |cddd       S # 1 sw Y   yxY w)
ay  Converts one or more images from Grayscale to RGB.

  Outputs a tensor of the same `DType` and rank as `images`.  The size of the
  last dimension of the output is 3, containing the RGB value of the pixels.
  The input images' last dimension must be size 1.

  >>> original = tf.constant([[[1.0], [2.0], [3.0]]])
  >>> converted = tf.image.grayscale_to_rgb(original)
  >>> print(converted.numpy())
  [[[1. 1. 1.]
    [2. 2. 2.]
    [3. 3. 3.]]]

  Args:
    images: The Grayscale tensor to convert. The last dimension must be size 1.
    name: A name for the operation (optional).

  Returns:
    The converted grayscale image(s).
  grayscale_to_rgbr   r   rq   r   r  rK   Nrp   )r   r   rm   r   r   r  rD   onesr   r   concattileru   r<   concatenate)r   r   rank_1
shape_list	multiplesrgbs         r2   r  r  (
  s    . ~~d.9 
T"6*F""69F""9>>&#9A#=qAF>>&=>((A./0J  Q/I
..
6CMM&""$Sb)55qc:;
 
 
s   C.DDzimage.random_huec                     |dkD  rt        d      |dk  rt        d      t        j                  g | ||      }t        | |      S )aO  Adjust the hue of RGB images by a random factor.

  Equivalent to `adjust_hue()` but uses a `delta` randomly
  picked in the interval `[-max_delta, max_delta)`.

  `max_delta` must be in the interval `[0, 0.5]`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.random_hue(x, 0.2)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=...>

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_hue`. Unlike using the `seed` param with
  `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the same
  results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    max_delta: float. The maximum value for the random delta.
    seed: An operation-specific seed. It will be used in conjunction with the
      graph-level seed to determine the real seeds that will be used in this
      operation. Please see the documentation of set_random_seed for its
      interaction with the graph-level random seed.

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `max_delta` is invalid.
  r   max_delta must be <= 0.5.r   r  ry   )rS   r   r   
adjust_huer  s       r2   
random_huer  M
  sP    N _
0
11]
6
77

#
#B
ID
I%	E5	!!r4   zimage.stateless_random_huec                     |dkD  rt        d      |dk  rt        d      t        j                  g | ||      }t        | |      S )a  Adjust the hue of RGB images by a random factor deterministically.

  Equivalent to `adjust_hue()` but uses a `delta` randomly picked in the
  interval `[-max_delta, max_delta)`.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  `max_delta` must be in the interval `[0, 0.5]`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...      [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> seed = (1, 2)
  >>> tf.image.stateless_random_hue(x, 0.2, seed)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 1.6514902,  1.       ,  3.       ],
          [ 4.65149  ,  4.       ,  6.       ]],
         [[ 7.65149  ,  7.       ,  9.       ],
          [10.65149  , 10.       , 12.       ]]], dtype=float32)>

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    max_delta: float. The maximum value for the random delta.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`.

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `max_delta` is invalid.
  r   r  r   r  r  )rS   r   r   r  r  s       r2   stateless_random_huer  ~
  sT    P _
0
11]
6
77

7
7	z)$@%	E5	!!r4   zimage.adjust_huec                    t        j                  |d| g      5 }t        j                         r|dk  s|dkD  rt	        d      t        j
                  | d      } | j                  }|t        j                  t        j                  fv r| }nt        | t        j                        }t        j                  ||      }t        ||      cddd       S # 1 sw Y   yxY w)aX  Adjust hue of RGB images.

  This is a convenience method that converts an RGB image to float
  representation, converts it to HSV, adds an offset to the
  hue channel, converts back to RGB and then back to the original
  data type. If several adjustments are chained it is advisable to minimize
  the number of redundant conversions.

  `image` is an RGB image.  The image hue is adjusted by converting the
  image(s) to HSV and rotating the hue channel (H) by
  `delta`.  The image is then converted back to RGB.

  `delta` must be in the interval `[-1, 1]`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_hue(x, 0.2)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 2.3999996,  1.       ,  3.       ],
          [ 5.3999996,  4.       ,  6.       ]],
        [[ 8.4      ,  7.       ,  9.       ],
          [11.4      , 10.       , 12.       ]]], dtype=float32)>

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    delta: float.  How much to add to the hue channel.
    name: A name for this operation (optional).

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    InvalidArgumentError: image must have at least 3 dimensions.
    InvalidArgumentError: The size of the last dimension must be 3.
    ValueError: if `delta` is not in the interval of `[-1, 1]`.

  Usage Example:

  >>> image = [[[1, 2, 3], [4, 5, 6]],
  ...          [[7, 8, 9], [10, 11, 12]],
  ...          [[13, 14, 15], [16, 17, 18]]]
  >>> image = tf.constant(image)
  >>> tf.image.adjust_hue(image, 0.2)
  <tf.Tensor: shape=(3, 2, 3), dtype=int32, numpy=
  array([[[ 2,  1,  3],
        [ 5,  4,  6]],
       [[ 8,  7,  9],
        [11, 10, 12]],
       [[14, 13, 15],
        [17, 16, 18]]], dtype=int32)>
  r  rp   rq   z%delta must be in the interval [-1, 1]rC   r   N)r   r   r   executing_eagerlyrS   r   r   r   r  rb  r  r   r  )rC   r  r   r  r  rgb_altereds         r2   r  r  
  s    t ~~dL5'2 8d  "	uqy@AA!!%g6EJfnnfnn55i%eV^^<i**9e<K{J78 8 8s   B-CCzimage.random_jpeg_qualityc                     |dk  s|dk  s
|dkD  s|dkD  rt        d      ||k\  rt        d      t        j                  g |||t        j                        }t        | |      S )a  Randomly changes jpeg encoding quality for inducing jpeg noise.

  `min_jpeg_quality` must be in the interval `[0, 100]` and less than
  `max_jpeg_quality`.
  `max_jpeg_quality` must be in the interval `[0, 100]`.

  Usage Example:

  >>> x = tf.constant([[[1, 2, 3],
  ...                   [4, 5, 6]],
  ...                  [[7, 8, 9],
  ...                   [10, 11, 12]]], dtype=tf.uint8)
  >>> tf.image.random_jpeg_quality(x, 75, 95)
  <tf.Tensor: shape=(2, 2, 3), dtype=uint8, numpy=...>

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_jpeg_quality`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: 3D image. Size of the last dimension must be 1 or 3.
    min_jpeg_quality: Minimum jpeg encoding quality to use.
    max_jpeg_quality: Maximum jpeg encoding quality to use.
    seed: An operation-specific seed. It will be used in conjunction with the
      graph-level seed to determine the real seeds that will be used in this
      operation. Please see the documentation of set_random_seed for its
      interaction with the graph-level random seed.

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `min_jpeg_quality` or `max_jpeg_quality` is invalid.
  r   d   .jpeg encoding range must be between 0 and 100.8`min_jpeg_quality` must be less than `max_jpeg_quality`.)rz   r   )rS   r   r   r   r   adjust_jpeg_qualityrC   min_jpeg_qualitymax_jpeg_qualityrz   jpeg_qualitys        r2   random_jpeg_qualityr  
  sw    N .26F6L
E
FF))
O
PP**2+;+;0417	?,
 
UL	11r4   z#image.stateless_random_jpeg_qualityc                     |dk  s|dk  s
|dkD  s|dkD  rt        d      ||k\  rt        d      t        j                  g |||t        j                        }t        | |      S )a5  Deterministically radomize jpeg encoding quality for inducing jpeg noise.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  `min_jpeg_quality` must be in the interval `[0, 100]` and less than
  `max_jpeg_quality`.
  `max_jpeg_quality` must be in the interval `[0, 100]`.

  Usage Example:

  >>> x = tf.constant([[[1, 2, 3],
  ...                   [4, 5, 6]],
  ...                  [[7, 8, 9],
  ...                   [10, 11, 12]]], dtype=tf.uint8)
  >>> seed = (1, 2)
  >>> tf.image.stateless_random_jpeg_quality(x, 75, 95, seed)
  <tf.Tensor: shape=(2, 2, 3), dtype=uint8, numpy=
  array([[[ 0,  4,  5],
          [ 1,  5,  6]],
         [[ 5,  9, 10],
          [ 5,  9, 10]]], dtype=uint8)>

  Args:
    image: 3D image. Size of the last dimension must be 1 or 3.
    min_jpeg_quality: Minimum jpeg encoding quality to use.
    max_jpeg_quality: Maximum jpeg encoding quality to use.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `min_jpeg_quality` or `max_jpeg_quality` is invalid.
  r   r  r  r  )rA   r   r   rz   r   )rS   r   r   r   r   r  r  s        r2   stateless_random_jpeg_qualityr  3  sw    V .26F6L
E
FF))
O
PP%>>'0@tLL, 
UL	11r4   zimage.adjust_jpeg_qualityc                    t        j                  |d| g      5  t        j                  | d      } | j                  j	                         d   }| j
                  }t        | t        j                  d      } t        |      s%t        j                  |t        j                        }t        j                  | |      } t        j                  | ||      } t        | |d      cd	d	d	       S # 1 sw Y   y	xY w)
a  Adjust jpeg encoding quality of an image.

  This is a convenience method that converts an image to uint8 representation,
  encodes it to jpeg with `jpeg_quality`, decodes it, and then converts back
  to the original data type.

  `jpeg_quality` must be in the interval `[0, 100]`.

  Usage Examples:

  >>> x = [[[0.01, 0.02, 0.03],
  ...       [0.04, 0.05, 0.06]],
  ...      [[0.07, 0.08, 0.09],
  ...       [0.10, 0.11, 0.12]]]
  >>> x_jpeg = tf.image.adjust_jpeg_quality(x, 75)
  >>> x_jpeg.numpy()
  array([[[0.00392157, 0.01960784, 0.03137255],
          [0.02745098, 0.04313726, 0.05490196]],
         [[0.05882353, 0.07450981, 0.08627451],
          [0.08235294, 0.09803922, 0.10980393]]], dtype=float32)

  Note that floating point values are expected to have values in the range
  [0,1) and values outside this range are clipped.

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_jpeg_quality(x, 75)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[1., 1., 1.],
          [1., 1., 1.]],
         [[1., 1., 1.],
          [1., 1., 1.]]], dtype=float32)>

  Note that `jpeg_quality` 100 is still lossy compression.

  >>> x = tf.constant([[[1, 2, 3],
  ...                   [4, 5, 6]],
  ...                  [[7, 8, 9],
  ...                   [10, 11, 12]]], dtype=tf.uint8)
  >>> tf.image.adjust_jpeg_quality(x, 100)
  <tf.Tensor: shape(2, 2, 3), dtype=uint8, numpy=
  array([[[ 0,  1,  3],
          [ 3,  4,  6]],
         [[ 6,  7,  9],
          [ 9, 10, 12]]], dtype=uint8)>

  Args:
    image: 3D image. The size of the last dimension must be None, 1 or 3.
    jpeg_quality: Python int or Tensor of type int32. jpeg encoding quality.
    dct_method: An optional string. Specifies the DCT method to use for JPEG
      decompression. Currently available options are ["INTEGER_FAST",
      "INTEGER_ACCURATE"]. Defaults to "" which maps to "INTEGER_FAST",
      sacrificing image quality for speed.
    name: A name for this operation (optional).

  Returns:
    Adjusted image, same shape and DType as `image`.

  Raises:
    InvalidArgumentError: quality must be in [0,100]
    InvalidArgumentError: image must have 1 or 3 channels
  r  rC   r   rp   Tr  r  )channels
dct_methodN)r   r   r   rA   r>   r   r  r   uint8r.   r   r   encode_jpeg_variable_qualitydecode_jpeg)rC   r  r  r   r  r  s         r2   r  r  k  s    F ~~d1E7; A!!%g6E{{""$R(HJv||dCEl#**<v||Ll66ulKE%%ZE uj4@A A As   CC++C4zimage.random_saturationc                     ||k  rt        d      |dk  rt        d      t        j                  g |||      }t        | |      S )a  Adjust the saturation of RGB images by a random factor.

  Equivalent to `adjust_saturation()` but uses a `saturation_factor` randomly
  picked in the interval `[lower, upper)`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.random_saturation(x, 5, 10)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 0. ,  1.5,  3. ],
          [ 0. ,  3. ,  6. ]],
         [[ 0. ,  4.5,  9. ],
          [ 0. ,  6. , 12. ]]], dtype=float32)>

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_random_saturation`. Unlike using the `seed` param
  with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops guarantee the
  same results given the same seed independent of how many times the function is
  called, and independent of global seed settings (e.g. tf.random.set_seed).

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    lower: float.  Lower bound for the random saturation factor.
    upper: float.  Upper bound for the random saturation factor.
    seed: An operation-specific seed. It will be used in conjunction with the
      graph-level seed to determine the real seeds that will be used in this
      operation. Please see the documentation of set_random_seed for its
      interaction with the graph-level random seed.

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `upper <= lower` or if `lower < 0`.
  r  r   r  ry   )rS   r   r   adjust_saturationrC   r  r  rz   saturation_factors        r2   random_saturationr     sP    T e^
-
..
QY
2
33 //E5tL	5"3	44r4   z!image.stateless_random_saturationc                     ||k  rt        d      |dk  rt        d      t        j                  g |||      }t        | |      S )aW  Adjust the saturation of RGB images by a random factor deterministically.

  Equivalent to `adjust_saturation()` but uses a `saturation_factor` randomly
  picked in the interval `[lower, upper)`.

  Guarantees the same results given the same `seed` independent of how many
  times the function is called, and independent of global seed settings (e.g.
  `tf.random.set_seed`).

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...      [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> seed = (1, 2)
  >>> tf.image.stateless_random_saturation(x, 0.5, 1.0, seed)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 1.1559395,  2.0779698,  3.       ],
          [ 4.1559396,  5.07797  ,  6.       ]],
         [[ 7.1559396,  8.07797  ,  9.       ],
          [10.155939 , 11.07797  , 12.       ]]], dtype=float32)>

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    lower: float.  Lower bound for the random saturation factor.
    upper: float.  Upper bound for the random saturation factor.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    ValueError: if `upper <= lower` or if `lower < 0`.
  r  r   r  r  )rS   r   r   r  r  s        r2   stateless_random_saturationr"    sS    N e^
-
..
QY
2
33*CCuU7	5"3	44r4   zimage.adjust_saturationc                 f   t        j                  |d| g      5 }t        j                  | d      } | j                  }|t        j
                  t        j                  fv r| }nt        | t        j                        }t        j                  ||      }t        ||      cddd       S # 1 sw Y   yxY w)a@  Adjust saturation of RGB images.

  This is a convenience method that converts RGB images to float
  representation, converts them to HSV, adds an offset to the
  saturation channel, converts back to RGB and then back to the original
  data type. If several adjustments are chained it is advisable to minimize
  the number of redundant conversions.

  `image` is an RGB image or images.  The image saturation is adjusted by
  converting the images to HSV and multiplying the saturation (S) channel by
  `saturation_factor` and clipping. The images are then converted back to RGB.

  `saturation_factor` must be in the interval `[0, inf)`.

  Usage Example:

  >>> x = [[[1.0, 2.0, 3.0],
  ...       [4.0, 5.0, 6.0]],
  ...     [[7.0, 8.0, 9.0],
  ...       [10.0, 11.0, 12.0]]]
  >>> tf.image.adjust_saturation(x, 0.5)
  <tf.Tensor: shape=(2, 2, 3), dtype=float32, numpy=
  array([[[ 2. ,  2.5,  3. ],
          [ 5. ,  5.5,  6. ]],
         [[ 8. ,  8.5,  9. ],
          [11. , 11.5, 12. ]]], dtype=float32)>

  Args:
    image: RGB image or images. The size of the last dimension must be 3.
    saturation_factor: float. Factor to multiply the saturation by.
    name: A name for this operation (optional).

  Returns:
    Adjusted image(s), same shape and DType as `image`.

  Raises:
    InvalidArgumentError: input must have 3 channels
  r  rC   r   N)
r   r   r   r   r   r  rb  r  r   r  )rC   r  r   r  r  r  s         r2   r  r  %  s    R ~~d/%9 5T!!%g6EJfnnfnn55i%eV^^<i..y:KLHx45 5 5s   BB''B0z
io.is_jpegzimage.is_jpegc                     t        j                  |d      5  t        j                  | dd      }t	        j
                  |d|      cddd       S # 1 sw Y   yxY w)a;  Convenience function to check if the 'contents' encodes a JPEG image.

  Args:
    contents: 0-D `string`. The encoded image bytes.
    name: A name for the operation (optional)

  Returns:
     A scalar boolean tensor indicating if 'contents' may be a JPEG image.
     is_jpeg is susceptible to false positives.
  is_jpegr   rK   s   r   Nr   r   r   substrr   r   contentsr   r'  s      r2   r%  r%  \  sK     ~~dI& >xA.F>>&/=> > >   /AAc                     t        j                  |d      5  t        j                  | dd      }t	        j
                  |d|      cddd       S # 1 sw Y   yxY w)a8  Convenience function to check if the 'contents' encodes a PNG image.

  Args:
    contents: 0-D `string`. The encoded image bytes.
    name: A name for the operation (optional)

  Returns:
     A scalar boolean tensor indicating if 'contents' may be a PNG image.
     is_png is susceptible to false positives.
  is_pngr   rK   s   PNr   Nr&  r(  s      r2   _is_pngr-  p  sK     ~~dH% 8xA.F>>&)$78 8 8r*  zio.decode_and_crop_jpegzimage.decode_and_crop_jpegzio.decode_bmpzimage.decode_bmpzio.decode_gifzimage.decode_gifzio.decode_jpegzimage.decode_jpegzio.decode_pngzimage.decode_pngzio.encode_jpegzimage.encode_jpegzio.extract_jpeg_shapezimage.extract_jpeg_shapezio.encode_pngzimage.encode_pngc                 V    t        j                  t        j                  |       ||      S )a  PNG-encode an image.

  `image` is a rank-N Tensor of type uint8 or uint16 with shape `batch_dims +
  [height, width, channels]`, where `channels` is:

  *   1: for grayscale.
  *   2: for grayscale + alpha.
  *   3: for RGB.
  *   4: for RGBA.

  The ZLIB compression level, `compression`, can be -1 for the PNG-encoder
  default or a value from 0 to 9.  9 is the highest compression level,
  generating the smallest output, but is slower.

  Args:
    image: A `Tensor`. Must be one of the following types: `uint8`, `uint16`.
      Rank N >= 3 with shape `batch_dims + [height, width, channels]`.
    compression: An optional `int`. Defaults to `-1`. Compression level.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `string`.
  )r   
encode_pngr   r   )rC   compressionr   s      r2   r/  r/    s)    4 
	!	!	E"K
7 7r4   zio.decode_imagezimage.decode_imagec           	      v   t        j                  |d      5  |dn|}|t        j                  t        j                  t        j
                  fvr>|}t        j
                  }t        t        j                  | |||      |      cddd       S t        j                  | |||      cddd       S # 1 sw Y   yxY w)a  Function for `decode_bmp`, `decode_gif`, `decode_jpeg`, and `decode_png`.

  Detects whether an image is a BMP, GIF, JPEG, or PNG, and performs the
  appropriate operation to convert the input bytes `string` into a `Tensor`
  of type `dtype`.

  Note: `decode_gif` returns a 4-D array `[num_frames, height, width, 3]`, as
  opposed to `decode_bmp`, `decode_jpeg` and `decode_png`, which return 3-D
  arrays `[height, width, num_channels]`. Make sure to take this into account
  when constructing your graph if you are intermixing GIF files with BMP, JPEG,
  and/or PNG files. Alternately, set the `expand_animations` argument of this
  function to `False`, in which case the op will return 3-dimensional tensors
  and will truncate animated GIF files to the first frame.

  NOTE: If the first frame of an animated GIF does not occupy the entire
  canvas (maximum frame width x maximum frame height), then it fills the
  unoccupied areas (in the first frame) with zeros (black). For frames after the
  first frame that does not occupy the entire canvas, it uses the previous
  frame to fill the unoccupied areas.

  Args:
    contents: A `Tensor` of type `string`. 0-D. The encoded image bytes.
    channels: An optional `int`. Defaults to `0`. Number of color channels for
      the decoded image.
    dtype: The desired DType of the returned `Tensor`.
    name: A name for the operation (optional)
    expand_animations: An optional `bool`. Defaults to `True`. Controls the
      shape of the returned op's output. If `True`, the returned op will produce
      a 3-D tensor for PNG, JPEG, and BMP files; and a 4-D tensor for all GIFs,
      whether animated or not. If, `False`, the returned op will produce a 3-D
      tensor for all file types and will truncate animated GIFs to the first
      frame.

  Returns:
    `Tensor` with type `dtype` and a 3- or 4-dimensional shape, depending on
    the file type and the value of the `expand_animations` parameter.

  Raises:
    ValueError: On incorrect number of channels.
  decode_imageNr   )r)  r  expand_animationsr   )	r   r   r   rb  r  uint16r  r   r2  )r)  r  r   r   r3  
dest_dtypes         r2   r2  r2    s    d ~~dN+ $q(HV^^V\\6==AAjmme 

$
$ 1	 '(  ''-	  s   A,B/B//B8zimage.total_variationc                 v   t        j                  |d      5  | j                         j                  }|dk(  r=| ddddddf   | ddddddf   z
  }| ddddddf   | ddddddf   z
  }d}n[|dk(  rK| ddddddddf   | ddddddddf   z
  }| ddddddddf   | ddddddddf   z
  }g d}nt	        d      t        j                  t        j                  |      |	      t        j                  t        j                  |      |	      z   }ddd       |S # 1 sw Y   S xY w)
aq  Calculate and return the total variation for one or more images.

  The total variation is the sum of the absolute differences for neighboring
  pixel-values in the input images. This measures how much noise is in the
  images.

  This can be used as a loss-function during optimization so as to suppress
  noise in images. If you have a batch of images, then you should calculate
  the scalar loss-value as the sum:
  `loss = tf.reduce_sum(tf.image.total_variation(images))`

  This implements the anisotropic 2-D version of the formula described here:

  https://en.wikipedia.org/wiki/Total_variation_denoising

  Args:
    images: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
      of shape `[height, width, channels]`.
    name: A name for the operation (optional).

  Raises:
    ValueError: if images.shape is not a 3-D or 4-D vector.

  Returns:
    The total variation of `images`.

    If `images` was 4-D, return a 1-D float Tensor of shape `[batch]` with the
    total variation for each image in the batch.
    If `images` was 3-D, return a scalar float with the total variation for
    that image.
  total_variationrK   rq   Nrp   r   )rq   ro   rK   z+'images' must be either 3 or 4-dimensional.r  )r   r   r<   rh   rS   r   
reduce_sumabs)r   r   rh   
pixel_dif1
pixel_dif2sum_axistot_vars          r2   r7  r7  
  sW   F ~~d-.  F$$Ez
 !"a(#fSbS!QY&77j!QR(#fQQY&77j h	! !QRA+&3B31)==j!QA+&1crc1)==j hFGG
 	HLL48DHLL48D	E = FD 
.E FD 
.s   DD..D8z#image.sample_distorted_bounding_boxc	                    |rt        j                  |      \  }	}
n(t        j                         rt	        d| d      d\  }	}
t        j                  |d      5  t        j                  | ||	|
||||||
      cddd       S # 1 sw Y   yxY w)aR  Generate a single randomly distorted bounding box for an image.

  Bounding box annotations are often supplied in addition to ground-truth labels
  in image recognition or object localization tasks. A common technique for
  training such a system is to randomly distort an image while preserving
  its content, i.e. *data augmentation*. This Op outputs a randomly distorted
  localization of an object, i.e. bounding box, given an `image_size`,
  `bounding_boxes` and a series of constraints.

  The output of this Op is a single bounding box that may be used to crop the
  original image. The output is returned as 3 tensors: `begin`, `size` and
  `bboxes`. The first 2 tensors can be fed directly into `tf.slice` to crop the
  image. The latter may be supplied to `tf.image.draw_bounding_boxes` to
  visualize what the bounding box looks like.

  Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
  The bounding box coordinates are floats in `[0.0, 1.0]` relative to the width
  and the height of the underlying image.

  For example,

  ```python
      # Generate a single distorted bounding box.
      begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
          tf.shape(image),
          bounding_boxes=bounding_boxes,
          min_object_covered=0.1)

      # Draw the bounding box in an image summary.
      image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0),
                                                    bbox_for_draw)
      tf.compat.v1.summary.image('images_with_box', image_with_box)

      # Employ the bounding box to distort the image.
      distorted_image = tf.slice(image, begin, size)
  ```

  Note that if no bounding box information is available, setting
  `use_image_if_no_bounding_boxes = true` will assume there is a single implicit
  bounding box covering the whole image. If `use_image_if_no_bounding_boxes` is
  false and no bounding boxes are supplied, an error is raised.

  For producing deterministic results given a `seed` value, use
  `tf.image.stateless_sample_distorted_bounding_box`. Unlike using the `seed`
  param with `tf.image.random_*` ops, `tf.image.stateless_random_*` ops
  guarantee the same results given the same seed independent of how many times
  the function is called, and independent of global seed settings
  (e.g. tf.random.set_seed).

  Args:
    image_size: A `Tensor`. Must be one of the following types: `uint8`, `int8`,
      `int16`, `int32`, `int64`. 1-D, containing `[height, width, channels]`.
    bounding_boxes: A `Tensor` of type `float32`. 3-D with shape `[batch, N, 4]`
      describing the N bounding boxes associated with the image.
    seed: An optional `int`. Defaults to `0`. If `seed` is set to non-zero, the
      random number generator is seeded by the given `seed`.  Otherwise, it is
      seeded by a random seed.
    min_object_covered: A Tensor of type `float32`. Defaults to `0.1`. The
      cropped area of the image must contain at least this fraction of any
      bounding box supplied. The value of this parameter should be non-negative.
      In the case of 0, the cropped area does not need to overlap any of the
      bounding boxes supplied.
    aspect_ratio_range: An optional list of `floats`. Defaults to `[0.75,
      1.33]`. The cropped area of the image must have an aspect `ratio = width /
      height` within this range.
    area_range: An optional list of `floats`. Defaults to `[0.05, 1]`. The
      cropped area of the image must contain a fraction of the supplied image
      within this range.
    max_attempts: An optional `int`. Defaults to `100`. Number of attempts at
      generating a cropped region of the image of the specified constraints.
      After `max_attempts` failures, return the entire image.
    use_image_if_no_bounding_boxes: An optional `bool`. Defaults to `False`.
      Controls behavior if no bounding boxes supplied. If true, assume an
      implicit bounding box covering the whole input. If false, raise an error.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (begin, size, bboxes).

    begin: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[offset_height, offset_width, 0]`. Provide as input to
      `tf.slice`.
    size: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[target_height, target_width, -1]`. Provide as input to
      `tf.slice`.
    bboxes: A `Tensor` of type `float32`. 3-D with shape `[1, 1, 4]` containing
    the distorted bounding box.
    Provide as input to `tf.image.draw_bounding_boxes`.

  Raises:
    ValueError: If no seed is specified and op determinism is enabled.
  zztf.image.sample_distorted_bounding_box requires a non-zero seed to be passed in when determinism is enabled, but got seed=z;. Please pass in a non-zero seed, e.g. by passing "seed=1".)r   r   sample_distorted_bounding_boxrz   seed2min_object_coveredaspect_ratio_range
area_rangemax_attemptsuse_image_if_no_bounding_boxesr   N)	r	   get_seedr   is_op_determinism_enabledrS   r   r   r    sample_distorted_bounding_box_v2)
image_sizebounding_boxesrz   rB  rC  rD  rE  rF  r   seed1rA  s              r2   rI  rI  R  s    N 
''-LE5'')DDH6 JFGH H LE5
~~d;< 99--!'E
  s   BBz-image.stateless_sample_distorted_bounding_boxc	                     t        j                  |d      5  t        j                  | ||||||||	      cddd       S # 1 sw Y   yxY w)a   Generate a randomly distorted bounding box for an image deterministically.

  Bounding box annotations are often supplied in addition to ground-truth labels
  in image recognition or object localization tasks. A common technique for
  training such a system is to randomly distort an image while preserving
  its content, i.e. *data augmentation*. This Op, given the same `seed`,
  deterministically outputs a randomly distorted localization of an object, i.e.
  bounding box, given an `image_size`, `bounding_boxes` and a series of
  constraints.

  The output of this Op is a single bounding box that may be used to crop the
  original image. The output is returned as 3 tensors: `begin`, `size` and
  `bboxes`. The first 2 tensors can be fed directly into `tf.slice` to crop the
  image. The latter may be supplied to `tf.image.draw_bounding_boxes` to
  visualize what the bounding box looks like.

  Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
  The bounding box coordinates are floats in `[0.0, 1.0]` relative to the width
  and the height of the underlying image.

  The output of this Op is guaranteed to be the same given the same `seed` and
  is independent of how many times the function is called, and independent of
  global seed settings (e.g. `tf.random.set_seed`).

  Example usage:

  >>> image = np.array([[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]])
  >>> bbox = tf.constant(
  ...   [0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
  >>> seed = (1, 2)
  >>> # Generate a single distorted bounding box.
  >>> bbox_begin, bbox_size, bbox_draw = (
  ...   tf.image.stateless_sample_distorted_bounding_box(
  ...     tf.shape(image), bounding_boxes=bbox, seed=seed))
  >>> # Employ the bounding box to distort the image.
  >>> tf.slice(image, bbox_begin, bbox_size)
  <tf.Tensor: shape=(2, 2, 1), dtype=int64, numpy=
  array([[[1],
          [2]],
         [[4],
          [5]]])>
  >>> # Draw the bounding box in an image summary.
  >>> colors = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
  >>> tf.image.draw_bounding_boxes(
  ...   tf.expand_dims(tf.cast(image, tf.float32),0), bbox_draw, colors)
  <tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy=
  array([[[[1.],
           [1.],
           [3.]],
          [[1.],
           [1.],
           [6.]],
          [[7.],
           [8.],
           [9.]]]], dtype=float32)>

  Note that if no bounding box information is available, setting
  `use_image_if_no_bounding_boxes = true` will assume there is a single implicit
  bounding box covering the whole image. If `use_image_if_no_bounding_boxes` is
  false and no bounding boxes are supplied, an error is raised.

  Args:
    image_size: A `Tensor`. Must be one of the following types: `uint8`, `int8`,
      `int16`, `int32`, `int64`. 1-D, containing `[height, width, channels]`.
    bounding_boxes: A `Tensor` of type `float32`. 3-D with shape `[batch, N, 4]`
      describing the N bounding boxes associated with the image.
    seed: A shape [2] Tensor, the seed to the random number generator. Must have
      dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)
    min_object_covered: A Tensor of type `float32`. Defaults to `0.1`. The
      cropped area of the image must contain at least this fraction of any
      bounding box supplied. The value of this parameter should be non-negative.
      In the case of 0, the cropped area does not need to overlap any of the
      bounding boxes supplied.
    aspect_ratio_range: An optional list of `floats`. Defaults to `[0.75,
      1.33]`. The cropped area of the image must have an aspect `ratio = width /
      height` within this range.
    area_range: An optional list of `floats`. Defaults to `[0.05, 1]`. The
      cropped area of the image must contain a fraction of the supplied image
      within this range.
    max_attempts: An optional `int`. Defaults to `100`. Number of attempts at
      generating a cropped region of the image of the specified constraints.
      After `max_attempts` failures, return the entire image.
    use_image_if_no_bounding_boxes: An optional `bool`. Defaults to `False`.
      Controls behavior if no bounding boxes supplied. If true, assume an
      implicit bounding box covering the whole input. If false, raise an error.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (begin, size, bboxes).

    begin: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[offset_height, offset_width, 0]`. Provide as input to
      `tf.slice`.
    size: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[target_height, target_width, -1]`. Provide as input to
      `tf.slice`.
    bboxes: A `Tensor` of type `float32`. 3-D with shape `[1, 1, 4]` containing
    the distorted bounding box.
    Provide as input to `tf.image.draw_bounding_boxes`.
  'stateless_sample_distorted_bounding_box	rJ  rK  rz   rB  rC  rD  rE  rF  r   N)r   r   r   rN  rO  s	            r2   rN  rN    sR    ^ ~~dEF 
@@%--!'E	
 
 
s	   ?AzG`seed2` arg is deprecated.Use sample_distorted_bounding_box_v2 instead.)dateinstructionsc
                     |s'|s%t        j                         rt        d| d|       t        j                  |	d      5  t        j                  | |||||||||	
      cddd       S # 1 sw Y   yxY w)a:  Generate a single randomly distorted bounding box for an image.

  Bounding box annotations are often supplied in addition to ground-truth labels
  in image recognition or object localization tasks. A common technique for
  training such a system is to randomly distort an image while preserving
  its content, i.e. *data augmentation*. This Op outputs a randomly distorted
  localization of an object, i.e. bounding box, given an `image_size`,
  `bounding_boxes` and a series of constraints.

  The output of this Op is a single bounding box that may be used to crop the
  original image. The output is returned as 3 tensors: `begin`, `size` and
  `bboxes`. The first 2 tensors can be fed directly into `tf.slice` to crop the
  image. The latter may be supplied to `tf.image.draw_bounding_boxes` to
  visualize what the bounding box looks like.

  Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
  The
  bounding box coordinates are floats in `[0.0, 1.0]` relative to the width and
  height of the underlying image.

  For example,

  ```python
      # Generate a single distorted bounding box.
      begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
          tf.shape(image),
          bounding_boxes=bounding_boxes,
          min_object_covered=0.1)

      # Draw the bounding box in an image summary.
      image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0),
                                                    bbox_for_draw)
      tf.compat.v1.summary.image('images_with_box', image_with_box)

      # Employ the bounding box to distort the image.
      distorted_image = tf.slice(image, begin, size)
  ```

  Note that if no bounding box information is available, setting
  `use_image_if_no_bounding_boxes = True` will assume there is a single implicit
  bounding box covering the whole image. If `use_image_if_no_bounding_boxes` is
  false and no bounding boxes are supplied, an error is raised.

  Args:
    image_size: A `Tensor`. Must be one of the following types: `uint8`, `int8`,
      `int16`, `int32`, `int64`. 1-D, containing `[height, width, channels]`.
    bounding_boxes: A `Tensor` of type `float32`. 3-D with shape `[batch, N, 4]`
      describing the N bounding boxes associated with the image.
    seed: An optional `int`. Defaults to `0`. If either `seed` or `seed2` are
      set to non-zero, the random number generator is seeded by the given
      `seed`.  Otherwise, it is seeded by a random seed.
    seed2: An optional `int`. Defaults to `0`. A second seed to avoid seed
      collision.
    min_object_covered: A Tensor of type `float32`. Defaults to `0.1`. The
      cropped area of the image must contain at least this fraction of any
      bounding box supplied. The value of this parameter should be non-negative.
      In the case of 0, the cropped area does not need to overlap any of the
      bounding boxes supplied.
    aspect_ratio_range: An optional list of `floats`. Defaults to `[0.75,
      1.33]`. The cropped area of the image must have an aspect ratio = width /
      height within this range.
    area_range: An optional list of `floats`. Defaults to `[0.05, 1]`. The
      cropped area of the image must contain a fraction of the supplied image
      within this range.
    max_attempts: An optional `int`. Defaults to `100`. Number of attempts at
      generating a cropped region of the image of the specified constraints.
      After `max_attempts` failures, return the entire image.
    use_image_if_no_bounding_boxes: An optional `bool`. Defaults to `False`.
      Controls behavior if no bounding boxes supplied. If true, assume an
      implicit bounding box covering the whole input. If false, raise an error.
    name: A name for the operation (optional).

  Returns:
    A tuple of `Tensor` objects (begin, size, bboxes).

    begin: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[offset_height, offset_width, 0]`. Provide as input to
      `tf.slice`.
    size: A `Tensor`. Has the same type as `image_size`. 1-D, containing
    `[target_height, target_width, -1]`. Provide as input to
      `tf.slice`.
    bboxes: A `Tensor` of type `float32`. 3-D with shape `[1, 1, 4]` containing
    the distorted bounding box.
      Provide as input to `tf.image.draw_bounding_boxes`.

  Raises:
    ValueError: If no seed is specified and op determinism is enabled.
  ztf.compat.v1.image.sample_distorted_bounding_box requires "seed" or "seed2" to be non-zero when determinism is enabled. Please pass in a non-zero seed, e.g. by passing "seed=1". Got seed=z and seed2=r?  r@  N)r   rH  rS   r   r   r   rI  )
rJ  rK  rz   rA  rB  rC  rD  rE  rF  r   s
             r2   r?  r?  L  s    P 
e @ @ B
??Cf E	 
 ~~d;< 99--!'E
  s    A))A2zimage.non_max_suppressionr   -infc                     t        j                  |d      5  t        j                  |d      }t        j                  |d      }t        j                  | ||||      cddd       S # 1 sw Y   yxY w)a  Greedily selects a subset of bounding boxes in descending order of score.

  Prunes away boxes that have high intersection-over-union (IOU) overlap
  with previously selected boxes.  Bounding boxes are supplied as
  `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the coordinates of any
  diagonal pair of box corners and the coordinates can be provided as normalized
  (i.e., lying in the interval `[0, 1]`) or absolute.  Note that this algorithm
  is agnostic to where the origin is in the coordinate system.  Note that this
  algorithm is invariant to orthogonal transformations and translations
  of the coordinate system; thus translating or reflections of the coordinate
  system result in the same boxes being selected by the algorithm.
  The output of this operation is a set of integers indexing into the input
  collection of bounding boxes representing the selected boxes.  The bounding
  box coordinates corresponding to the selected indices can then be obtained
  using the `tf.gather` operation.  For example:
    ```python
    selected_indices = tf.image.non_max_suppression(
        boxes, scores, max_output_size, iou_threshold)
    selected_boxes = tf.gather(boxes, selected_indices)
    ```

  Args:
    boxes: A 2-D float `Tensor` of shape `[num_boxes, 4]`.
    scores: A 1-D float `Tensor` of shape `[num_boxes]` representing a single
      score corresponding to each box (each row of boxes).
    max_output_size: A scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non-max suppression.
    iou_threshold: A 0-D float tensor representing the threshold for deciding
      whether boxes overlap too much with respect to IOU.
    score_threshold: A 0-D float tensor representing the threshold for deciding
      when to remove boxes based on score.
    name: A name for the operation (optional).

  Returns:
    selected_indices: A 1-D integer `Tensor` of shape `[M]` representing the
      selected indices from the boxes tensor, where `M <= max_output_size`.
  non_max_suppressioniou_thresholdr   score_thresholdN)r   r   r   r   non_max_suppression_v3)boxesscoresmax_output_sizerV  rW  r   s         r2   rU  rU    so    Z ~~d12 P))-oNM++/1O//v0=P	P P Ps   AA((A1z%image.non_max_suppression_with_scoresr   c           
      ,   t        j                  |d      5  t        j                  |d      }t        j                  |d      }t        j                  |d      }t        j                  | |||||d      \  }}}	||fcddd       S # 1 sw Y   yxY w)	a  Greedily selects a subset of bounding boxes in descending order of score.

  Prunes away boxes that have high intersection-over-union (IOU) overlap
  with previously selected boxes.  Bounding boxes are supplied as
  `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the coordinates of any
  diagonal pair of box corners and the coordinates can be provided as normalized
  (i.e., lying in the interval `[0, 1]`) or absolute.  Note that this algorithm
  is agnostic to where the origin is in the coordinate system.  Note that this
  algorithm is invariant to orthogonal transformations and translations
  of the coordinate system; thus translating or reflections of the coordinate
  system result in the same boxes being selected by the algorithm.
  The output of this operation is a set of integers indexing into the input
  collection of bounding boxes representing the selected boxes.  The bounding
  box coordinates corresponding to the selected indices can then be obtained
  using the `tf.gather` operation.  For example:
    ```python
    selected_indices, selected_scores = tf.image.non_max_suppression_padded(
        boxes, scores, max_output_size, iou_threshold=1.0, score_threshold=0.1,
        soft_nms_sigma=0.5)
    selected_boxes = tf.gather(boxes, selected_indices)
    ```

  This function generalizes the `tf.image.non_max_suppression` op by also
  supporting a Soft-NMS (with Gaussian weighting) mode (c.f.
  Bodla et al, https://arxiv.org/abs/1704.04503) where boxes reduce the score
  of other overlapping boxes instead of directly causing them to be pruned.
  Consequently, in contrast to `tf.image.non_max_suppression`,
  `tf.image.non_max_suppression_with_scores` returns the new scores of each
  input box in the second output, `selected_scores`.

  To enable this Soft-NMS mode, set the `soft_nms_sigma` parameter to be
  larger than 0.  When `soft_nms_sigma` equals 0, the behavior of
  `tf.image.non_max_suppression_with_scores` is identical to that of
  `tf.image.non_max_suppression` (except for the extra output) both in function
  and in running time.

  Note that when `soft_nms_sigma` > 0, Soft-NMS is performed and `iou_threshold`
  is ignored. `iou_threshold` is only used for standard NMS.

  Args:
    boxes: A 2-D float `Tensor` of shape `[num_boxes, 4]`.
    scores: A 1-D float `Tensor` of shape `[num_boxes]` representing a single
      score corresponding to each box (each row of boxes).
    max_output_size: A scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non-max suppression.
    iou_threshold: A 0-D float tensor representing the threshold for deciding
      whether boxes overlap too much with respect to IOU.
    score_threshold: A 0-D float tensor representing the threshold for deciding
      when to remove boxes based on score.
    soft_nms_sigma: A 0-D float tensor representing the sigma parameter for Soft
      NMS; see Bodla et al (c.f. https://arxiv.org/abs/1704.04503).  When
      `soft_nms_sigma=0.0` (which is default), we fall back to standard (hard)
      NMS.
    name: A name for the operation (optional).

  Returns:
    selected_indices: A 1-D integer `Tensor` of shape `[M]` representing the
      selected indices from the boxes tensor, where `M <= max_output_size`.
    selected_scores: A 1-D float tensor of shape `[M]` representing the
      corresponding scores for each selected box, where `M <= max_output_size`.
      Scores only differ from corresponding input scores when using Soft NMS
      (i.e. when `soft_nms_sigma>0`)
  non_max_suppression_with_scoresrV  r   rW  soft_nms_sigmaF)pad_to_max_output_sizeN)r   r   r   r   non_max_suppression_v5)
rY  rZ  r[  rV  rW  r^  r   selected_indicesselected_scoresr:  s
             r2   r]  r]    s    P ~~d=> -))-oNM++/1O**-/N 
.
.						 %' _,- - -s   A)B

Bz"image.non_max_suppression_overlapsc                     t        j                  |d      5  t        j                  |d      }t        j                  | ||||      cddd       S # 1 sw Y   yxY w)a7  Greedily selects a subset of bounding boxes in descending order of score.

  Prunes away boxes that have high overlap with previously selected boxes.
  N-by-n overlap values are supplied as square matrix.
  The output of this operation is a set of integers indexing into the input
  collection of bounding boxes representing the selected boxes.  The bounding
  box coordinates corresponding to the selected indices can then be obtained
  using the `tf.gather` operation.  For example:
    ```python
    selected_indices = tf.image.non_max_suppression_overlaps(
        overlaps, scores, max_output_size, iou_threshold)
    selected_boxes = tf.gather(boxes, selected_indices)
    ```

  Args:
    overlaps: A 2-D float `Tensor` of shape `[num_boxes, num_boxes]`
      representing the n-by-n box overlap values.
    scores: A 1-D float `Tensor` of shape `[num_boxes]` representing a single
      score corresponding to each box (each row of boxes).
    max_output_size: A scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non-max suppression.
    overlap_threshold: A 0-D float tensor representing the threshold for
      deciding whether boxes overlap too much with respect to the provided
      overlap values.
    score_threshold: A 0-D float tensor representing the threshold for deciding
      when to remove boxes based on score.
    name: A name for the operation (optional).

  Returns:
    selected_indices: A 1-D integer `Tensor` of shape `[M]` representing the
      selected indices from the overlaps tensor, where `M <= max_output_size`.
  non_max_suppression_overlapsoverlap_thresholdr   N)r   r   r   r   !non_max_suppression_with_overlaps)overlapsrZ  r[  re  rW  r   s         r2   rf  rf  W  sa    P ~~d:; O-- 35 ::&/+<oO	O O Os   0AA)A`"?g6?gOn?)r  gQ#VѿgR4A)r  gԿg(q?zimage.rgb_to_yiqc                     t        j                  | d      } t        j                  t        | j                  d      }| j	                         j
                  }t        j                  | ||dz
  gdgg      S )aW  Converts one or more images from RGB to YIQ.

  Outputs a tensor of the same shape as the `images` tensor, containing the YIQ
  value of the pixels.
  The output is only well defined if the value in images are in [0,1].

  Usage Example:

  >>> x = tf.constant([[[1.0, 2.0, 3.0]]])
  >>> tf.image.rgb_to_yiq(x)
  <tf.Tensor: shape=(1, 1, 3), dtype=float32,
  numpy=array([[[ 1.815     , -0.91724455,  0.09962624]]], dtype=float32)>

  Args:
    images: 2-D or higher rank. Image data to convert. Last dimension must be
      size 3.

  Returns:
    images: tensor with the same shape as `images`.
  r   r   kernelr   rq   r   axes)r   r   _rgb_to_yiq_kernelr   r<   rh   r   r  r   rj  rh   s      r2   
rgb_to_yiqro    sg    .   h7&  8=&




"
"%			FF519+s1C	DDr4   )rq   rq   rq   )g 7p?ghP<hѿg85)gv?g=g>V0D?zimage.yiq_to_rgbc                     t        j                  | d      } t        j                  t        | j                  d      }| j	                         j
                  }t        j                  | ||dz
  gdgg      S )a  Converts one or more images from YIQ to RGB.

  Outputs a tensor of the same shape as the `images` tensor, containing the RGB
  value of the pixels.
  The output is only well defined if the Y value in images are in [0,1],
  I value are in [-0.5957,0.5957] and Q value are in [-0.5226,0.5226].

  Args:
    images: 2-D or higher rank. Image data to convert. Last dimension must be
      size 3.

  Returns:
    images: tensor with the same shape as `images`.
  r   r   rj  r   rq   r   rk  )r   r   _yiq_to_rgb_kernelr   r<   rh   r   r  rn  s      r2   
yiq_to_rgbrr    sg    "   h7&  8=&




"
"%			FF519+s1C	DDr4   )rh  gxÅ¿g>?)r  gx|ҿgb!z)r  g}?gEzimage.rgb_to_yuvc                     t        j                  | d      } t        j                  t        | j                  d      }| j	                         j
                  }t        j                  | ||dz
  gdgg      S )a=  Converts one or more images from RGB to YUV.

  Outputs a tensor of the same shape as the `images` tensor, containing the YUV
  value of the pixels.
  The output is only well defined if the value in images are in [0, 1].
  There are two ways of representing an image: [0, 255] pixel values range or
  [0, 1] (as float) pixel values range. Users need to convert the input image
  into a float [0, 1] range.

  Args:
    images: 2-D or higher rank. Image data to convert. Last dimension must be
      size 3.

  Returns:
    images: tensor with the same shape as `images`.
  r   r   rj  r   rq   r   rk  )r   r   _rgb_to_yuv_kernelr   r<   rh   r   r  rn  s      r2   
rgb_to_yuvru    sg    &   h7&  8=&




"
"%			FF519+s1C	DDr4   )r   g;jAٿgA @)g<?g "Ftr   zimage.yuv_to_rgbc                     t        j                  | d      } t        j                  t        | j                  d      }| j	                         j
                  }t        j                  | ||dz
  gdgg      S )an  Converts one or more images from YUV to RGB.

  Outputs a tensor of the same shape as the `images` tensor, containing the RGB
  value of the pixels.
  The output is only well defined if the Y value in images are in [0,1],
  U and V value are in [-0.5,0.5].

  As per the above description, you need to scale your YUV images if their
  pixel values are not in the required range. Below given example illustrates
  preprocessing of each channel of images before feeding them to `yuv_to_rgb`.

  ```python
  yuv_images = tf.random.uniform(shape=[100, 64, 64, 3], maxval=255)
  last_dimension_axis = len(yuv_images.shape) - 1
  yuv_tensor_images = tf.truediv(
      tf.subtract(
          yuv_images,
          tf.reduce_min(yuv_images)
      ),
      tf.subtract(
          tf.reduce_max(yuv_images),
          tf.reduce_min(yuv_images)
       )
  )
  y, u, v = tf.split(yuv_tensor_images, 3, axis=last_dimension_axis)
  target_uv_min, target_uv_max = -0.5, 0.5
  u = u * (target_uv_max - target_uv_min) + target_uv_min
  v = v * (target_uv_max - target_uv_min) + target_uv_min
  preprocessed_yuv_images = tf.concat([y, u, v], axis=last_dimension_axis)
  rgb_tensor_images = tf.image.yuv_to_rgb(preprocessed_yuv_images)
  ```

  Args:
    images: 2-D or higher rank. Image data to convert. Last dimension must be
      size 3.

  Returns:
    images: tensor with the same shape as `images`.
  r   r   rj  r   rq   r   rk  )r   r   _yuv_to_rgb_kernelr   r<   rh   r   r  rn  s      r2   
yuv_to_rgbrx    sh    T   h7&  8=&




"
"%			FF519+s1C	DDr4   c                 F   | j                         j                  d      }|j                         j                  d      }|dd j                  |dd        |j                  z|j                  nt	        t        |j                  dd       t        |j                  dd             D ]3  \  }}|dk(  r|dk(  r|j                  |      r$t        d|d|       t        j                  | |g      \  }}g }|j                  t        j                  t        j                  t        j                   |      d      ||gd             |j                  t        j                  t        j"                  t        j$                  |dd |dd             ||gd             |||fS )	a  Checks if two image tensors are compatible for applying SSIM or PSNR.

  This function checks if two sets of images have ranks at least 3, and if the
  last three dimensions match.

  Args:
    img1: Tensor containing the first image batch.
    img2: Tensor containing the second image batch.

  Returns:
    A tuple containing: the first tensor shape, the second tensor shape, and a
    list of control_flow_ops.Assert() ops implementing the checks.

  Raises:
    ValueError: When static shape check fails.
  rK   re   Nrq   zTwo images are not compatible: z and 
   	summarize)r<   ri   assert_is_compatible_withrh   rB   reversedr   ra  rS   r   shape_nappendr   r/   r   greater_equalr^  
reduce_allr   )img1img2shape1shape2dim1dim2checkss          r2   _verify_compatible_image_shapesr    s   " >>..q1&>>..q1&+''rs4\\&,,":Sb!"HV[["-=$>@ +
dai419(?(?(E &* + 	++ $$dD\2.&& &--  

 
 !7
;ff=M 	--  


hnnVBC[&+F
G6

 
	r4   z
image.psnrc           
      N   t        j                  |d| |g      5  t        j                  || j                        }t        |t        j                        }t        | t        j                        } t        |t        j                        }t        j                  t        j                  | |      g d      }t        j                  dt        j                  |      z  t        j                  d      z  t        j                  dt        j                  d      z        t        j                  |      z  d      }t        | |      \  }}}t        j                  |      5  t        j                   |      cddd       cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)	as  Returns the Peak Signal-to-Noise Ratio between a and b.

  This is intended to be used on signals (or images). Produces a PSNR value for
  each image in batch.

  The last three dimensions of input are expected to be [height, width, depth].

  Example:

  ```python
      # Read images from file.
      im1 = tf.decode_png('path/to/im1.png')
      im2 = tf.decode_png('path/to/im2.png')
      # Compute PSNR over tf.uint8 Tensors.
      psnr1 = tf.image.psnr(im1, im2, max_val=255)

      # Compute PSNR over tf.float32 Tensors.
      im1 = tf.image.convert_image_dtype(im1, tf.float32)
      im2 = tf.image.convert_image_dtype(im2, tf.float32)
      psnr2 = tf.image.psnr(im1, im2, max_val=1.0)
      # psnr1 and psnr2 both have type tf.float32 and are almost equal.
  ```

  Args:
    a: First set of images.
    b: Second set of images.
    max_val: The dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
    name: Namespace to embed the computation in.

  Returns:
    The scalar PSNR between a and b. The returned tensor has type `tf.float32`
    and shape [batch_size, 1].
  PSNR)re   r  rp      g      $@rz  psnrr   N)r   r   r   r   r   r  r   rb  r  squared_differencesubtractlognpr  control_dependenciesr   r  )abmax_valr   msepsnr_valr:  r  s           r2   r  r  H  sD   J ~~dFQF+ * mmGQWW-G!'6>>:GAv~~.AAv~~.A


x::1a@,
OC  
X\\'""X\\$%77


2r
?#hll3&77H
 31a8LAq&		!	!&	) *)* ** ** * ** * *s$   EFF2	FF	
FF${Gz?Q?c                 p   ||z  dz  }||z  dz  } ||       }	 ||      }
|	|
z  dz  }t        j                  |	      t        j                  |
      z   }||z   ||z   z  } || |z        dz  } |t        j                  |       t        j                  |      z         }||z  }||z
  |z   ||z
  |z   z  }||fS )a  Helper function for computing SSIM.

  SSIM estimates covariances with weighted sums.  The default parameters
  use a biased estimate of the covariance:
  Suppose `reducer` is a weighted sum, then the mean estimators are
    \mu_x = \sum_i w_i x_i,
    \mu_y = \sum_i w_i y_i,
  where w_i's are the weighted-sum weights, and covariance estimator is
    cov_{xy} = \sum_i w_i (x_i - \mu_x) (y_i - \mu_y)
  with assumption \sum_i w_i = 1. This covariance estimator is biased, since
    E[cov_{xy}] = (1 - \sum_i w_i ^ 2) Cov(X, Y).
  For SSIM measure with unbiased covariance estimators, pass as `compensation`
  argument (1 - \sum_i w_i ^ 2).

  Args:
    x: First set of images.
    y: Second set of images.
    reducer: Function that computes 'local' averages from the set of images. For
      non-convolutional version, this is usually tf.reduce_mean(x, [1, 2]), and
      for convolutional version, this is usually tf.nn.avg_pool2d or
      tf.nn.conv2d with weighted-sum kernel.
    max_val: The dynamic range (i.e., the difference between the maximum
      possible allowed value and the minimum allowed value).
    compensation: Compensation factor. See above.
    k1: Default value 0.01
    k2: Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so
      it would be better if we took the values in the range of 0 < K2 < 0.4).

  Returns:
    A pair containing the luminance measure, and the contrast-structure measure.
  ro          @)r   square)r:   r1  reducerr  compensationk1k2c1c2mean0mean1num0den0	luminancenum1den1css                    r2   _ssim_helperr    s    B 	Wq"
Wq" !*%
!*%		$		(//%"8	8$byTBY') 
Q#	$	#hooa&88	9$"tbTD[2-." 
Br4   c                    t        j                  | t        j                        } t        j                  |      }t	        j
                  t	        j                  |       |j                        }|t	        j
                  | dz
  |j                        dz  z  }t	        j                  |      }|dt	        j                  |      z  z  }t        j                  |ddg      t        j                  |ddg      z   }t        j                  |ddg      }t        j                  |      }t        j                  || | ddg      S )z:Function to mimic the 'fspecial' gaussian MATLAB function.rq   r  g      rp   rA   )r   r   r   r   r   r   ranger   r  r   r   r   softmax)r^  sigmacoordsgs       r2   _fspecial_gaussr    s    			tV\\	2$



&%==-u{{;&HMM$(EKK0366&oof!thooe$$$!!R)I,=,=aAw,OO!!R)!nnQ!			1T4A$6	77r4            ?c           
         t        j                  |t        j                        }t        j                  || j                        }t        j                  | |g      \  }}	t        j                  t        j                  t        j                  |dd |            ||gd      t        j                  t        j                  t        j                  |	dd |            |	|gd      g}
t        j                  |
      5  t        j                  |       } ddd       t        ||      t        j                   dd|d   dg      d	}fd
}t#        | ||||||      \  }}|r	||z  }||fS t        j                  ddgt        j                        }t        j$                  ||z  |      }t        j$                  ||      }||fS # 1 sw Y   xY w)ay  Computes SSIM index between img1 and img2 per color channel.

  This function matches the standard SSIM implementation from:
  Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image
  quality assessment: from error visibility to structural similarity. IEEE
  transactions on image processing.

  Details:
    - 11x11 Gaussian filter of width 1.5 is used.
    - k1 = 0.01, k2 = 0.03 as in the original paper.

  Args:
    img1: First image batch.
    img2: Second image batch.
    max_val: The dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
    filter_size: Default value 11 (size of gaussian filter).
    filter_sigma: Default value 1.5 (width of gaussian filter).
    k1: Default value 0.01
    k2: Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so
      it would be better if we took the values in the range of 0 < K2 < 0.4).
    return_index_map: If True returns local SSIM map instead of the global mean.

  Returns:
    A pair of tensors containing and channel-wise SSIM and contrast-structure
    values. The shape is [..., channels].
  r  re   rp      r{  Nrq   )r  r   c           	      P   t        j                  |       }t        j                  | t        j                  dg|dd  gd            } t	        j
                  | g dd      }t        j                  |t        j                  |d d t        j                  |      dd  gd            S )	Nrp   re   r   r  rq   rq   rq   rq   VALID)stridespaddingrq   )r   rA   r   r  r   depthwise_conv2d)r:   rA   r1  rj  s      r2   r  z"_ssim_per_channel.<locals>.reducer  s    OOAE!9#3#3bT5:4F#JKA  	6<	:A	9U3BZ);AB)?@!DF Fr4   r  )r   constantr   r   r   r   r  r   r/   r   r  r  r   r  r  r  r  r  r  )r  r  r  filter_sizefilter_sigmar  r  return_index_mapr  r  r  r  r  r  r  ssim_valrl  rj  s                    @r2   _ssim_per_channelr    s   F $$[E+%%l$**E,$$dD\2.&&  


$$VBr]K@B;
	
   


$$VBr]K@B;
	& ' $d#D$ ;5&>>&Q6":q,AB& ,
F tT7G\2!#-)R 2~H
 
2 R=D##IND9H			b$	'B	2A$ $s   ?GGz
image.ssimc                    t        j                  dd| |g      5  t        j                  | d      } t        j                  |d      }t        | |      \  }}}	t        j                  |	      5  t        j                  |       } ddd       t        j                  || j                        }t        |t        j                        }t        | t        j                        } t        |t        j                        }t        | |||||||      \  }
}t        j                  |
dg      cddd       S # 1 sw Y   xY w# 1 sw Y   yxY w)a>  Computes SSIM index between img1 and img2.

  This function is based on the standard SSIM implementation from:
  Wang, Z., Bovik, A. C., Sheikh, H. R., & Simoncelli, E. P. (2004). Image
  quality assessment: from error visibility to structural similarity. IEEE
  transactions on image processing.

  Note: The true SSIM is only defined on grayscale.  This function does not
  perform any colorspace transform.  (If the input is already YUV, then it will
  compute YUV SSIM average.)

  Details:
    - 11x11 Gaussian filter of width 1.5 is used.
    - k1 = 0.01, k2 = 0.03 as in the original paper.

  The image sizes must be at least 11x11 because of the filter size.

  Example:

  ```python
      # Read images (of size 255 x 255) from file.
      im1 = tf.image.decode_image(tf.io.read_file('path/to/im1.png'))
      im2 = tf.image.decode_image(tf.io.read_file('path/to/im2.png'))
      tf.shape(im1)  # `img1.png` has 3 channels; shape is `(255, 255, 3)`
      tf.shape(im2)  # `img2.png` has 3 channels; shape is `(255, 255, 3)`
      # Add an outer batch for each image.
      im1 = tf.expand_dims(im1, axis=0)
      im2 = tf.expand_dims(im2, axis=0)
      # Compute SSIM over tf.uint8 Tensors.
      ssim1 = tf.image.ssim(im1, im2, max_val=255, filter_size=11,
                            filter_sigma=1.5, k1=0.01, k2=0.03)

      # Compute SSIM over tf.float32 Tensors.
      im1 = tf.image.convert_image_dtype(im1, tf.float32)
      im2 = tf.image.convert_image_dtype(im2, tf.float32)
      ssim2 = tf.image.ssim(im1, im2, max_val=1.0, filter_size=11,
                            filter_sigma=1.5, k1=0.01, k2=0.03)
      # ssim1 and ssim2 both have type tf.float32 and are almost equal.
  ```

  Args:
    img1: First image batch. 4-D Tensor of shape `[batch, height, width,
      channels]` with only Positive Pixel Values.
    img2: Second image batch. 4-D Tensor of shape `[batch, height, width,
      channels]` with only Positive Pixel Values.
    max_val: The dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
    filter_size: Default value 11 (size of gaussian filter).
    filter_sigma: Default value 1.5 (width of gaussian filter).
    k1: Default value 0.01
    k2: Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so
      it would be better if we took the values in the range of 0 < K2 < 0.4).
    return_index_map: If True returns local SSIM map instead of the global mean.

  Returns:
    A tensor containing an SSIM value for each image in batch or a tensor
    containing an SSIM value for each pixel for each image in batch if
    return_index_map is True. Returned SSIM values are in range (-1, 1], when
    pixel values are non-negative. Returns a tensor with shape:
    broadcast(img1.shape[:-3], img2.shape[:-3]) or broadcast(img1.shape[:-1],
    img2.shape[:-1]).
  NSSIMr  r   r  rp   )r   r   r   r  r  r   r  r   r   r   r  r   rb  r  r  )r  r  r  r  r  r  r  r  r:  r  ssim_per_channels              r2   ssimr  "  s   P ~~dFT4L1 8  F3D  F3D24>LAq&		!	!&	) &%d&
 mmGTZZ0G!'6>>:GtV^^4DtV^^4D+D$,8"b,<>a  02$7'8 8& &8 8s%   AD;.D/B!D;/D8	4D;;E)gǺ?g48EG?ga4?g??g9EGr?zimage.ssim_multiscalec                     t        j                  dd| |g      5  t        j                  | d      } t        j                  |d      }t        | |      \  }}	}
t        j                  |
      5  t        j                  |       } ddd       t        j                  || j                        }t        |t        j                        }t        | t        j                        } t        |t        j                        }| |g}||	g}|D cg c]  }|dd 	 }}|D cg c]  }|dd 	 }}g d}t        j                  |dd t        j                  	      }d
 g }t!        t#        |            D ]  }t        j                  dd|z  |      5  |dkD  r1t%        ||      D cg c]2  \  }}t        j&                  |t        j(                  dg|gd            4 c}}|d   |z   t        j*                  t        j,                   d            }t/        j0                  | fdfd      }|D cg c]  }t3        j4                  |||d       }}t        j6                  |      D cg c]  }|dd 	 }}t%        |||      D cg c]2  \  }}}t        j&                  |t        j(                  ||gd            4 }}}}t9        ||||||d\  }}|j;                  t3        j<                  |             ddd        |j?                          tA        jB                  |t3        j<                        gz   d      }t        jD                  t        jF                  ||      dg      }t        jH                  |dg      cddd       S # 1 sw Y   	xY wc c}w c c}w c c}}w c c}w c c}w c c}}}w # 1 sw Y   XxY w# 1 sw Y   yxY w)a  Computes the MS-SSIM between img1 and img2.

  This function assumes that `img1` and `img2` are image batches, i.e. the last
  three dimensions are [height, width, channels].

  Note: The true SSIM is only defined on grayscale.  This function does not
  perform any colorspace transform.  (If the input is already YUV, then it will
  compute YUV SSIM average.)

  Original paper: Wang, Zhou, Eero P. Simoncelli, and Alan C. Bovik. "Multiscale
  structural similarity for image quality assessment." Signals, Systems and
  Computers, 2004.

  Args:
    img1: First image batch with only Positive Pixel Values.
    img2: Second image batch with only Positive Pixel Values. Must have the
    same rank as img1.
    max_val: The dynamic range of the images (i.e., the difference between the
      maximum the and minimum allowed values).
    power_factors: Iterable of weights for each of the scales. The number of
      scales used is the length of the list. Index 0 is the unscaled
      resolution's weight and each increasing scale corresponds to the image
      being downsampled by 2.  Defaults to (0.0448, 0.2856, 0.3001, 0.2363,
      0.1333), which are the values obtained in the original paper.
    filter_size: Default value 11 (size of gaussian filter).
    filter_sigma: Default value 1.5 (width of gaussian filter).
    k1: Default value 0.01
    k2: Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so
      it would be better if we took the values in the range of 0 < K2 < 0.4).

  Returns:
    A tensor containing an MS-SSIM value for each image in batch.  The values
    are in range [0, 1].  Returns a tensor with shape:
    broadcast(img1.shape[:-3], img2.shape[:-3]).
  NzMS-SSIMr  r   r  re   )rq   ro   ro   rq   rq   r  c                     t        j                  |d      }t        j                  |ddgddgg      }| D cg c]  }t        j                  ||d       c}S c c}w )Nrp   rq   r   	SYMMETRICmode)r   r  r  )r   	remainderr  r:   s       r2   do_padzssim_multiscale.<locals>.do_pad  sR    %%i4ggAA'78gCIJaimmAw[9JJJs   AzScale%dr   rp   c                              S r   rN   )r  	flat_imgsr  s   r2   r   z!ssim_multiscale.<locals>.<lambda>  s    y)(D r4   c                       S r   rN   )r  s   r2   r   z!ssim_multiscale.<locals>.<lambda>  s    	 r4   r  )ksizer  r  )r  r  r  r  r  r  )%r   r   r   r  r  r   r  r   r   r   r  r   rb  r   r  r   r  lenrB   r   r  
reduce_any	not_equalr   r   r   avg_poolr  r  r  relupopr   r   r  powr  )!r  r  r  power_factorsr  r  r  r  r  r  r  imgsshapesrG   headstailsdivisordivisor_tensormcsr   r:   tneed_paddingr!  
downscaledhr  r  mcs_and_ssimms_ssimr  r  r  s!                                 @@@r2   ssim_multiscaler    s   Z ~~dId|4 Q/  F3D  F3D<T4HFFF		!	!&	) &%d&
 mmGTZZ0G!'6>>:GtV^^4DtV^^4D$<DfF
 $$QsV$E$#$QrsV$E$G ))'!"+V\\JNK
 C3}%& $$>>$	At4 #$q5 dE*!Q 9#3#3bT1Iq#AB)
 Ah/)!,,X-?-?	1-MN,<< D 13&   oo7GWF* 
 #,"3"3J"?@Q1QR5@%@ !UE: !Q 9#3#3QFA#>?$   1#% " 	

6;;r?#G#$ #$$$P GGI"((v{{+,--B8L ""\=1B49G ".cQ/ Q/& &  %$
 A-#$ #$GQ/ Q/s   AO	1NBO		NO	N 'A%O	N<"7N%AN<1 N+
N<*N0
6N<	7N5 :N<:B
O	N	O	%N<<OO		Ozimage.image_gradientsc                    | j                         j                  dk7  r(t        dj                  | j                                     t	        j
                  |       }t        j                  |      \  }}}}| ddddddddf   | ddddddddf   z
  }| ddddddddf   | ddddddddf   z
  }t        j                  |d||g      }t	        j                  |t	        j                  || j                        gd      }t	        j                  ||      }t        j                  ||d|g      }t	        j                  |t	        j                  || j                        gd      }t	        j                  ||      }||fS )a  Returns image gradients (dy, dx) for each color channel.

  Both output tensors have the same shape as the input: [batch_size, h, w,
  d]. The gradient values are organized so that [I(x+1, y) - I(x, y)] is in
  location (x, y). That means that dy will always have zeros in the last row,
  and dx will always have zeros in the last column.

  Usage Example:
    ```python
    BATCH_SIZE = 1
    IMAGE_HEIGHT = 5
    IMAGE_WIDTH = 5
    CHANNELS = 1
    image = tf.reshape(tf.range(IMAGE_HEIGHT * IMAGE_WIDTH * CHANNELS,
      delta=1, dtype=tf.float32),
      shape=(BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS))
    dy, dx = tf.image.image_gradients(image)
    print(image[0, :,:,0])
    tf.Tensor(
      [[ 0.  1.  2.  3.  4.]
      [ 5.  6.  7.  8.  9.]
      [10. 11. 12. 13. 14.]
      [15. 16. 17. 18. 19.]
      [20. 21. 22. 23. 24.]], shape=(5, 5), dtype=float32)
    print(dy[0, :,:,0])
    tf.Tensor(
      [[5. 5. 5. 5. 5.]
      [5. 5. 5. 5. 5.]
      [5. 5. 5. 5. 5.]
      [5. 5. 5. 5. 5.]
      [0. 0. 0. 0. 0.]], shape=(5, 5), dtype=float32)
    print(dx[0, :,:,0])
    tf.Tensor(
      [[1. 1. 1. 1. 0.]
      [1. 1. 1. 1. 0.]
      [1. 1. 1. 1. 0.]
      [1. 1. 1. 1. 0.]
      [1. 1. 1. 1. 0.]], shape=(5, 5), dtype=float32)
    ```

  Args:
    image: Tensor with shape [batch_size, h, w, d].

  Returns:
    Pair of tensors (dy, dx) holding the vertical and horizontal image
    gradients (1-step finite difference).

  Raises:
    ValueError: If `image` is not a 4D tensor.
  r   zBimage_gradients expects a 4D tensor [batch_size, h, w, d], not {}.Nrq   rp   ro   )r<   rh   rS   r   r   rA   r   r@   r   r  r  r   r   )	rC   rX   r   r  r  r  dydxrA   s	            r2   image_gradientsr    sg   j __!
 66<fU__=N6OQ Q&+%4%<%<[%I"*feUQAq[E!SbS!Q,//"Q12q[E!QQ,//" 

Qu =
>%Y__UEKK@A1E"[)"


VQ >
?%Y__UEKK@A1E"[)"	R-r4   zimage.sobel_edgesc                    | j                         }t        j                  |       }g dg dg dgg dg dg dgg}t        |      }t	        j
                  t        j                  |      d      }t	        j                  |d      }t        j                  || j                        }t        j                  |d	d	|d
   d	gd      }ddgd	d	gd	d	gddgg}t        j                  | |d      }g d}t        j                  |||d      }	t        j                   ||ggd      }
t        j"                  |	|
      }	|	j%                  |j'                  |g             |	S )aa  Returns a tensor holding Sobel edge maps.

  Example usage:

  For general usage, `image` would be loaded from a file as below:

  ```python
  image_bytes = tf.io.read_file(path_to_image_file)
  image = tf.image.decode_image(image_bytes)
  image = tf.cast(image, tf.float32)
  image = tf.expand_dims(image, 0)
  ```
  But for demo purposes, we are using randomly generated values for `image`:

  >>> image = tf.random.uniform(
  ...   maxval=255, shape=[1, 28, 28, 3], dtype=tf.float32)
  >>> sobel = tf.image.sobel_edges(image)
  >>> sobel_y = np.asarray(sobel[0, :, :, :, 0]) # sobel in y-direction
  >>> sobel_x = np.asarray(sobel[0, :, :, :, 1]) # sobel in x-direction

  For displaying the sobel results, PIL's [Image Module](
  https://pillow.readthedocs.io/en/stable/reference/Image.html) can be used:

  ```python
  # Display edge maps for the first channel (at index 0)
  Image.fromarray(sobel_y[..., 0] / 4 + 0.5).show()
  Image.fromarray(sobel_x[..., 0] / 4 + 0.5).show()
  ```

  Args:
    image: Image tensor with shape [batch_size, h, w, d] and type float32 or
      float64.  The image(s) must be 2x2 or larger.

  Returns:
    Tensor holding edge maps for each channel. Returns a tensor with shape
    [batch_size, h, w, d, 2] where the last two dimensions hold [[dy[0], dx[0]],
    [dy[1], dx[1]], ..., [dy[d-1], dx[d-1]]] calculated using the Sobel filter.
  )rp   r  rp   r   r   r   )rq   ro   rq   )rp   r   rq   )r  r   ro   )rq   ro   r   r  r  rq   rp   sobel_filtersr   r   REFLECTr  r  r  r  )r<   r   rA   r  r  r   r!   
np_asarrayr  r   r  r   r  r  r   r  r  r   ru   r  )rC   static_image_shaperX   kernelsnum_kernels
kernels_tf	pad_sizesr!  r  outputrA   s              r2   sobel_edgesr  O  sE   T (&+Iy1*j13'G+LL0099E'NN7B''##G5;;?*~~1aR!,?D* 1v1v1v1v.)==		:& '##FJI& 

K+7
;%V51&%11;-@A	-r4   zimage.resize_bicubiczAUse `tf.image.resize(...method=ResizeMethod.BICUBIC...)` instead.c                 6    t        j                  | ||||      S N)r   r^  rw  r  r   )r   r{  r   r^  rw  r   r  s        r2   r{  r{    s&     
	%	%!+
 r4   zimage.resize_bilinearzBUse `tf.image.resize(...method=ResizeMethod.BILINEAR...)` instead.c                 6    t        j                  | ||||      S r  )r   ry  r  s        r2   ry  ry    s&     
	&	&!+
 r4   zimage.resize_nearest_neighborzJUse `tf.image.resize(...method=ResizeMethod.NEAREST_NEIGHBOR...)` instead.c                 6    t        j                  | ||||      S r  )r   rz  r  s        r2   rz  rz    s&     
	.	.!+
 r4   z>Use `tf.image.resize(...method=ResizeMethod.AREA...)` instead.zimage.resize_areazimage.crop_and_resizec           	      8    t        j                  | ||||||      S )a
  Extracts crops from the input image tensor and resizes them.

  Extracts crops from the input image tensor and resizes them using bilinear
  sampling or nearest neighbor sampling (possibly with aspect ratio change) to a
  common output size specified by `crop_size`. This is more general than the
  `crop_to_bounding_box` op which extracts a fixed size slice from the input
  image and does not allow resizing or aspect ratio change. The crops occur
  first and then the resize.

  Returns a tensor with `crops` from the input `image` at positions defined at
  the bounding box locations in `boxes`. The cropped boxes are all resized (with
  bilinear or nearest neighbor interpolation) to a fixed
  `size = [crop_height, crop_width]`. The result is a 4-D tensor
  `[num_boxes, crop_height, crop_width, depth]`. The resizing is corner aligned.
  In particular, if `boxes = [[0, 0, 1, 1]]`, the method will give identical
  results to using `tf.compat.v1.image.resize_bilinear()` or
  `tf.compat.v1.image.resize_nearest_neighbor()`(depends on the `method`
  argument) with
  `align_corners=True`.

  Args:
    image: A 4-D tensor of shape `[batch, image_height, image_width, depth]`.
      Both `image_height` and `image_width` need to be positive.
    boxes: A 2-D tensor of shape `[num_boxes, 4]`. The `i`-th row of the tensor
      specifies the coordinates of a box in the `box_ind[i]` image and is
      specified in normalized coordinates `[y1, x1, y2, x2]`. A normalized
      coordinate value of `y` is mapped to the image coordinate at `y *
      (image_height - 1)`, so as the `[0, 1]` interval of normalized image
      height is mapped to `[0, image_height - 1]` in image height coordinates.
      We do allow `y1` > `y2`, in which case the sampled crop is an up-down
      flipped version of the original image. The width dimension is treated
      similarly. Normalized coordinates outside the `[0, 1]` range are allowed,
      in which case we use `extrapolation_value` to extrapolate the input image
      values.
    box_indices: A 1-D tensor of shape `[num_boxes]` with int32 values in `[0,
      batch)`. The value of `box_ind[i]` specifies the image that the `i`-th box
      refers to.
    crop_size: A 1-D tensor of 2 elements, `size = [crop_height, crop_width]`.
      All cropped image patches are resized to this size. The aspect ratio of
      the image content is not preserved. Both `crop_height` and `crop_width`
      need to be positive.
    method: An optional string specifying the sampling method for resizing. It
      can be either `"bilinear"` or `"nearest"` and default to `"bilinear"`.
      Currently two sampling methods are supported: Bilinear and Nearest
        Neighbor.
    extrapolation_value: An optional `float`. Defaults to `0.0`. Value used for
      extrapolation, when applicable.
    name: A name for the operation (optional).

  Returns:
    A 4-D tensor of shape `[num_boxes, crop_height, crop_width, depth]`.

  Usage example:

  >>> BATCH_SIZE = 1
  >>> NUM_BOXES = 5
  >>> IMAGE_HEIGHT = 256
  >>> IMAGE_WIDTH = 256
  >>> CHANNELS = 3
  >>> CROP_SIZE = (24, 24)

  >>> image = tf.random.normal(shape=(
  ...   BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, CHANNELS) )
  >>> boxes = tf.random.uniform(shape=(NUM_BOXES, 4))
  >>> box_indices = tf.random.uniform(shape=(NUM_BOXES,), minval=0,
  ...   maxval=BATCH_SIZE, dtype=tf.int32)
  >>> output = tf.image.crop_and_resize(image, boxes, box_indices, CROP_SIZE)
  >>> output.shape
  TensorShape([5, 24, 24, 3])

  Example with linear interpolation:

  >>> image = np.arange(0, 18, 2).astype('float32').reshape(3, 3)
  >>> result = tf.image.crop_and_resize(
  ...   image[None, :, :, None],
  ...   np.asarray([[0.5,0.5,1,1]]), [0], [3, 3], method='bilinear')
  >>> result[0][:, :, 0]
  <tf.Tensor: shape=(3, 3), dtype=float32, numpy=
    array([[ 8.,  9., 10.],
           [11., 12., 13.],
           [14., 15., 16.]], dtype=float32)>

  Example with nearest interpolation:

  >>> image = np.arange(0, 18, 2).astype('float32').reshape(3, 3)
  >>> result = tf.image.crop_and_resize(
  ...   image[None, :, :, None],
  ...   np.asarray([[0.5,0.5,1,1]]), [0], [3, 3], method='nearest')
  >>> result[0][:, :, 0]
  <tf.Tensor: shape=(3, 3), dtype=float32, numpy=
    array([[ 8., 10., 10.],
           [14., 16., 16.],
           [14., 16., 16.]], dtype=float32)>


  )r   crop_and_resize)rC   rY  box_indices	crop_sizer  extrapolation_valuer   s          r2   crop_and_resize_v2r    s*    R 
	&	&ue[)'-/BD
J Jr4   z.box_ind is deprecated, use box_indices insteadbox_indc           	      h    t        j                  d|d|      }t        j                  | ||||||      S )Nr  r  )r   deprecated_argument_lookupr   r   )rC   rY  r  r  r  r  r   r  s           r2   crop_and_resize_v1r  L  sB     22=+3<gG'		&	&ueWi':D
B Br4   zimage.extract_glimpsec           	      :    t        j                  | ||||||      S )a
  Extracts a glimpse from the input tensor.

  Returns a set of windows called glimpses extracted at location
  `offsets` from the input tensor. If the windows only partially
  overlaps the inputs, the non-overlapping areas will be filled with
  random noise.

  The result is a 4-D tensor of shape `[batch_size, glimpse_height,
  glimpse_width, channels]`. The channels and batch dimensions are the
  same as that of the input tensor. The height and width of the output
  windows are specified in the `size` parameter.

  The argument `normalized` and `centered` controls how the windows are built:

  * If the coordinates are normalized but not centered, 0.0 and 1.0
    correspond to the minimum and maximum of each height and width
    dimension.
  * If the coordinates are both normalized and centered, they range from
    -1.0 to 1.0. The coordinates (-1.0, -1.0) correspond to the upper
    left corner, the lower right corner is located at (1.0, 1.0) and the
    center is at (0, 0).
  * If the coordinates are not normalized they are interpreted as
    numbers of pixels.

  Usage Example:

  >>> x = [[[[0.0],
  ...           [1.0],
  ...           [2.0]],
  ...          [[3.0],
  ...           [4.0],
  ...           [5.0]],
  ...          [[6.0],
  ...           [7.0],
  ...           [8.0]]]]
  >>> tf.compat.v1.image.extract_glimpse(x, size=(2, 2), offsets=[[1, 1]],
  ...                                    centered=False, normalized=False)
  <tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
  array([[[[0.],
           [1.]],
          [[3.],
           [4.]]]], dtype=float32)>

  Args:
    input: A `Tensor` of type `float32`. A 4-D float tensor of shape
      `[batch_size, height, width, channels]`.
    size: A `Tensor` of type `int32`. A 1-D tensor of 2 elements containing the
      size of the glimpses to extract.  The glimpse height must be specified
      first, following by the glimpse width.
    offsets: A `Tensor` of type `float32`. A 2-D integer tensor of shape
      `[batch_size, 2]` containing the y, x locations of the center of each
      window.
    centered: An optional `bool`. Defaults to `True`. indicates if the offset
      coordinates are centered relative to the image, in which case the (0, 0)
      offset is relative to the center of the input images. If false, the (0,0)
      offset corresponds to the upper left corner of the input images.
    normalized: An optional `bool`. Defaults to `True`. indicates if the offset
      coordinates are normalized.
    uniform_noise: An optional `bool`. Defaults to `True`. indicates if the
      noise should be generated using a uniform distribution or a Gaussian
      distribution.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  inputr^  offsetscentered
normalizeduniform_noiser   )r   extract_glimpser
  s          r2   r  r  c  s-    X 
	&	&!
 r4   c           
      <    t        j                  | |||||d|      S )a
  Extracts a glimpse from the input tensor.

  Returns a set of windows called glimpses extracted at location
  `offsets` from the input tensor. If the windows only partially
  overlaps the inputs, the non-overlapping areas will be filled with
  random noise.

  The result is a 4-D tensor of shape `[batch_size, glimpse_height,
  glimpse_width, channels]`. The channels and batch dimensions are the
  same as that of the input tensor. The height and width of the output
  windows are specified in the `size` parameter.

  The argument `normalized` and `centered` controls how the windows are built:

  * If the coordinates are normalized but not centered, 0.0 and 1.0
    correspond to the minimum and maximum of each height and width
    dimension.
  * If the coordinates are both normalized and centered, they range from
    -1.0 to 1.0. The coordinates (-1.0, -1.0) correspond to the upper
    left corner, the lower right corner is located at (1.0, 1.0) and the
    center is at (0, 0).
  * If the coordinates are not normalized they are interpreted as
    numbers of pixels.

  Usage Example:

  >>> x = [[[[0.0],
  ...           [1.0],
  ...           [2.0]],
  ...          [[3.0],
  ...           [4.0],
  ...           [5.0]],
  ...          [[6.0],
  ...           [7.0],
  ...           [8.0]]]]
  >>> tf.image.extract_glimpse(x, size=(2, 2), offsets=[[1, 1]],
  ...                         centered=False, normalized=False)
  <tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy=
  array([[[[4.],
           [5.]],
          [[7.],
           [8.]]]], dtype=float32)>

  Args:
    input: A `Tensor` of type `float32`. A 4-D float tensor of shape
      `[batch_size, height, width, channels]`.
    size: A `Tensor` of type `int32`. A 1-D tensor of 2 elements containing the
      size of the glimpses to extract.  The glimpse height must be specified
      first, following by the glimpse width.
    offsets: A `Tensor` of type `float32`. A 2-D integer tensor of shape
      `[batch_size, 2]` containing the y, x locations of the center of each
      window.
    centered: An optional `bool`. Defaults to `True`. indicates if the offset
      coordinates are centered relative to the image, in which case the (0, 0)
      offset is relative to the center of the input images. If false, the (0,0)
      offset corresponds to the upper left corner of the input images.
    normalized: An optional `bool`. Defaults to `True`. indicates if the offset
      coordinates are normalized.
    noise: An optional `string`. Defaults to `uniform`. indicates if the noise
      should be `uniform` (uniform distribution), `gaussian` (gaussian
      distribution), or `zero` (zero padding).
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `float32`.
  F)r  r^  r  r  r  noiser  r   )r   extract_glimpse_v2)r  r^  r  r  r  r  r   s          r2   r  r    s0    X 
	)	)
 r4   z"image.combined_non_max_suppressionc	                 T   t        j                  |d      5  t        j                  |t        j                  d      }t        j                  |t        j                  d      }t        j                  |      }t        j                  | |||||||      cddd       S # 1 sw Y   yxY w)aH  Greedily selects a subset of bounding boxes in descending order of score.

  This operation performs non_max_suppression on the inputs per batch, across
  all classes.
  Prunes away boxes that have high intersection-over-union (IOU) overlap
  with previously selected boxes.  Bounding boxes are supplied as
  [y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any
  diagonal pair of box corners and the coordinates can be provided as normalized
  (i.e., lying in the interval [0, 1]) or absolute.  Note that this algorithm
  is agnostic to where the origin is in the coordinate system. Also note that
  this algorithm is invariant to orthogonal transformations and translations
  of the coordinate system; thus translating or reflections of the coordinate
  system result in the same boxes being selected by the algorithm.
  The output of this operation is the final boxes, scores and classes tensor
  returned after performing non_max_suppression.

  Args:
    boxes: A 4-D float `Tensor` of shape `[batch_size, num_boxes, q, 4]`. If `q`
      is 1 then same boxes are used for all classes otherwise, if `q` is equal
      to number of classes, class-specific boxes are used.
    scores: A 3-D float `Tensor` of shape `[batch_size, num_boxes, num_classes]`
      representing a single score corresponding to each box (each row of boxes).
    max_output_size_per_class: A scalar integer `Tensor` representing the
      maximum number of boxes to be selected by non-max suppression per class
    max_total_size: A int32 scalar representing maximum number of boxes retained
      over all classes. Note that setting this value to a large number may
      result in OOM error depending on the system workload.
    iou_threshold: A float representing the threshold for deciding whether boxes
      overlap too much with respect to IOU.
    score_threshold: A float representing the threshold for deciding when to
      remove boxes based on score.
    pad_per_class: If false, the output nmsed boxes, scores and classes are
      padded/clipped to `max_total_size`. If true, the output nmsed boxes,
      scores and classes are padded to be of length
      `max_size_per_class`*`num_classes`, unless it exceeds `max_total_size` in
      which case it is clipped to `max_total_size`. Defaults to false.
    clip_boxes: If true, the coordinates of output nmsed boxes will be clipped
      to [0, 1]. If false, output the box coordinates as it is. Defaults to
      true.
    name: A name for the operation (optional).

  Returns:
    'nmsed_boxes': A [batch_size, max_detections, 4] float32 tensor
      containing the non-max suppressed boxes.
    'nmsed_scores': A [batch_size, max_detections] float32 tensor containing
      the scores for the boxes.
    'nmsed_classes': A [batch_size, max_detections] float32 tensor
      containing the class for boxes.
    'valid_detections': A [batch_size] int32 tensor indicating the number of
      valid detections per batch item. Only the top valid_detections[i] entries
      in nms_boxes[i], nms_scores[i] and nms_class[i] are valid. The rest of the
      entries are zero paddings.
  combined_non_max_suppressionrV  r   rW  N)r   r   r   r   rb  r   r  )	rY  rZ  max_output_size_per_classmax_total_sizerV  rW  pad_per_class
clip_boxesr   s	            r2   r  r    s    @ ~~d:; 4))V^^/CM++v~~4EGO **>:N55v0.-
4!4 4 4s   A=BB'c           	         t        j                  d      5  t        j                  | dd      \  }}}}t        j                  |dd      \  }}}}	t	        j
                  |t        j                  |g d            }
t	        j                  |t        j                  |	g d            }t	        j
                  |t        j                  |g d            }t	        j                  |t        j                  |g d            }t	        j
                  ||
z
  d      t	        j
                  ||z
  d      z  }||z
  ||z
  z  }||z
  |	|z
  z  }d}|t        j                  |g d      z   |z
  |z   }||z  }|cddd       S # 1 sw Y   yxY w)	ay  Calculates the overlap (iou - intersection over union) between boxes_a and boxes_b.

  Args:
    boxes_a: a tensor with a shape of [batch_size, N, 4]. N is the number of
      boxes per image. The last dimension is the pixel coordinates in
      [ymin, xmin, ymax, xmax] form.
    boxes_b: a tensor with a shape of [batch_size, M, 4]. M is the number of
      boxes. The last dimension is the pixel coordinates in
      [ymin, xmin, ymax, xmax] form.
  Returns:
    intersection_over_union: a tensor with as a shape of [batch_size, N, M],
    representing the ratio of intersection area over union area (IoU) between
    two boxes
  bbox_overlapr   ro   r   num_or_size_splitsr  )r   ro   rq   r   g:0yE>N)r   r   r   splitr   r.  r   r5  )boxes_aboxes_ba_y_mina_x_mina_y_maxa_x_maxb_y_minb_x_minb_y_maxb_x_maxi_xmini_xmaxi_ymini_ymaxi_areaa_areab_areaEPSILONu_areaintersection_over_unions                       r2   _bbox_overlapr3  e  s}    ~~n% #)2!!*5&GWgw)2!!*5&GWgw $$Wi8:F$$Wi8:F$$Wi8:F$$Wi8:F	&1 ( 0 0&6/A FGF Gg$56FGg$56FGi))&)<<vEOF %vo"9# # #s   EE==Fc                    t        j                  |       d   }t        j                  t        j                  t        j
                  | d      |k  |ddg      | j                        }t        j                  t        j                  t        j
                  || z  d      |k  | j                        |ddg      | z  }t        j                  |ddg      }|t        j                  ||z
  |kD        ||gS )a  Suppress boxes in the same tile.

     Compute boxes that cannot be suppressed by others (i.e.,
     can_suppress_others), and then use them to suppress boxes in the same tile.

  Args:
    iou: a tensor of shape [batch_size, num_boxes_with_padding] representing
    intersection over union.
    iou_sum: a scalar tensor.
    iou_threshold: a scalar tensor.

  Returns:
    iou_suppressed: a tensor of shape [batch_size, num_boxes_with_padding].
    iou_diff: a scalar tensor representing whether any box is supressed in
      this step.
    iou_sum_new: a scalar tensor of shape [batch_size] that represents
      the iou sum after suppression.
    iou_threshold: a scalar tensor.
  r   rq   rp   ro   )	r   rA   r   r   r   
reduce_maxr   r8  r  )iour:  iou_sumrV  r   can_suppress_othersiou_after_suppressioniou_sum_news           r2   _self_suppressionr;    s    ( s#A&* 


c1
%
5
B7JL	ii $++mm


1C7
;m
K
)) 2q	 !	!
 ##$9Aq6B+'K/-?@+
 r4   c                 :   t        j                  |       d   }t        j                  | d||z  dg||dg      }t        ||      }t        j                  t        j                  t        j                  ||k  dg      |j                        d      |z  }| |||dz   fS )a  Suppress boxes between different tiles.

  Args:
    boxes: a tensor of shape [batch_size, num_boxes_with_padding, 4]
    box_slice: a tensor of shape [batch_size, tile_size, 4]
    iou_threshold: a scalar tensor
    inner_idx: a scalar tensor representing the tile index of the tile
      that is used to supress box_slice
    tile_size: an integer representing the number of boxes in a tile

  Returns:
    boxes: unchanged boxes as input
    box_slice_after_suppression: box_slice after suppression
    iou_threshold: unchanged
  r   r   rq   ro   )	r   rA   r   r3  r  r   r   r  r   )	rY  	box_slicerV  	inner_idx	tile_sizer   	new_slicer6  box_slice_after_suppressions	            r2   _cross_suppressionrB    s      u%a(*ooaY&*9a ") 	i+# ) 5 5mmH''m(;aSAOO%!	 ! 
+]IM	IIr4   c                 f   t        j                  d      5  t        j                  |       d   z  }t        j                  |       d   }fd}t        j                  | dz  dg|dg      }t        j
                  fd|| ||t        j                  d      g      \  }	}}	}	t        ||      }
t        j                  t        j                  t        j                        ddg      t        j                  t        j                        ddg      kD  d      }|
t        j                  t        j                  ||
|k\        |
j                        z  }
t        j
                  d t         |
t        j                  d	      t        j"                  |
dd
g      |g      \  }}	}	}	t        j"                  |d      dkD  }|t        j                  dt        j                  ||j                        z
  d
      z  }t        j                  t        j                  t        j$                  t        j                  |            | j                        g d      }t        j&                  t        j                  |dg      d|ddg      |z  t        j                  | ||dg      d|z
  z  z   } t        j                  | |ddg      } |t        j"                  t        j                  t        j(                  |dkD  d
g      t*        j,                        dg      z  }ddd       | ||dz   fS # 1 sw Y   xY w)a  Process boxes in the range [idx*tile_size, (idx+1)*tile_size).

  Args:
    boxes: a tensor with a shape of [batch_size, anchors, 4].
    iou_threshold: a float representing the threshold for deciding whether boxes
      overlap too much with respect to IOU.
    output_size: an int32 tensor of size [batch_size]. Representing the number
      of selected boxes for each batch.
    idx: an integer scalar representing induction variable.
    tile_size: an integer representing the number of boxes in a tile

  Returns:
    boxes: updated boxes.
    iou_threshold: pass down iou_threshold to the next iteration.
    output_size: the updated output_size.
    idx: the updated induction variable.
  suppression_loop_bodyrq   r   c                 "    t        | |||      S r   )rB  )rY  r=  rV  r>  r?  s       r2   cross_suppression_funcz6_suppression_loop_body.<locals>.cross_suppression_func  s    y- )+ +r4   r   c                     |k  S r   rN   )_boxes
_box_slice
_thresholdr>  r   s       r2   r   z(_suppression_loop_body.<locals>.<lambda>  s    )c/ r4   rp   c                     |S r   rN   )_iouloop_condition_iou_sumr:  s       r2   r   z(_suppression_loop_body.<locals>.<lambda>  s    . r4   Tro   r   )rq   rp   rq   rq   N)r   r   r   rA   r   r   r   r  r3  r  r   r   r  r   logical_andr   r;  r8  r   r  r  r   r   )rY  rV  output_sizer   r?  	num_tilesr   rF  r=  r:  r6  masksuppressed_iousuppressed_boxs      ``         r2   _suppression_loop_bodyrU    s   $ ~~-. 1I&q)Y6I'*J+
 3?A'>!+Y :<I#..I		=			a	 	"#Ay!Q 	9
-C  NN9%2w	02;2C2Cy)B734	4568D 8==T3-#78#))E EC(33@  &aV,m
NAq! ((;a?N&&hmmNIOO<<aA AI NN8>>)4c:EKK	ID NN900A3Y1-/1568A8I8IJ	9a89:=>X9GGE ej"a%89E 8&&	As3V\\	CEFCI IK_1Id 
{C!G	33e1I 1Is   K>L''L0z image.non_max_suppression_paddedi   c
                    t        j                  |d      5  |sQ| j                         j                  7| j                         j                  dkD  rt	        dj                  |            |d}t        | |||||||	      \  }
}|s
|
dd|f   }
nXt        j                  t        j                  |       dd t        j                  |d      gd      }t        j                  |
|      }
|
|fcddd       S # 1 sw Y   yxY w)aL  Greedily selects a subset of bounding boxes in descending order of score.

  Performs algorithmically equivalent operation to tf.image.non_max_suppression,
  with the addition of an optional parameter which zero-pads the output to
  be of size `max_output_size`.
  The output of this operation is a tuple containing the set of integers
  indexing into the input collection of bounding boxes representing the selected
  boxes and the number of valid indices in the index set.  The bounding box
  coordinates corresponding to the selected indices can then be obtained using
  the `tf.slice` and `tf.gather` operations.  For example:
    ```python
    selected_indices_padded, num_valid = tf.image.non_max_suppression_padded(
        boxes, scores, max_output_size, iou_threshold,
        score_threshold, pad_to_max_output_size=True)
    selected_indices = tf.slice(
        selected_indices_padded, tf.constant([0]), num_valid)
    selected_boxes = tf.gather(boxes, selected_indices)
    ```

  Args:
    boxes: a tensor of rank 2 or higher with a shape of [..., num_boxes, 4].
      Dimensions except the last two are batch dimensions.
    scores: a tensor of rank 1 or higher with a shape of [..., num_boxes].
    max_output_size: a scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non max suppression. Note that setting this
      value to a large number may result in OOM error depending on the system
      workload.
    iou_threshold: a float representing the threshold for deciding whether boxes
      overlap too much with respect to IoU (intersection over union).
    score_threshold: a float representing the threshold for box scores. Boxes
      with a score that is not larger than this threshold will be suppressed.
    pad_to_max_output_size: whether to pad the output idx to max_output_size.
      Must be set to True when the input is a batch of images.
    name: name of operation.
    sorted_input: a boolean indicating whether the input boxes and scores
      are sorted in descending order by the score.
    canonicalized_coordinates: if box coordinates are given as
    `[y_min, x_min, y_max, x_max]`, setting to True eliminate redundant
     computation to canonicalize box coordinates.
    tile_size: an integer representing the number of boxes in a tile, i.e.,
      the maximum number of boxes per image that can be used to suppress other
      boxes in parallel; larger tile_size means larger parallelism and
      potentially more redundant work.
  Returns:
    idx: a tensor with a shape of [..., num_boxes] representing the
      indices selected by non-max suppression. The leading dimensions
      are the batch dimensions of the input boxes. All numbers are within
      [0, num_boxes). For each image (i.e., idx[i]), only the first num_valid[i]
      indices (i.e., idx[i][:num_valid[i]]) are valid.
    num_valid: a tensor of rank 0 or higher with a shape of [...]
      representing the number of valid indices in idx. Its dimensions are the
      batch dimensions of the input boxes.
   Raises:
    ValueError: When set pad_to_max_output_size to False for batched input.
  non_max_suppression_paddedNro   zB'pad_to_max_output_size' (value {}) must be True for batched input r   r  )r   r   r<   rD   rS   r   non_max_suppression_padded_v2r   r  rA   r  r   )rY  rZ  r[  rV  rW  r_  r   sorted_inputcanonicalized_coordinatesr?  r   	num_valid
batch_dimss                r2   rW  rW    s   F ~~d89 !
 
				+0A0F0F0J ))/0F)GI 	I|d2v/<NC ":I:c##
//%
 "
%



3% 
j c:.c	>/  s   CC55C>rY  )experimental_implementsc                 N  #$%&' d }t        j                  |       dd }	t        j                  |       d   }
t        j                  | d|
dg      } t        j                  |d|
g      }t        j                  |       d   }|t        d      k7  rt	        j
                  d      5  t        j                  ||kD  |j                        }||z  }t        j                  t        j                  || j                        d	      }| |z  } ddd       |st	        j
                  d
      5  t        j                  | dd	      \  &$'%t        j                  t        j                  &d   'd               }t        j                  |&'fd&'fd      \  }}t        j                  t        j                  $d   %d               }t        j                  |$%fd$%fd      \  }}t        j                  ||||gd	      } ddd       |s |||       \  }} }n%t        j                   |t"        j$                        }t        j                  t        j&                  t        j                  t        j(                  |
      t"        j*                        t        j                  t"        j*                        z        t"        j$                        z  |
z
  }t        j,                  t        j                  | t"        j*                        ddgd|gddgg      } t        j,                  t        j                  |t"        j*                        ddgd|gg      }|
|z   }|z  ##fd}fd}t/        j.                  ||| |t        j0                  |gt"        j$                        t3        j4                  d      gt7        j8                  g d      t7        j8                  g       t7        j8                  dg      t7        j8                  g       g      \  }}}}t        j:                  |      }|t        j                  t=        j>                  t        j                  t        j@                  |dkD  d	g      t"        j$                        t        j                  t        jB                  |dd      d      z        d   t"        j$                        z
  }t        j:                  ||
dz
        }|st        jB                  |      |
z  }t        j                  |t        j                  |d      z   dg      }t        j                  t        jD                  t        j                  |dg      |      |dg      }t        j0                  |gt"        j$                        } t        j                  t        jB                        d      }!t        j                  |d      }"t        jF                  |!|"k  ||       }t        j                  ||	      }||fS # 1 sw Y   xY w# 1 sw Y   xY w)a  Non-maximum suppression.

  Prunes away boxes that have high intersection-over-union (IOU) overlap
  with previously selected boxes. Bounding boxes are supplied as
  `[y1, x1, y2, x2]`, where `(y1, x1)` and `(y2, x2)` are the coordinates of any
  diagonal pair of box corners and the coordinates can be provided as normalized
  (i.e., lying in the interval `[0, 1]`) or absolute. The bounding box
  coordinates are cannonicalized to `[y_min, x_min, y_max, x_max]`,
  where `(y_min, x_min)` and `(y_max, x_mas)` are the coordinates of the lower
  left and upper right corner. User may indiciate the input box coordinates are
  already canonicalized to eliminate redundant work by setting
  canonicalized_coordinates to `True`. Note that this algorithm is agnostic to
  where the origin is in the coordinate system. Note that this algorithm is
  invariant to orthogonal transformations and translations of the coordinate
  system; thus translating or reflections of the coordinate system result in the
  same boxes being selected by the algorithm.

  Similar to tf.image.non_max_suppression, non_max_suppression_padded
  implements hard NMS but can operate on a batch of images and improves
  performance by titling the bounding boxes. Non_max_suppression_padded should
  be preferred over tf.image_non_max_suppression when running on devices with
  abundant parallelsim for higher computation speed. For soft NMS, refer to
  tf.image.non_max_suppression_with_scores.

  While a serial NMS algorithm iteratively uses the highest-scored unprocessed
  box to suppress boxes, this algorithm uses many boxes to suppress other boxes
  in parallel. The key idea is to partition boxes into tiles based on their
  score and suppresses boxes tile by tile, thus achieving parallelism within a
  tile. The tile size determines the degree of parallelism.

  In cross suppression (using boxes of tile A to suppress boxes of tile B),
  all boxes in A can independently suppress boxes in B.

  Self suppression (suppressing boxes of the same tile) needs to be iteratively
  applied until there's no more suppression. In each iteration, boxes that
  cannot be suppressed are used to suppress boxes in the same tile.

  boxes = boxes.pad_to_multiply_of(tile_size)
  num_tiles = len(boxes) // tile_size
  output_boxes = []
  for i in range(num_tiles):
    box_tile = boxes[i*tile_size : (i+1)*tile_size]
    for j in range(i - 1):
      # in parallel suppress boxes in box_tile using boxes from suppressing_tile
      suppressing_tile = boxes[j*tile_size : (j+1)*tile_size]
      iou = _bbox_overlap(box_tile, suppressing_tile)
      # if the box is suppressed in iou, clear it to a dot
      box_tile *= _update_boxes(iou)
    # Iteratively handle the diagnal tile.
    iou = _box_overlap(box_tile, box_tile)
    iou_changed = True
    while iou_changed:
      # boxes that are not suppressed by anything else
      suppressing_boxes = _get_suppressing_boxes(iou)
      # boxes that are suppressed by suppressing_boxes
      suppressed_boxes = _get_suppressed_boxes(iou, suppressing_boxes)
      # clear iou to 0 for boxes that are suppressed, as they cannot be used
      # to suppress other boxes any more
      new_iou = _clear_iou(iou, suppressed_boxes)
      iou_changed = (new_iou != iou)
      iou = new_iou
    # remaining boxes that can still suppress others, are selected boxes.
    output_boxes.append(_get_suppressing_boxes(iou))
    if len(output_boxes) >= max_output_size:
      break

  Args:
    boxes: a tensor of rank 2 or higher with a shape of [..., num_boxes, 4].
      Dimensions except the last two are batch dimensions. The last dimension
      represents box coordinates, given as [y_1, x_1, y_2, x_2]. The coordinates
      on each dimension can be given in any order
      (see also `canonicalized_coordinates`) but must describe a box with
      a positive area.
    scores: a tensor of rank 1 or higher with a shape of [..., num_boxes].
    max_output_size: a scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non max suppression.
    iou_threshold: a float representing the threshold for deciding whether boxes
      overlap too much with respect to IoU (intersection over union).
    score_threshold: a float representing the threshold for box scores. Boxes
      with a score that is not larger than this threshold will be suppressed.
    sorted_input: a boolean indicating whether the input boxes and scores
      are sorted in descending order by the score.
    canonicalized_coordinates: if box coordinates are given as
    `[y_min, x_min, y_max, x_max]`, setting to True eliminate redundant
     computation to canonicalize box coordinates.
    tile_size: an integer representing the number of boxes in a tile, i.e.,
      the maximum number of boxes per image that can be used to suppress other
      boxes in parallel; larger tile_size means larger parallelism and
      potentially more redundant work.
  Returns:
    idx: a tensor with a shape of [..., num_boxes] representing the
      indices selected by non-max suppression. The leading dimensions
      are the batch dimensions of the input boxes. All numbers are within
      [0, num_boxes). For each image (i.e., idx[i]), only the first num_valid[i]
      indices (i.e., idx[i][:num_valid[i]]) are valid.
    num_valid: a tensor of rank 0 or higher with a shape of [...]
      representing the number of valid indices in idx. Its dimensions are the
      batch dimensions of the input boxes.
   Raises:
    ValueError: When set pad_to_max_output_size to False for batched input.
  c                     t        j                  d      5  t        j                  | dd      }t	        j
                  | |dd      }t	        j
                  ||dd      }ddd       fS # 1 sw Y   xY w)a^  Sort boxes based their score from highest to lowest.

    Args:
      scores: a tensor with a shape of [batch_size, num_boxes] representing
        the scores of boxes.
      boxes: a tensor with a shape of [batch_size, num_boxes, 4] representing
        the boxes.
    Returns:
      sorted_scores: a tensor with a shape of [batch_size, num_boxes]
        representing the sorted scores.
      sorted_boxes: a tensor representing the sorted boxes.
      sorted_scores_indices: a tensor with a shape of [batch_size, num_boxes]
        representing the index of the scores in a sorted descending order.
    sort_scores_and_boxesrq   
DESCENDING)r  	direction)r  r]  N)r   r   r   argsortr   gather)rZ  rY  sorted_scores_indicessorted_scoressorted_boxess        r2   _sort_scores_and_boxesz=non_max_suppression_padded_v2.<locals>._sort_scores_and_boxes  s     
/	0 &..
qL2&&
'aAm %%
&Q1l ,(=== s   AA..A7Nr  rp   r   r   rS  filter_by_scorero   canonicalize_coordinatesr  r  c                       fS r   rN   y_1y_2s   r2   r   z/non_max_suppression_padded_v2.<locals>.<lambda>      sCj r4   c                       fS r   rN   rm  s   r2   r   z/non_max_suppression_padded_v2.<locals>.<lambda>      3* r4   c                       fS r   rN   x_1x_2s   r2   r   z/non_max_suppression_padded_v2.<locals>.<lambda>  rp  r4   c                       fS r   rN   rt  s   r2   r   z/non_max_suppression_padded_v2.<locals>.<lambda>  rr  r4   r  r  c                 b    t        j                  t        j                  |      k  |k        S r   )r   rO  
reduce_min)unused_boxesunused_thresholdrP  r   r[  num_iterationss       r2   
_loop_condz1non_max_suppression_padded_v2.<locals>._loop_cond6  s1    K(?:n r4   c                 "    t        | |||      S r   )rU  )rY  rV  rP  r   r?  s       r2   rD  z<non_max_suppression_padded_v2.<locals>.suppression_loop_body;  s    !}k3	; ;r4   )NNr   )shape_invariantsrq   )$r   rA   r   r   r   r   r   r   r   r  r  r  
less_equalr   r   r  
zeros_liker   r   ceilr.  rb  r  r   r  r   r  r   TensorShaper5  r   top_kr  r  re  where)(rY  rZ  r[  rV  rW  rZ  r[  r?  ri  r]  	num_boxesr   
score_maskbox_mask
y_1_is_miny_miny_max
x_1_is_minx_minx_maxsorted_indicesr  num_boxes_after_paddingr}  rD  selected_boxesr:  rP  r\  r   index_offsets
gather_idxinvalid_index	idx_indexnum_valid_expandedr|  ru  rv  rn  ro  s(     `    `                           @@@@@r2   rY  rY  {  sK   ^>4 u%cr**ooe$R()


EB	1#5
6%Vb)_5&u%a(*f%	)	* ==/!96<<Hj
f&&
--
EKK
0!5hxe 
#	2	3 E$??!!5c3S&&


c'lCL
9;j\\
(*<>leU&&


c'lCL
9;j\\
(*<>leUueU;!DeE$ 
$:65$I!FE> ))&ENmm
--y/:FNNL
--	6>>
234 ll	
  	 
 #,	,# --mmE6>>*aVaX1v,FH%==mmFFNN+q!fq#h-?A&%O*i7.
; '1&;&;

//:,
5


q
! 
"
"?
3

"
"2
&

"
"D6
*

"
"2
&	'#.![! {O<)(--ll
--++q 1#'(.6


nn4a<aAA 
 
  &||#- 	-# 	i!m,#	NN:.:M""i##M155t=J


**>B4@#	%	RC //:"?(.6-##HNN?$CQG) ,,Y:	$66]	,# 	:6)	i} E Es   'A"X(CXXX$c           	          t        j                  |d      5  t        j                  |d      }t        j                  |d      }t        j                  | |||||      cddd       S # 1 sw Y   yxY w)a   Greedily selects a subset of bounding boxes in descending order of score.

  Performs algorithmically equivalent operation to tf.image.non_max_suppression,
  with the addition of an optional parameter which zero-pads the output to
  be of size `max_output_size`.
  The output of this operation is a tuple containing the set of integers
  indexing into the input collection of bounding boxes representing the selected
  boxes and the number of valid indices in the index set.  The bounding box
  coordinates corresponding to the selected indices can then be obtained using
  the `tf.slice` and `tf.gather` operations.  For example:
    ```python
    selected_indices_padded, num_valid = tf.image.non_max_suppression_padded(
        boxes, scores, max_output_size, iou_threshold,
        score_threshold, pad_to_max_output_size=True)
    selected_indices = tf.slice(
        selected_indices_padded, tf.constant([0]), num_valid)
    selected_boxes = tf.gather(boxes, selected_indices)
    ```

  Args:
    boxes: A 2-D float `Tensor` of shape `[num_boxes, 4]`.
    scores: A 1-D float `Tensor` of shape `[num_boxes]` representing a single
      score corresponding to each box (each row of boxes).
    max_output_size: A scalar integer `Tensor` representing the maximum number
      of boxes to be selected by non-max suppression.
    iou_threshold: A float representing the threshold for deciding whether boxes
      overlap too much with respect to IOU.
    score_threshold: A float representing the threshold for deciding when to
      remove boxes based on score.
    pad_to_max_output_size: bool.  If True, size of `selected_indices` output is
      padded to `max_output_size`.
    name: A name for the operation (optional).

  Returns:
    selected_indices: A 1-D integer `Tensor` of shape `[M]` representing the
      selected indices from the boxes tensor, where `M <= max_output_size`.
    valid_outputs: A scalar integer `Tensor` denoting how many elements in
    `selected_indices` are valid.  Valid elements occur first, then padding.
  rW  rV  r   rW  N)r   r   r   r   non_max_suppression_v4)rY  rZ  r[  rV  rW  r_  r   s          r2   non_max_suppression_padded_v1r  k  sr    \ ~~d89 H))-oNM++/1O//v0=0FH	H H Hs   AA))A2zimage.draw_bounding_boxesc                 d    |t        j                  | ||      S t        j                  | |||      S )a'  Draw bounding boxes on a batch of images.

  Outputs a copy of `images` but draws on top of the pixels zero or more
  bounding boxes specified by the locations in `boxes`. The coordinates of the
  each bounding box in `boxes` are encoded as `[y_min, x_min, y_max, x_max]`.
  The bounding box coordinates are floats in `[0.0, 1.0]` relative to the width
  and the height of the underlying image.

  For example, if an image is 100 x 200 pixels (height x width) and the bounding
  box is `[0.1, 0.2, 0.5, 0.9]`, the upper-left and bottom-right coordinates of
  the bounding box will be `(40, 10)` to `(180, 50)` (in (x,y) coordinates).

  Parts of the bounding box may fall outside the image.

  Args:
    images: A `Tensor`. Must be one of the following types: `float32`, `half`.
      4-D with shape `[batch, height, width, depth]`. A batch of images.
    boxes: A `Tensor` of type `float32`. 3-D with shape `[batch,
      num_bounding_boxes, 4]` containing bounding boxes.
    colors: A `Tensor` of type `float32`. 2-D. A list of RGBA colors to cycle
      through for the boxes.
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `images`.

  Usage Example:

  >>> # create an empty image
  >>> img = tf.zeros([1, 3, 3, 3])
  >>> # draw a box around the image
  >>> box = np.array([0, 0, 1, 1])
  >>> boxes = box.reshape([1, 1, 4])
  >>> # alternate between red and blue
  >>> colors = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
  >>> tf.image.draw_bounding_boxes(img, boxes, colors)
  <tf.Tensor: shape=(1, 3, 3, 3), dtype=float32, numpy=
  array([[[[1., 0., 0.],
          [1., 0., 0.],
          [1., 0., 0.]],
          [[1., 0., 0.],
          [0., 0., 0.],
          [1., 0., 0.]],
          [[1., 0., 0.],
          [1., 0., 0.],
          [1., 0., 0.]]]], dtype=float32)>
  )r   draw_bounding_boxesdraw_bounding_boxes_v2)r   rY  colorsr   s       r2   r  r    s6    d ^,,VUDAA		-	-feVT	JJr4   c                     t        | |||      S )a'  Draw bounding boxes on a batch of images.

  Outputs a copy of `images` but draws on top of the pixels zero or more
  bounding boxes specified by the locations in `boxes`. The coordinates of the
  each bounding box in `boxes` are encoded as `[y_min, x_min, y_max, x_max]`.
  The bounding box coordinates are floats in `[0.0, 1.0]` relative to the width
  and the height of the underlying image.

  For example, if an image is 100 x 200 pixels (height x width) and the bounding
  box is `[0.1, 0.2, 0.5, 0.9]`, the upper-left and bottom-right coordinates of
  the bounding box will be `(40, 10)` to `(180, 50)` (in (x,y) coordinates).

  Parts of the bounding box may fall outside the image.

  Args:
    images: A `Tensor`. Must be one of the following types: `float32`, `half`.
      4-D with shape `[batch, height, width, depth]`. A batch of images.
    boxes: A `Tensor` of type `float32`. 3-D with shape `[batch,
      num_bounding_boxes, 4]` containing bounding boxes.
    name: A name for the operation (optional).
    colors: A `Tensor` of type `float32`. 2-D. A list of RGBA colors to cycle
      through for the boxes.

  Returns:
    A `Tensor`. Has the same type as `images`.

  Usage Example:

  >>> # create an empty image
  >>> img = tf.zeros([1, 3, 3, 3])
  >>> # draw a box around the image
  >>> box = np.array([0, 0, 1, 1])
  >>> boxes = box.reshape([1, 1, 4])
  >>> # alternate between red and blue
  >>> colors = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
  >>> tf.image.draw_bounding_boxes(img, boxes, colors)
  <tf.Tensor: shape=(1, 3, 3, 3), dtype=float32, numpy=
  array([[[[1., 0., 0.],
          [1., 0., 0.],
          [1., 0., 0.]],
          [[1., 0., 0.],
          [0., 0., 0.],
          [1., 0., 0.]],
          [[1., 0., 0.],
          [1., 0., 0.],
          [1., 0., 0.]]]], dtype=float32)>
  )r  )r   rY  r   r  s       r2   r  r    s    d 
 vt	<<r4   z%image.generate_bounding_box_proposalsc	                 >    t        j                  | ||||||||	      S )a1  Generate bounding box proposals from encoded bounding boxes.

  Args:
    scores: A 4-D float `Tensor` of shape
     `[num_images, height, width, num_achors]` containing scores of
      the boxes for given anchors, can be unsorted.
    bbox_deltas: A 4-D float `Tensor` of shape
     `[num_images, height, width, 4 x num_anchors]` encoding boxes
      with respect to each anchor. Coordinates are given
      in the form `[dy, dx, dh, dw]`.
    image_info: A 2-D float `Tensor` of shape `[num_images, 5]`
      containing image information Height, Width, Scale.
    anchors: A 2-D float `Tensor` of shape `[num_anchors, 4]`
      describing the anchor boxes.
      Boxes are formatted in the form `[y1, x1, y2, x2]`.
    nms_threshold: A scalar float `Tensor` for non-maximal-suppression
      threshold. Defaults to 0.7.
    pre_nms_topn: A scalar int `Tensor` for the number of
      top scoring boxes to be used as input. Defaults to 6000.
    min_size: A scalar float `Tensor`. Any box that has a smaller size
      than min_size will be discarded. Defaults to 16.
    post_nms_topn: An integer. Maximum number of rois in the output.
    name: A name for this operation (optional).

  Returns:
    rois: Region of interest boxes sorted by their scores.
    roi_probabilities: scores of the ROI boxes in the ROIs' `Tensor`.
  	rZ  bbox_deltas
image_infoanchorsnms_thresholdpre_nms_topnmin_sizepost_nms_topnr   )r   generate_bounding_box_proposalsr  s	            r2   r  r    s3    N 
	6	6!!	
 	r4   )Tr   )rq   N)rq   rq   )FN)rX  N)rp   N)r   皙?NNNNN)r  NNNNN)NNr  NNNNN)r   r  r  )r   r  r  r  r  F)r  r  r  r  F)FNF)rP  r   N)NNrP  r   NN)TTTN)TTuniformN)NN)gffffff?ip     i,  N)rI  r}   numpyr  tensorflow.python.eagerr   r   tensorflow.python.frameworkr   r   r   r   r	   r
   r7   r   r   tensorflow.python.opsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   tensorflow.python.utilr   r    r!    tensorflow.python.util.tf_exportr"   NotDifferentiabler3   r.   rI   rY   r^   ra   r`   rm   rl   rw   add_dispatch_supportr{   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r%  r+  rE  rO  rs  rJ  r  r  r  r  r  r  register_unary_elementwise_apir  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r"  r  r%  r-  decode_and_crop_jpeg
decode_bmp
decode_gifr  
decode_pngencode_jpegextract_jpeg_shaper/  r  r2  r7  rI  rN  
deprecatedr?  r   rU  r]  rf  rm  ro  rq  rr  rt  ru  rw  rx  r  r  r  r  r  r  _MSSSIM_WEIGHTSr  r  r  r{  ry  rz  resize_area_deprecationr|  r  deprecated_argsr  r   r  r  r  r3  r;  rB  rU  rW  functionrY  r  r  r  r  rN   r4   r2   <module>r     s|   #   + 0 . 3 . + 3 < 4 3 + 1 + 1 5 3 2 / * ) ( , . * 6 , + , . + / 6   l #   j !   ) *   2 3   4 5   & '   ) *   + ,   5 6   4 50	@, F9,@,(V@.(V& &'	*D  (*DZ )*	+G  ++G\ 3;	A  <A: 0R8	>  9>:3Nl "#	,  $,B  	)  !)B'MT =	=N  =N@>@ "35L!MN	AN  OANH  	N  !Nb &'	>  (>H[| '(	f  )fR #')NOQ 
V QVr #$%  & B'	" 	" (	"K\ $n56	 (00 %(-Q   7Q h >b!	 )11+0$n!  "n!bHV ,-.	 %3$;$;+0	&3  /&3R "r*	 %1$9$9',	$3  +$3N ,-	6  .6r $%	((	&)  ) &&)R .26	((	))  ) 7))X "#	)1  $)1X ,4	*1  5*1Z $%	((	/D  ) &/Dd "#	6D  $6Dr  	((	<H  ) !<H~ &'	((	s9  ) (s9l #$	B  %BD #$	  %F 	,"  ,"^ 'B/	."  0."b 	F8  F8T &'	12  (12h 0R8	32  932l &'	OA  (OAd $%	/5  &/5d .26	-5  7-5` $%	25  &25j <lO-LM> N>&8 By !#?@B 	&%%m&H&HI	K .Y+,. 	&%%m&>&>?	A

.Y+,. 	&%%m&>&>?	A

0i-.0 	&%%m&?&?@	B
.Y+,. 	&%%m&>&>?	A
0i-.0 	&%%m&?&?@	B
>Y!;<> 	&%%m&F&FG	I  ?./	7  078 /02 
||#'	= 	2
=@ "#	C  $CL 0R8	 +,8;8<0426DH*.y  9yx :rB	 @C?C7;9=KO15w  Cwt 456		45 (,(,5859-1/3AE'+s	5  7sl &'	 '*(-f!0P  (0Pf 23	 3649&M36)-U-  4U-p /0	 9<6;Fm+/+O  1+O^ 2768 
 	E  E8  !G:<  	E  E, 7768 
 	E  E0  !>24  	,E  ,E^* Z <	2*  2*j7t8& ""$#&',Up <	 Y8  Y8z ; "#	 #2 "!$|/  $|/~ "#	E  $EP 	@   @F %&'		K "'&+	
  (
 &'(		L #(',	
  )
 ./0			 +0!%/4	
  1
 1+00	HK  2i/01%%%m&?&?@ "r*	
 )+- hJ  +hJV &'(	TM&( 	B(  )
B +::BB   &'(	
 	Q  )Qh "r*	
 
	R  +Rj /0	
 0316v/4,0&*P4  1P4f+#\#LJ8D4N -.	 .1/4V}6;$(,19>),X  /Xz ;=
 1427-/4<A,/k=kb 1427-9>'+4Hn &2.	2K  /2Kj *+,	0=  -0=f 23	
 3615-/25)-.  4.r4   