栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

YOLO v3中torch.meshgrid()函数的用法说明

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

YOLO v3中torch.meshgrid()函数的用法说明

1. 函数说明

PyTorch关于torch.meshgrid()函数的说明:torch.meshgrid

y, x = torch.meshgrid(a, b)的功能是生成网格,可以用于生成坐标。

一般函数输入两个一维tensor a, b,返回两个tensor -> y, x,其中:

  • y, x的行数均为a的元素个数
  • y, x的列数均为b的元素个数
  • y: 记录y轴坐标
  • x: 记录x轴坐标

我们一般都会通过torch.stack((x, y), dim=2)方法将x, y拼接在一起

2. 例子
import torch

y, x = torch.meshgrid([torch.arange(4), torch.arange(6)])

print(f"x.shape: {x.shape}")
print(f"y.shape: {y.shape}")
print(f"x:n {x}")
print(f"y:n {y}")

print("n --------------- n")

grid = torch.stack((x, y), dim=2)
print(f"grid.shape: {grid.shape}")
print(f"grid:n {grid}")


"""
    x.shape: torch.Size([4, 6])
    y.shape: torch.Size([4, 6])
    x:
     tensor([[0, 1, 2, 3, 4, 5],
            [0, 1, 2, 3, 4, 5],
            [0, 1, 2, 3, 4, 5],
            [0, 1, 2, 3, 4, 5]])
    y:
     tensor([[0, 0, 0, 0, 0, 0],
            [1, 1, 1, 1, 1, 1],
            [2, 2, 2, 2, 2, 2],
            [3, 3, 3, 3, 3, 3]])

     --------------- 

    grid.shape: torch.Size([4, 6, 2])
    grid:
     tensor([[[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0]],

            [[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1]],

            [[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2]],

            [[0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3]]])
"""

生成的tensor对应着每个grid中cell的左上角坐标,很容易理解。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916142.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号