o
    ॵik,                     @   s   d Z ddlZddlZejdkrddlZnddlmZ ejdkr#ejj	ZG dd de
dg dZddd	ZdddZej		dddZ				dddZdS )a  Contains building blocks for various versions of Residual Networks.
Residual networks (ResNets) were proposed in:
  Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
  Deep Residual Learning for Image Recognition. arXiv:1512.03385, 2015
More variants were introduced in:
  Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
  Identity Mappings in Deep Residual Networks. arXiv: 1603.05027, 2016
We can obtain different ResNet variants by changing the network depth, width,
and form of residual unit. This module implements the infrastructure for
building them. Concrete ResNet units and full ResNet networks are implemented in
the accompanying resnet_v1.py and resnet_v2.py modules.
Compared to https://github.com/KaimingHe/deep-residual-networks, in the current
implementation we subsample the output activations in the last residual unit of
each block, instead of subsampling the input activations in the first residual
unit of each block. The two implementations give identical results but our
implementation is more memory efficient.
    Nz2.0)slimc                   @   s   e Zd ZdZdS )Blocka  A named tuple describing a ResNet block.
    Its parts are:
      scope: The scope of the `Block`.
      unit_fn: The ResNet unit function which takes as input a `Tensor` and
        returns another `Tensor` with the output of the ResNet unit.
      args: A list of length equal to the number of units in the `Block`. The list
        contains one (depth, depth_bottleneck, stride) tuple for each unit in the
        block to serve as argument to unit_fn.
    N)__name__
__module____qualname____doc__ r   r   b/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/ocr_utils/resnet_utils.pyr   .   s    r   )scopeunit_fnargsc                 C   s"   |dkr| S t j| ddg||dS )a  Subsamples the input along the spatial dimensions.
    Args:
      inputs: A `Tensor` of size [batch, height_in, width_in, channels].
      factor: The subsampling factor.
      scope: Optional variable_scope.
    Returns:
      output: A `Tensor` of size [batch, height_out, width_out, channels] with the
        input, either intact (if factor == 1) or subsampled (if factor > 1).
       )strider
   )r   
max_pool2d)inputsfactorr
   r   r   r	   	subsample:   s   
r   r   c           
   	   C   s   |dkrt j| ||d|d|dS ||d |d   }|d }|d }|| }	t| ddg||	g||	gddgg} t j| ||||d|dS )a4  Strided 2-D convolution with 'SAME' padding.
    When stride > 1, then we do explicit zero-padding, followed by conv2d with
    'VALID' padding.
    Note that
       net = conv2d_same(inputs, num_outputs, 3, stride=stride)
    is equivalent to
       net = slim.conv2d(inputs, num_outputs, 3, stride=1, padding='SAME')
       net = subsample(net, factor=stride)
    whereas
       net = slim.conv2d(inputs, num_outputs, 3, stride=stride, padding='SAME')
    is different when the input's height or width is even, which is why we add the
    current function. For more details, see ResnetUtilsTest.testConv2DSameEven().
    Args:
      inputs: A 4-D tensor of size [batch, height_in, width_in, channels].
      num_outputs: An integer, the number of output filters.
      kernel_size: An int with the kernel_size of the filters.
      stride: An integer, the output stride.
      rate: An integer, rate for atrous convolution.
      scope: Scope.
    Returns:
      output: A 4-D tensor of size [batch, height_out, width_out, channels] with
        the convolution output.
    r   SAME)r   ratepaddingr
      r   VALID)r   conv2dtfpad)
