taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.57k stars 2.29k forks source link

Modify the shape of the ndarray returned by field.to_numpy() method #3997

Closed neozhaoliang closed 2 years ago

neozhaoliang commented 2 years ago

The shape of the numpy.ndarray returned by field.to_numpy() method is (width, height), which has width rows and height columns. This is the transposed version of the usual convention. Shall we do this transpose internally so that the returned shape is (height, width)?

For example:

import taichi as ti
ti.init(arch=ti.cpu)
import matplotlib.pyplot as plt

resolution = (400, 200)  # image width, image height
x = ti.Vector.field(3, ti.f32, shape=resolution)
x.fill(0)

arr = x.to_numpy()
print(arr.shape)
plt.imshow(arr)
plt.show()

shows the following image, which is the transposition of the intended image:

Figure_1

import taichi as ti
ti.init()

resolution = (400, 200)  # image width, image height
x = ti.Vector.field(3, ti.f32, shape=resolution)
x.fill(0)

gui = ti.GUI("test", res=resolution)
gui.set_image(x)
gui.show()
ailzhang commented 2 years ago

@neozhaoliang would you mind showing a small example code for this?

neozhaoliang commented 2 years ago

@ailzhang Example code and image are added.

ifsheldon commented 2 years ago

The "usual convention" you said is not a usual convention in linear algebra. The dimension convention for images happens to be the transposed generic 2D matrix. For general linear algebra use cases, the first dimension of a matrix (generally a tensor) is just the first dimension and nothing else. Even if you see the convention in CV applications of PyTorch, it's not like what you usually expect to represent images. If you decide a 2D matrix to be an image, just transpose it yourself and please don't enforce any more conventions in the APIs.