使用Keras预训练的InceptionV3 / Xception模型时的预测不正确

时间:2019-02-19 20:01:46

标签: keras deep-learning tensorflow.js pre-trained-model

我试图在tensorflow.js中使用经过预训练的Keras InceptionV3 / Xception模型。这些模型可以很好地加载,但是输出的预测结果并不正确(请参阅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 / tfjs @ 1.0.0-alpha3

InceptionV3 predictions

ResNet50 predictions

1 个答案:

答案 0 :(得分:0)

我已经解决了该问题以供将来参考,事实证明,在将图像输入为Mobilenet does.之前,我并没有将图像标准化为[-1,1]范围 我不确定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;