
    |h%                     2    d dl Zd dlmZ dZ G d de      Zy)    N)LooseVersionu   Iván de Paz Centenoc                       e Zd ZdZdZd Zed        Zedededefd       Z	ed	        Z
d
edefdZd
edefdZ	 	 dd
ededededededededefdZdd
edefdZ	 	 dd
edededefdZdd
ededefdZddefdZy) LayerFactoryz<
    Allows to create stack layers for a given network.
    )SAMEVALIDc                     || _         y N)_LayerFactory__network)selfnetworks     R/var/www/html/test/engine/venv/lib/python3.12/site-packages/mtcnn/layer_factory.py__init__zLayerFactory.__init__'   s	         c                 \    | t         j                  vrt        dj                  |             y )NzPadding {} not valid)r   AVAILABLE_PADDINGS	Exceptionformat)paddings    r   __validate_paddingzLayerFactory.__validate_padding*   s,    ,999299'BCC :r   channels_inputchannels_outputgroupc                 P    | |z  dk7  rt        d      ||z  dk7  rt        d      y )Nr   z<The number of channels in the input does not match the groupz=The number of channels in the output does not match the group)r   )r   r   r   s      r   __validate_groupingz LayerFactory.__validate_grouping/   s9    E!Q&Z[[U"a'[\\ (r   c                     | j                         }|j                  dk(  rDd}|dd  j                         D ]  }|t        |      z  } t	        j
                  | d|g      }||fS | |d   }}||fS )N      )	get_shapendimsas_listinttfreshape)input_layerinput_shapedimxvectorized_inputs        r   vectorize_inputzLayerFactory.vectorize_input7   s    !++-!C _,,. s1v  "zz+CyA  $$ &1+b/c$$r   nameshapec                     t         j                  j                  j                  ||| j                  j                         d      S )z
        Creates a tensorflow variable with the given name and shape.
        :param name: name to set for the variable.
        :param shape: list defining the shape of the variable.
        :return: created TF variable.
        F)	trainableuse_resource)r#   compatv1get_variabler
   is_trainable)r   r+   r,   s      r   
__make_varzLayerFactory.__make_varH   s:     yy||((u@[@[@]6; ) = 	=r   layer_shapec                     t         j                  j                  j                  t         j                  |d      }| j
                  j                  ||       y)z
        Creates a feed layer. This is usually the first layer in the network.
        :param name: name of the layer
        :return:
        inputlayer_outputN)r#   r0   r1   placeholderfloat32r
   	add_layer)r   r+   r5   	feed_datas       r   new_feedzLayerFactory.new_feedR   s;     IILL,,RZZgN	  I >r   Nkernel_sizestride_sizer   biasedreluinput_layer_namec
                    | j                         | j                  j                  |	      }
t        |
j	                         d         }| j                  |||       fd}t        j                  j                  j                  |      5 }| j                  d|d   |d   ||z  |g      } ||
|      }|r3| j                  d|g      }t        j                  j                  ||      }|r+t        j                  j                  ||j                        }d	d	d	       | j                  j                  |
       y	# 1 sw Y   'xY w)uA  
        Creates a convolution layer for the network.
        :param name: name for the layer
        :param kernel_size: tuple containing the size of the kernel (Width, Height)
        :param channels_output: ¿? Perhaps number of channels in the output? it is used as the bias size.
        :param stride_size: tuple containing the size of the stride (Width, Height)
        :param padding: Type of padding. Available values are: ('SAME', 'VALID')
        :param group: groups for the kernel operation. More info required.
        :param biased: boolean flag to set if biased or not.
        :param relu: boolean flag to set if ReLu should be applied at the end of the layer or not.
        :param input_layer_name: name of the input layer for this layer. If None, it will take the last added layer of
        the network.
        r   c                 ^    t         j                  j                  | |dd   d   dg      S )Nr   r   )r7   filtersstridesr   )r#   nnconv2d)	input_valkernelr   r@   s     r   <lambda>z'LayerFactory.new_conv.<locals>.<lambda>y   s5    RUU\\	KNKNA> .: .! r   weightsr   r   r,   biasesr+   Nr8   )_LayerFactory__validate_paddingr
   	get_layerr"   r    _LayerFactory__validate_groupingr#   r0   r1   variable_scope_LayerFactory__make_varrH   bias_addrB   r+   r<   )r   r+   r?   r   r@   r   r   rA   rB   rC   r%   r   convolvescoperK   outputrO   s       ``           r   new_convzLayerFactory.new_conv\   s.   $ 	(nn../?@ [224R89 	  %H!
 YY\\((. 	=%__Y{1~{ST~WeinWnp  7A_  BFk62F O3DE7 F<	= 	  F ;	= 	=s   BD??Ec                    | j                   j                  |      }t        j                  j                  j                  |      5  t        |j                         d         }| j                  d|g      }t        j                  j                  |      t        j                  |t        j                  j                  |              z   }ddd       | j                   j                  |       y# 1 sw Y   'xY w)z
        Creates a new prelu layer with the given name and input.
        :param name: name for this layer.
        :param input_layer_name: name of the layer that serves as input for this one.
        r   alpharN   Nr8   )r
   rR   r#   r0   r1   rT   r"   r   rU   rH   rB   multiplyr<   )r   r+   rC   r%   r   r\   rY   s          r   	new_preluzLayerFactory.new_prelu   s     nn../?@YY\\((. 	] !6!6!8!<=NOOGN3CODEUUZZ,r{{5255::{lC[B[/\\F	]
 	  F ;	] 	]s   BC11C:c                    | j                  |       | j                  j                  |      }t        j                  j                  |d|d   |d   dgd|d   |d   dg||      }| j                  j                  ||       y)a  
        Creates a new max pooling layer.
        :param name: name for the layer.
        :param kernel_size: tuple containing the size of the kernel (Width, Height)
        :param stride_size: tuple containing the size of the stride (Width, Height)
        :param padding: Type of padding. Available values are: ('SAME', 'VALID')
        :param input_layer_name: name of the input layer for this layer. If None, it will take the last added layer of
        the network.
        r   r   )r7   ksizerG   r   r+   r8   N)rQ   r
   rR   r#   rH   
