我正在尝试让预先训练的 Keras InceptionV3/Xception 模型在 tensorflow.js 中工作。模型加载得非常好,但是输出预测远不正确(参见 InceptionV3 预测照片)
我还保存/转换了 ResNet50 模型,该模型工作得很好。
这些模型目前是否与tensorflow.js根本不兼容?或者我的代码有问题吗?
使用以下内容保存/转换模型:
from keras.applications import inception_v3
model = inception_v3.InceptionV3(include_top=True, weights='imagenet')
model.save("InceptionV3.h5", False)
tensorflowjs_converter --input_format=keras InceptionV3.h5 InceptionV3
此处提供代码(角度应用程序):https://github.com/BenMcFadyen/tfjs_test
重要部分:https://github.com/BenMcFadyen/tfjs_test/blob/master/src/app/app.component.ts
版本:
- Chrome:72.0.3626.109
- @tensorflow/[email protected]
最佳答案
我已经解决了这个问题以供将来引用,事实证明,在将图像输入到模型中之前,我没有将图像标准化为范围 [-1, 1] Mobilenet does. 但我不确定为什么 ResNet50 在没有标准化的情况下也能工作。
标准化代码:
let tensor = tf.browser.fromPixels(canvas, number_channels);
let normalizationOffset = tf.scalar(127.5);
var normalized = tensor.toFloat().sub(normalizationOffset).div(normalizationOffset);
var batched = resized.reshape([1, imgSize, imgSize, 3]);
var output = model.predict(batched) as any;
关于keras - 使用 Keras 预训练的 InceptionV3/Xception 模型时预测不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774073/