o
    پi                     @   sl	  d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlZddlmZ ddlm  mZ ddlmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ dd	l m!Z! dd
l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 ddgZ6G dd dej7Z8G dd dej7Z9dde:de;de8fddZ<	dde:de=de;de8fddZ>				dde:de=de=dee? de;de8fddZ@dde:de=de;de8fd d!ZAdde:de=de;de8fd"d#ZB			dde:de=dee? de;de8f
d$d%ZCdd'e:dee:ef fd(d)ZDe3i d*eDd&d+d,eDd-d.d/d0d1eDd/d-eed2d3dd4d5eDd6d7d8d9d:d;d/d<d=eDd>d/d9d:d6d7d8d?d@dAeDd/eed3d2dBdCddDdEeDdFd/d-dGdHeDdId/d-dGdJeDdKd/d-dGdLeDdMd/d-dGdNeDdOd/eedPdQeDdRd/eedPdSeDdTd/eedPdUeDdVd/eedPdWeDdXd/eedPdYeDdZd/eedPd[eDd\d/d3d2d]i d^eDd_d/d3d2d]d`eDdad/dbdcd2dBdddeeD dfeDdgd/d-dGdheDdid/d-dGdjeDdkd/d-dGdleD dmeDeed3d2d-dndoeDd/eed3d2d-dpdqeDd/d3d2d-drdseDd/d3d2d-drdteDd/eed3dBd2dCdd-du	dveDd/d3dBd2dCdd-dwdxeDd/d2d3dd-dydzeDd/d{d|d2d-d}d~eDd/dd{d|dd-ddeDd/dd{d|dd-di deDd/dd{d|dd-ddeDd/d{d|d2ddd-dwdeDd/d3dBd2dCdd-dwdeDd/d3dBd2dCdd-dwdeDd/d3dBd2dCdd-dwdeDd/d{d|d2ddd-dwdeDd/d2d3dd-dydeDd/dd3dBd2dCdd-ddeDd/d{d|d2ddd-dwdeDd/d{d|d2ddd-dwdeDd3dBd2d-ddeDd/d2d3dd-dydeDd/ddd2ddd-dwdeDd/d{d|d2ddd-dwdeDd/d{d|d2ddd-dwdeDd/dd{d|d2ddd-ddeDd2d-ddeDd3dBd2d-diZEe4dde;de8fddZFe4dde;de8fddZGe4dde;de8fddZHe4dde;de8fddZIe4dde;de8fddZJe4dde;de8fddZKe4dde;de8fddZLe4dde;de8fddZMe4dde;de8fddZNe4dde;de8fddZOe4dde;de8fddZPe4dde;de8fddZQe4dde;de8fddZRe4dde;de8fddZSe4dde;de8fddZTe4dde;de8fddZUe4dde;de8fddZVe4dde;de8fddZWe4dde;de8fddZXe4dde;de8fddZYe4dde;de8fddĄZZe4dde;de8fddƄZ[e4dde;de8fddȄZ\e4dde;de8fddʄZ]e4dde;de8fdd̄Z^e4dde;de8fdd΄Z_e4dde;de8fddЄZ`e4dde;de8fdd҄Zae4dde;de8fddԄZbe4dde;de8fddքZce4dde;de8fdd؄Zde4dde;de8fddڄZee4dde;de8fdd܄Zfe5egd5d=dݜ dS )z MobileNet V3

A PyTorch impl of MobileNet-V3, compatible with TF weights from official impl.

Paper: Searching for MobileNetV3 - https://arxiv.org/abs/1905.02244

Hacked together by / Copyright 2019, Ross Wightman
    )partial)AnyDictCallableListOptionalTupleUnionN)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDIMAGENET_INCEPTION_MEANIMAGENET_INCEPTION_STD)SelectAdaptivePool2dLinear	LayerTypePadTypecreate_conv2dget_norm_act_layer   )build_model_with_cfgpretrained_cfg_for_features)SqueezeExcite)	BlockArgsEfficientNetBuilderdecode_arch_defefficientnet_init_weightsround_channelsresolve_bn_argsresolve_act_layerBN_EPS_TF_DEFAULT)FeatureInfoFeatureHooksfeature_take_indices)checkpoint_seq
checkpoint)generate_default_cfgsregister_modelregister_model_deprecationsMobileNetV3MobileNetV3Featuresc                '       s  e Zd ZdZddddddddd	d	d	d	ded
d
d	dfdededededededededede	e
 de	e
 de	e
 de	e
 dedededede	e def& fdd Zd!ejfd"d#ZejjdDd$ed!eeef fd%d&ZejjdEd'ed!d	fd(d)Zejjd!ejfd*d+ZdFdeded!d	fd,d-Z					.		dGd/ejd0e	eeee f  d1ed2ed3ed4ed5ed!eeej eejeej f f fd6d7Z	8			dHd0eeee f d9ed:ed5ed!ee f
