Node.js 如何加载要由tfjs模型处理的一批图像

Node.js 如何加载要由tfjs模型处理的一批图像,node.js,tensorflow,tensorflow.js,tfjs-node,Node.js,Tensorflow,Tensorflow.js,Tfjs Node,我在Keras中创建了一个模型,并将其转换为Tensorflow.js模型,然后将其加载到node.js项目中。现在我想从Tensorflow.js中的这个模型中得到预测。我已经了解了如何加载单个图像: var singleImageData = fs.readFileSync('path/to/image.jpeg'); var image = tf.node.decodeImage(new Uint8Array(singleImageData), 3); image = tf.cast(im

我在Keras中创建了一个模型,并将其转换为Tensorflow.js模型,然后将其加载到node.js项目中。现在我想从Tensorflow.js中的这个模型中得到预测。我已经了解了如何加载单个图像:

var singleImageData = fs.readFileSync('path/to/image.jpeg');
var image = tf.node.decodeImage(new Uint8Array(singleImageData), 3);
image = tf.cast(image, 'float32');
//other image processing
这将创建形状的单个张量
(imageWidth,imageHeight,3)
。但是我想将一批图像加载到一个张量中,其形状为
(batchNumber,imageWidth,imageHeight,3)



我该怎么做呢?

如果要向图像添加批处理维度,可以使用

如果要创建一批多个图像,可以使用

// let say that image has the following shape: (32,32,3)
imageWithBatchDim = image.expandDims(0)
// imageWithBatchDim has the following shape: (1,32,32,3)
// image1 and image2 must have the same shape (i.e (32,32,3))
batchImages = tf.stack([image1, image2])
// batchImages will have the following shape: (2,32,32,3)