本文整理汇总了Python中torch.nn.functional.fold方法的典型用法代码示例。如果您正苦于以下问题:Python functional.fold方法的具体用法?Python functional.fold怎么用?Python functional.fold使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.fold方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: col2im_indices
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def col2im_indices(
cols: Tensor,
x_shape: Tuple[int, int, int, int],
kernel_height: int,
kernel_width: int,
padding: Tuple[int, int] = (0, 0),
stride: Tuple[int, int] = (1, 1),
) -> Tensor:
# language=rst
"""
col2im is a special case of fold which is implemented inside of Pytorch.
:param cols: Image tensor in column-wise format.
:param x_shape: Shape of original image tensor.
:param kernel_height: Height of the convolutional kernel in pixels.
:param kernel_width: Width of the convolutional kernel in pixels.
:param padding: Amount of zero padding on the input image.
:param stride: Amount to stride over image by per convolution.
:return: Image tensor in original image shape.
"""
return F.fold(
cols, x_shape, (kernel_height, kernel_width), padding=padding, stride=stride
)
示例2: col2im_indices
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def col2im_indices(
cols: Tensor,
x_shape: Tuple[int, int, int, int],
kernel_height: int,
kernel_width: int,
padding: Tuple[int, int] = (0, 0),
stride: Tuple[int, int] = (1, 1),
) -> Tensor:
# language=rst
"""
col2im is a special case of fold which is implemented inside of Pytorch.
:param cols: Image tensor in column-wise format.
:param x_shape: Shape of original image tensor.
:param kernel_height: Height of the convolutional kernel in pixels.
:param kernel_width: Width of the convolutional kernel in pixels.
:param padding: Amount of zero padding on the input image.
:param stride: Amount to stride over image by per convolution.
:return: Image tensor in original image shape.
"""
return F.fold(
cols,
x_shape,
(kernel_height, kernel_width),
padding=padding,
stride=stride,
)
示例3: test_fold
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def test_fold(self):
inp = torch.randn(3, 20, 20, device='cuda', dtype=self.dtype)
inp_folded = F.fold(inp, (4, 5), (1, 1))
示例4: get_max_window
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def get_max_window(input_image, window_shape, pooling_logic="avg"):
"""
Function that makes a sliding window of size window_shape over the
input_image and return the UPPER_LEFT corner index with max sum
:param input_image: N*C*H*W
:param window_shape: h*w
:return: N*C*2 tensor
"""
N, C, H, W = input_image.size()
if pooling_logic == "avg":
# use average pooling to locate the window sums
pool_map = torch.nn.functional.avg_pool2d(input_image, window_shape, stride=1)
elif pooling_logic in ["std", "avg_entropy"]:
# create sliding windows
output_size = (H - window_shape[0] + 1, W - window_shape[1] + 1)
sliding_windows = F.unfold(input_image, kernel_size=window_shape).view(N,C, window_shape[0]*window_shape[1], -1)
# apply aggregation function on each sliding windows
if pooling_logic == "std":
agg_res = sliding_windows.std(dim=2, keepdim=False)
elif pooling_logic == "avg_entropy":
agg_res = -sliding_windows*torch.log(sliding_windows)-(1-sliding_windows)*torch.log(1-sliding_windows)
agg_res = agg_res.mean(dim=2, keepdim=False)
# merge back
pool_map = F.fold(agg_res, kernel_size=(1, 1), output_size=output_size)
_, _, _, W_map = pool_map.size()
# transform to linear and get the index of the max val locations
_, max_linear_idx = torch.max(pool_map.view(N, C, -1), -1)
# convert back to 2d index
max_idx_x = max_linear_idx / W_map
max_idx_y = max_linear_idx - max_idx_x * W_map
# put together the 2d index
upper_left_points = torch.cat([max_idx_x.unsqueeze(-1), max_idx_y.unsqueeze(-1)], dim=-1)
return upper_left_points
示例5: get_arguments
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--batchSz', type=int, default=1)
parser.add_argument('--dataset_name', type=str, default="iseg2017")
parser.add_argument('--dim', nargs="+", type=int, default=(64, 64, 64))
parser.add_argument('--nEpochs', type=int, default=250)
parser.add_argument('--classes', type=int, default=4)
parser.add_argument('--samples_train', type=int, default=1)
parser.add_argument('--samples_val', type=int, default=1)
parser.add_argument('--split', type=float, default=0.8)
parser.add_argument('--inChannels', type=int, default=2)
parser.add_argument('--inModalities', type=int, default=2)
parser.add_argument('--fold_id', default='1', type=str, help='Select subject for fold validation')
parser.add_argument('--lr', default=1e-2, type=float,
help='learning rate (default: 1e-3)')
parser.add_argument('--cuda', action='store_true', default=True)
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('--model', type=str, default='UNET3D',
choices=('VNET', 'VNET2', 'UNET3D', 'DENSENET1', 'DENSENET2', 'DENSENET3', 'HYPERDENSENET'))
parser.add_argument('--opt', type=str, default='sgd',
choices=('sgd', 'adam', 'rmsprop'))
parser.add_argument('--pretrained',
default='../saved_models/UNET3D_checkpoints/UNET3D_25_05___15_15_iseg2017_/UNET3D_25_05___15_15_iseg2017__last_epoch.pth',
type=str, metavar='PATH',
help='path to pretrained model')
args = parser.parse_args()
args.save = '../inference_checkpoints/' + args.model + '_checkpoints/' + args.model + '_{}_{}_'.format(
utils.datestr(), args.dataset_name)
args.tb_log_dir = '../runs/'
return args
示例6: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def forward(self, mixture_w):
"""
Args:
mixture_w (:class:`torch.Tensor`): Tensor of shape
[batch, n_filters, n_frames]
Returns:
:class:`torch.Tensor`
estimated mask of shape [batch, n_src, n_filters, n_frames]
"""
batch, n_filters, n_frames = mixture_w.size()
output = self.bottleneck(mixture_w) # [batch, bn_chan, n_frames]
output = unfold(output.unsqueeze(-1), kernel_size=(self.chunk_size, 1),
padding=(self.chunk_size, 0), stride=(self.hop_size, 1))
n_chunks = output.size(-1)
output = output.reshape(batch, self.bn_chan, self.chunk_size, n_chunks)
# Apply stacked DPRNN Blocks sequentially
output = self.net(output)
# Map to sources with kind of 2D masks
output = self.first_out(output)
output = output.reshape(batch * self.n_src, self.bn_chan,
self.chunk_size, n_chunks)
# Overlap and add:
# [batch, out_chan, chunk_size, n_chunks] -> [batch, out_chan, n_frames]
to_unfold = self.bn_chan * self.chunk_size
output = fold(output.reshape(batch * self.n_src, to_unfold, n_chunks),
(n_frames, 1), kernel_size=(self.chunk_size, 1),
padding=(self.chunk_size, 0),
stride=(self.hop_size, 1))
# Apply gating
output = output.reshape(batch * self.n_src, self.bn_chan, -1)
output = self.net_out(output) * self.net_gate(output)
# Compute mask
score = self.mask_net(output)
est_mask = self.output_act(score)
est_mask = est_mask.view(batch, self.n_src, self.out_chan, n_frames)
return est_mask
示例7: __init__
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import fold [as 别名]
def __init__(self, in_chan, n_src, out_chan=None, bn_chan=128, hid_size=128,
chunk_size=100, hop_size=None, n_repeats=6, norm_type="gLN",
mask_act='relu', bidirectional=True, rnn_type="LSTM",
num_layers=1, dropout=0):
super(DPRNN, self).__init__()
self.in_chan = in_chan
out_chan = out_chan if out_chan is not None else in_chan
self.out_chan = out_chan
self.bn_chan = bn_chan
self.hid_size = hid_size
self.chunk_size = chunk_size
hop_size = hop_size if hop_size is not None else chunk_size // 2
self.hop_size = hop_size
self.n_repeats = n_repeats
self.n_src = n_src
self.norm_type = norm_type
self.mask_act = mask_act
self.bidirectional = bidirectional
self.rnn_type = rnn_type
self.num_layers = num_layers
self.dropout = dropout
layer_norm = norms.get(norm_type)(in_chan)
bottleneck_conv = nn.Conv1d(in_chan, bn_chan, 1)
self.bottleneck = nn.Sequential(layer_norm, bottleneck_conv)
# Succession of DPRNNBlocks.
net = []
for x in range(self.n_repeats):
net += [DPRNNBlock(bn_chan, hid_size, norm_type=norm_type,
bidirectional=bidirectional, rnn_type=rnn_type,
num_layers=num_layers, dropout=dropout)]
self.net = nn.Sequential(*net)
# Masking in 3D space
net_out_conv = nn.Conv2d(bn_chan, n_src*bn_chan, 1)
self.first_out = nn.Sequential(nn.PReLU(), net_out_conv)
# Gating and masking in 2D space (after fold)
self.net_out = nn.Sequential(nn.Conv1d(bn_chan, bn_chan, 1), nn.Tanh())
self.net_gate = nn.Sequential(nn.Conv1d(bn_chan, bn_chan, 1),
nn.Sigmoid())
self.mask_net = nn.Conv1d(bn_chan, out_chan, 1, bias=False)
# Get activation function.
mask_nl_class = activations.get(mask_act)
# For softmax, feed the source dimension.
if has_arg(mask_nl_class, 'dim'):
self.output_act = mask_nl_class(dim=1)
else:
self.output_act = mask_nl_class()