d;d<Z d/ejd!ejfd=d>Z!dDd/ejd?ed!ejfd@dAZ"d/ejd!ejfdBdCZ#  Z$S )Ir(   a  MobileNetV3.

    Based on my EfficientNet implementation and building blocks, this model utilizes the MobileNet-v3 specific
    'efficient head', where global pooling is done before the head convolution without a final batch-norm
    layer before the classifier.

    Paper: `Searching for MobileNetV3` - https://arxiv.org/abs/1905.02244

    Other architectures utilizing MobileNet-V3 efficient head that are supported by this impl include:
      * HardCoRe-NAS - https://arxiv.org/abs/2102.11646 (defn in hardcorenas.py uses this class)
      * FBNet-V3 - https://arxiv.org/abs/2006.02049
      * LCNet - https://arxiv.org/abs/2109.15099
      * MobileNet-V4 - https://arxiv.org/abs/2404.10518
            F   T N        avg
block_argsnum_classesin_chans	stem_sizefix_stemnum_features	head_bias	head_normpad_type	act_layer
norm_layeraa_layerse_layerse_from_expround_chs_fn	drop_ratedrop_path_ratelayer_scale_init_valueglobal_poolc                    s  t t|   |
ptj}
|ptj}t||
}|pt}|| _|| _	d| _
|s)||}t||dd|	d| _||dd| _td|	|||
|||||d
}tj||| | _|j| _d	d
 | jD | _|j| _|| _t|d| _| j| j  }|rt|| jd|	d| _|| j| _t | _nt|| jd|	|d| _t | _|
dd| _|rtdnt | _|dkrt | j|nt | _!t"|  dS )a0  Initialize MobileNetV3.

        Args:
            block_args: Arguments for blocks of the network.
            num_classes: Number of classes for classification head.
            in_chans: Number of input image channels.
            stem_size: Number of output channels of the initial stem convolution.
            fix_stem: If True, don't scale stem by round_chs_fn.
            num_features: Number of output channels of the conv head layer.
            head_bias: If True, add a learnable bias to the conv head layer.
            head_norm: If True, add normalization to the head layer.
            pad_type: Type of padding to use for convolution layers.
            act_layer: Type of activation layer.
            norm_layer: Type of normalization layer.
            aa_layer: Type of anti-aliasing layer.
            se_layer: Type of Squeeze-and-Excite layer.
            se_from_exp: If True, calculate SE channel reduction from expanded mid channels.
            round_chs_fn: Callable to round number of filters based on depth multiplier.
            drop_rate: Dropout rate.
            drop_path_rate: Stochastic depth rate.
            layer_scale_init_value: Enable layer scale on compatible blocks if not None.
            global_pool: Type of pooling to use for global pooling features of the FC head.
        Fr+      stridepaddingTinplace    )
output_strider9   r?   r>   r:   r;   r<   r=   rA   rB   c                 S   s   g | ]}|d  qS )stage .0frM   rM   K/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/mobilenetv3.py
<listcomp>x   s    z(MobileNetV3.__init__.<locals>.<listcomp>	pool_typer   )rG   )rG   biasr   N)#superr(   __init__nnReLUBatchNorm2dr   r   r2   r@   grad_checkpointingr   	conv_stembn1r   
Sequentialblocksfeaturesfeature_info
stage_endsin_chsr6   head_hidden_sizer   rC   	feat_mult	conv_head	norm_headIdentityact2Flattenflattenr   
classifierr   )selfr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   norm_act_layerbuildernum_pooled_chs	__class__rM   rQ   rW   -   sR   -



zMobileNetV3.__init__returnc                 C   sZ   | j | jg}|| j || j| j| j| jg |t	 t
| j| jg tj| S )zqConvert model to sequential form.

        Returns:
            Sequential module containing all layers.
        )r\   r]   extendr_   rC   rf   rg   ri   rX   rj   Dropoutr@   rl   r^   )rm   layersrM   rM   rQ   as_sequential   s
   
zMobileNetV3.as_sequentialcoarsec                 C   s   t d|rddS ddS )z"Group parameters for optimization.z^conv_stem|bn1z^blocks\.(\d+)z^blocks\.(\d+)\.(\d+))stemr_   )dict)rm   rx   rM   rM   rQ   group_matcher   s   zMobileNetV3.group_matcherenablec                 C   
   || _ dS z)Enable or disable gradient checkpointing.Nr[   rm   r|   rM   rM   rQ   set_grad_checkpointing      
z"MobileNetV3.set_grad_checkpointingc                 C   s   | j S )zGet the classifier head.)rl   rm   rM   rM   rQ   get_classifier   s   zMobileNetV3.get_classifierc                 C   sR   || _ t|d| _|rtdnt | _|dkr"t| j|| _	dS t | _	dS )zReset the classifier head.

        Args:
            num_classes: Number of classes for new classifier.
            global_pool: Global pooling type.
        rS   r   r   N)
r2   r   rC   rX   rj   rh   rk   r   rd   rl   )rm   r2   rC   rM   rM   rQ   reset_classifier   s   (zMobileNetV3.reset_classifierNCHWxindicesnorm
stop_early
output_fmtintermediates_onlyextra_blocksc                    s  |dv sJ d|r|sJ dg }|r!t t jd |\}	}
nt t j|\}	}
 fdd|	D }	 j|
 }
