torch.meshgrid 的函数原型是

torch.meshgrid(*tensors, indexing=None)

indexing 是 torch.meshgrid 的一个参数。

torch.meshgrid 的功能是生成 “网格数据”,比如生成一张图像的所有像素坐标。

本文,以 高度为 4,宽度为 7(即 H=4,W=7)的图像为例子说明。

返回值不同

import torchH = 4
W = 7H_arange = torch.arange(0,H)
W_arange = torch.arange(0,W)print("\nH: 4,    W:7\n")
grid_i, grid_j = torch.meshgrid(W_arange, H_arange ,indexing='ij')
print("grid_i shape: ",grid_i.shape,"      grid_j shape: ",grid_j.shape)
print("grid_i:\n",grid_i,"\n","grid_j:\n",grid_j,'\n')grid_x, grid_y = torch.meshgrid(W_arange, H_arange ,indexing='xy')
print("grid_x shape: ",grid_x.shape,"      grid_y shape: ",grid_y.shape)
print("grid_x:\n",grid_x,"\n","grid_y:\n",grid_y,'\n')

indexing=‘ij’

当设 indexing='ij'
返回的:
grid_i shape: torch.Size([7, 4])
grid_j shape: torch.Size([7, 4])

他们的shape都是 [W,H]
shape 第 0 维是 W,第 1 维是 H
这跟 输入 torch.meshgrid 参数 W_arange, H_arange的相对顺序是一致的

相关代码输出如下


grid_i:tensor([[0, 0, 0, 0],[1, 1, 1, 1],[2, 2, 2, 2],[3, 3, 3, 3],[4, 4, 4, 4],[5, 5, 5, 5],[6, 6, 6, 6]]) grid_j:tensor([[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3],[0, 1, 2, 3]])

indexing=‘xy’

当设 indexing='xy'
grid_x shape: torch.Size([4, 7])
grid_y shape: torch.Size([4, 7])

他们的shape都是 [H,W]

shape 第 1 维是 W,第 0 维是 H
这跟 输入 torch.meshgrid 参数 W_arange, H_arange的相对顺序是相反的

相关代码输出如下

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

进一步生成像素坐标

indexing=‘ij’

代码

# OpenCV Convention: uv first column then row
coords_ij_col_row = torch.stack([grid_i,grid_j], dim=-1).reshape(-1, 2)
print(coords_ij_col_row)# Matrix Index Convention: first row then column
coords_ij_row_col = torch.stack([grid_j,grid_i], dim=-1).reshape(-1, 2)
print(coords_ij_row_col)

输出:

# OpenCV Convention: uv first column then row
tensor([[0, 0],[0, 1],[0, 2],[0, 3],......[6, 0],[6, 1],[6, 2],[6, 3]])
# Matrix Index Convention: first row then column
tensor([[0, 0],[1, 0],[2, 0],[3, 0],......[0, 6],[1, 6],[2, 6],[3, 6]])Process finished with exit code 0

indexing=‘xy’

# OpenCV Convention: uv first column then row
coords_xy_col_row = torch.stack([grid_x,grid_y], dim=-1).reshape(-1, 2)
print(coords_xy_col_row)# Matrix Index Convention: first row then column
coords_xy_row_col = torch.stack([grid_y,grid_x], dim=-1).reshape(-1, 2)
print(coords_xy_row_col)

输出:

# OpenCV Convention: uv first column then row
tensor([[0, 0],[1, 0],[2, 0],[3, 0],[4, 0],[5, 0],[6, 0],......[0, 3],[1, 3],[2, 3],[3, 3],[4, 3],[5, 3],[6, 3]])# Matrix Index Convention: first row then column
tensor([[0, 0],[0, 1],[0, 2],[0, 3],[0, 4],[0, 5],[0, 6],
......[3, 0],[3, 1],[3, 2],[3, 3],[3, 4],[3, 5],[3, 6]])Process finished with exit code 0

如果不认为设置indexing参数,pytorch 默认 indexing = ‘ij’

如果不设置,pytorch 会报错

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument.

总结

根据矩阵索引传统,先 row 后 column,那就要选择indexing = ‘xy’

grid_x, grid_y = torch.meshgrid(W_arange, H_arange ,indexing='xy')
# Matrix Index Convention: first row then column
coords_xy_row_col = torch.stack([grid_y,grid_x], dim=-1).reshape(-1, 2)
print(coords_xy_row_col)

