【问题标题】:The use of meshgrid in pytorch/numpypytorch/numpy中meshgrid的使用
【发布时间】:2020-12-04 15:32:38
【问题描述】:

下面的代码是取自https://blog.paperspace.com/how-to-implement-a-yolo-v3-object-detector-from-scratch-in-pytorch-part-3/ 的sn-p,我对它试图实现的目标感到困惑。

    grid = np.arange(grid_size)
    a,b = np.meshgrid(grid, grid)

    x_offset = torch.FloatTensor(a).view(-1,1)
    y_offset = torch.FloatTensor(b).view(-1,1)

    if CUDA:
        x_offset = x_offset.cuda()
        y_offset = y_offset.cuda()

    x_y_offset = torch.cat((x_offset, y_offset), 1).repeat(1,num_anchors).view(-1,2).unsqueeze(0)

我尝试了grid_size = 3时的情况,它输出:

tensor([[0., 1.],
        [2., 0.],
        [1., 2.],
        [0., 1.],
        [2., 0.],
        [0., 0.],
        [1., 1.],
        [1., 2.],
        [2., 2.],
        [0., 1.],
        [2., 0.],
        [1., 2.],
        [0., 1.],
        [2., 0.],
        [0., 0.],
        [1., 1.],
        [1., 2.],
        [2., 2.],
        [0., 1.],
        [2., 0.],
        [1., 2.],
        [0., 1.],
        [2., 0.],
        [0., 0.],
        [1., 1.],
        [1., 2.],
        [2., 2.]])

我不太明白这里的模式是什么。根据给定链接中的描述,我认为我真的应该期待这样的事情:

tensor([[0,0],
        [0,0],
        [0,0],
        [0,1],
        [0,1],
        [0,1],
        [0,2],
        [0,2],
        ...]])

【问题讨论】:

  • 你不能一步步运行它并检查结果。如果我安装了torch,我会这样做。我尝试猜测等效的numpy 步骤,但放弃了。 ab 很明显。
  • 我认为操作在 numpy 和 torch 中都做同样的事情,所以使用 numpy 或 torch 并不重要。我确实是一步步运行的,但结果与我的预期不符。

标签: numpy pytorch tensor


【解决方案1】:

如果您期望显示的第二个输出,只需更改

x_y_offset = (
    torch.cat((x_offset, y_offset), 1).repeat(1, num_anchors).view(-1, 2).unsqueeze(0)
)

x_y_offset = (
    torch.cat((y_offset, x_offset), 1).repeat(1, num_anchors).view(-1, 2).unsqueeze(0)
)

它只是与meshgrid的输出顺序有关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2013-08-14
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多