使用 Tensorflow JS 创建具有 tf.browser.fromPixels 的多个图像的张量

Create a tensor of multiple images with tf.browser.fromPixels with Tensorflow JS

我想使用 tf.browser.fromPixels.

创建特征 多个图像的张量
const image1 = new ImageData(1, 1);
image1.data[0] = 100;
image1.data[1] = 150;
image1.data[2] = 200;
image1.data[3] = 255;

const image2 = new ImageData(1, 1);
image2.data[0] = 200;
image2.data[1] = 250;
image2.data[2] = 200;
image2.data[3] = 255;

const imageTensor1 = tf.browser.fromPixels(image1);
const imageTensor2 = tf.broser.fromPixels(image2);

// How do I now get imageTensor1 and imageTensor2 into one merged tensor?
const featureData = ???

我现在如何将两个张量合并为一个张量,包含有关两个图像的信息?是否有任何操作,如 tensor.push() 或任何可用的操作?我是否需要将这些值保留为一个数组,最后只将其合并为一个张量?

实际上我一开始就知道没有必要这样做,因为 model.fit() 也接受张量数组。所以你可以用多个张量创建一个数组。

https://js.tensorflow.org/api/latest/#tf.LayersModel.fit

您可以将张量与 tf.concat(tensors, axis?)

连接起来
const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
a.concat(b);

或多个张量

const a = tf.tensor1d([1, 2]);
const b = tf.tensor1d([3, 4]);
const c = tf.tensor1d([5, 6]);
tf.concat([a, b, c]);