微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何加载一批要由我的 tfjs 模型处理的图像

我在 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,3) 的张量中。

我该怎么做?

解决方法

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

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

如果要批量创建多张图片,可以使用tf.stack

// image1 and image2 must have the same shape (i.e (32,3))
batchImages = tf.stack([image1,image2])
// batchImages will have the following shape: (2,3)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。