Harry
Harry

Reputation: 4050

Tensorflow.js converted model predicting different/inaccurate results than the frozen model

When i convert a frozen PB model to a tensorflow JS model I loose all acuracy with predictions. Can anyone tell me why and what I am doing wrong?

I have done the following things - I have retraining the ImageNet model with my own dataset as described here: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0

I get accurate results with the frozen model when i run the following command for example:

python3 -m scripts.label_image \
    --graph=tf_files/retrained_graph.pb  \
    --image=/mnt/c//Users/Harry/Pictures/220px-Afghane.jpg

The follow output it gives is spot on:

afghan hound (score=0.98313)
briard (score=0.00433)
lhasa (score=0.00401)
sussex spaniel (score=0.00346)
otterhound (score=0.00116)

I have converted my frozen model to a Tensorflow JS using the tensorflow JS converter with the following command:

tensorflowjs_converter \
    --input_format=tf_frozen_model \
    --output_node_names='final_result' \
    'C:/Code/tensorflow-for-poets-2/tf_files/retrained_graph.pb' \
    'C:/tensorflow output 2'

When i run a prediction on the tensorflow JS model with the same image i used with the frozen model i get terrible results:

Loading model:

const MODEL_URL = 'assets/dog-model/tensorflowjs_model.pb';
const WEIGHTS_URL = 'assets/dog-model/weights_manifest.json';
loadFrozenModel(MODEL_URL, WEIGHTS_URL).then(
  result => (this.model = result) 
);

Predicting results:

const image = tf.browser
  .fromPixels(this.staticImage.nativeElement)
  .resizeNearestNeighbor([224, 224])
  .toFloat()
  .sub(meanImageNetRGB)
  .expandDims();
console.log(image);

const prediction = this.model.predict(image);

Output:

yorkshire terrier: 0.2447875738143921
komondor: 0.22793063521385193
ibizan hound: 0.0579879954457283
saluki: 0.04560968279838562
maltese dog: 0.04430125281214714

Upvotes: 1

Views: 808

Answers (1)

edkeveked
edkeveked

Reputation: 18401

The inaccuracy has to do with the input to the model. Make sure that the operations - cropping, reshaping, ... used to create the tensor representing the image in both version (python and js) are alike.

Upvotes: 2

Related Questions