node.js - 如何加载一批图像以供我的 tfjs 模型处理

标签 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(image, 'float32');
//other image processing

这将创建一个形状为(imageWidth, imageHeight, 3)的单个张量。但我想将一批图像加载到形状为 (batchNumber, imageWidth, imageHeight, 3) 的张量中。

我该如何去做呢?

最佳答案

如果您想为图像添加批量尺寸,可以使用 tf.expandDims

// let say that image has the following shape: (32,32,3)
imageWithBatchDim = image.expandDims(0)
// imageWithBatchDim has the following shape: (1,32,32,3)

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

// 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)

关于node.js - 如何加载一批图像以供我的 tfjs 模型处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66140820/

相关文章:

tensorflow - 在 TensorFlow 中保存特定权重

c++ - 在 Ubuntu 17.10 中从 C/C++ 源构建 Tensorflow 1.4.0

javascript - TensorFlow.js 返回类型错误 : Cannot read property 'concat' of undefined when loading models

javascript - 需要配置 : get `paths` object from JSON in Gruntfile. js 和 main.js

node.js - Watson Discovery API - NodeJS 未按预期工作

python - 如何在 Resnet 50 分类中输出置信度?

Tensorflow js VS Tensorflow Lite

image - 将路径图像转换为新的 Tensor 为 [1, 224, 224, 3] 形状

node.js - 为什么 setImmediate() 比顺序代码运行得更快?

node.js - 设计 Mongoose 模型