o
    "j0\                     @   sr   d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZ g Zd
ddZe	 d
dd	ZdS )    N)OrderedDict)nn)no_grad)	InputSpecc                    s  |du r|du rt d|du ra|durat|rt|j}nBt|ttfr6g }|D ]
}|t|j q*n+t|trOg }|	 D ]}|t|| j qAnt|tj
jjr]t|j}nt dt|trlt|j}nCt|trg }|D ]-}t|tr|f}t|ttfsJ dt| t|tr|t|j qu|| qunt|tr|f}n|}t std d}n| j}|r|   dd d	d
  fdd  |}t| |||\}	}
t|	 |r|   |
S )a)  Prints a string summary of the network.

    Args:
        net (Layer): The network which must be a subinstance of Layer.
        input_size (tuple|InputSpec|list[tuple|InputSpec], optional): Size of input tensor. if model only
                    have one input, input_size can be tuple or InputSpec. if model
                    have multiple input, input_size must be a list which contain
                    every input's shape. Note that input_size only dim of
                    batch_size can be None or -1. Default: None. Note that
                    input_size and input cannot be None at the same time.
        dtypes (str, optional): If dtypes is None, 'float32' will be used, Default: None.
        input (Tensor, optional): If input is given, input_size and dtype will be ignored, Default: None.

    Returns:
        Dict: A summary of the network including total params and total trainable params.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import paddle.nn as nn
            >>> paddle.seed(2023)
            >>> class LeNet(nn.Layer):
            ...     def __init__(self, num_classes=10):
            ...         super().__init__()
            ...         self.num_classes = num_classes
            ...         self.features = nn.Sequential(
            ...             nn.Conv2D(1, 6, 3, stride=1, padding=1),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2),
            ...             nn.Conv2D(6, 16, 5, stride=1, padding=0),
            ...             nn.ReLU(),
            ...             nn.MaxPool2D(2, 2))
            ...
            ...         if num_classes > 0:
            ...             self.fc = nn.Sequential(
            ...                 nn.Linear(400, 120),
            ...                 nn.Linear(120, 84),
            ...                 nn.Linear(84, 10))
            ...
            ...     def forward(self, inputs):
            ...         x = self.features(inputs)
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x)
            ...         return x
            ...
            >>> lenet = LeNet()

            >>> params_info = paddle.summary(lenet, (1, 1, 28, 28))
            >>> print(params_info)
            ---------------------------------------------------------------------------
            Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
              Conv2D-1       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-1        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-1     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
              Conv2D-2       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-2       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-2    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
              Linear-1          [[1, 400]]            [1, 120]           48,120
              Linear-2          [[1, 120]]            [1, 84]            10,164
              Linear-3          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            {'total_params': 61610, 'trainable_params': 61610}
            >>> # multi input demo
            >>> class LeNetMultiInput(LeNet):
            ...     def forward(self, inputs, y):
            ...         x = self.features(inputs)
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + y)
            ...         return x
            ...
            >>> lenet_multi_input = LeNetMultiInput()

            >>> params_info = paddle.summary(lenet_multi_input,
            ...                              [(1, 1, 28, 28), (1, 400)],
            ...                              dtypes=['float32', 'float32'])
            >>> print(params_info)
            ---------------------------------------------------------------------------
            Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
              Conv2D-3       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-3        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-3     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
              Conv2D-4       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-4       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-4    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
              Linear-4          [[1, 400]]            [1, 120]           48,120
              Linear-5          [[1, 120]]            [1, 84]            10,164
              Linear-6          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            {'total_params': 61610, 'trainable_params': 61610}
            >>> # list input demo
            >>> class LeNetListInput(LeNet):
            ...     def forward(self, inputs):
            ...         x = self.features(inputs[0])
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + inputs[1])
            ...         return x
            ...
            >>> lenet_list_input = LeNetListInput()
            >>> input_data = [paddle.rand([1, 1, 28, 28]), paddle.rand([1, 400])]
            >>> params_info = paddle.summary(lenet_list_input, input=input_data)
            >>> print(params_info)
            ---------------------------------------------------------------------------
            Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
              Conv2D-5       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-5        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-5     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
              Conv2D-6       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-6       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-6    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
              Linear-7          [[1, 400]]            [1, 120]           48,120
              Linear-8          [[1, 120]]            [1, 84]            10,164
              Linear-9          [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            {'total_params': 61610, 'trainable_params': 61610}
            >>> # dict input demo
            >>> class LeNetDictInput(LeNet):
            ...     def forward(self, inputs):
            ...         x = self.features(inputs['x1'])
            ...
            ...         if self.num_classes > 0:
            ...             x = paddle.flatten(x, 1)
            ...             x = self.fc(x + inputs['x2'])
            ...         return x
            ...
            >>> lenet_dict_input = LeNetDictInput()
            >>> input_data = {'x1': paddle.rand([1, 1, 28, 28]),
            ...               'x2': paddle.rand([1, 400])}
            >>> params_info = paddle.summary(lenet_dict_input, input=input_data)
            >>> print(params_info)
            ---------------------------------------------------------------------------
            Layer (type)       Input Shape          Output Shape         Param #
            ===========================================================================
              Conv2D-7       [[1, 1, 28, 28]]      [1, 6, 28, 28]          60
                ReLU-7        [[1, 6, 28, 28]]      [1, 6, 28, 28]           0
              MaxPool2D-7     [[1, 6, 28, 28]]      [1, 6, 14, 14]           0
              Conv2D-8       [[1, 6, 14, 14]]     [1, 16, 10, 10]         2,416
                ReLU-8       [[1, 16, 10, 10]]     [1, 16, 10, 10]           0
              MaxPool2D-8    [[1, 16, 10, 10]]      [1, 16, 5, 5]            0
              Linear-10         [[1, 400]]            [1, 120]           48,120
              Linear-11         [[1, 120]]            [1, 84]            10,164
              Linear-12         [[1, 84]]             [1, 10]              850
            ===========================================================================
            Total params: 61,610
            Trainable params: 61,610
            Non-trainable params: 0
            ---------------------------------------------------------------------------
            Input size (MB): 0.00
            Forward/backward pass size (MB): 0.11
            Params size (MB): 0.24
            Estimated Total Size (MB): 0.35
            ---------------------------------------------------------------------------
            {'total_params': 61610, 'trainable_params': 61610}

    Nz4input_size and input cannot be None at the same timezcInput is not tensor, list, tuple and dict, unable to determine input_size, please input input_size.z`When input_size is list,             expect item in input_size is a tuple or InputSpec, but got zZYour model was created in static graph mode, this may not get correct summary information!Fc                 S   s"   | D ]}t |ttfr dS qdS NFT
isinstancelisttuple)shapeitem r   Z/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/hapi/model_summary.py	_is_shape  s
   zsummary.<locals>._is_shapec                 S   s   d}g }t t| D ]3}| | }|d u s|dkr'|d7 }|dkr$tdd}nt|tjr8|dkr8td| || q
t|S )Nr      z?Option input_size only the dim of batch_size can be None or -1.z:Expected element in input size greater than zero, but got )rangelen
ValueErrorr   numbersNumberappendr
   )r   Znum_unknownZ	new_shapeir   r   r   r   _check_shape  s$   zsummary.<locals>._check_shapec                    s0   t | ttfr| r| S  fdd| D S )Nc                       g | ]} |qS r   r   .0r   )_check_inputr   r   
<listcomp>0      z1summary.<locals>._check_input.<locals>.<listcomp>r   )
input_sizer   r   r   r   r   r   ,  s   zsummary.<locals>._check_input)r   paddleZ	is_tensorr
   r   r   r	   r   dictkeysbaseZ	frameworkVariabler   inttypein_dynamic_modewarningswarnZtrainingevalsummary_stringprinttrain)netr    dtypesinputxkeyZ_input_sizer   Zin_train_moderesultZparams_infor   r!   r   summary   sv    A






r6   c                    sf  dd   fddt |ttfs||}d}d}tt fddfd	d
	fdd}t |trB|g} fddt 	g | |d ur^|}| n	||}|  D ]}|  qidd }	|		}
|d|
d  d 7 }dd|
d d|
d d|
d d|
d }||d 7 }|d|
d  d 7 }d}d}d}d}	D ]y}d||
d t		| d |
d t		| d  |
d d!	| d" |
d }|	| d" 7 }z|t
t
j	| d  d#d$7 }W n   	| d  D ]}|t
t
j|d#d$7 }qY d%	| v r#	| d% r#|	| d& 7 }||d 7 }q fd'd(|d}td)| d* d+ }t|d* d+ }|| | }|d|
d  d 7 }|d,|d-d 7 }|d.|d-d 7 }|d/|| d-d 7 }|d|
d  d 7 }|d0| d 7 }|d1| d 7 }|d2| d 7 }|d3| d 7 }|d|
d  d 7 }|||d4fS )5Nc                 S   s    | D ]}t |tjs dS qdS r   )r   r   r   )itemsr   r   r   r   _all_is_numper?  s
   z&summary_string.<locals>._all_is_numperc                    s<    d u rd t | ttfr| r gS  fdd| D S )NZfloat32c                       g | ]} |qS r   r   r   )_build_dtypesdtyper   r   r   L      z9summary_string.<locals>._build_dtypes.<locals>.<listcomp>r   )r    r;   )r8   r:   )r;   r   r:   E  s
   z%summary_string.<locals>._build_dtypesr    c                    sH   t | tjjtjjjjfrt| jS t | tt	fr" fdd| D S d S )Nc                    r   r   r   )r   xx_get_shape_from_tensorr   r   r   [  r   zBsummary_string.<locals>._get_shape_from_tensor.<locals>.<listcomp>)
r   r"   r%   r&   coreeagerZTensorr	   r   r
   )r3   r?   r   r   r@   W  s
   
z.summary_string.<locals>._get_shape_from_tensorc                    sD   t | ttfr fdd| D }|S t| drt| j}|S g }|S )Nc                    r   r   r   )r   o_get_output_shaper   r   r   _  r   z=summary_string.<locals>._get_output_shape.<locals>.<listcomp>r   )r   r	   r
   hasattrr   )outputoutput_shaperD   r   r   rE   ]  s   

z)summary_string.<locals>._get_output_shapec                    sx    fdd}t | tjs&t | tjs&| krdk r&| | d S t| dr8| jr:| | d S d S d S )Nc                    s  t | jdd dd }zt| jdd }W n   t}Y d||d f }t |< z
|| d< W n   td	 g | d< Y z
 || d
< W n   td | d
  Y d}t	
 ro| j}n|  }d| d< d}| D ]D\}	}
|t|
j7 }z-t| |	jrt| |	js| d  t|
j7  < d| d< d}n|sd| d< W q   d| d< Y q|| d< d S )N.r   'r   _z%s-%ir   input_shapez Get layer {} input shape failed!rH   z!Get layer {} output shape failed!trainable_paramsFT	trainable	nb_params)str	__class__splitr'   Z
_full_namer   r   r*   r+   r"   r)   _parametersZ
state_dictr7   npprodr   getattrrN   Zstop_gradient)layerr2   rG   
class_nameZ	layer_idxZm_keyparamsZlayer_state_dictZtrainable_flagkv)rE   r@   r6   r   r   hookg  sL   




z3summary_string.<locals>.register_hook.<locals>.hookr   could_use_cudnn)r   r   Z
SequentialZ	LayerListr   Zregister_forward_post_hookrF   r]   )rW   r\   )rE   r@   depthhooksmodelr6   r   r   register_hookf  s   
2
z%summary_string.<locals>.register_hookc                    s`   t | ttfr$ | r$t |ttfr|d }n|}ttt| |S fddt| |D S )Nr   c                    s   g | ]	\}} ||qS r   r   )r   r   r;   )build_inputr   r   r     s    z7summary_string.<locals>.build_input.<locals>.<listcomp>)r   r	   r
   r"   castZrandzip)r    r1   r;   )r8   rb   r   r   rb     s   

