o - i!@sxddlmZddlmZddlZddlmZddlmZddl m Z ddl m Z Gdddej ZGd d d ej ZdS) ) annotations)SequenceN) Convolution)Act) normal_initcsHeZdZdZddejddifdddd dfd$fdd Zd%d"d#ZZS)&MultiScalePatchDiscriminatorap Multi-scale Patch-GAN discriminator based on Pix2PixHD: High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs (https://arxiv.org/abs/1711.11585) The Multi-scale discriminator made up of several PatchGAN discriminators, that process the images at different spatial scales. Args: num_d: number of discriminators num_layers_d: number of Convolution layers (Conv + activation + normalisation + [dropout]) in the first discriminator. Each subsequent discriminator has one additional layer, meaning the output size is halved. spatial_dims: number of spatial dimensions (1D, 2D etc.) channels: number of filters in the first convolutional layer (doubled for each subsequent layer) in_channels: number of input channels out_channels: number of output channels in each discriminator kernel_size: kernel size of the convolution layers activation: activation layer type norm: normalisation type bias: introduction of layer bias dropout: probability of dropout applied, defaults to 0. minimum_size_im: minimum spatial size of the input image. Introduced to make sure the architecture requested isn't going to downsample the input image beyond value of 1. last_conv_kernel_size: kernel size of the last convolutional layer. negative_slope皙?BATCHFnum_dint num_layers_d spatial_dimschannels in_channels out_channels kernel_size activation str | tuplenormbiasbooldropout float | tupleminimum_size_imlast_conv_kernel_sizereturnNonecst||_||_||_tt|ddg||_t|jD]:}|j|d}t | d|}|dkrAt d|d|dt ||j|||||| | |j| | d }| d||q!dS)Nrz.Your image size is too small to take in up to z" discriminators with num_layers = z?.Please reduce num_layers, reduce num_D or enter bigger images.) rrrrrrrrrpaddingrrzdiscriminator_%d) super__init__rr num_channelstuplerr#rangefloatAssertionErrorPatchDiscriminator add_module)selfrrrrrrrrrrrrri_Znum_layers_d_i output_sizeZsubnet_d __class__l/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/networks/nets/patchgan_discriminator.pyr%2s8 z%MultiScalePatchDiscriminator.__init__i torch.Tensor3tuple[list[torch.Tensor], list[list[torch.Tensor]]]cCsFg}g}|D]}||}||d||ddq||fS)z Args: i: Input tensor Returns: list of outputs and another list of lists with the intermediate features of each discriminator. Nchildrenappend)r-r4outZintermediate_featuresdiscout_dr2r2r3forward`s  z$MultiScalePatchDiscriminator.forward)rrrrrrrrrrrrrrrrrrrrrrrrrrr r!)r4r5r r6 __name__ __module__ __qualname____doc__r LEAKYRELUr%r> __classcell__r2r2r0r3rs  .rc sJeZdZdZdddejddifdddd d f d&fdd Zd'd$d%ZZS)(r+a) Patch-GAN discriminator based on Pix2PixHD: High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs (https://arxiv.org/abs/1711.11585) Args: spatial_dims: number of spatial dimensions (1D, 2D etc.) channels: number of filters in the first convolutional layer (doubled for each subsequent layer) in_channels: number of input channels out_channels: number of output channels num_layers_d: number of Convolution layers (Conv + activation + normalisation + [dropout]) in the discriminator. kernel_size: kernel size of the convolution layers act: activation type and arguments. Defaults to LeakyReLU. norm: feature normalization type and arguments. Defaults to batch norm. bias: whether to have a bias term in convolution blocks. Defaults to False. padding: padding to be applied to the convolutional layers dropout: proportion of dropout applied, defaults to 0. last_conv_kernel_size: kernel size of the last convolutional layer. rr r r r Fr Nrrrrrrrrrrrrr#int | Sequence[int]rrr int | Noner r!c st||_||_| dur|} |dt|||||dd| | dd |} |d}t|jD]*}||jdkr:d}nd}t||| ||| || | |d }|d|||} |d}q.|dt|| | |ddt| ddddd |t dS) NZ initial_convTr") rrrractrrrr#stridesrz%d final_convr ) rrrrr conv_onlyr#rrJ) r$r%rr&r,rr(rapplyr)r-rrrrrrrrrr#rrinput_channelsoutput_channelsZl_stridelayerr0r2r3r%sp   zPatchDiscriminator.__init__xr5list[torch.Tensor]cCs6|g}|D] }||d}||q|ddS)z Args: x: input tensor Returns: list of intermediate features, with the last element being the output. r7rNr8)r-rRr;ZsubmodelZintermediate_outputr2r2r3r>s     zPatchDiscriminator.forward)rrrrrrrrrrrrrrrrrrr#rGrrrrHr r!)rRr5r rSr?r2r2r0r3r+ts Pr+) __future__rcollections.abcrtorchtorch.nnnnmonai.networks.blocksrmonai.networks.layersrmonai.networks.utilsr Sequentialrr+r2r2r2r3s     \