TensorFlow.js 中的频道优先格式

Channels first format in TensorFlow.js

我正在转换 https://github.com/Richard-An/StyleNAS to a browser compatible model. I am using TensorFlow.js. Due to some issues in the conversion libraries (https://github.com/nerox8664/pytorch2keras),我想将模型保留为 channels_first (NCHW) 格式。

我知道 TensorFlow.js 支持这个。我可以以所需的格式成功导入我的模型。但是我的数据有问题。当我通过 https://js.tensorflow.org/api/2.7.0/#browser.fromPixels 将图像转换为张量时,它们总是采用 channels_last (NHWC) 格式。有什么办法可以明确地改变这个吗?我是 TensorFlow.js 的新手,找不到任何答案。

谢谢

轴可以交换

HWC -> CHW(对于 3d 张量)

tensor.transpose([2, 0, 1])

NHWC -> NCHW(对于 4d 张量)

tensor.transpose([0, 3, 1, 2])