d} |} |}||	v rN|| tj	 sU|sY j}n jd|
 }t
|dd	D ]\}} jrxtj	 sxt||}n||}||	v r|| qf|r|S ||fS )
aa   Forward features that returns intermediates.

        Args:
            x: Input image tensor
            indices: Take last n blocks if int, all if None, select matching indices if sequence
            norm: Apply norm layer to compatible intermediates
            stop_early: Stop iterating over blocks when last desired intermediate hit
            output_fmt: Shape of intermediate feature outputs
            intermediates_only: Only return intermediate features
            extra_blocks: Include outputs of all blocks and head conv in output, does not align with feature_info
        Returns:

        )r   zOutput shape must be NCHW.z/Must use intermediates_only for early stopping.r   c                    s   g | ]} j | qS rM   )rb   )rO   ir   rM   rQ   rR      s    z5MobileNetV3.forward_intermediates.<locals>.<listcomp>r   N)start)r"   lenr_   rb   r\   r]   appendtorchjitis_scripting	enumerater[   r#   )rm   r   r   r   r   r   r   r   intermediatestake_indices	max_indexfeat_idxr_   blkrM   r   rQ   forward_intermediates   s6   




z!MobileNetV3.forward_intermediatesr   
prune_norm
prune_headc                 C   s   |rt t| jd |\}}nt t| j|\}}| j| }| jd| | _|t| jk r7t | _t | _|rIt | _t | _| dd |S )a~  Prune layers not required for specified intermediates.

        Args:
            indices: Indices of intermediate layers to keep.
            prune_norm: Whether to prune normalization layer.
            prune_head: Whether to prune the classifier head.
            extra_blocks: Include outputs of all blocks.

        Returns:
            List of indices that were kept.
        r   Nr   r.   )	r"   r   r_   rb   rX   rh   rf   rg   r   )rm   r   r   r   r   r   r   rM   rM   rQ   prune_intermediate_layers   s   




z%MobileNetV3.prune_intermediate_layersc                 C   sF   |  |}| |}| jrtj st| j|dd}|S | |}|S )zForward pass through feature extraction layers.

        Args:
            x: Input tensor.

        Returns:
            Feature tensor.
        T)rk   )r\   r]   r[   r   r   r   r#   r_   rm   r   rM   rM   rQ   forward_features  s   
	

zMobileNetV3.forward_features
pre_logitsc                 C   sb   |  |}| |}| |}| |}| |}| jdkr(tj|| j| jd}|r,|S | 	|S )zForward pass through classifier head.

        Args:
            x: Input features.
            pre_logits: Return features before final linear layer.

        Returns:
            Classification logits or features.
        r/   )ptraining)
rC   rf   rg   ri   rk   r@   Fdropoutr   rl   )rm   r   r   rM   rM   rQ   forward_head%  s   







zMobileNetV3.forward_headc                 C   s   |  |}| |}|S )zoForward pass.

        Args:
            x: Input tensor.

        Returns:
            Output logits.
        )r   r   r   rM   rM   rQ   forward:  s   
	
zMobileNetV3.forwardFT)r0   )NFFr   FF)r   FTF)%__name__
__module____qualname____doc__r   r   intboolstrr   r   r   floatrW   rX   r^   rw   r   r   ignorer   r   r{   r   Moduler   r   Tensorr	   r   r   r   r   r   r   r   __classcell__rM   rM   rq   rQ   r(      s    	
a 	
<
!c                $       s   e Zd ZdZddddddded	d
d
d
d
ddd
fdedeedf dededede	dede
dede	dee dee dee dee dededee f" fddZejjd'd e	d!d
fd"d#Zd$ejd!eej fd%d&Z  ZS )(r)   zMobileNetV3 Feature Extractor.

    A work-in-progress feature extraction module for MobileNet-V3 to use as a backbone for segmentation
    and object detection models.
    )r   r   rD   r+      
bottleneckr+   r,   FrJ   r.   TNr/   r1   out_indices.feature_locationr3   r4   r5   rK   r9   r?   r>   r:   r;   r<   r=   r@   rA   rB   c                    s   t t|   |ptj}|ptj}|pt}|| _d| _|s!|	|}t	||dd|d| _
||| _|dd| _t|||	|
|||||||d}tj||| | _t|j|| _dd	 | j D | _t|  d
| _|dkr|| jjdd}t||  | _d
S d
S )a  Initialize MobileNetV3Features.

        Args:
            block_args: Arguments for blocks of the network.
            out_indices: Output from stages at indices.
            feature_location: Location of feature before/after each block, must be in ['bottleneck', 'expansion'].
            in_chans: Number of input image channels.
            stem_size: Number of output channels of the initial stem convolution.
            fix_stem: If True, don't scale stem by round_chs_fn.
            output_stride: Output stride of the network.
            pad_type: Type of padding to use for convolution layers.
            round_chs_fn: Callable to round number of filters based on depth multiplier.
            se_from_exp: If True, calculate SE channel reduction from expanded mid channels.
            act_layer: Type of activation layer.
            norm_layer: Type of normalization layer.
            aa_layer: Type of anti-aliasing layer.
            se_layer: Type of Squeeze-and-Excite layer.
            drop_rate: Dropout rate.
            drop_path_rate: Stochastic depth rate.
            layer_scale_init_value: Enable layer scale on compatible blocks if not None.
        Fr+   rD   rE   TrH   )rK   r9   r?   r>   r:   r;   r<   r=   rA   rB   r   c                 S   s   i | ]	}|d  |d qS )rL   indexrM   rN   rM   rM   rQ   
