pytorch-image-models
48 строк · 1.6 Кб
1from typing import Tuple2
3import torch4
5
6def ndgrid(*tensors) -> Tuple[torch.Tensor, ...]:7"""generate N-D grid in dimension order.8
9The ndgrid function is like meshgrid except that the order of the first two input arguments are switched.
10
11That is, the statement
12[X1,X2,X3] = ndgrid(x1,x2,x3)
13
14produces the same result as
15
16[X2,X1,X3] = meshgrid(x2,x1,x3)
17
18This naming is based on MATLAB, the purpose is to avoid confusion due to torch's change to make
19torch.meshgrid behaviour move from matching ndgrid ('ij') indexing to numpy meshgrid defaults of ('xy').
20
21"""
22try:23return torch.meshgrid(*tensors, indexing='ij')24except TypeError:25# old PyTorch < 1.10 will follow this path as it does not have indexing arg,26# the old behaviour of meshgrid was 'ij'27return torch.meshgrid(*tensors)28
29
30def meshgrid(*tensors) -> Tuple[torch.Tensor, ...]:31"""generate N-D grid in spatial dim order.32
33The meshgrid function is similar to ndgrid except that the order of the
34first two input and output arguments is switched.
35
36That is, the statement
37
38[X,Y,Z] = meshgrid(x,y,z)
39produces the same result as
40
41[Y,X,Z] = ndgrid(y,x,z)
42Because of this, meshgrid is better suited to problems in two- or three-dimensional Cartesian space,
43while ndgrid is better suited to multidimensional problems that aren't spatially based.
44"""
45
46# NOTE: this will throw in PyTorch < 1.10 as meshgrid did not support indexing arg or have47# capability of generating grid in xy order before then.48return torch.meshgrid(*tensors, indexing='xy')49
50