当前位置: 首页>>代码示例>>Python>>正文


Python Variable.bmm方法代码示例

本文整理汇总了Python中torch.autograd.Variable.bmm方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.bmm方法的具体用法?Python Variable.bmm怎么用?Python Variable.bmm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在torch.autograd.Variable的用法示例。


在下文中一共展示了Variable.bmm方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: F_affine2d

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import bmm [as 别名]
def F_affine2d(x, matrix, center=True):
    """
    2D Affine image transform on torch.autograd.Variable
    """
    if matrix.dim() == 2:
        matrix = matrix.view(-1,2,3)

    A_batch = matrix[:,:,:2]
    if A_batch.size(0) != x.size(0):
        A_batch = A_batch.repeat(x.size(0),1,1)
    b_batch = matrix[:,:,2].unsqueeze(1)

    # make a meshgrid of normal coordinates
    _coords = th_iterproduct(x.size(1),x.size(2))
    coords = Variable(_coords.unsqueeze(0).repeat(x.size(0),1,1).float(),
                    requires_grad=False)
    if center:
        # shift the coordinates so center is the origin
        coords[:,:,0] = coords[:,:,0] - (x.size(1) / 2. + 0.5)
        coords[:,:,1] = coords[:,:,1] - (x.size(2) / 2. + 0.5)

    # apply the coordinate transformation
    new_coords = coords.bmm(A_batch.transpose(1,2)) + b_batch.expand_as(coords)

    if center:
        # shift the coordinates back so origin is origin
        new_coords[:,:,0] = new_coords[:,:,0] + (x.size(1) / 2. + 0.5)
        new_coords[:,:,1] = new_coords[:,:,1] + (x.size(2) / 2. + 0.5)

    # map new coordinates using bilinear interpolation
    x_transformed = F_bilinear_interp2d(x, new_coords)

    return x_transformed
开发者ID:BrianDo2005,项目名称:torchsample,代码行数:35,代码来源:affine.py

示例2: F_batch_affine3d

# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import bmm [as 别名]
def F_batch_affine3d(x, matrix, center=True):
    """

    x : torch.Tensor
        shape = (Samples, C, H, W)
        NOTE: Assume C is always equal to 1!
    matrix : torch.Tensor
        shape = (Samples, 6) or (Samples, 2, 3)

    Example
    -------
    >>> x = Variable(torch.zeros(3,1,10,10,10))
    >>> x[:,:,3:7,3:7,3:7] = 1
    >>> m1 = torch.FloatTensor([[1.2,0,0,0],[0,1.2,0,0],[0,0,1.2,0]])
    >>> m2 = torch.FloatTensor([[0.8,0,0,0],[0,0.8,0,0],[0,0,0.8,0]])
    >>> m3 = torch.FloatTensor([[1.0,0,0,3],[0,1.0,0,3],[0,0,1.0,3]])
    >>> matrix = Variable(torch.stack([m1,m2,m3]))
    >>> xx = F_batch_affine3d(x,matrix)
    """
    if matrix.dim() == 2:
        matrix = matrix.view(-1,3,4)

    A_batch = matrix[:,:3,:3]
    b_batch = matrix[:,:3,3].unsqueeze(1)

    # make a meshgrid of normal coordinates
    _coords = th_iterproduct(x.size(2),x.size(3),x.size(4))
    coords = Variable(_coords.unsqueeze(0).repeat(x.size(0),1,1).float(),
                requires_grad=False)
    
    if center:
        # shift the coordinates so center is the origin
        coords[:,:,0] = coords[:,:,0] - (x.size(2) / 2. + 0.5)
        coords[:,:,1] = coords[:,:,1] - (x.size(3) / 2. + 0.5)
        coords[:,:,2] = coords[:,:,2] - (x.size(4) / 2. + 0.5)
    
    # apply the coordinate transformation
    new_coords = coords.bmm(A_batch.transpose(1,2)) + b_batch.expand_as(coords)

    if center:
        # shift the coordinates back so origin is origin
        new_coords[:,:,0] = new_coords[:,:,0] + (x.size(2) / 2. + 0.5)
        new_coords[:,:,1] = new_coords[:,:,1] + (x.size(3) / 2. + 0.5)
        new_coords[:,:,2] = new_coords[:,:,2] + (x.size(4) / 2. + 0.5)

    # map new coordinates using bilinear interpolation
    x_transformed = F_batch_trilinear_interp3d(x, new_coords)

    return x_transformed
开发者ID:BrianDo2005,项目名称:torchsample,代码行数:51,代码来源:affine.py


注:本文中的torch.autograd.Variable.bmm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。