使用 Keras 预训练 InceptionV3/Xception 模型时的错误预测

Incorrect predictions when using the Keras pre-trained InceptionV3/Xception models

我正在尝试让预训练的 Keras InceptionV3/Xception 模型在 tensorflow.js 中运行。模型加载完美,但输出预测远非正确(参见 InceptionV3 预测照片)

我还有 saved/converted ResNet50 模型,它工作得很好。

这些模型目前与 tensorflow.js 不兼容吗?还是我的代码有问题?

模型 saved/converted 具有以下特征:

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

此处提供代码(angular 应用程序):https://github.com/BenMcFadyen/tfjs_test

重要部分:https://github.com/BenMcFadyen/tfjs_test/blob/master/src/app/app.component.ts

版本:

InceptionV3 predictions

ResNet50 predictions

我已经解决了这个问题以供将来参考,事实证明我在将图像输入到模型之前没有将图像标准化到 [-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;