Search code examples
kerasdeep-learningtensorflow.jspre-trained-model

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


I'm trying to get the pre-trained Keras InceptionV3/Xception models working in tensorflow.js. The models load perfectly fine, however the output predictions are far from correct (see InceptionV3 prediction photo)

I've also saved/converted the ResNet50 model, which is working perfectly fine.

Are these models simply incompatible with tensorflow.js currently? or is there something amiss with my code?

Models were saved/converted with the following:

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

Code available here (angular app): https://github.com/BenMcFadyen/tfjs_test

The important part: https://github.com/BenMcFadyen/tfjs_test/blob/master/src/app/app.component.ts

Versions:

  • Chrome: 72.0.3626.109
  • @tensorflow/tfjs@1.0.0-alpha3

InceptionV3 predictions

ResNet50 predictions


Solution

  • I've solved the issue for future reference, it turns out I wasn't normalizing the images into the range [-1, 1] before inputting them to the model as Mobilenet does. I'm not sure why the ResNet50 works without the normalization however.

    Normalization code:

     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;