document: v2怎么使用训练出来的model进行图片分类判断?
caoyonghao opened this issue · comments
按照官网进行了训练,发现v2版本没有提到怎么运用model进行判断,类似v1的这个
const predict = require('/path/to/output');
const app = express();
app.post('/predict', async (req, res) => {
const r = await predict(req.body.image);
res.json(r);
});
app.listen(8080);
简单示例
const tf = require('@tensorflow/tfjs-node');
const { Image } = require('@pipcook/datacook');
(async () => {
const model = await tf.loadLayersModel(
'file:///pipcook/example/pipelines/20210804172628/model/model.json' // 模型本地位置
);
model.summary(); // 查看模型概要信息
const img = (
await Image.read(
'/pipcook/example/pipelines/20210804172628/data/test/0a3e8b60-1cb9-11ea-a3c0-69b27346a20f-screenshot.png'
)
)
.resize(416, 416) // 根据概要输入来修改
.toTensor();
// 转成四维
const res = model.predict(img.reshape([1, ...img.shape]));
console.log(res.toString());
})();
参考demo运行成功
简单示例
const tf = require('@tensorflow/tfjs-node'); const { Image } = require('@pipcook/datacook'); (async () => { const model = await tf.loadLayersModel( 'file:///pipcook/example/pipelines/20210804172628/model/model.json' // 模型本地位置 ); model.summary(); // 查看模型概要信息 const img = ( await Image.read( '/pipcook/example/pipelines/20210804172628/data/test/0a3e8b60-1cb9-11ea-a3c0-69b27346a20f-screenshot.png' ) ) .resize(416, 416) // 根据概要输入来修改 .toTensor(); // 转成四维 const res = model.predict(img.reshape([1, ...img.shape])); console.log(res.toString()); })();
使用文本分类训练出了一个model.json。怎么运用model进行判断 ? 使用@tensorflow/tfjs 的loadLayersModel时报错
Error: The JSON from HTTP path http://127.0.0.1:8080/textClassification/model/model.json contains neither model topology or manifest for weights.
@weimingxinas 参考一下text-classification-bayes训练模型的源码,注意输入参数的处理和predict函数对预测结构的处理