max_pool2dr<   )r   r+   r?   r@   r   rC   r%   rY   s           r   new_max_poolzLayerFactory.new_max_pool   s     	(nn../?@!!'(+a.+a.!&L)*KNKNA(N(/%)	 " + 	  F ;r   output_countc                 >   t         j                  j                  j                  |      5  | j                  j                  |      }| j                  |      \  }}| j                  d||g      }| j                  d|g      }	|r.t         j                  j                  j                  j                  n-t         j                  j                  j                  j                  }
 |
|||	|      }ddd       | j                  j                  |       y# 1 sw Y   'xY w)a  
        Creates a new fully connected layer.

        :param name: name for the layer.
        :param output_count: number of outputs of the fully connected layer.
        :param relu: boolean flag to set if ReLu should be applied at the end of this layer.
        :param input_layer_name: name of the input layer for this layer. If None, it will take the last added layer of
        the network.
        rM   rN   rO   rP   Nr8   )r#   r0   r1   rT   r
   rR   r*   rU   rH   
relu_layer	xw_plus_br<   )r   r+   rc   rB   rC   r%   r)   	dimensionrM   rO   	operationfcs               r   new_fully_connectedz LayerFactory.new_fully_connected   s     YY\\((. 	I..223CDK*.*>*>{*K'iooi	<7PoQG__Xl^_DF6:		22		@Y@YI+Wf4HB	I 	  B 7	I 	Is   CDDc                 "   | j                   j                  |      }t        t        j                        t        d      k  rIt        j
                  ||d      }t        j                  ||z
        }t        j                  ||d      }nHt        j
                  ||d      }t        j                  ||z
        }t        j                  ||d      }t        j                  j                  |||      }| j                   j                  ||       y)z
        Creates a new softmax layer
        :param name: name to set for the layer
        :param axis:
        :param input_layer_name: name of the input layer for this layer. If None, it will take the last added layer of
        the network.
        z1.5.0T)input_tensoraxiskeepdimsr8   N)r
   rR   r   r#   __version__
reduce_maxexp
reduce_summathdivider<   )	r   r+   rm   rC   r%   max_axis
target_exp	normalizesoftmaxs	            r   new_softmaxzLayerFactory.new_softmax   s     nn../?@',w*??}}+DSWXHh 67J:DSWXI}}+DSWXHh 67J:DSWXI''..Y=  G <r   )r   r   TTNr	   )r   N)TN)__name__
__module____qualname____doc__r   r   staticmethodrQ   r"   rS   r*   strlistrU   tupler>   boolrZ   r^   rb   rj   ry    r   r   r   r       sK    +! D D ]C ]# ]c ] ] % % =s =4 =?S ?u ? 39Y]1<S 1<u 1<s 1<#1<.11<1<'+1<8<1<UX1<f<c <S < V\+/< <% <e <'*<08 83 8]` 8.= =r   r   )
tensorflowr#   distutils.versionr   
__author__objectr   r   r   r   <module>r      s    4  *#
B=6 B=r   