本文整理汇总了Python中torch.nn.functional.conv_transpose2d方法的典型用法代码示例。如果您正苦于以下问题:Python functional.conv_transpose2d方法的具体用法?Python functional.conv_transpose2d怎么用?Python functional.conv_transpose2d使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.conv_transpose2d方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_one_iter
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def compute_one_iter(self):
if not self.initialized:
raise ValueError('Layer needs to be initialized first.')
domain, codomain = self.compute_domain_codomain()
if self.kernel_size == (1, 1):
u = self.u.detach()
v = self.v.detach()
weight = self.weight.detach().view(self.out_channels, self.in_channels)
u = normalize_u(torch.mv(weight, v), codomain)
v = normalize_v(torch.mv(weight.t(), u), domain)
return torch.dot(u, torch.mv(weight, v))
else:
u = self.u.detach()
v = self.v.detach()
weight = self.weight.detach()
c, h, w = self.in_channels, int(self.spatial_dims[0].item()), int(self.spatial_dims[1].item())
u_s = F.conv2d(v.view(1, c, h, w), weight, stride=self.stride, padding=self.padding, bias=None)
out_shape = u_s.shape
u = normalize_u(u_s.view(-1), codomain)
v_s = F.conv_transpose2d(
u.view(out_shape), weight, stride=self.stride, padding=self.padding, output_padding=0
)
v = normalize_v(v_s.view(-1), domain)
weight_v = F.conv2d(v.view(1, c, h, w), weight, stride=self.stride, padding=self.padding, bias=None)
return torch.dot(u.view(-1), weight_v.view(-1))
示例2: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x):
# change to in_channels, out_channels, kernel_size, kernel_size
weight = self.weight.permute([1, 0, 2, 3])
weight = F.pad(weight, [1, 1, 1, 1])
weight = (weight[:, :, 1:, 1:]
+ weight[:, :, :-1, 1:]
+ weight[:, :, 1:, :-1]
+ weight[:, :, :-1, :-1]
)
x = F.conv_transpose2d(x,
weight,
self.bias, # note if bias set to False, this will be None
stride=2,
padding=self.padding)
return x
# TODO: this needs to be better wrappered by ConstrainedLayer for bias
示例3: pacconv_transpose2d
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def pacconv_transpose2d(input, kernel, weight, bias=None, stride=1, padding=0, output_padding=0, dilation=1,
shared_filters=False, native_impl=False):
kernel_size = tuple(weight.shape[-2:])
stride = _pair(stride)
padding = _pair(padding)
output_padding = _pair(output_padding)
dilation = _pair(dilation)
if native_impl:
ch = input.shape[1]
w = input.new_ones((ch, 1, 1, 1))
x = F.conv_transpose2d(input, w, stride=stride, groups=ch)
pad = [(kernel_size[i] - 1) * dilation[i] - padding[i] for i in range(2)]
x = F.pad(x, (pad[1], pad[1] + output_padding[1], pad[0], pad[0] + output_padding[0]))
output = pacconv2d(x, kernel, weight.permute(1, 0, 2, 3), bias, dilation=dilation,
shared_filters=shared_filters, native_impl=True)
else:
output = PacConvTranspose2dFn.apply(input, kernel, weight, bias, stride, padding, output_padding, dilation,
shared_filters)
return output
示例4: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x, rev=False):
if not rev:
self.last_jac = self.elements / 4 * (np.log(16.) + 4 * np.log(self.fac_fwd))
out = F.conv2d(x[0], self.haar_weights,
bias=None, stride=2, groups=self.in_channels)
if self.permute:
return [out[:, self.perm] * self.fac_fwd]
else:
return [out * self.fac_fwd]
else:
self.last_jac = self.elements / 4 * (np.log(16.) + 4 * np.log(self.fac_rev))
if self.permute:
x_perm = x[0][:, self.perm_inv]
else:
x_perm = x[0]
return [F.conv_transpose2d(x_perm * self.fac_rev, self.haar_weights,
bias=None, stride=2, groups=self.in_channels)]
示例5: lipschitz_correction
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def lipschitz_correction(self):
with torch.no_grad():
# Power method to approximate spectral norm
# Following https://arxiv.org/pdf/1804.04368.pdf
for i in range(len(self.layers)):
W = self.layers[i].weight
x = torch.randn(self.lipschitz_batchsize, W.shape[1], *self.dims_in[1:], device=W.device)
if len(self.dims_in) == 1:
# Linear case
for j in range(self.lipschitz_iterations):
x = W.t().matmul(W.matmul(x.unsqueeze(-1))).squeeze(-1)
spectral_norm = (torch.norm(W.matmul(x.unsqueeze(-1)).squeeze(-1), dim=1) /\
torch.norm(x, dim=1)).max()
else:
# Convolutional case
for j in range(self.lipschitz_iterations):
x = conv2d(x, W)
x = conv_transpose2d(x, W)
spectral_norm = (torch.norm(conv2d(x, W).view(self.lipschitz_batchsize, -1), dim=1) /\
torch.norm(x.view(self.lipschitz_batchsize, -1), dim=1)).max()
if spectral_norm > self.spectral_norm_max:
self.layers[i].weight.data *= self.spectral_norm_max / spectral_norm
示例6: quaternion_transpose_conv
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def quaternion_transpose_conv(input, r_weight, i_weight, j_weight, k_weight, bias, stride,
padding, output_padding, groups, dilatation):
"""
Applies a quaternion trasposed convolution to the incoming data:
"""
cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=1)
cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=1)
cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=1)
cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=1)
cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0)
if input.dim() == 3:
convfunc = F.conv_transpose1d
elif input.dim() == 4:
convfunc = F.conv_transpose2d
elif input.dim() == 5:
convfunc = F.conv_transpose3d
else:
raise Exception("The convolutional input is either 3, 4 or 5 dimensions."
" input.dim = " + str(input.dim()))
return convfunc(input, cat_kernels_4_quaternion, bias, stride, padding, output_padding, groups, dilatation)
示例7: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, *input):
x = input[0]
size = x.size()
kernel = self.renet(x)
kernel = F.softmax(kernel, 1)
# print(kernel.size())
x = F.unfold(x, [10, 10], dilation=[3, 3])
x = x.reshape(size[0], size[1], 10 * 10)
kernel = kernel.reshape(size[0], 100, -1)
x = torch.matmul(x, kernel)
x = x.reshape(size[0], size[1], size[2], size[3])
# for attention visualization
# print(torch.cuda.memory_allocated() / 1024 / 1024)
attention = kernel.data
attention = attention.requires_grad_(False)
attention = torch.reshape(attention, (size[0], -1, 10, 10))
# attention = F.conv_transpose2d(torch.ones((1, 1, 1, 1)).cuda(), attention, dilation=3)
attention = F.interpolate(attention, 224, mode='bilinear', align_corners=True)
# attention = F.interpolate(attention, 224, mode='area')
attention = torch.reshape(attention, (size[0], size[2], size[3], 224, 224))
return x, attention
示例8: _apply_feat_transpose_v1
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def _apply_feat_transpose_v1(feat, input, filter_ksz):
"""This one is slow as hell!!!!"""
num_images = feat.shape[0]
num_sequences = feat.shape[1] if feat.dim() == 5 else 1
feat_sz = (feat.shape[-2], feat.shape[-1])
if isinstance(filter_ksz, int):
filter_ksz = (filter_ksz, filter_ksz)
# trans_pad = sz + padding - filter_ksz
trans_pad = [sz + ksz//2 - ksz for sz, ksz in zip(feat_sz, filter_ksz)]
filter_grad = F.conv_transpose2d(input.flip((2, 3)).view(1, -1, input.shape[-2], input.shape[-1]),
feat.reshape(-1, feat.shape[-3], feat.shape[-2], feat.shape[-1]),
padding=trans_pad, groups=num_images * num_sequences)
return filter_grad.view(num_images, num_sequences, -1, filter_grad.shape[-2], filter_grad.shape[-1]).sum(dim=0)
示例9: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x):
x = self.pixel_norm(x)
x = self.upsample(x)
if self.use_conv2d_transpose:
kernel = self.weight * self.scale
kernel = F.pad(kernel, (0, 0, 0, 0, 1, 1, 1, 1), 'constant', 0.0)
kernel = (kernel[1:, 1:] + kernel[:-1, 1:] +
kernel[1:, :-1] + kernel[:-1, :-1])
kernel = kernel.permute(2, 3, 0, 1)
x = F.conv_transpose2d(x, kernel, stride=2, padding=1)
x = x / self.scale
else:
x = self.conv(x)
x = self.wscale(x)
x = self.activate(x)
return x
示例10: quaternion_transpose_conv
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def quaternion_transpose_conv(input, r_weight, i_weight, j_weight, k_weight, bias, stride,
padding, output_padding, groups, dilatation):
"""
Applies a quaternion trasposed convolution to the incoming data:
"""
cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=1)
cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=1)
cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=1)
cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=1)
cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=0)
if input.dim() == 3:
convfunc = F.conv_transpose1d
elif input.dim() == 4:
convfunc = F.conv_transpose2d
elif input.dim() == 5:
convfunc = F.conv_transpose3d
else:
raise Exception("The convolutional input is either 3, 4 or 5 dimensions."
" input.dim = " + str(input.dim()))
return convfunc(input, cat_kernels_4_quaternion, bias, stride, padding, output_padding, groups, dilatation)
示例11: __init__
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def __init__(self, dim_in, dim_out, ksize=3, stride=1, padding=0, dilation=1, groups=1, bias=True, transpose=False):
super(HyperConv2d, self).__init__()
assert dim_in % groups == 0 and dim_out % groups == 0, "dim_in and dim_out must both be divisible by groups."
self.dim_in = dim_in
self.dim_out = dim_out
self.ksize = ksize
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.bias = bias
self.transpose = transpose
self.params_dim = int(dim_in * dim_out * ksize * ksize / groups)
if self.bias:
self.params_dim += dim_out
self._hypernet = nn.Linear(1, self.params_dim)
self.conv_fn = F.conv_transpose2d if transpose else F.conv2d
self._hypernet.apply(weights_init)
示例12: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x):
x = self.pixel_norm(x)
x = self.upsample(x)
if hasattr(self, 'conv'):
x = self.conv(x)
else:
kernel = self.weight * self.scale
kernel = F.pad(kernel, (0, 0, 0, 0, 1, 1, 1, 1), 'constant', 0.0)
kernel = (kernel[1:, 1:] + kernel[:-1, 1:] +
kernel[1:, :-1] + kernel[:-1, :-1])
kernel = kernel.permute(2, 3, 0, 1)
x = F.conv_transpose2d(x, kernel, stride=2, padding=1)
x = x / self.scale
x = self.wscale(x)
x = self.activate(x)
return x
示例13: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x):
"""
forward pass of the layer
:param x: input
:return: y => output
"""
from torch.nn.functional import conv_transpose2d
return conv_transpose2d(input=x,
weight=self.weight * self.scale, # scale the weight on runtime
bias=self.bias if self.use_bias else None,
stride=self.stride,
padding=self.pad)
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def forward(self, x):
return F.conv_transpose2d(x, Variable(self.w), stride=self.factor)
示例15: nd2col
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import conv_transpose2d [as 别名]
def nd2col(input_nd, kernel_size, stride=1, padding=0, output_padding=0, dilation=1, transposed=False,
use_pyinn_if_possible=False):
"""
Shape:
- Input: :math:`(N, C, L_{in})`
- Output: :math:`(N, C, *kernel_size, *L_{out})` where
:math:`L_{out} = floor((L_{in} + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1)` for non-transposed
:math:`L_{out} = (L_{in} - 1) * stride - 2 * padding + dilation * (kernel_size - 1) + 1 + output_padding` for transposed
"""
n_dims = len(input_nd.shape[2:])
kernel_size = (kernel_size,) * n_dims if isinstance(kernel_size, Number) else kernel_size
stride = (stride,) * n_dims if isinstance(stride, Number) else stride
padding = (padding,) * n_dims if isinstance(padding, Number) else padding
output_padding = (output_padding,) * n_dims if isinstance(output_padding, Number) else output_padding
dilation = (dilation,) * n_dims if isinstance(dilation, Number) else dilation
if transposed:
assert n_dims == 2, 'Only 2D is supported for fractional strides.'
w_one = input_nd.new_ones(1, 1, 1, 1)
pad = [(k - 1) * d - p for (k, d, p) in zip(kernel_size, dilation, padding)]
input_nd = F.conv_transpose2d(input_nd, w_one, stride=stride)
input_nd = F.pad(input_nd, (pad[1], pad[1] + output_padding[1], pad[0], pad[0] + output_padding[0]))
stride = _pair(1)
padding = _pair(0)
(bs, nch), in_sz = input_nd.shape[:2], input_nd.shape[2:]
out_sz = tuple([((i + 2 * p - d * (k - 1) - 1) // s + 1)
for (i, k, d, p, s) in zip(in_sz, kernel_size, dilation, padding, stride)])
# Use PyINN if possible (about 15% faster) TODO confirm the speed-up
if n_dims == 2 and dilation == 1 and has_pyinn and torch.cuda.is_available() and use_pyinn_if_possible:
output = P.im2col(input_nd, kernel_size, stride, padding)
else:
output = F.unfold(input_nd, kernel_size, dilation, padding, stride)
out_shape = (bs, nch) + tuple(kernel_size) + out_sz
output = output.view(*out_shape).contiguous()
return output