z#summary_string.<locals>.build_inputc                 S   s   dddddd}| D ]b}|d t t| | d k r&t t| | d |d< |d t t| | d k r@t t| | d |d< |d	 t t|k rRt t||d	< |d
 t t| | d k rlt t| | d |d
< q
d}| D ]\}}|dkr||7 }qs|d |d k r|d |d< |S )N      K   )layer_widthinput_shape_widthoutput_shape_widthparams_widthtable_widthrj   rH   ri   rL   rh   rk   rO   r   rl      )r   rP   r7   )r6   Zhead_lengthrW   Z_temp_widthrZ   r[   r   r   r   _get_str_length  sH   z'summary_string.<locals>._get_str_length-rl   
z{:^{}} {:^{}} {:^{}} {:^{}}zLayer (type)rh   zInput Shaperi   zOutput Shaperj   zParam #rk   =r   rL   rH   z{:,}rO   r   )ZaxisrN   rM   c                    sL   t | ttfr| rtt| d d   S t fdd| D   S )N      @      0Ac                    r9   r   r   r   )_get_input_sizesizer   r   r     r<   z;summary_string.<locals>._get_input_size.<locals>.<listcomp>)r   r	   r
   absrT   rU   sum)r    ru   )r8   rt   )ru   r   rt     s
   z'summary_string.<locals>._get_input_sizeg       @rr   rs   zTotal params: ,zTrainable params: zNon-trainable params: zInput size (MB): %0.2fz&Forward/backward pass size (MB): %0.2fzParams size (MB): %0.2fz Estimated Total Size (MB): %0.2f)total_paramsrM   )r   r	   r
   r   Z	sublayersr   applyremoveformatrP   rT   rw   rU   rv   )r`   r    r1   r2   Z
batch_sizeZsummary_strra   r3   hrn   rl   Zline_newry   Ztotal_outputrM   
max_lengthrW   rH   Ztotal_input_sizeZtotal_output_sizeZtotal_params_sizeZ
total_sizer   )
r8   r:   rt   rE   r@   rb   r^   r_   r`   r6   r   r-   =  s   	
	
<



)




r-   )NNN)r   r*   collectionsr   numpyrT   r"   r   Zpaddle.autogradr   Zpaddle.staticr   __all__r6   r-   r   r   r   r   <module>   s   
  "