o eE@sddlmZmZmZddlZddlmZddlmZddl m Z ddl m Z dZ eeZddgZdd d ZGd d d ejZGdddejZGdddejZeedZGddde Ze defddZdS))absolute_importdivisionprint_functionN) ShapeSpec)BACKBONE_REGISTRY)Backboneg?build_pose_hrnet_backbonePoseHigherResolutionNetcCstj||d|dddS)z3x3 convolution with paddingr F kernel_sizestridepaddingbiasN)nnConv2d) in_planes out_planesrrL/home/jovyan/fileviewer/workspace/yisol/IDM-VTON/densepose/modeling/hrnet.pyconv3x3srcs*eZdZdZdfdd ZddZZS) BasicBlockr Ncsftt|t||||_tj|td|_tj dd|_ t|||_ tj|td|_ ||_ ||_dS)NmomentumTinplace)superr__init__rconv1r BatchNorm2d BN_MOMENTUMbn1ReLUreluconv2bn2 downsamplerselfinplanesplanesrr' __class__rrr s  zBasicBlock.__init__cCs`|}||}||}||}||}||}|jdur%||}||7}||}|SN)rr"r$r%r&r'r)xresidualoutrrrforward*s        zBasicBlock.forwardr N__name__ __module__ __qualname__ expansionrr3 __classcell__rrr,rrs rcs*eZdZdZdfdd ZddZZS) Bottleneckr Ncstt|tj||ddd|_tj|td|_tj||d|ddd|_ tj|td|_ tj|||j ddd|_ tj||j td|_ tjdd|_||_||_dS) Nr F)r rrr r Tr)rr;rrrrr r!r"r%r&r9conv3bn3r#r$r'rr(r,rrr@s zBottleneck.__init__cCs~|}||}||}||}||}||}||}||}||}|jdur4||}||7}||}|Sr.)rr"r$r%r&r=r>r'r/rrrr3Ls           zBottleneck.forwardr4r5rrr,rr;=s r;csVeZdZdZ dfdd ZddZddd Zd d Zd d ZddZ ddZ Z S)HighResolutionModuleaHighResolutionModule Building block of the PoseHigherResolutionNet (see lower) arXiv: https://arxiv.org/abs/1908.10357 Args: num_branches (int): number of branches of the modyle blocks (str): type of block of the module num_blocks (int): number of blocks of the module num_inchannels (int): number of input channels of the module num_channels (list): number of channels of each branch multi_scale_output (bool): only used by the last module of PoseHigherResolutionNet Tcs^tt|||||||||_||_||_||||||_| |_ t d|_ dS)NT)rr?r_check_branchesnum_inchannels num_branchesmulti_scale_output_make_branchesbranches_make_fuse_layers fuse_layersrr#r$)r)rBblocks num_blocksrA num_channelsrCr,rrrps  zHighResolutionModule.__init__cCs|t|krd|t|}t|t||t|kr.d|t|}t|t||t|krEd|t|}t|t|dS)Nz"NUM_BRANCHES({}) <> NUM_BLOCKS({})z$NUM_BRANCHES({}) <> NUM_CHANNELS({})z&NUM_BRANCHES({}) <> NUM_INCHANNELS({}))lenformatloggererror ValueError)r)rBrHrIrArJ error_msgrrrr@s"      z$HighResolutionModule._check_branchesr c Csd}|dks|j||||jkr2ttj|j||||jd|ddtj|||jtd}g}|||j||||||||j|j|<td||D]}|||j|||qTtj|SNr F)r rrr) rAr9r Sequentialrr r!appendrange) r)Z branch_indexblockrIrJrr'layers_rrr_make_one_branchs.   z%HighResolutionModule._make_one_branchc Cs2g}t|D] }||||||qt|Sr.)rTrSrXr ModuleList)r)rBrUrIrJrEirrrrDs  z#HighResolutionModule._make_branchesc Csv|jdkrdS|j}|j}g}t|jr|ndD]}g}t|D]}||krL|ttj||||dddddt||tj d||ddq ||krV|dq g}t||D]E}|||dkr||} |ttj||| dddddt| q^||} |ttj||| dddddt| t d q^|tj|q |t |qt |S) Nr rFrnearest) scale_factormoder T) rBrArTrCrSrrRrr Upsampler#rY) r)rBrArGrZZ fuse_layerjconv3x3skZnum_outchannels_conv3x3rrrrFsj     z&HighResolutionModule._make_fuse_layerscCs|jSr.)rA)r)rrrget_num_inchannelssz'HighResolutionModule.get_num_inchannelsc Cs|jdkr|jd|dgSt|jD] }|j|||||<qg}tt|jD]T}|dkr5|dn |j|d|d}td|jD]0}||krS|||}qF|j||||ddddd|jdd|jdf}||}qF|||q+|S)Nr rr\r )rBrErTrKrGshaperSr$)r)r0rZZx_fuseyrazrrrr3s &> zHighResolutionModule.forwardTr ) r6r7r8__doc__rr@rXrDrFrdr3r:rrr,rr?cs ;r?)BASICZ BOTTLENECKcsLeZdZdZfddZddZddZdd d Zdd d ZddZ Z S)r zPoseHigherResolutionNet Composed of several HighResolutionModule tied together with ConvNets Adapted from the GitHub version to fit with HRFPN and the Detectron2 infrastructure arXiv: https://arxiv.org/abs/1908.10357 c s |jjj|_tt|tjddddddd|_ tj dt d|_ tjddddddd|_ tj dt d|_tjdd |_|tdd |_|jjj|_|jjt|jjfd d ttD|d g|_||j\|_}|jjj|_ |j jt|j jfdd ttD|||_!||j \|_"}|jjj#|_$|j$jt|j$jfdd ttD|||_%|j|j$dd\|_&}g|_'i|_(i|_)t|jjj#j*D](}|j'+d|d|j(,|j'd|jjj#j|i|j),|j'ddiqdS)Nr @r\r Fr rTrr<cg|] }|jqSrr9.0rZrUrJrr )z4PoseHigherResolutionNet.__init__..crmrrnrorqrrrr0rscrmrrnrorqrrrr7rs)rCzp%d)-MODELHRNET STEM_INPLANESr*rr rrrrr r!r"r%r&r#r$ _make_layerr;layer1STAGE2 stage2_cfg NUM_CHANNELS blocks_dictBLOCKrTrK_make_transition_layer transition1 _make_stagestage2STAGE3 stage3_cfg transition2stage3STAGE4 stage4_cfg transition3stage4 _out_features_out_feature_channels_out_feature_strides NUM_BRANCHESrSupdate)r)cfgkwargsZpre_stage_channelsrZr,rqrrsL        z PoseHigherResolutionNet.__init__cCs>|dkr d}d}n|dkrd}d}n|dkrd}d}|||fS)Nr<r rr r\r)r) deconv_kernelroutput_paddingrrr_get_deconv_cfgHs z'PoseHigherResolutionNet._get_deconv_cfgc Cst|}t|}g}t|D]t}||krC||||kr=|ttj||||dddddt||tjddq|dqg}t|d|D],}|d} |||kr]||n| } |ttj| | dddddt| tjddqM|tj|qt|S) Nr r Fr[Trrur\) rKrTrSrrRrr r#rY) r)Znum_channels_pre_layerZnum_channels_cur_layerZnum_branches_curZnum_branches_preZtransition_layersrZrbraZ inchannelsZ outchannelsrrrrUsF      z.PoseHigherResolutionNet._make_transition_layerr c Csd}|dks|j||jkr(ttj|j||jd|ddtj||jtd}g}|||j|||||j|_td|D] }|||j|q@tj|SrQ) r*r9rrRrr r!rSrT)r)rUr+rHrr'rVrWrrrrys$   z#PoseHigherResolutionNet._make_layerTc Cs|d}|d}|d}|d}t|d}g} t|D]!} |s)| |dkr)d} nd} | t|||||| | d }qtj| |fS) N NUM_MODULESr NUM_BLOCKSr}rr FTru)r~rTrSr?rdrrR) r)Z layer_configrArCZ num_modulesrBrIrJrUmodulesrZZreset_multi_scale_outputrrrrs,   z#PoseHigherResolutionNet._make_stagecCsf||}||}||}||}||}||}||}g}t|jjD]}|j |dur?| |j ||q+| |q+| |}g}t|j jD]}|j |durh| |j ||dqR| ||qR||}g}t|jjD]}|j|dur| |j||dq}| ||q}||}t|jt|ksJtt|j|S)Nru)rr"r$r%r&rzrTr|rrrSrrrrrrrrKrdictzip)r)r0x_listrZy_listrrrr3s6           zPoseHigherResolutionNet.forwardrirh) r6r7r8rjrrrryrr3r:rrr,rr s . *  input_shapecCs t|}|Sr.)r )rrmodelrrrrsri) __future__rrrloggingtorch.nnrdetectron2.layersrdetectron2.modeling.backboner%detectron2.modeling.backbone.backbonerr! getLoggerr6rM__all__rModulerr;r?r~r registerrrrrrs&        & .E