<dictcomp>  s    z0MobileNetV3Features.__init__.<locals>.<dictcomp>Nr   )module	hook_type)keys)rV   r)   rW   rX   rY   rZ   r   r@   r[   r   r\   r]   act1r   r^   r_   r    r`   ra   	get_dicts_stage_out_idxr   feature_hooksr!   named_modules)rm   r1   r   r   r3   r4   r5   rK   r9   r?   r>   r:   r;   r<   r=   r@   rA   rB   ro   hooksrq   rM   rQ   rW   O  sB   )


zMobileNetV3Features.__init__r|   rs   c                 C   r}   r~   r   r   rM   rM   rQ   r     r   z*MobileNetV3Features.set_grad_checkpointingr   c                 C   s   |  |}| |}| |}| jdu rJg }d| jv r || t| jD ]"\}}| jr7t	j
 s7t||}n||}|d | jv rG|| q%|S | | | j|j}t| S )zForward pass through feature extraction.

        Args:
            x: Input tensor.

        Returns:
            List of feature tensors.
        Nr   r   )r\   r]   r   r   r   r   r   r_   r[   r   r   r   r$   
get_outputdevicelistvalues)rm   r   r`   r   boutrM   rM   rQ   r     s$   
	






zMobileNetV3Features.forwardr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r   r   r   r   rM   rM   rq   rQ   r)   H  sp    	
	
Q"Fvariant
pretrainedrs   c                 K   sv   d}t }d}|ddrd|v sd|v rd}nd}t}d	}t|| |f|dk|d	k|d
|}|d	kr9t|j|_|S )zCreate a MobileNetV3 model.

    Args:
        variant: Model variant name.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    r.   Nfeatures_onlyFfeature_cfgfeature_clscfg)r2   r6   	head_convr7   r8   rC   cls)r   pretrained_strictkwargs_filter)r(   popr)   r   r   default_cfg)r   r   kwargsfeatures_mode	model_clsr   modelrM   rM   rQ   _create_mnv3  s.   	r         ?channel_multiplierc                 K   s   dgddgdgg ddgdgdgg}t dt|d	tt|d
ttjfi t|t|dttddd|}t	| |fi |}|S )ad  Creates a MobileNet-V3 model.

    Ref impl: ?
    Paper: https://arxiv.org/abs/1905.02244

    Args:
        variant: Model variant name.
        channel_multiplier: Multiplier to number of channels per layer.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    ds_r1_k3_s1_e1_c16_nre_noskipir_r1_k3_s2_e4_c24_nreir_r1_k3_s1_e3_c24_nreir_r3_k5_s2_e3_c40_se0.25_nreir_r1_k3_s2_e6_c80zir_r1_k3_s1_e2.5_c80zir_r2_k3_s1_e2.3_c80ir_r2_k3_s1_e6_c112_se0.25ir_r3_k5_s2_e6_c160_se0.25cn_r1_k1_s1_c960F
multiplier
hard_swishhard_sigmoid)
gate_layer)r1   r7   r?   r;   r:   r=   NrM   )
rz   r   r   r   rX   rZ   r   r   r   r   r   r   r   r   arch_defmodel_kwargsr   rM   rM   rQ   _gen_mobilenet_v3_rw  s(   

	r   depth_multiplier
group_sizec                 K   sF  d| v r6d}d| v r t |d}dgddgdd	gd
gdgdgg}nKt |d}dgddgddgdgdgdgg}n5d}d| v rTt |d}dgddgdgg ddgdgdgg}nt |d}dgdd gd!gg dd"gd#gdgg}ttd$tjtd%}	td,t|||d&|d'|d(k tt|d)ttjfi t	|||	d*|}
t
| |fi |
}|S )-a  Creates a MobileNet-V3 model.

    Ref impl: ?
    Paper: https://arxiv.org/abs/1905.02244

    Args:
        variant: Model variant name.
        channel_multiplier: Multiplier to number of channels per layer.
        depth_multiplier: Depth multiplier for model scaling.
        group_size: Group size for grouped convolutions.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    smalli   minimalreluds_r1_k3_s2_e1_c16zir_r1_k3_s2_e4.5_c24zir_r1_k3_s1_e3.67_c24ir_r1_k3_s2_e4_c40ir_r2_k3_s1_e6_c40ir_r2_k3_s1_e3_c48ir_r3_k3_s2_e6_c96cn_r1_k1_s1_c576r   zds_r1_k3_s2_e1_c16_se0.25_nrezir_r1_k3_s2_e4.5_c24_nrezir_r1_k3_s1_e3.67_c24_nreir_r1_k5_s2_e4_c40_se0.25zir_r2_k5_s1_e6_c40_se0.25zir_r2_k5_s1_e3_c48_se0.25zir_r3_k5_s2_e6_c96_se0.25r-   ds_r1_k3_s1_e1_c16ir_r1_k3_s2_e4_c24ir_r1_k3_s1_e3_c24ir_r3_k3_s2_e3_c40r   ir_r2_k3_s1_e6_c112ir_r3_k3_s2_e6_c160r   ds_r1_k3_s1_e1_c16_nrer   r   r   r   r   r   )r   force_act_layerrd_round_fn)r   r   r,         ?r   )r1   r6   r4   r5   r?   r;   r:   r=   NrM   )r   r   r   rX   rY   r   rz   r   rZ   r   r   )r   r   r   r   r   r   r6   r:   r   r=   r   r   rM   rM   rQ   _gen_mobilenet_v3  sl   




	r  c                 K   s.  |  dd }|dv r#d}dgddgdd	gd
