
    AVhI:                        d 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j                  d      d	ej                  fd
       Z ej                  d      d	ej                  fd       Z ej                  d        ej                  d      d        Z ej                  d      d	ej                  fd       Z ej                  d      d	ej                  fd       Zd Z ej                  d      d	ej                  fd       Zy)z*Contains Gradient functions for image ops.    )dtypes)ops)	array_ops)array_ops_stack)gen_image_ops)math_opsResizeNearestNeighboropc                 0   | j                   d   }|j                         dd j                         r|j                         dd }nt        j                  |      dd }t        j                  ||| j                  d      | j                  d            }|dgS )zThe derivatives for nearest neighbor resizing.

  Args:
    op: The ResizeNearestNeighbor op.
    grad: The tensor representing the gradient w.r.t. the output.

  Returns:
    The gradients w.r.t. the input and the output.
  r         align_cornershalf_pixel_centersr   r   N)inputs	get_shapeis_fully_definedr   shaper   resize_nearest_neighbor_gradget_attr)r
   gradimageimage_shapegradss        P/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/image_grad.py_ResizeNearestNeighborGradr      s     ))A,%
__q,,.//#Aa(K//%(1-K

4
4
KK0%9:	<%
     ResizeBilinearc                     t        j                  || j                  d   | j                  d      | j                  d            }|dgS )zThe derivatives for bilinear resizing.

  Args:
    op: The ResizeBilinear op.
    grad: The tensor representing the gradient w.r.t. the output.

  Returns:
    The gradients w.r.t. the input.
  r   r   r   r   N)r   resize_bilinear_gradr   r   r
   r   grad0s      r   _ResizeBilinearGradr#   2   sG     
,
,
iilKK0%9:	<%
 r   ResizeBilinearGradScaleAndTranslatec           
          t        j                  || j                  d   | j                  d   | j                  d   | j                  d      | j                  d            }|dddgS )zThe derivatives for ScaleAndTranslate transformation op.

  Args:
    op: The ScaleAndTranslate op.
    grad: The tensor representing the gradient w.r.t. the output.

  Returns:
    The gradients w.r.t. the input.
  r      r   kernel_type	antialias)r(   r)   N)r   scale_and_translate_gradr   r   r!   s      r   _ScaleAndTranslateGradr+   H   sa     
0
0
iiliiliil++m,K(*% tT	""r   ResizeBicubicc                    t         j                  t         j                  g}d}| j                  d   j                  |v rDt        j                  || j                  d   | j                  d      | j                  d            }|dgS )zThe derivatives for bicubic resizing.

  Args:
    op: The ResizeBicubic op.
    grad: The tensor representing the gradient w.r.t. the output.

  Returns:
    The gradients w.r.t. the input.
  Nr   r   r   r   )r   float32float64r   dtyper   resize_bicubic_gradr   )r
   r   allowed_typesr"   s       r   _ResizeBicubicGradr3   ^   st     >>6>>2-
%YYq\=(--
		!kk/2;;';<	>E
 r   CropAndResizec           
         | j                   d   }|j                         j                         r|j                         j                         }nt	        j
                  |      }t        j                  t        j                  t        j                  g}| j                   d   j                  |v rTt        j                  || j                   d   | j                   d   || j                  d      | j                  d            }nd}t        j                  || j                   d   | j                   d   | j                   d         }||ddgS )a  The derivatives for crop_and_resize.

  We back-propagate to the image only when the input image tensor has floating
  point dtype but we always back-propagate to the input boxes tensor.

  Args:
    op: The CropAndResize op.
    grad: The tensor representing the gradient w.r.t. the output.

  Returns:
    The gradients w.r.t. the input image, boxes, as well as the always-None
    gradients w.r.t. box_ind and crop_size.
  r   r   r'   Tmethod)r6   r7   N)r   r   r   as_listr   r   r   float16r.   r/   r0   r   crop_and_resize_grad_imager   crop_and_resize_grad_boxes)r
   r   r   r   r2   r"   grad1s          r   _CropAndResizeGradr=   t   s     ))A,%
__'')//#++-K//%(K>>6>>6>>B-YYq\=(44biilBIIaL+S9I{{8$&E
 E 
2
2
BIIaL"))A,		!6% d	##r   c                 j    t        j                  t        j                  d| j                        |       S )zWrapper function around `math_ops.div_no_nan()` to perform a "safe" reciprocal incase the input is zero. Avoids divide by zero and NaNs.

  Input:
    x -> input tensor to be reciprocat-ed.
  Returns:
    x_reciprocal -> reciprocal of x without NaNs.
  g      ?)r   
div_no_nancastr0   )xs    r   _CustomReciprocalrB      s&     
		X]]38!	<<r   RGBToHSVc           
         | j                   d   d   }| j                   d   d   }| j                   d   d   }| j                  d   d   }| j                  d   d   }| j                   d   j                  }t        j                  ||k\  ||k\  z  |      }t        j                  ||kD  ||k\  z  |      }	t        j                  ||kD  ||kD  z  |      }
t        j                  ||k  ||k  z  |      }t        j                  ||k  ||k  z  |      }t        j                  ||k  ||k  z  |      }|}|	}|
}t        j                  |dkD  |      t        j
                  |t        j
                  ||z  ||z        z  t        t        j                  |            z  |dz  t        |	|z  |
