本文整理匯總了Python中torch.nn.InstanceNorm2d方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.InstanceNorm2d方法的具體用法?Python nn.InstanceNorm2d怎麽用?Python nn.InstanceNorm2d使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.InstanceNorm2d方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: define_Dis
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例2: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例3: get_norm_layer
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例5: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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()
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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()
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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()
示例11: main
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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')
示例12: get_norm_layer
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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
示例15: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import InstanceNorm2d [as 別名]
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)