dgddgg ddgg}n:|dkr?d}dgddgddgddgddgg ddgg}n|dkr[d}dgddgd d!gd"d#gd$d%gg d&d'gg}nttt|d(d)}ttd*|d+}t|d,}	td1t|d-d.||d.ttj	fi t
||	|d/	|}
t| |fi |
}|S )2a  FBNetV3 model generator.

    Paper: `FBNetV3: Joint Architecture-Recipe Search using Predictor Pretraining`
        - https://arxiv.org/abs/2006.02049
    FIXME untested, this is a preliminary impl of some FBNet-V3 variants.

    Args:
        variant: Model variant name.
        channel_multiplier: Channel width multiplier.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    _)ar   r,   ds_r2_k3_s1_e1_c16ir_r1_k5_s2_e4_c24ir_r3_k5_s1_e2_c24zir_r1_k5_s2_e5_c40_se0.25zir_r4_k5_s1_e3_c40_se0.25ir_r1_k5_s2_e5_c72ir_r4_k3_s1_e3_c72zir_r1_k3_s1_e5_c120_se0.25zir_r5_k5_s1_e3_c120_se0.25)zir_r1_k3_s2_e6_c184_se0.25zir_r5_k5_s1_e4_c184_se0.25zir_r1_k5_s1_e6_c224_se0.25cn_r1_k1_s1_c1344d   ir_r1_k3_s2_e5_c24ir_r5_k3_s1_e2_c24r   zir_r4_k3_s1_e3_c40_se0.25ir_r1_k3_s2_e5_c72zir_r1_k3_s1_e5_c128_se0.25zir_r6_k5_s1_e3_c128_se0.25)zir_r1_k3_s2_e6_c208_se0.25zir_r5_k5_s1_e5_c208_se0.25zir_r1_k5_s1_e6_c240_se0.25cn_r1_k1_s1_c1440grJ   ds_r3_k3_s1_e1_c24ir_r1_k5_s2_e4_c40ir_r4_k5_s1_e2_c40zir_r1_k5_s2_e4_c56_se0.25zir_r4_k5_s1_e3_c56_se0.25ir_r1_k5_s2_e5_c104ir_r4_k3_s1_e3_c104zir_r1_k3_s1_e5_c160_se0.25zir_r8_k5_s1_e3_c160_se0.25)zir_r1_k3_s2_e6_c264_se0.25zir_r6_k5_s1_e5_c264_se0.25zir_r2_k5_s1_e6_c288_se0.25cn_r1_k1_s1_c1728ffffff?)r   round_limitr   )r   r  r   i  F)	r1   r6   r7   r4   r?   r>   r;   r:   r=   NrM   )splitNotImplementedr   r   r   r   rz   r   rX   rZ   r   r   )r   r   r   r   vlr4   r   r?   r=   r:   r   r   rM   rM   rQ   _gen_fbnetv3  sd   		


r"  c                 K   s   dgdgdgddgdgdgg}t dt|dtt|d	ttjfi t|t|d
ttdtj	ddd|}t
| |fi |}|S )a  LCNet model generator.

    Essentially a MobileNet-V3 crossed with a MobileNet-V1

    Paper: `PP-LCNet: A Lightweight CPU Convolutional Neural Network` - https://arxiv.org/abs/2109.15099

    Args:
        variant: Model variant name.
        channel_multiplier: Multiplier to number of channels per layer.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    dsa_r1_k3_s1_c32dsa_r2_k3_s2_c64dsa_r2_k3_s2_c128dsa_r1_k3_s2_c256dsa_r1_k5_s1_c256dsa_r4_k5_s1_c256zdsa_r2_k5_s2_c512_se0.25r,   r   r   r   )r   r  r-   )r1   r4   r?   r;   r:   r=   r6   NrM   )rz   r   r   r   rX   rZ   r   r   r   rY   r   r   rM   rM   rQ   
