o ) i#@sddlmZddlZddlmZddlmmZddlm Z ddl m Z ddl m Z mZmZddlmZeddd \ZZGd d d ejZGd d d ejZGdddejZGdddeZdS)) annotationsN) Convolution)UpSample)ActConvNorm)optional_import torchvisionmodels)namecs.eZdZdZddfdd Zdd d ZZS)GCNzq The Global Convolutional Network module using large 1D Kx1 and 1xK kernels to represent 2D kernels. inplanesintplaneskscstttjdf}||||df|ddfd|_|||d|fd|dfd|_|||d|fd|dfd|_||||df|ddfd|_dS)z Args: inplanes: number of input channels. planes: number of output channels. ks: kernel size for one dimension. Defaults to 7. r in_channels out_channels kernel_sizepaddingN)super__init__rCONVconv_l1conv_l2conv_r1conv_r2)selfrrr conv2d_type __class__[/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/blocks/fcn.pyr s "z GCN.__init__x torch.TensorreturncCs4||}||}||}||}||}|S)zX Args: x: in shape (batch, inplanes, spatial_1, spatial_2). )rrrr)r r&x_lx_rr$r$r%forward/s    z GCN.forward)r )rrrrrrr&r'r(r'__name__ __module__ __qualname____doc__rr+ __classcell__r$r$r"r%r sr cs,eZdZdZd fdd Zd d d ZZS) RefinezM Simple residual block to refine the details of the activation maps. rrcsptttj}ttjdf}ttjdf}||d|_|dd|_ |||ddd|_ |||ddd|_ dS) zE Args: planes: number of input channels. r) num_featuresT)inplacerrN) rrrRELUrrrBATCHbnreluconv1conv2)r r relu_typer! norm2d_typer"r$r%rAs    zRefine.__init__r&r'r(cCsH|}||}||}||}||}||}||}||S)zV Args: x: in shape (batch, planes, spatial_1, spatial_2). )r9r:r;r<)r r&residualr$r$r%r+Qs      zRefine.forward)rrr,r-r$r$r"r%r3<sr3cs0eZdZdZ ddfd d ZdddZZS)FCNa 2D FCN network with 3 input channels. The small decoder is built with the GCN and Refine modules. The code is adapted from `lsqshr's official 2D code `_. Args: out_channels: number of output channels. Defaults to 1. upsample_mode: [``"transpose"``, ``"bilinear"``] The mode of upsampling manipulations. Using the second mode cannot guarantee the model's reproducibility. Defaults to ``bilinear``. - ``transpose``, uses transposed convolution layers. - ``bilinear``, uses bilinear interpolation. pretrained: If True, returns a model pre-trained on ImageNet progress: If True, displays a progress bar of the download to stderr. rbilinearTrr upsample_modestr pretrainedboolprogresscsztttjdf}||_||_||_tj||rtj j ndd}|j |_ |j |_ |j|_|j|_|j|_|j|_|j|_|j|_td|j|_td|j|_td|j|_td|j|_td|j|_t|j|_t|j|_t|j|_t|j|_t|j|_t|j|_ t|j|_!t|j|_"t|j|_#t|j|_$|jdddd |_%|jd krt&d|jdd d |_'dSdS) Nr)rFweightsiii@r)rrr transposedeconv) spatial_dimsr scale_factormode)(rrrrrBr!rr resnet50ResNet50_Weights IMAGENET1K_V1r;bn1bn0r:maxpoollayer1layer2layer3layer4r gcn1gcn2gcn3gcn4gcn5r3refine1refine2refine3refine4refine5refine6refine7refine8refine9refine10 transformerrup_conv)r rrBrDrFr!resnetr"r$r%rtsF            z FCN.__init__r&r'cCs|}||}||}||}|}||}|}||}||}||}||}|| |} | | |} | | |} |||} |||} |jdkr||| | }|||| }|||| }|||| }|||S|tj| |dd|jdd| }|tj||dd|jdd| }|tj||dd|jdd| }|tj||dd|jdd| }|tj||dd|jddS)zQ Args: x: in shape (batch, 3, spatial_1, spatial_2). rJrNT)rN align_corners)r;rSr:rTrUrVrWrXr^rYr_rZr`r[rar\rbr]rBrcrirdrerfrgF interpolatesize)r r&Z org_inputconv_xpool_xfm1fm2fm3fm4Zgcfm1Zgcfm2Zgcfm3Zgcfm4Zgcfm5Zfs1fs2Zfs3Zfs4r$r$r%r+s6         ****&z FCN.forward)rrATT)rrrBrCrDrErFrEr&r'r-r$r$r"r%r@as -r@cs<eZdZdZ     ddfdd Zdfdd ZZS)MCFCNa The multi-channel version of the 2D FCN module. Adds a projection layer to take arbitrary number of inputs. Args: in_channels: number of input channels. Defaults to 3. out_channels: number of output channels. Defaults to 1. upsample_mode: [``"transpose"``, ``"bilinear"``] The mode of upsampling manipulations. Using the second mode cannot guarantee the model's reproducibility. Defaults to ``bilinear``. - ``transpose``, uses transposed convolution layers. - ``bilinear``, uses bilinear interpolate. pretrained: If True, returns a model pre-trained on ImageNet progress: If True, displays a progress bar of the download to stderr. r6rrATrrrrBrCrDrErFc s:tj||||dtd|dddddiftjdd |_dS) N)rrBrDrFrr6rr:r5TF)rLrrractnormbias)rrrrr8 init_proj)r rrrBrDrFr"r$r%rs  zMCFCN.__init__r&r'cs||}t|S)z[ Args: x: in shape (batch, in_channels, spatial_1, spatial_2). )r{rr+)r r&r"r$r%r+s  z MCFCN.forward)r6rrATT) rrrrrBrCrDrErFrErvr-r$r$r"r%rwsrw) __future__rtorchtorch.nnnntorch.nn.functional functionalrl"monai.networks.blocks.convolutionsrZmonai.networks.blocks.upsamplermonai.networks.layers.factoriesrrr monai.utilsrr _Moduler r3r@rwr$r$r$r%s     "%e