o
    i e9                     @   s   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	dd	d
 Z
e	ddd Ze	ddd Ze	ddd Ze	ddd Zdd Ze	ddd ZdS )z*Contains Gradient functions for image ops.    )dtypes)ops)	array_ops)array_ops_stack)gen_image_ops)math_opsResizeNearestNeighborc                 C   sh   | j d }| dd  r| dd }n	t|dd }tj||| d| d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)opgradimageimage_shapegrads r   O/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/ops/image_grad.py_ResizeNearestNeighborGrad   s   
r   ResizeBilinearc                 C   s,   t j|| jd | d| d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   grad0r   r   r   _ResizeBilinearGrad2   s   r    ScaleAndTranslatec              	   C   s@   t j|| jd | jd | jd | d| d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   r   r   r   _ScaleAndTranslateGradE   s   r&   ResizeBicubicc                 C   sL   t jt jg}d}| jd j|v r"tj|| jd | d| d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   r   r   r   _ResizeBicubicGrad[   s   r-   CropAndResizec              	   C   s   | j d }|  r|  }nt|}tjtjtj	g}| j d j
|v r?tj|| j d | j d || d| dd}nd}t|| 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)r/   r0   N)r   r   r   as_listr   r   r   float16r(   r)   r*   r   crop_and_resize_grad_imager   crop_and_resize_grad_boxes)r   r   r   r   r,   r   grad1r   r   r   _CropAndResizeGradq   s   

r6   c                 C   s   t t 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castr*   )xr   r   r   _CustomReciprocal   s   r:   RGBToHSVc           *   	   C   s  | j d d }| j d d }| j d d }| jd d }| jd d }| j d j}t||k||k@ |}t||k||k@ |}	t||k||k@ |}
t||k ||k @ |}t||k||k @ |}t||k||k@ |}|}|	}|
}t|dk|t|t|| ||  tt| |d t|	| |
|    }t|dk|t|	t|| ||  tt| |d t|| |
|    }t|dk|t|
t|| ||  tt| |d t|	| ||    }dt|dk|| d ||  tt| tt|  }dt|dk||	 | ||  tt||   }dt|dk||	 | d t||   }dt|dk||
 | ||  tt||   }dt|dk||
 | t||   }|| | | | }|d }dt|dk||	 d ||  tt| tt|  }dt|dk|| | ||  tt||   }dt|dk|| | t||   }dt|dk||
 | ||  tt||   }dt|dk||
 | d t||   }|| | | | }|d }dt|dk||
 d ||  tt| tt|  } dt|dk|| | ||  tt||   }!dt|dk|| | d t||   }"dt|dk||	 | ||  tt||   }#dt|dk||	 | t||   }$| |! |" |# |$ }%|%d }%tj	|d | |d | |d | gdd}&tj	|d | |d | |d | gdd}'tj	|d | |d | |d |% gdd}(tt|&|'|(})|)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   outputsr*   r   r8   addr:   squarer   stack)*r   r   redsgreensblues
saturationvaluer*   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_inputr   r   r   _RGBToHSVGrad   sl  












   rj   N)__doc__tensorflow.python.frameworkr   r   tensorflow.python.opsr   r   r   r   RegisterGradientr   r    r&   r-   r6   r:   rj   r   r   r   r   <module>   s(   




*