_gen_lcnet  s(   

r)  c                 K   s  d}d| v rFd}d| v r#d}t |d}dgdd	gg d
g ddgg}	nd| v r>d}t |d}dgddgg dg ddgg}	ndJ d|  dd}d| v rdd}t |d}ddgddgg dg ddgg}	n>d| v rd}t |d}dgdd	gg dg d dgg}	n#d| v rd}t |d}dgddgg d!g d"dgg}	nJ d|  dtd(t|	|d#dd$|||d%k tt|d&ttjfi t|||d'
|}
t| |fi |
}|S ))a  Creates a MobileNet-V4 model.

    Paper: https://arxiv.org/abs/2404.10518

    Args:
        variant: Model variant name.
        channel_multiplier: Multiplier to number of channels per layer.
        group_size: Group size for grouped convolutions.
        pretrained: Load pretrained weights.
        **kwargs: Additional model arguments.

    Returns:
        MobileNetV3 model instance.
    r-   hybridgh㈵>mediumrJ   r   er_r1_k3_s2_e4_c48uir_r1_a3_k5_s2_e4_c80uir_r1_a3_k3_s1_e2_c80)uir_r1_a3_k5_s2_e6_c160uir_r1_a0_k0_s1_e2_c160uir_r1_a3_k3_s1_e4_c160uir_r1_a3_k5_s1_e4_c160mqa_r1_k3_h4_s1_v2_d64_c160r1  r3  uir_r1_a3_k0_s1_e4_c160r3  r1  r3  r4  )uir_r1_a5_k5_s2_e6_c256uir_r1_a5_k5_s1_e4_c256uir_r2_a3_k5_s1_e4_c256uir_r1_a0_k0_s1_e2_c256uir_r1_a3_k5_s1_e2_c256r8  uir_r1_a0_k0_s1_e4_c256mqa_r1_k3_h4_s1_d64_c256uir_r1_a3_k0_s1_e4_c256r;  r6  r;  uir_r1_a5_k0_s1_e4_c256r;  r=  r   larger  geluuir_r1_a3_k5_s2_e4_c96uir_r1_a3_k3_s1_e4_c96)uir_r1_a3_k5_s2_e4_c192uir_r3_a3_k3_s1_e4_c192uir_r1_a3_k5_s1_e4_c192uir_r2_a5_k3_s1_e4_c192mqa_r1_k3_h8_s1_v2_d48_c192uir_r1_a5_k3_s1_e4_c192rF  rG  rF  rG  rF  uir_r1_a3_k0_s1_e4_c192)uir_r4_a5_k5_s2_e4_c512uir_r1_a5_k0_s1_e4_c512uir_r1_a5_k3_s1_e4_c512uir_r2_a5_k0_s1_e4_c512rK  uir_r1_a5_k5_s1_e4_c512mqa_r1_k3_h8_s1_d64_c512rJ  rN  rJ  rN  rJ  rN  rJ  FzUnknown variant .Nr   cn_r1_k3_s2_e1_c32cn_r1_k1_s1_e1_c32cn_r1_k3_s2_e1_c96cn_r1_k1_s1_e1_c64)uir_r1_a5_k5_s2_e3_c96uir_r4_a0_k3_s1_e2_c96uir_r1_a3_k0_s1_e4_c96)uir_r1_a3_k3_s2_e6_c128uir_r1_a5_k5_s1_e4_c128uir_r1_a0_k5_s1_e4_c128uir_r1_a0_k5_s1_e3_c128uir_r2_a0_k3_s1_e4_c128)r/  uir_r2_a3_k3_s1_e4_c160r2  r1  r4  r0  r4  )	r5  r6  r7  r:  r<  r9  r6  uir_r2_a0_k0_s1_e4_c256uir_r1_a5_k0_s1_e2_c256)rB  rC  rD  uir_r5_a5_k3_s1_e4_c192rH  )rI  rJ  rK  rL  rK  rM  uir_r3_a5_k0_s1_e4_c512)r   Tr   r   )
r1   r7   r8   r6   r4   r5   r?   r;   r:   rB   rM   )	r   rz   r   r   r   rX   rZ   r   r   )r   r   r   r   r   r6   rB   r4   r:   r   r   r   rM   rM   rQ   _gen_mobilenet_v4  s   
0
0
	

%
#

ra  r.   urlc                 K   s   | dddddt tddd
|S )	zCreate default configuration dictionary.

    Args:
        url: Model weight URL.
        **kwargs: Additional configuration options.

    Returns:
        Configuration dictionary.
    r*   )r+      rc  )   rd  g      ?bilinearr\   rl   )
rb  r2   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_convrl   )r
   r   )rb  r   rM   rM   rQ   _cfg  s   rm  zmobilenetv3_large_075.untrained)rb  zmobilenetv3_large_100.ra_in1kbicubiczvhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/mobilenetv3_large_100_ra-f55367f5.pthztimm/)ri  rb  	hf_hub_idz)mobilenetv3_large_100.ra4_e3600_r224_in1kr  )r+      rp  )ro  ri  rj  rk  rh  test_input_sizetest_crop_pctz(mobilenetv3_large_100.miil_in21k_ft_in1kre  )r/   r/   r/   )r   r   r   z+https://github.com/Alibaba-MIIL/ImageNet21KzarXiv:2104.10972v4zhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-tresnet/mobilenetv3_large_100_1k_miil_78_0-66471c13.pth)ri  rj  rk  
