我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用torch.nn.ReflectionPad2d()。
def __init__(self, dim=64): super(Generator, self).__init__() conv_bn_relu = conv_norm_act dconv_bn_relu = dconv_norm_act self.ls = nn.Sequential(nn.ReflectionPad2d(3), conv_bn_relu(3, dim * 1, 7, 1), conv_bn_relu(dim * 1, dim * 2, 3, 2, 1), conv_bn_relu(dim * 2, dim * 4, 3, 2, 1), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), dconv_bn_relu(dim * 4, dim * 2, 3, 2, 1, 1), dconv_bn_relu(dim * 2, dim * 1, 3, 2, 1, 1), nn.ReflectionPad2d(3), nn.Conv2d(dim, 3, 7, 1), nn.Tanh())
def __init__(self, h_size): super(Residual_block, self).__init__() # Two Conv layers with same output size model = [] if param.padding == "reflect": model += [nn.ReflectionPad2d(padding=1)] model += [nn.Conv2d(h_size, h_size, kernel_size=3, stride=1, padding=pad)] if param.SELU: model += [torch.nn.SELU()] else: model += [Norm2D(h_size), nn.ReLU(True)] if param.padding == "reflect": model += [nn.ReflectionPad2d(padding=1)] model += [nn.Conv2d(h_size, h_size, kernel_size=3, stride=1, padding=pad)] if not param.SELU: model += [Norm2D(h_size)] self.model = nn.Sequential(*model)
def __init__(self, in_dim, out_dim): super(ResiduleBlock, self).__init__() conv_bn_relu = conv_norm_act self.ls = nn.Sequential(nn.ReflectionPad2d(1), conv_bn_relu(in_dim, out_dim, 3, 1), nn.ReflectionPad2d(1), nn.Conv2d(out_dim, out_dim, 3, 1), nn.BatchNorm2d(out_dim))
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias): conv_block = [] p = 0 if padding_type == 'reflect': conv_block += [nn.ReflectionPad2d(1)] elif padding_type == 'replicate': conv_block += [nn.ReplicationPad2d(1)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim), nn.ReLU(True)] if use_dropout: conv_block += [nn.Dropout(0.5)] p = 0 if padding_type == 'reflect': conv_block += [nn.ReflectionPad2d(1)] elif padding_type == 'replicate': conv_block += [nn.ReplicationPad2d(1)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim)] return nn.Sequential(*conv_block)
def __init__(self, in_channels, out_channels, kernel_size, stride): super(ConvLayer, self).__init__() reflection_padding = int(np.floor(kernel_size / 2)) self.reflection_pad = nn.ReflectionPad2d(reflection_padding) self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None): super(UpsampleConvLayer, self).__init__() self.upsample = upsample 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.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
def __init__(self, h_size): super(Residual_block, self).__init__() # Two Conv layers with same output size model = [nn.ReflectionPad2d(padding=1), nn.Conv2d(h_size, h_size, kernel_size=3, stride=1, padding=0), Norm2D(h_size), nn.ReLU(True)] if param.use_dropout: model += [nn.Dropout(0.5)] model += [nn.ReflectionPad2d(padding=1), nn.Conv2d(h_size, h_size, kernel_size=3, stride=1, padding=0), nn.ReLU(True)] self.model = nn.Sequential(*model)
def __init__(self): super(CycleGAN_G, self).__init__() ### Downsample block ## Reflection padding is an alternative to 0 padding (like looking at water reflection) # n_colors x image_size x image_size model = [nn.ReflectionPad2d(padding=3), nn.Conv2d(param.n_colors, param.G_h_size, kernel_size=7, stride=1, padding=0), Norm2D(param.G_h_size), nn.ReLU(True)] # param.G_h_size x image_size x image_size model += [nn.Conv2d(param.G_h_size, param.G_h_size * 2, kernel_size=3, stride=2, padding=1), Norm2D(param.G_h_size * 2), nn.ReLU(True)] # (param.G_h_size * 2) x (image_size / 2) x (image_size / 2) model += [nn.Conv2d(param.G_h_size * 2, param.G_h_size * 4, kernel_size=3, stride=2, padding=1), Norm2D(param.G_h_size * 4), nn.ReLU(True)] # (param.G_h_size * 4) x (image_size / 4) x (image_size / 4) ### Residual blocks for i in range(param.G_residual_blocks): model += [Residual_block(h_size=param.G_h_size * 4)] ### Upsample block (pretty much inverse of downsample) # (param.G_h_size * 4) x (image_size / 4) x (image_size / 4) model += [nn.ConvTranspose2d(param.G_h_size * 4, param.G_h_size * 2, kernel_size=3, stride=2, padding=1, output_padding=1), Norm2D(param.G_h_size * 2), nn.ReLU(True)] # (param.G_h_size * 2) x (image_size / 2) x (image_size / 2) model += [nn.ConvTranspose2d(param.G_h_size * 2, param.G_h_size, kernel_size=3, stride=2, padding=1, output_padding=1), Norm2D(param.G_h_size), nn.ReLU(True)] # param.G_h_size x image_size x image_size model += [nn.ReflectionPad2d(padding=3), nn.Conv2d(param.G_h_size, param.n_colors, kernel_size=7, stride=1, padding=0), nn.Tanh()] # Size = n_colors x image_size x image_size self.model = nn.Sequential(*model)
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, gpu_ids=[], padding_type='reflect'): assert(n_blocks >= 0) super(ResnetGenerator, self).__init__() self.input_nc = input_nc self.output_nc = output_nc self.ngf = ngf self.gpu_ids = gpu_ids model = [nn.ReflectionPad2d(3), nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0), norm_layer(ngf), nn.ReLU(True)] n_downsampling = 2 for i in range(n_downsampling): mult = 2**i model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1), norm_layer(ngf * mult * 2), nn.ReLU(True)] mult = 2**n_downsampling for i in range(n_blocks): model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout)] for i in range(n_downsampling): mult = 2**(n_downsampling - i) model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=2, padding=1, output_padding=1), norm_layer(int(ngf * mult / 2)), nn.ReLU(True)] model += [nn.ReflectionPad2d(3)] model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)] model += [nn.Tanh()] self.model = nn.Sequential(*model)
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout): conv_block = [] p = 0 if padding_type == 'reflect': conv_block += [nn.ReflectionPad2d(1)] elif padding_type == 'replicate': conv_block += [nn.ReplicationPad2d(1)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), norm_layer(dim), nn.ReLU(True)] if use_dropout: conv_block += [nn.Dropout(0.5)] p = 0 if padding_type == 'reflect': conv_block += [nn.ReflectionPad2d(1)] elif padding_type == 'replicate': conv_block += [nn.ReplicationPad2d(1)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), norm_layer(dim)] return nn.Sequential(*conv_block)
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, gpu_ids=[], use_parallel = True, learn_residual = False, padding_type='reflect'): assert(n_blocks >= 0) super(ResnetGenerator, self).__init__() self.input_nc = input_nc self.output_nc = output_nc self.ngf = ngf self.gpu_ids = gpu_ids self.use_parallel = use_parallel self.learn_residual = learn_residual if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d model = [nn.ReflectionPad2d(3), nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias), norm_layer(ngf), nn.ReLU(True)] n_downsampling = 2 for i in range(n_downsampling): mult = 2**i model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias), norm_layer(ngf * mult * 2), nn.ReLU(True)] mult = 2**n_downsampling for i in range(n_blocks): model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)] for i in range(n_downsampling): mult = 2**(n_downsampling - i) model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=2, padding=1, output_padding=1, bias=use_bias), norm_layer(int(ngf * mult / 2)), nn.ReLU(True)] model += [nn.ReflectionPad2d(3)] model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)] model += [nn.Tanh()] self.model = nn.Sequential(*model)
def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, gpu_ids=[], padding_type='reflect'): assert(n_blocks >= 0) super(ResnetGenerator, self).__init__() self.input_nc = input_nc self.output_nc = output_nc self.ngf = ngf 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 model = [nn.ReflectionPad2d(3), nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias), norm_layer(ngf), nn.ReLU(True)] n_downsampling = 2 for i in range(n_downsampling): mult = 2**i model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias), norm_layer(ngf * mult * 2), nn.ReLU(True)] mult = 2**n_downsampling for i in range(n_blocks): model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)] for i in range(n_downsampling): mult = 2**(n_downsampling - i) model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=2, padding=1, output_padding=1, bias=use_bias), norm_layer(int(ngf * mult / 2)), nn.ReLU(True)] model += [nn.ReflectionPad2d(3)] model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)] model += [nn.Tanh()] self.model = nn.Sequential(*model)