r   num_outputskernel_sizer   r   r
   kernel_size_effective	pad_totalpad_begpad_endr   r   r	   conv2d_sameJ   s4   	r!   c                 C   s*  d}d}|D ]}t |jd| gm t|jD ]_\}}|dur'||kr'tdt jd|d  | gd;}	|\}
}}|durP||krP|j| |
|d|d} ||9 }n|j| |
||dd} ||9 }tj	||	j
| } W d   n1 sqw   Y  qW d   n1 sw   Y  q|dur||krtd| S )a=  Stacks ResNet `Blocks` and controls output feature density.
    First, this function creates scopes for the ResNet in the form of
    'block_name/unit_1', 'block_name/unit_2', etc.
    Second, this function allows the user to explicitly control the ResNet
    output_stride, which is the ratio of the input to output spatial resolution.
    This is useful for dense prediction tasks such as semantic segmentation or
    object detection.
    Most ResNets consist of 4 ResNet blocks and subsample the activations by a
    factor of 2 when transitioning between consecutive ResNet blocks. This results
    to a nominal ResNet output_stride equal to 8. If we set the output_stride to
    half the nominal network stride (e.g., output_stride=4), then we compute
    responses twice.
    Control of the output feature density is implemented by atrous convolution.
    Args:
      net: A `Tensor` of size [batch, height, width, channels].
      blocks: A list of length equal to the number of ResNet `Blocks`. Each
        element is a ResNet `Block` object describing the units in the `Block`.
      output_stride: If `None`, then the output will be computed at the nominal
        network stride. If output_stride is not `None`, it specifies the requested
        ratio of input to output spatial resolution, which needs to be equal to
        the product of unit strides from the start up to some level of the ResNet.
        For example, if the ResNet employs units with strides 1, 2, 1, 3, 4, 1,
        then valid values for the output_stride are 1, 2, 6, 24 or None (which
        is equivalent to output_stride=24).
      outputs_collections: Collection to add the ResNet block outputs.
    Returns:
      net: Output tensor with stride equal to the specified output_stride.
    Raises:
      ValueError: If the target output_stride is not valid.
    r   blockNz+The target output_stride cannot be reached.zunit_%d)values)depthdepth_bottleneckr   r   )r   variable_scoper
   	enumerater   
ValueErrorr   r   utilscollect_named_outputsname)netblocksoutput_strideoutputs_collectionscurrent_strider   r"   iunitsc
unit_depthunit_depth_bottleneckunit_strider   r   r	   stack_blocks_dense|   sT   '

 r7   -C6?灕Cl?h㈵>Tc              
   C   s   |||t jjd}tjtjgt| t t jj	tj
|dU tjtj
gfi |1 tjtjgdd}|W  d   W  d   W  d   S 1 sOw   Y  W d   n1 s^w   Y  W d   dS W d   dS 1 svw   Y  dS )ax  Defines the default ResNet arg scope.
    TODO(gpapan): The batch-normalization related default values above are
      appropriate for use in conjunction with the reference ResNet models
      released at https://github.com/KaimingHe/deep-residual-networks. When
      training ResNets from scratch, they might need to be tuned.
    Args:
      weight_decay: The weight decay to use for regularizing the model.
      batch_norm_decay: The moving average decay when estimating layer activation
        statistics in batch normalization.
      batch_norm_epsilon: Small constant to prevent division by zero when
        normalizing activations by their variance in batch normalization.
      batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the
        activations in the batch normalization layer.
    Returns:
      An `arg_scope` to use for the resnet models.
    )decayepsilonscaleupdates_collections)weights_regularizerweights_initializeractivation_fnnormalizer_fnnormalizer_paramsr   )r   N)r   	GraphKeys
UPDATE_OPSr   	arg_scoper   l2_regularizervariance_scaling_initializernnrelu
batch_normr   )weight_decaybatch_norm_decaybatch_norm_epsilonbatch_norm_scalebatch_norm_paramsarg_scr   r   r	   resnet_arg_scope   s0   "rR   )N)r   N)NN)r8   r9   r:   T)r   collections
tensorflowr   __version__tf_slimr   tensorflow.contribcompatv1
namedtupler   r   r!   add_arg_scoper7   rR   r   r   r   r	   <module>   s(   




2R