o
    ®?eŠ9  ć                   @   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_opsZResizeNearestNeighborc                 C   sh   | j d }| ” dd  ” r| ” dd }n	t |”dd }tj|||  d”|  d”d}|dgS )zįThe 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   Zresize_nearest_neighbor_gradŚget_attr)ŚopŚgradŚimageŚimage_shapeZgrads© r   śa/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/ops/image_grad.pyŚ_ResizeNearestNeighborGrad   s   
ür   ZResizeBilinearc                 C   s,   t j|| jd |  d”|  d”d}|dgS )zĆThe 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   Zresize_bilinear_gradr   r   ©r   r   Śgrad0r   r   r   Ś_ResizeBilinearGrad2   s   ür   ZScaleAndTranslatec              	   C   s@   t j|| jd | jd | jd |  d”|  d”d}|dddgS )zŲThe 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   Zscale_and_translate_gradr   r   r   r   r   r   Ś_ScaleAndTranslateGradE   s   śr   ZResizeBicubicc                 C   sL   t jt jg}d}| jd j|v r"tj|| jd |  d”|  d”d}|dgS )zĮThe 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   Zresize_bicubic_gradr   )r   r   Śallowed_typesr   r   r   r   Ś_ResizeBicubicGrad[   s   ür$   Z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%   r&   N)r   r   r   Śas_listr   r   r   Zfloat16r    r!   r"   r   Zcrop_and_resize_grad_imager   Zcrop_and_resize_grad_boxes)r   r   r   r   r#   r   Zgrad1r   r   r   Ś_CropAndResizeGradq   s   

ž’r(   c                 C   s   t  t  d| j”| ”S )zżWrapper 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   Z
div_no_nanŚcastr"   )Śxr   r   r   Ś_CustomReciprocal   s   r+   Z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  )Zaxis)
r   Zoutputsr"   r   r)   Śaddr+   Zsquarer   Śstack)*r   r   ZredsZgreensZbluesZ
saturationŚvaluer"   Zred_biggestZgreen_biggestZblue_biggestZred_smallestZgreen_smallestZblue_smallestZdv_drZdv_dgZdv_dbZds_drZds_dgZds_dbZdh_dr_1Zdh_dr_2Zdh_dr_3Zdh_dr_4Zdh_dr_5Zdh_drZdh_dg_1Zdh_dg_2Zdh_dg_3Zdh_dg_4Zdh_dg_5Zdh_dgZdh_db_1Zdh_db_2Zdh_db_3Zdh_db_4Zdh_db_5Zdh_dbZdv_drgbZds_drgbZdh_drgbZgradient_inputr   r   r   Ś_RGBToHSVGrad§   sl  
’’
’’
’’
’’
’’
’’
’’ž’’ž’’ž’’ž’’’’
’’’’’
’’’’ž’’’’
’’’’’
’’’’ž’’’’
’’’’’
’’ ž ž žr1   N)Ś__doc__Ztensorflow.python.frameworkr   r   Ztensorflow.python.opsr   r   r   r   ZRegisterGradientr   r   r   r$   r(   r+   r1   r   r   r   r   Ś<module>   s(   




*