origin_url	paper_idsrb  ro  z mobilenetv3_large_100.miil_in21kz~https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-tresnet/mobilenetv3_large_100_in21k_miil-d71cc17b.pthi+  )rb  ro  rs  rt  ri  rj  rk  r2   z*mobilenetv3_large_150d.ra4_e3600_r256_in1k)   ru  )r+   @  rv  )ro  rj  rk  rf  rh  rg  rq  rr  zmobilenetv3_small_050.lamb_in1kzyhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/mobilenetv3_small_050_lambc-4b7bbe87.pth)rb  ro  ri  zmobilenetv3_small_075.lamb_in1kzyhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/mobilenetv3_small_075_lambc-384766db.pthzmobilenetv3_small_100.lamb_in1kzxhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/mobilenetv3_small_100_lamb-266a294c.pthzmobilenetv3_rw.rmsp_in1kzmhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/mobilenetv3_100-35495452.pthztf_mobilenetv3_large_075.in1kzvhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_075-150ee8b0.pth)rb  ro  rj  rk  ztf_mobilenetv3_large_100.in1kzvhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_100-427764d5.pthz%tf_mobilenetv3_large_minimal_100.in1kz~https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_large_minimal_100-8596ae28.pthztf_mobilenetv3_small_075.in1kzvhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_075-da427f52.pthztf_mobilenetv3_small_100.in1kzvhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_100-37f49e2b.pthz%tf_mobilenetv3_small_minimal_100.in1kz~https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/tf_mobilenetv3_small_minimal_100-922a7843.pthzfbnetv3_b.ra2_in1kzkhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/fbnetv3_b_224-ead5d2a1.pth)rb  ro  rq  rh  zfbnetv3_d.ra2_in1kzkhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/fbnetv3_d_224-c98bce42.pthzfbnetv3_g.ra2_in1kzkhttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/fbnetv3_g_240-0b1df83b.pth)r+      rw  )r+      rx  )rb  ro  rf  rq  rh  rg  zlcnet_035.untrainedzlcnet_050.ra2_in1kzghttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/lcnet_050-f447553b.pthzlcnet_075.ra2_in1kzghttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/lcnet_075-318cad2c.pthzlcnet_100.ra2_in1kzghttps://github.com/rwightman/pytorch-image-models/releases/download/v0.1-weights/lcnet_100-a929038c.pthzlcnet_150.untrainedz$mobilenetv4_conv_small_035.untrained)rj  rk  rq  rr  ri  z*mobilenetv4_conv_small_050.e3000_r224_in1k)ro  rj  rk  rq  rr  ri  z&mobilenetv4_conv_small.e2400_r224_in1k)ro  rq  rr  ri  z&mobilenetv4_conv_small.e1200_r224_in1kz&mobilenetv4_conv_small.e3600_r256_in1k)	ro  rj  rk  rf  rg  rh  rq  rr  ri  z&mobilenetv4_conv_medium.e500_r256_in1k)ro  rf  rg  rh  rq  rr  ri  z&mobilenetv4_conv_medium.e500_r224_in1k)ro  rh  rq  rr  ri  z/mobilenetv4_conv_medium.e250_r384_in12k_ft_in1k)r+     ry  )   rz  )ro  rf  rg  rh  ri  z'mobilenetv4_conv_medium.e180_r384_in12ki-.  )ro  r2   rf  rg  rh  ri  z*mobilenetv4_conv_medium.e180_ad_r384_in12kz'mobilenetv4_conv_medium.e250_r384_in12kz%mobilenetv4_conv_large.e600_r384_in1k)r+     r{  z%mobilenetv4_conv_large.e500_r256_in1kz1mobilenetv4_hybrid_medium.e200_r256_in12k_ft_in1kz+mobilenetv4_hybrid_medium.ix_e550_r256_in1kz+mobilenetv4_hybrid_medium.ix_e550_r384_in1kz(mobilenetv4_hybrid_medium.e500_r224_in1kz)mobilenetv4_hybrid_medium.e200_r256_in12k)ro  r2   rf  rg  rh  rq  rr  ri  z*mobilenetv4_hybrid_large.ix_e600_r384_in1kz'mobilenetv4_hybrid_large.e600_r384_in1kz$mobilenetv4_conv_aa_medium.untrained)rf  rg  rh  ri  z+mobilenetv4_conv_blur_medium.e500_r224_in1kz1mobilenetv4_conv_aa_large.e230_r448_in12k_ft_in1k)   r|  )r+      r}  z1mobilenetv4_conv_aa_large.e230_r384_in12k_ft_in1k)r+     r~  z(mobilenetv4_conv_aa_large.e600_r384_in1kz)mobilenetv4_conv_aa_large.e230_r384_in12kz'mobilenetv4_hybrid_medium_075.untrained)rh  ri  z&mobilenetv4_hybrid_large_075.untrainedc                 K      t dd| i|}|S ) MobileNet V3 mobilenetv3_large_075r  r   N)r  r  r  r   r   r   rM   rM   rQ   r       r  c                 K   r  )r  mobilenetv3_large_100r   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   s   t dd| d|}|S )r  mobilenetv3_large_150d      ?g333333?)r   r   N)r  r  r  r  rM   rM   rQ   r       r  c                 K   r  )r  mobilenetv3_small_050      ?r   N)r  r  r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv3_small_075r  r   N)r  r  r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv3_small_100r   r   N)r  r   r  r  rM   rM   rQ   r  
  r  r  c                 K   s"   | dt tdd| i|}|S )r  bn_epsmobilenetv3_rwr   r   N)r  r   )
