a *f@sdZddlZddlmZddlmmZddlmZddl m Z ddl m Z ddl mZGdd d e Ze eed d d ZdS) a' MIT License Copyright (c) 2019 Microsoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. N) ShapeSpec)BACKBONE_REGISTRY)Backbone)build_pose_hrnet_backbonecs2eZdZdZd fdd ZddZdd ZZS) HRFPNaHRFPN (High Resolution Feature Pyramids) Transforms outputs of HRNet backbone so they are suitable for the ROI_heads arXiv: https://arxiv.org/abs/1904.04514 Adapted from https://github.com/open-mmlab/mmdetection/blob/master/mmdet/models/necks/hrfpn.py Args: bottom_up: (list) output of HRNet in_features (list): names of the input features (output of HRNet) in_channels (list): number of channels for each branch out_channels (int): output channels of feature pyramids n_out_features (int): number of output stages pooling (str): pooling for generating feature pyramids (from {MAX, AVG}) share_conv (bool): Have one conv per output, or share one with all the outputs AVGFc stt|t|tsJ||_||_||_||_||_ t ||_ ||_ |j rft j||ddd|_n4t |_t|jD]}|jt j||dddqzt |_tt |jD]P} |jt t j|| || dd| ddddt j|| d d t jd d qt |_t|jD]H} |jt t jt||d| d| d t j|d d t jd d q|dkrvtj|_ntj|_g|_i|_i|_ t|jD]P} |jd| d|j!|jd|j i|j !|jdd| diqdS)Nr) in_channels out_channels kernel_sizepaddingrF)r r r strider output_paddingbiasg?)momentumT)inplace)r rMAXp%d)"superr__init__ isinstancelist bottom_up in_featuresn_out_featuresr r lenZnum_ins share_convnnConv2dfpn_conv ModuleListrangeappend interp_conv SequentialConvTranspose2d BatchNorm2dReLUreduction_pooling_convsumF max_pool2dpooling avg_pool2d _out_features_out_feature_channels_out_feature_stridesupdate) selfrrrr r r0r _i __class__E/data1/chongzheng_p23/Projects/CatVTON-hf/densepose/modeling/hrfpn.pyr2sv            zHRFPN.__init__cCs@|D]2}t|tjrtjj|jddtj|jdqdS)Nr)ar) modulesrr!r"initkaiming_normal_weight constant_r)r6mr;r;r< init_weightss  zHRFPN.init_weightsc s||tt|jks Jfdd|jD}g}tt|D]}||j|||qDtdd|Dtdd|Dtjfdd|Ddd}g}t|j D]}||j ||qtt|D]X}|d |ddddd|d j d d |d|d j d d |f|d |<qg}tt|D]<}|j rd|| ||n||j |||qBt|jt|ksJtt|j|S) Ncsg|] }|qSr;r;).0f)bottom_up_featuresr;r< z!HRFPN.forward..css|]}|jdVqdS)rNshaperEor;r;r< rIz HRFPN.forward..css|]}|jdVqdS)r NrJrLr;r;r<rNrIcs,g|]$}|ddddddfqS)Nr;rL)shape_2shape_3r;r<rHrIr)dimrrr )rrrr%r&r'mintorchcatrr,rKr r#r2dictzip)r6inputsoutsr8outoutputsr;)rGrOrPr<forwards.  >z HRFPN.forward)rF)__name__ __module__ __qualname____doc__rrDr[ __classcell__r;r;r9r<r#s Qr) input_shapereturnc Csd|jjjj}ddt|jjjjD}t|jjj}|jjj j }t ||}t |||||ddd}|S)NcSsg|]}d|dqS)rrr;)rEr8r;r;r<rHrIz(build_hrfpn_backbone..rF)r0r ) MODELHRNETSTAGE4 NUM_CHANNELSr% NUM_BRANCHESr ROI_HEADS IN_FEATURESr OUT_CHANNELSr)cfgrar rrr hrnetZhrfpnr;r;r<build_hrfpn_backbones    rm)r_rStorch.nnr!Ztorch.nn.functional functionalr.Zdetectron2.layersrZdetectron2.modeling.backbonerZ%detectron2.modeling.backbone.backbonerrlrrregisterrmr;r;r;r<s