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. 首战大吉:红帆科技参加第八届IT羽毛球赛
  2. java ssh框架 struts spring hibernate 简介
  3. 题目1170:找最小数
  4. 包装类java_java中的包装类
  5. BZOJ4818 [SDOI2017] 序列计数 【矩阵快速幂】
  6. java处理json回车_json中换行符的处理方法示例介绍
  7. Java编程讲义之Eclipse开发工具
  8. SpringMVC源码解析(四)——请求处理
  9. The Two Routes CodeForces - 601A(水最短路)
  10. 量数据导出Excel 之 多重影分身之术
  11. Docker学习总结(54)——save,load,import 命令有何区别
  12. Google Chrome 势要消灭不安全的非 HTTPS 页面!
  13. 2020年Mybatis源码解读
  14. ModBus TCP/IP协议
  15. mysql cmd定时_windows下定时执行mysql冷备份
  16. ba无标度网络python_python绘制BA无标度网络
  17. SELinux用audio2allow生成添加权限的格式及neverallow解决方法
  18. 网站加载速度影响因素以及如何增强
  19. 计算机网络—学习笔记
  20. linux在指定行添加内容,linux下利用shell在指定的行添加内容的方法

热门文章

  1. Oracle Parallel Query
  2. 微信公众号订阅号留言点赞采集抓取爬虫
  3. 7.python基础之基础数据类型--字典dict
  4. Transitive attribute传递属性
  5. PTA 7-2 一帮一
  6. android应用开发完全
  7. puzzle(0711)《机关排布》接水管、搭桥
  8. DC1+2: Tcl与Design Compiler (一二)——前言+DC综合与Tcl语法结构概述+DCT/DCG+wireloadmodel+Comments
  9. 【ubuntu拷贝目录】cp: omitting directory”错误的解释和解决办法
  10. Excel调整行高,鼠标右键直接选中一行进行调整了