setdefaultr   r   r  rM   rM   rQ   r    s   r  c                 K   .   | dt | dd tdd| i|}|S )	r  r  r9   sametf_mobilenetv3_large_075r  r   N)r  r  r  r   r  r  rM   rM   rQ   r       r  c                 K   r  )	r  r  r9   r  tf_mobilenetv3_large_100r   r   N)r  r   r  r  rM   rM   rQ   r  "  r  r  c                 K   r  )	r  r  r9   r   tf_mobilenetv3_large_minimal_100r   r   N)r  r   r  r  rM   rM   rQ   r  +  r  r  c                 K   r  )	r  r  r9   r  tf_mobilenetv3_small_075r  r   N)r  r  r  r  rM   rM   rQ   r  4  r  r  c                 K   r  )	r  r  r9   r  tf_mobilenetv3_small_100r   r   N)r  r   r  r  rM   rM   rQ   r  =  r  r  c                 K   r  )	r  r  r9   r   tf_mobilenetv3_small_minimal_100r   r   N)r  r   r  r  rM   rM   rQ   r  F  r  r  c                 K      t dd| i|}|S )z FBNetV3-B 	fbnetv3_br   N)r  r"  r  rM   rM   rQ   r  O  r  r  c                 K   r  )z FBNetV3-D 	fbnetv3_dr   N)r  r  r  rM   rM   rQ   r  V  r  r  c                 K   r  )z FBNetV3-G 	fbnetv3_gr   N)r  r  r  rM   rM   rQ   r  ]  r  r  c                 K   r  )z PP-LCNet 0.35	lcnet_035ffffff?r   N)r  r  r)  r  rM   rM   rQ   r  d  r  r  c                 K   r  )z PP-LCNet 0.5	lcnet_050r  r   N)r  r  r  r  rM   rM   rQ   r  k  r  r  c                 K   r  ) PP-LCNet 1.0	lcnet_075r  r   N)r  r  r  r  rM   rM   rQ   r  r  r  r  c                 K   r  )r  	lcnet_100r   r   N)r  r   r  r  rM   rM   rQ   r  y  r  r  c                 K   r  )z PP-LCNet 1.5	lcnet_150r  r   N)r  r  r  r  rM   rM   rQ   r    r  r  c                 K   r  ) MobileNet V4 mobilenetv4_conv_small_035r  r   N)r  r  ra  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_conv_small_050r  r   N)r  r  r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_conv_smallr   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_conv_mediumr   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_conv_larger   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  ) MobileNet V4 Hybrid mobilenetv4_hybrid_mediumr   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  ) MobileNet V4 Hybridmobilenetv4_hybrid_larger   r   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K      t d| dd|}|S ) MobileNet V4 w/ AvgPool AA mobilenetv4_conv_aa_mediumr   r0   r   r<   N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  )z MobileNet V4 Conv w/ Blur AA mobilenetv4_conv_blur_mediumr   blurpcr  N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_conv_aa_larger   r0   r  N)r  r   r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_hybrid_medium_075r  r   N)r  r  r  r  rM   rM   rQ   r    r  r  c                 K   r  )r  mobilenetv4_hybrid_large_075r  r   N)r  r  r  r  rM   rM   rQ   r    r  r  )mobilenetv3_large_100_miil mobilenetv3_large_100_miil_in21kr   )r   F)r   r   NF)r   NF)r.   )hr   	functoolsr   typingr   r   r   r   r   r   r	   r   torch.nnrX   torch.nn.functional
functionalr   	timm.datar
   r   r   r   timm.layersr   r   r   r   r   r   _builderr   r   _efficientnet_blocksr   _efficientnet_builderr   r   r   r   r   r   r   r   	_featuresr    r!   r"   _manipulater#   r$   	_registryr%   r&   r'   __all__r   r(   r)   r   r   r   r   r   r   r  r"  r)  ra  rm  default_cfgsr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   rM   rM   rM   rQ   <module>   s   $ (  -{%
0
pG/
  


#(-159=AFJNSTY^cehlorw{   	          !  %  )  ,  1  5  ;  >  A  E  I  M  R  U  [