Python torch.nn.InstanceNorm2d() Examples
The following are 30
code examples of torch.nn.InstanceNorm2d().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn
, or try the search function
.
Example #1
Source File: networks.py From EvolutionaryGAN-pytorch with MIT License | 6 votes |
def __init__(self, input_nc, ndf=64, norm_layer=nn.BatchNorm2d): """Construct a 1x1 PatchGAN discriminator Parameters: input_nc (int) -- the number of channels in input images ndf (int) -- the number of filters in the last conv layer norm_layer -- normalization layer """ super(PixelDiscriminator, self).__init__() if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters use_bias = norm_layer.func != nn.InstanceNorm2d else: use_bias = norm_layer != nn.InstanceNorm2d self.net = [ nn.Conv2d(input_nc, ndf, kernel_size=1, stride=1, padding=0), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf, ndf * 2, kernel_size=1, stride=1, padding=0, bias=use_bias), norm_layer(ndf * 2), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf * 2, 1, kernel_size=1, stride=1, padding=0, bias=use_bias)] self.net = nn.Sequential(*self.net)
Example #2
Source File: networks.py From MeshCNN with MIT License | 6 votes |
def __init__(self, in_channels, out_channels, blocks=0, unroll=0, residual=True, batch_norm=True, transfer_data=True): super(UpConv, self).__init__() self.residual = residual self.bn = [] self.unroll = None self.transfer_data = transfer_data self.up_conv = MeshConv(in_channels, out_channels) if transfer_data: self.conv1 = MeshConv(2 * out_channels, out_channels) else: self.conv1 = MeshConv(out_channels, out_channels) self.conv2 = [] for _ in range(blocks): self.conv2.append(MeshConv(out_channels, out_channels)) self.conv2 = nn.ModuleList(self.conv2) if batch_norm: for _ in range(blocks + 1): self.bn.append(nn.InstanceNorm2d(out_channels)) self.bn = nn.ModuleList(self.bn) if unroll: self.unroll = MeshUnpool(unroll)
Example #3
Source File: pix2pix.py From ncsn with GNU General Public License v3.0 | 6 votes |
def get_norm_layer(norm_type='instance'): """Return a normalization layer Parameters: norm_type (str) -- the name of the normalization layer: batch | instance | none For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev). For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics. """ if norm_type == 'batch': norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True) elif norm_type == 'instance': norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False) elif norm_type == 'none': norm_layer = None else: raise NotImplementedError('normalization layer [%s] is not found' % norm_type) return norm_layer
Example #4
Source File: base_networks.py From STARnet with MIT License | 6 votes |
def __init__(self, input_size, output_size, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm=None): super(ConvBlock, self).__init__() self.conv = nn.Conv2d(input_size, output_size, kernel_size, stride, padding, bias=bias) self.norm = norm if self.norm =='batch': self.bn = nn.BatchNorm2d(output_size) elif self.norm == 'instance': self.bn = nn.InstanceNorm2d(output_size) self.activation = activation if self.activation == 'relu': self.act = nn.ReLU(True) elif self.activation == 'prelu': self.act = nn.PReLU() elif self.activation == 'lrelu': self.act = nn.LeakyReLU(0.1, True) elif self.activation == 'tanh': self.act = nn.Tanh() elif self.activation == 'sigmoid': self.act = nn.Sigmoid()
Example #5
Source File: common.py From imgclsmob with MIT License | 6 votes |
def __init__(self, channels, first_fraction=0.5, inst_first=True): super(IBN, self).__init__() self.inst_first = inst_first h1_channels = int(math.floor(channels * first_fraction)) h2_channels = channels - h1_channels self.split_sections = [h1_channels, h2_channels] if self.inst_first: self.inst_norm = nn.InstanceNorm2d( num_features=h1_channels, affine=True) self.batch_norm = nn.BatchNorm2d(num_features=h2_channels) else: self.batch_norm = nn.BatchNorm2d(num_features=h1_channels) self.inst_norm = nn.InstanceNorm2d( num_features=h2_channels, affine=True)
Example #6
Source File: networks.py From DepthNets with MIT License | 6 votes |
def __init__(self, input_nc, ndf=64, norm_layer=nn.BatchNorm2d, use_sigmoid=False, gpu_ids=[]): super(PixelDiscriminator, self).__init__() self.gpu_ids = gpu_ids if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d self.net = [ nn.Conv2d(input_nc, ndf, kernel_size=1, stride=1, padding=0), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf, ndf * 2, kernel_size=1, stride=1, padding=0, bias=use_bias), norm_layer(ndf * 2), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf * 2, 1, kernel_size=1, stride=1, padding=0, bias=use_bias)] if use_sigmoid: self.net.append(nn.Sigmoid()) self.net = nn.Sequential(*self.net)
Example #7
Source File: base_networks.py From STARnet with MIT License | 6 votes |
def __init__(self, input_size, output_size, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm=None): super(ConvBlock3D, self).__init__() self.conv = nn.Conv3d(input_size, output_size, kernel_size, stride, padding) self.norm = norm if self.norm =='batch': self.bn = nn.BatchNorm2d(output_size) elif self.norm == 'instance': self.bn = nn.InstanceNorm2d(output_size) self.activation = activation if self.activation == 'relu': self.act = nn.ReLU(True) elif self.activation == 'prelu': self.act = nn.PReLU() elif self.activation == 'lrelu': self.act = nn.LeakyReLU(0.1, True) elif self.activation == 'tanh': self.act = nn.Tanh() elif self.activation == 'sigmoid': self.act = nn.Sigmoid()
Example #8
Source File: ibnbresnet.py From imgclsmob with MIT License | 6 votes |
def __init__(self, in_channels, out_channels, stride, use_inst_norm): super(IBNbResUnit, self).__init__() self.use_inst_norm = use_inst_norm self.resize_identity = (in_channels != out_channels) or (stride != 1) self.body = ResBottleneck( in_channels=in_channels, out_channels=out_channels, stride=stride, conv1_stride=False) if self.resize_identity: self.identity_conv = conv1x1_block( in_channels=in_channels, out_channels=out_channels, stride=stride, activation=None) if self.use_inst_norm: self.inst_norm = nn.InstanceNorm2d( num_features=out_channels, affine=True) self.activ = nn.ReLU(inplace=True)
Example #9
Source File: base_networks.py From STARnet with MIT License | 6 votes |
def __init__(self, input_size, output_size, kernel_size=4, stride=2, padding=1, bias=True, activation='prelu', norm=None): super(DeconvBlock, self).__init__() self.deconv = nn.ConvTranspose2d(input_size, output_size, kernel_size, stride, padding, bias=bias) self.norm = norm if self.norm == 'batch': self.bn = nn.BatchNorm2d(output_size) elif self.norm == 'instance': self.bn = nn.InstanceNorm2d(output_size) self.activation = activation if self.activation == 'relu': self.act = nn.ReLU(True) elif self.activation == 'prelu': self.act = nn.PReLU() elif self.activation == 'lrelu': self.act = nn.LeakyReLU(0.1, True) elif self.activation == 'tanh': self.act = nn.Tanh() elif self.activation == 'sigmoid': self.act = nn.Sigmoid()
Example #10
Source File: base_networks.py From STARnet with MIT License | 6 votes |
def __init__(self, num_filter, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm='batch'): super(ResnetBlock, self).__init__() self.conv1 = nn.Conv2d(num_filter, num_filter, kernel_size, stride, padding, bias=bias) self.conv2 = nn.Conv2d(num_filter, num_filter, kernel_size, stride, padding, bias=bias) self.norm = norm if self.norm == 'batch': self.bn = nn.BatchNorm2d(num_filter) elif norm == 'instance': self.bn = nn.InstanceNorm2d(num_filter) self.activation = activation if self.activation == 'relu': self.act = nn.ReLU(True) elif self.activation == 'prelu': self.act = nn.PReLU() elif self.activation == 'lrelu': self.act = nn.LeakyReLU(0.1, True) elif self.activation == 'tanh': self.act = nn.Tanh() elif self.activation == 'sigmoid': self.act = nn.Sigmoid()
Example #11
Source File: networks.py From 2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement with MIT License | 6 votes |
def __init__(self, input_nc, ndf=64, norm_layer=nn.BatchNorm2d): """Construct a 1x1 PatchGAN discriminator Parameters: input_nc (int) -- the number of channels in input images ndf (int) -- the number of filters in the last conv layer norm_layer -- normalization layer """ super(PixelDiscriminator, self).__init__() if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d self.net = [ nn.Conv2d(input_nc, ndf, kernel_size=1, stride=1, padding=0), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf, ndf * 2, kernel_size=1, stride=1, padding=0, bias=use_bias), norm_layer(ndf * 2), nn.LeakyReLU(0.2, True), nn.Conv2d(ndf * 2, 1, kernel_size=1, stride=1, padding=0, bias=use_bias)] self.net = nn.Sequential(*self.net)
Example #12
Source File: base_networks.py From STARnet with MIT License | 6 votes |
def __init__(self, num_filter, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm='batch'): super(ResnetBlock3D, self).__init__() self.conv1 = nn.Conv3d(num_filter, num_filter, kernel_size, stride, padding) self.conv2 = nn.Conv3d(num_filter, num_filter, kernel_size, stride, padding) self.norm = norm if self.norm == 'batch': self.bn = nn.BatchNorm2d(num_filter) elif norm == 'instance': self.bn = nn.InstanceNorm2d(num_filter) self.activation = activation if self.activation == 'relu': self.act = nn.ReLU(True) elif self.activation == 'prelu': self.act = nn.PReLU() elif self.activation == 'lrelu': self.act = nn.LeakyReLU(0.1, True) elif self.activation == 'tanh': self.act = nn.Tanh() elif self.activation == 'sigmoid': self.act = nn.Sigmoid()
Example #13
Source File: networks.py From EvolutionaryGAN-pytorch with MIT License | 6 votes |
def get_norm_layer(norm_type='instance'): """Return a normalization layer Parameters: norm_type (str) -- the name of the normalization layer: batch | instance | none For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev). For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics. """ if norm_type == 'batch': norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True) elif norm_type == 'instance': norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False) elif norm_type == 'none': norm_layer = lambda x: Identity() else: raise NotImplementedError('normalization layer [%s] is not found' % norm_type) return norm_layer
Example #14
Source File: networks.py From 2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement with MIT License | 6 votes |
def get_norm_layer(norm_type='instance'): """Return a normalization layer Parameters: norm_type (str) -- the name of the normalization layer: batch | instance | none For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev). For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics. """ if norm_type == 'batch': norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True) elif norm_type == 'instance': norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False) elif norm_type == 'none': norm_layer = lambda x: Identity() else: raise NotImplementedError('normalization layer [%s] is not found' % norm_type) return norm_layer
Example #15
Source File: ghiasi.py From style-augmentation with MIT License | 6 votes |
def __init__(self, channels_in, channels_out, kernel_size, upsample, stride=1, activation=nn.ReLU): super(UpsampleConvInRelu, self).__init__() self.n_params = channels_out * 2 self.upsample = upsample self.channels = channels_out if upsample: self.upsample_layer = torch.nn.Upsample(scale_factor=upsample) reflection_padding = int(np.floor(kernel_size / 2)) self.reflection_pad = nn.ReflectionPad2d(reflection_padding) self.conv = nn.Conv2d(channels_in, channels_out, kernel_size, stride) self.instancenorm = nn.InstanceNorm2d(channels_out) self.fc_beta = nn.Linear(100,channels_out) self.fc_gamma = nn.Linear(100,channels_out) if activation: self.activation = activation(inplace=False) else: self.activation = None
Example #16
Source File: models.py From neural-odes-segmentation with MIT License | 6 votes |
def __init__(self, nf, time_dependent=False, non_linearity='relu'): """ Block for ConvODEUNet Args: nf (int): number of filters for the conv layers time_dependent (bool): whether to concat the time as a feature map before the convs non_linearity (str): which non_linearity to use (for options see get_nonlinearity) """ super(ConvODEFunc, self).__init__() self.time_dependent = time_dependent self.nfe = 0 # Number of function evaluations if time_dependent: self.norm1 = nn.InstanceNorm2d(nf) self.conv1 = Conv2dTime(nf, nf, kernel_size=3, stride=1, padding=1) self.norm2 = nn.InstanceNorm2d(nf) self.conv2 = Conv2dTime(nf, nf, kernel_size=3, stride=1, padding=1) else: self.norm1 = nn.InstanceNorm2d(nf) self.conv1 = nn.Conv2d(nf, nf, kernel_size=3, stride=1, padding=1) self.norm2 = nn.InstanceNorm2d(nf) self.conv2 = nn.Conv2d(nf, nf, kernel_size=1, stride=1, padding=1) self.non_linearity = get_nonlinearity(non_linearity)
Example #17
Source File: coinrun.py From torchsupport with MIT License | 6 votes |
def __init__(self, in_size=3, out_size=15): super().__init__() self.blocks = nn.Sequential( nn.Conv2d(in_size, 32, 3), nn.ReLU(), nn.InstanceNorm2d(32), nn.MaxPool2d(2), nn.Conv2d(32, 32, 3), nn.InstanceNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 32, 3), nn.InstanceNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 32, 3), nn.InstanceNorm2d(32), nn.ReLU() ) self.postprocess = nn.Linear(32, out_size)
Example #18
Source File: mfcc_networks.py From Talking-Face-Generation-DAVS with MIT License | 6 votes |
def __init__(self, norm_layer=nn.BatchNorm2d): super(mfcc_encoder, self).__init__() use_bias = norm_layer == nn.InstanceNorm2d self.relu = nn.LeakyReLU(0.2, True) self.conv1 = nn.Conv2d(1, 64, kernel_size=(3, 3), stride=(3, 2), padding=(1, 2), bias=use_bias) self.pool1 = nn.AvgPool2d((2, 2), 2) self.bn1 = norm_layer(64) self.conv2 = nn.Conv2d(64, 128, (3, 3), 2, 1, bias=use_bias) self.pool2 = nn.AvgPool2d(2,2) self.bn2 = norm_layer(128) self.conv3 = nn.Conv2d(128, 256, (3, 3), 1, 0, bias=use_bias) self.bn3 = norm_layer(256) self.conv4 = nn.Conv2d(256, 512, (2, 2), 1, bias=use_bias) self.bn5 = norm_layer(512) self.tanh = nn.Tanh()
Example #19
Source File: generators.py From cycleGAN-PyTorch with MIT License | 6 votes |
def __init__(self, input_nc=3, output_nc=3, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=True, num_blocks=6): super(ResnetGenerator, self).__init__() if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d res_model = [nn.ReflectionPad2d(3), conv_norm_relu(input_nc, ngf * 1, 7, norm_layer=norm_layer, bias=use_bias), conv_norm_relu(ngf * 1, ngf * 2, 3, 2, 1, norm_layer=norm_layer, bias=use_bias), conv_norm_relu(ngf * 2, ngf * 4, 3, 2, 1, norm_layer=norm_layer, bias=use_bias)] for i in range(num_blocks): res_model += [ResidualBlock(ngf * 4, norm_layer, use_dropout, use_bias)] res_model += [dconv_norm_relu(ngf * 4, ngf * 2, 3, 2, 1, 1, norm_layer=norm_layer, bias=use_bias), dconv_norm_relu(ngf * 2, ngf * 1, 3, 2, 1, 1, norm_layer=norm_layer, bias=use_bias), nn.ReflectionPad2d(3), nn.Conv2d(ngf, output_nc, 7), nn.Tanh()] self.res_model = nn.Sequential(*res_model)
Example #20
Source File: pix2pix.py From ncsn with GNU General Public License v3.0 | 6 votes |
def __init__(self, input_nc, ndf=64, norm_layer=nn.BatchNorm2d): """Construct a 1x1 PatchGAN discriminator Parameters: input_nc (int) -- the number of channels in input images ndf (int) -- the number of filters in the last conv layer norm_layer -- normalization layer """ super(PixelDiscriminator, self).__init__() if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters use_bias = norm_layer.func != nn.InstanceNorm2d else: use_bias = norm_layer != nn.InstanceNorm2d self.net = [ nn.Conv2d(input_nc, ndf, kernel_size=1, stride=1, padding=0), nn.ELU(), nn.Conv2d(ndf, ndf * 2, kernel_size=1, stride=1, padding=0, bias=use_bias), norm_layer(ndf * 2), nn.ELU(), nn.Conv2d(ndf * 2, 1, kernel_size=1, stride=1, padding=0, bias=use_bias)] self.net = nn.Sequential(*self.net)
Example #21
Source File: run_tom.py From viton-gan with MIT License | 6 votes |
def main(): opt = get_opt() print(opt) model = UnetGenerator(25, 4, 6, ngf=64, norm_layer=nn.InstanceNorm2d) load_checkpoint(model, opt.checkpoint) model.cuda() model.eval() mode = 'test' print('Run on {} data'.format(mode.upper())) dataset = TOMDataset(opt, mode, data_list=mode+'_pairs.txt', train=False) dataloader = DataLoader(dataset, batch_size=opt.batch_size, num_workers=opt.n_worker, shuffle=False) with torch.no_grad(): run(opt, model, dataloader, mode) print('Successfully completed')
Example #22
Source File: discriminators.py From cycleGAN-PyTorch with MIT License | 6 votes |
def define_Dis(input_nc, ndf, netD, n_layers_D=3, norm='batch', gpu_ids=[0]): dis_net = None norm_layer = get_norm_layer(norm_type=norm) if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d if netD == 'n_layers': dis_net = NLayerDiscriminator(input_nc, ndf, n_layers_D, norm_layer=norm_layer, use_bias=use_bias) elif netD == 'pixel': dis_net = PixelDiscriminator(input_nc, ndf, norm_layer=norm_layer, use_bias=use_bias) else: raise NotImplementedError('Discriminator model name [%s] is not recognized' % netD) return init_network(dis_net, gpu_ids)
Example #23
Source File: transformer.py From Neural-Style with MIT License | 6 votes |
def __init__(self): super(TransformerNet, self).__init__() #Convolution Layers self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1) self.ini1 = nn.InstanceNorm2d(32, affine=True) self.conv2 = ConvLayer(3, 64, kernel_size=3, stride=2) self.ini2 = nn.InstanceNorm2d(64, affine=True) self.conv3 = ConvLayer(3, 128, kernel_size=3, stride=2) self.ini3 = nn.InstanceNorm2d(128, affine=True) # Residual Blocks, there significance can be read from the original resnet paper self.res1 = ResidualBlock(128) self.res2 = ResidualBlock(128) self.res3 = ResidualBlock(128) self.res4 = ResidualBlock(128) self.res5 = ResidualBlock(128) #Upsampling layers self.deconv1 = UpsampleConvLayer(128, 64, kernel_size=3, stride=1, upsample=2) self.ini4 = nn.InstanceNorm2d(64, affine=True) self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, upsample=2) self.ini5 = nn.InstanceNorm2d(32, affine=True) self.deconv3 = UpsampleConvLayer(32, 3, kernel_size=9, stride=1) self.relu = nn.ReLU()
Example #24
Source File: common.py From TENet with MIT License | 5 votes |
def norm_layer(norm_type, nc): # helper selecting normalization layer norm_type = norm_type.lower() if norm_type == 'batch': layer = nn.BatchNorm2d(nc, affine=True) elif norm_type == 'instance': layer = nn.InstanceNorm2d(nc, affine=False) else: raise NotImplementedError('normalization layer [%s] is not found' % norm_type) return layer
Example #25
Source File: transformer.py From Neural-Style with MIT License | 5 votes |
def __init__(self, channels): super(ResidualBlock, self).__init__() self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in1 = nn.InstanceNorm2d(channels, affine=True) self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1) self.in2 = nn.InstanceNorm2d(channels, affine=True) self.relu = nn.ReLU()
Example #26
Source File: cond_refinenet_dilated.py From ncsn with GNU General Public License v3.0 | 5 votes |
def __init__(self, num_features, num_classes, bias=True): super().__init__() self.num_features = num_features self.bias = bias self.instance_norm = nn.InstanceNorm2d(num_features, affine=False, track_running_stats=False) if bias: self.embed = nn.Embedding(num_classes, num_features * 3) self.embed.weight.data[:, :2 * num_features].normal_(1, 0.02) # Initialise scale at N(1, 0.02) self.embed.weight.data[:, 2 * num_features:].zero_() # Initialise bias at 0 else: self.embed = nn.Embedding(num_classes, 2 * num_features) self.embed.weight.data.normal_(1, 0.02)
Example #27
Source File: cond_refinenet_dilated.py From ncsn with GNU General Public License v3.0 | 5 votes |
def __init__(self, num_features, num_classes, bias=True): super().__init__() self.num_features = num_features self.bias = bias self.instance_norm = nn.InstanceNorm2d(num_features, affine=False, track_running_stats=False) if bias: self.embed = nn.Embedding(num_classes, num_features * 2) self.embed.weight.data[:, :num_features].uniform_() # Initialise scale at N(1, 0.02) self.embed.weight.data[:, num_features:].zero_() # Initialise bias at 0 else: self.embed = nn.Embedding(num_classes, num_features) self.embed.weight.data.uniform_()
Example #28
Source File: refinenet_dilated_baseline.py From ncsn with GNU General Public License v3.0 | 5 votes |
def __init__(self, num_features, num_classes, bias=True): super().__init__() self.num_features = num_features self.bias = bias self.instance_norm = nn.InstanceNorm2d(num_features, affine=False, track_running_stats=False) self.alpha = nn.Parameter(torch.zeros(num_features)) self.gamma = nn.Parameter(torch.zeros(num_features)) self.alpha.data.normal_(1, 0.02) self.gamma.data.normal_(1, 0.02) if bias: self.beta = nn.Parameter(torch.zeros(num_features))
Example #29
Source File: networks.py From viton-gan with MIT License | 5 votes |
def __init__(self, outer_nc, inner_nc, input_nc=None, submodule=None, outermost=False, innermost=False, norm_layer=nn.BatchNorm2d, use_dropout=False): super(UnetSkipConnectionBlock, self).__init__() self.outermost = outermost use_bias = norm_layer == nn.InstanceNorm2d if input_nc is None: input_nc = outer_nc downconv = nn.Conv2d(input_nc, inner_nc, kernel_size=4, stride=2, padding=1, bias=use_bias) downrelu = nn.LeakyReLU(0.2, True) downnorm = norm_layer(inner_nc) uprelu = nn.ReLU(True) upnorm = norm_layer(outer_nc) if outermost: upsample = nn.Upsample(scale_factor=2, mode='bilinear') upconv = nn.Conv2d(inner_nc * 2, outer_nc, kernel_size=3, stride=1, padding=1, bias=use_bias) down = [downconv] up = [uprelu, upsample, upconv, upnorm] model = down + [submodule] + up elif innermost: upsample = nn.Upsample(scale_factor=2, mode='bilinear') upconv = nn.Conv2d(inner_nc, outer_nc, kernel_size=3, stride=1, padding=1, bias=use_bias) down = [downrelu, downconv] up = [uprelu, upsample, upconv, upnorm] model = down + up else: upsample = nn.Upsample(scale_factor=2, mode='bilinear') upconv = nn.Conv2d(inner_nc*2, outer_nc, kernel_size=3, stride=1, padding=1, bias=use_bias) down = [downrelu, downconv, downnorm] up = [uprelu, upsample, upconv, upnorm] if use_dropout: model = down + [submodule] + up + [nn.Dropout(0.5)] else: model = down + [submodule] + up self.model = nn.Sequential(*model)
Example #30
Source File: wideresnet.py From JEM with Apache License 2.0 | 5 votes |
def get_norm(n_filters, norm): if norm is None: return Identity() elif norm == "batch": return nn.BatchNorm2d(n_filters, momentum=0.9) elif norm == "instance": return nn.InstanceNorm2d(n_filters, affine=True) elif norm == "layer": return nn.GroupNorm(1, n_filters) elif norm == "act": return norms.ActNorm(n_filters, False)