U Ph@#@sddlmZddlZddlmZddlmmZddlm Z ddl m Z ddl m Z mZmZddlmZeddd \ZZGd d d ejZGd d d ejZGdddejZGdddeZdS)) annotationsN) Convolution)UpSample)ActConvNorm)optional_import torchvisionmodels)namecs<eZdZdZd ddddfdd Zdddd d ZZS) GCNzq The Global Convolutional Network module using large 1D Kx1 and 1xK kernels to represent 2D kernels. int)inplanesplaneskscstttjdf}||||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__N/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/networks/blocks/fcn.pyr s  z GCN.__init__ torch.TensorxreturncCs4||}||}||}||}||}|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 __name__ __module__ __qualname____doc__rr, __classcell__r$r$r"r%r sr cs6eZdZdZddfdd Zddddd ZZS) RefinezM Simple residual block to refine the details of the activation maps. r)rcsptttj}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'cCsH|}||}||}||}||}||}||}||S)zV Args: x: in shape (batch, planes, spatial_1, spatial_2). )r9r:r;r<)r r(residualr$r$r%r,Qs      zRefine.forwardr-r$r$r"r%r3<sr3cs<eZdZdZddddddfd d Zd d d dZZS)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. rbilinearTrstrboolr upsample_mode pretrainedprogresscsltttjdf}||_||_||_tj||d}|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 krht$d|jdd d |_%dS) Nr)rFrGiii@r)rrr transposedeconv) spatial_dimsr scale_factormode)&rrrrrEr!rr resnet50r;bn1bn0r:maxpoollayer1layer2layer3layer4r gcn1gcn2gcn3gcn4gcn5r3refine1refine2refine3refine4refine5refine6refine7refine8refine9refine10 transformerrup_conv)r rrErFrGr!resnetr"r$r%rts@            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;rQr:rRrSrTrUrVr\rWr]rXr^rYr_rZr`r[rErargrbrcrdreF interpolatesize)r r(Z org_inputconv_xpool_xfm1fm2fm3fm4Zgcfm1Zgcfm2Zgcfm3Zgcfm4Zgcfm5Zfs1fs2Zfs3Zfs4r$r$r%r,s6         ****z FCN.forward)rrATTr-r$r$r"r%r@as+r@csBeZdZdZddddddd fd d Zd d fdd 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. r6rrATrrBrC)rrrErFrGc s:tj||||dtd|dddddiftjdd |_dS) NrDrr6rr:r5TF)rLrrractnormbias)rrrrr8 init_proj)r rrrErFrGr"r$r%rs zMCFCN.__init__r&rics||}t|S)z[ Args: x: in shape (batch, in_channels, spatial_1, spatial_2). )ryrr,)r r(r"r$r%r,s z MCFCN.forward)r6rrATTr-r$r$r"r%rusru) __future__rtorchtorch.nnnntorch.nn.functional functionalrk"monai.networks.blocks.convolutionsrZmonai.networks.blocks.upsamplermonai.networks.layers.factoriesrrr monai.utilsrr _Moduler r3r@rur$r$r$r% s     "%c