根据OpenCV索引传统,先 column 后row ,那就要选择indexing = ‘ij’

grid_i, grid_j = torch.meshgrid(W_arange, H_arange ,indexing='ij')
# OpenCV Convention: uv first column then row
coords_ij_col_row = torch.stack([grid_i,grid_j], dim=-1).reshape(-1, 2)
print(coords_ij_col_row)

torch.meshgrid 使用探究相关推荐

  1. torch.meshgrid

    原理 ```python torch.meshgrid(*tensors) ``` 这个是干嘛的?根据输入来创建网格的,例如你告诉我两个列表[1,2],[3],我们可以在二维直角坐标系上标上两个点(1 ...

  2. 【pytorch】torch.meshgrid()==>常用于生成二维网格,比如图像的坐标点

    np.meshgrid()函数常用于生成二维网格,比如图像的坐标点. x1 ,y1 = torch.meshgrid(x,y) 输入参数: 参数是两个,第一个参数我们假设是x,第二个参数假设就是y   ...

  3. torch.meshgrid()函数解析

    torch.meshgrid()函数解析 torch.meshgrid()的功能是生成网格,可以用于生成坐标.函数输入两个数据类型相同的一维张量,两个输出张量的行数为第一个输入张量的元素个数,列数为第 ...

  4. torch.meshgrid函数

    参考资料: https://pytorch.org/docs/stable/generated/torch.meshgrid.html 在此记录下torch.meshgrid的用法,该函数常常用于生成 ...

  5. Yolov5训练时报错:UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the ind

    Yolov5训练时报错:UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the ind ...

  6. YOLO UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing a

    在运行yolo时出现了一个警告return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined] 解决方法:找到pyrcharm所 ...

  7. UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu

    在本地做yolov5时出现了以下错误(本地无gpu) 经查阅资料方向是内存不够,遇到这种情况可以将batchsize减小. 原来: python train.py --img 640 --batch ...

  8. 训练YOLOv7出现的UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the inde

    直接点击报错处转到functional文件 找到这一处,将indexing='ij'加入到return后

  9. python meshgrid_torch.meshgrid()和np.meshgrid()的区别

    np.meshgrid()函数常用于生成二维网格,比如图像的坐标点. pytorch中也有一个类似的函数torch.meshgrid(),功能也类似,但是两者的用法有区别,使用时需要注意(刚踩坑,因此 ...

最新文章

  1. python装饰器-python装饰器简介---这一篇也许就够了(推荐)
  2. ik mysql热加载分词_Elasticsearch 之(25)重写IK分词器源码来基于mysql热更新词库...
  3. AI基础:特征工程-文本特征处理
  4. c++将字符串转换成 int 类型
  5. python自动登录qq邮箱_selenium+python实现自动登陆QQ邮箱并发送邮件功能
  6. opencv-api matchTemplate
  7. 吴恩达《机器学习》第十五章:异常检测
  8. 使用axure的团队项目功能
  9. 解决jz2440不能ping同主机问题
  10. 《统计学习方法》P179页10.22前向后向算法公式推导
  11. c语言二叉树的序列化,不怕面试被问了!二叉树算法大盘点
  12. Leetcode之二叉树展开为链表(深搜)
  13. Chrome浏览器的Network面板介绍
  14. 数据库服务器位置,怎么查看服务器数据库的位置
  15. PMP考试备考指南基础知识
  16. 每天30分钟学python-每天 3 分钟,小闫带你学 Python(二十三)
  17. 深度学习目标检测模型汇总(论文,源码,数据集,推荐收藏)
  18. java登陆密码加密怎么做,Java如何实现密码加密
  19. Pandas数据分析groupby函数深度总结(1)
  20. 纯前端实现—网页钟表设计

热门文章

  1. Google Groups 精彩推荐
  2. 锦鱼课堂:2021做什么副业好?零门槛赚钱小副业
  3. 数据化管理 - 洞悉零售及电商运营【初始】
  4. via浏览器如何使用插件 Via浏览器添加使用插件教程
  5. 如何实现Word、PDF、TXT文件的全文内容检索?
  6. java计算机毕业设计基于安卓Android的校园单车租赁App
  7. Android 3年外包工面试笔记,有机会还是要去大厂学习提升
  8. 如何在官网下载tomcat
  9. [问题]mpu9250+bmp280数据读取
  10. 在word修改模式下如何进行修改