Highly incorrect predictions by any architecture retrained on any new classes of images
I'm using this tfhub script for retraining on flower images available here. My terminal output after retraining is
INFO:tensorflow:Restoring parameters from /tmp/_retrain_checkpoint
INFO:tensorflow:Final test accuracy = 92.5% (N=362)
INFO:tensorflow:Save final result to : tf_files/retrained_graph.pb
So, 92.5% accuracy with Inception V3 and the URL is
https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1.
But when I'm predicting the image with label_image script available here:
python -m scripts.label_image \
--graph=tf_files/retrained_graph.pb \
--image=tf_files/flower_photos/roses/568715474_bdb64ccc32.jpg \
--input_height=299 \
--input_width=299 \
--input_layer='Placeholder'
My output is
Evaluation time (1-image): 3.639s
daisy (score=0.54625)
tulips (score=0.18089)
roses (score=0.17955)
dandelion (score=0.06436)
sunflowers (score=0.02894)
For a perfect rose, the generated .pb model is predicting as daisy. This is happening for many images in the flower dataset. I've observed the misprediction for another dataset as well. There's another retraining script by Google Codelabs here. When I'm using the same flower dataset for retraining using this script, my terminal output is
INFO:tensorflow:Final test accuracy = 86.3% (N=4006)
INFO:tensorflow:Froze 2 variables.
Converted 2 variables to const ops.
So, 86.3% (92.5% earlier) accuracy with Inception V3 and the label_image script output is:
Evaluation time (1-image): 1.857s
roses (score=0.99969)
tulips (score=0.00030)
sunflowers (score=0.00001)
daisy (score=0.00000)
dandelion (score=0.00000)
The script at Google Codelabs is also predicting correctly that too with very high confidence. So, what's the difference between these two scripts and why is the 1st script predicting the images incorrectly despite showing very high final test accuracy in the teminal output?
@akhorlin Any ideas?
@arnoegw who has the most experience the script in question
What were the flags passed to retrain.py in both cases, if any.
@akhorlin Thanks for replying. The commads for retraining are For tfhub script:
CUDA_VISIBLE_DEVICES=4 python retrain_1.py \
--bottleneck_dir=/inception_url/bottlenecks \
--how_many_training_steps=4000 \
--model_dir=/inception_url/models \
--output_graph=/inception_url/retrained_graph_inception_url.pb \
--output_labels=/inception_url/retrained_graph_inception_url.txt \
--image_dir=/data \
--tfhub_module=https://tfhub.dev/google/imagenet/inception_v3/feature_vector
For tensorflow-for-poets-2 script: --architecture if not provided, the default is Inception V3.
CUDA_VISIBLE_DEVICES=4 python retrain.py \
--bottleneck_dir=/inception/bottlenecks \
--how_many_training_steps=4000 \
--model_dir=/inception/models \
--output_graph=/inception/retrained_graph_inception.pb \
--output_labels=/inception/retrained_graph_inception.txt \
--image_dir=/data
After doing some research on tfhub script, I made these observations.
- This tfhub retraining script that accepts URLs is working very badly on inception_v3, nasnet_large, pnasnet_large. The output in terminal after retraining is saying that the accuracy is approx 80% for all the above 3 CNNs.
- The tfhub retraining script working fine for mobilenet archirtectures. Why not for above 3 architectues?
- When I'm testing the label for an image using label_image script, the labels are wrong or the confidence is very less.
- This is the output I'm getting using tfhub script on inception_v3 on a class of images (pizza): (See the confidences)
150995.jpg
pizza (score=0.55401)
151453.jpg
pizza (score=0.57254)
147231.jpg
pizza (score=0.51323)
148102.jpg
pizza (score=0.22475)
153244.jpg
pizza (score=0.57784)
145426.jpg
tikkas (score=0.26052)
155635.jpg
pizza (score=0.46594)
155153.jpg
tikkas (score=0.47773)
145340.jpg
pizza (score=0.37451)
153522.jpg
pizza (score=0.33588)
146891.jpg
pizza (score=0.46095)
147557.jpg
pizza (score=0.72046)
148664.jpg
pizza (score=0.37788)
146649.jpg
pizza (score=0.40683)
151335.jpg
pizza (score=0.39485)
146885.jpg
pizza (score=0.65499)
147543.jpg
tikkas (score=0.53042)
- This is the output I'm getting using Google Codelabs script on inception_v3 on a class of images (pizza): (See the confidences)
150995.jpg
pizza (score=0.99977)
151453.jpg
pizza (score=0.95942)
147231.jpg
pizza (score=0.99963)
148102.jpg
pizza (score=0.99689)
153244.jpg
pizza (score=0.99998)
145426.jpg
pizza (score=0.91802)
155635.jpg
pizza (score=0.99915)
155153.jpg
pizza (score=0.97804)
145340.jpg
pizza (score=1.00000)
153522.jpg
pizza (score=1.00000)
146891.jpg
pizza (score=0.99997)
147557.jpg
pizza (score=0.99965)
148664.jpg
pizza (score=0.99969)
146649.jpg
pizza (score=0.88658)
151335.jpg
pizza (score=0.99723)
146885.jpg
pizza (score=1.00000)
147543.jpg
pizza (score=0.99819)
- There is a script for measuring the accuracy from the generated .pb file For tfhub inception v3:
Accuracy: 0.301698
Cross Entropy: 2.08433
For Google Codelabs inception v3:
Accuracy: 0.863387
Cross Entropy: 0.365336
- For tf hub inception v3, atleast the labels are correct with low confidences. For nasnet_large and pnasnet_large, the labels are also incorrect.
- Why is the terminal output while retraining using tfhub script reporting high accuracy despite the above problems?
Hi Sumanth,
thanks for reporting; indeed that looks bad. What happens when you call label_image with the added command-line flag --output_layer=final_result (like it says at tensorflow.org/tutorials/image_retraining#using_the_retrained_model)?
Arno
@arnoegw I didn't understand your question completely. final_result:0 is the last operation in all graphs. You can see the operations in a frozen TensorFlow graph using this code. Passing that flag isn't needed since label_image.py considers the last operation as final_result:0 by default.
@arnoegw Also what does this mean in this page?
Warning: An updated version of this module is available at https://tfhub.dev/google/imagenet/pnasnet_large/feature_vector/2.
A version of improved accuracy or are there some issues with https://tfhub.dev/google/imagenet/pnasnet_large/feature_vector/1 ?
Regarding versions /2 of certain image modules, please see issue https://github.com/tensorflow/hub/issues/86 and the underlying https://github.com/tensorflow/hub/issues/85
Sumanth, I could reproduce your problem. It comes from using another version of label_image.py. Calling it with added flags --input_mean=0 --input_std=255 fixes the problem.
Explanation:
The label_image.py from the codelab's special repo github.com/googlecodelabs/tensorflow-for-poets-2 differs subtly from the mainline tensorflow/examples/label_image/label_image.py in that it converts input images to the range [-1,+1] suitable for the hand-rolled model there instead of the conventional [0,1] for Hub modules.
Repro:
$ python codelab_label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder --input_{height,width}=299 \
--image=flower_photos/roses/568715474_bdb64ccc32.jpg
Evaluation time (1-image): 2.908s
daisy (score=0.44306)
tulips (score=0.22842)
roses (score=0.19549)
dandelion (score=0.08133)
sunflowers (score=0.05169)
Fix:
$ python codelab_label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \
--input_layer=Placeholder --input_{height,width}=299 \
--image=flower_photos/roses/568715474_bdb64ccc32.jpg \
--input_std=255 --input_mean=0
Evaluation time (1-image): 2.721s
roses (score=0.85875)
tulips (score=0.10620)
sunflowers (score=0.01968)
daisy (score=0.01303)
dandelion (score=0.00235)