|z  z         z        z  }t        j                  |dkD  |      t        j
                  |	t        j
                  ||z  ||z        z  t        t        j                  |            z  |dz  t        ||z  |
|z  z         z        z  }t        j                  |dkD  |      t        j
                  |
t        j
                  ||z  ||z        z  t        t        j                  |            z  |dz  t        |	|z  ||z  z         z        z  }dt        j                  |dkD  |      |z  dz  ||z
  z  t        t        j                  |            z  t        t        j                  |            z  z  }dt        j                  |dkD  |      |	z  |z  ||z
  z  t        t        j                  ||z
              z  z  }dt        j                  |dkD  |      |	z  |z  dz  t        ||z
        z  z  }dt        j                  |dkD  |      |
z  |z  ||z
  z  t        t        j                  ||z
              z  z  }dt        j                  |dkD  |      |
z  |z  t        ||z
        z  z  }||z   |z   |z   |z   }|dz  }dt        j                  |dkD  |      |	z  dz  ||z
  z  t        t        j                  |            z  t        t        j                  |            z  z  }dt        j                  |dkD  |      |z  |z  ||z
  z  t        t        j                  ||z
              z  z  }dt        j                  |dkD  |      |z  |z  t        ||z
        z  z  }dt        j                  |dkD  |      |
z  |z  ||z
  z  t        t        j                  ||z
              z  z  }dt        j                  |dkD  |      |
z  |z  dz  t        ||z
        z  z  }||z   |z   |z   |z   }|dz  }dt        j                  |dkD  |      |
z  dz  ||z
  z  t        t        j                  |            z  t        t        j                  |            z  z  } dt        j                  |dkD  |      |z  |z  ||z
  z  t        t        j                  ||z
              z  z  }!dt        j                  |dkD  |      |z  |z  dz  t        ||z
        z  z  }"dt        j                  |dkD  |      |	z  |z  ||z
  z  t        t        j                  ||z
              z  z  }#dt        j                  |dkD  |      |	z  |z  t        ||z
        z  z  }$| |!z   |"z   |#z   |$z   }%|%dz  }%t        j                  |d   |z  |d   |z  |d   |z  gd      }&t        j                  |d   |z  |d   |z  |d   |z  gd      }'t        j                  |d   |z  |d   |z  |d   |%z  gd      }(t        j
                  t        j
                  |&|'      |(      })|)S )	a6  The gradients for `rgb_to_hsv` operation.

  This function is a piecewise continuous function as defined here:
  https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
  We perform the multivariate derivative and compute all partial derivatives
  separately before adding them in the end. Formulas are given before each
  partial derivative calculation.

  Args:
    op: The `rgb_to_hsv` `Operation` that we are differentiating.
    grad: Gradient with respect to the output of the `rgb_to_hsv` op.

  Returns:
    Gradients with respect to the input of `rgb_to_hsv`.
  r   ).r   ).r   ).r'   <   ih  )axis)
r   outputsr0   r   r@   addrB   squarer   stack)*r
   r   redsgreensblues
saturationvaluer0   red_biggestgreen_biggestblue_biggestred_smallestgreen_smallestblue_smallestdv_drdv_dgdv_dbds_drds_dgds_dbdh_dr_1dh_dr_2dh_dr_3dh_dr_4dh_dr_5dh_drdh_dg_1dh_dg_2dh_dg_3dh_dg_4dh_dg_5dh_dgdh_db_1dh_db_2dh_db_3dh_db_4dh_db_5dh_dbdv_drgbds_drgbdh_drgbgradient_inputs*                                             r   _RGBToHSVGradrs      s   $ 
1f	$99Q<&
))A,v
%zz!}V$*
**Q-
%
))A,

%
 tu}&."#(*+--$eO!%&+--6> #$)+, u&= "#(*,==&D.e^"%&+-.--$VO!%&+-- %
%
% --q%
(8<<HLL&!8-%:OPP(//$/012>2C/L54HIJ3K,L L% --
E
*X\\hll<$#68MNN/012@22Et+u0DEF3G.H H% --	5
)HLLX\\.6"9<$;NOO(//%0123@23E/K$4FGH4I-J J%6 mmD1He${2R7~*8??:+FGH./01' mmFQJ&6Ev~*8??4&=+IJKL' mmFQJ&6FK'()' mmEAIu%4|Cv~*8??54<+HIJK' mmEAIu%4~E'()' G
g
%
/'
9% #+%. mmFQJ&6;t|()DEF./01' mmD1He${2^Ce|()GHIJ' mmD1He${2]Bu%&'' mmEAIu%4~Ee|()HIJK' mmEAIu%4|CbH%&'' G
g
%
/'
9% #+%, mmEAIu%4r9f})(//**EFG./01' mmD1He${2]B})(//$,*GHIJ' mmD1He${2^CbHv&'(' mmFQJ&6F})(//&5.*IJKL' mmFQJ&6E&'(' G
g
%
/'
9% #+% !!F|eT&\E14<%3GH' !!F|eT&\E14<%3GH' !!F|eT&\E14<%3GH' <<Wg >H.	r   N)__doc__tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r   RegisterGradient	Operationr   r#   NotDifferentiabler+   r3   r=   rB   rs    r   r   <module>r{      s8   1 . + + 1 / * -.3==  /0 &'CMM  ($   * + )*# +#* o&3==  '* o&'$3== '$ ''$T= j!Rcmm R "Rr   