TensorFlow.js 是 TensorFlow 在浏览器和 Node.js 环境中的版本,它允许开发者在不安装任何外部库的情况下,直接在浏览器中使用 TensorFlow。以下是一些 TensorFlow.js 的示例。

示例列表

图像分类示例

以下是一个简单的图像分类示例,它展示了如何使用 TensorFlow.js 加载和训练一个模型来识别图像中的对象。

// 加载模型
const model = await tf.loadLayersModel('https://storage.googleapis.com/tensorflow.js/tfjs-models/tfjs-models/mobilenet_v1_1.0_224/model.json');

// 加载图像
const img = await tf.loadImage('path/to/your/image.jpg');

// 调整图像大小
const resized = tf.image.resizeBilinear(img, [224, 224]);

// 进行预测
const prediction = model.predict(resized);

// 输出预测结果
console.log(prediction);

了解更多关于图像分类的细节

TensorFlow.js 图像分类示例