Tensorflow Lite: ResNet example model gave VERY poor result during validation with ImageNet

折月煮酒 提交于 2019-12-12 01:16:27

问题


I am studying tensorflow lite. I downloaded the ResNet frozen graph ResNet_V2_101 from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/models.md#image-classification-float-models .

And then I followed https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb to convert this frozen graph to both Lite model and quantized lite model.

import tensorflow as tf
import pathlib
import sys
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
import time
graph_def_file = "resnet_saved_model/resnet_v2_101_299_frozen.pb"
input_arrays = ["input"]
output_arrays = ["output"]
converter = tf.lite.TocoConverter.from_frozen_graph(str(graph_def_file),input_arrays,output_arrays,input_shapes = {"input":[1,299,299,3]})
tflite_model = converter.convert()
open("saved_model/resnet_v2_101_299_frozen.tflite", "wb").write(tflite_model) 

converter.post_training_quantize = True
tflite_quantized_model = converter.convert()
open("saved_model/resnet_v2_101_299_frozen_quantize.tflite", "wb").write(tflite_quantized_model) 

Then I followed https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/accuracy/ilsvrc to evalute its accuracy using ImageNet Validation Dataset (50000 images) on my desktop.

However, when I run

bazel run -c opt   --cxxopt='--std=c++11'   --   //tensorflow/lite/tools/accuracy/ilsvrc:imagenet_accuracy_eval   --model_file="/home/kathy/saved_model/ResNet_V2_101.tflite"   --ground_truth_images_path="/media/kathy/Documents/val_imgs"   --ground_truth_labels="/home/kathy/workspace/tensorflow/tensorflow/lite/tools/accuracy/ilsvrc/VALIDATION_LABELS.txt"   --model_output_labels="/home/kathy/workspace/tensorflow/tensorflow/lite/tools/accuracy/ilsvrc/resnet_output_labels.txt"   --output_file_path="/tmp/accuracy_output.txt" --num_images=0

and checked the output accuracy_output.txt. The accuracy is very poor. I can capture some results among the 50000 images.

Top 1, Top 2, Top 3, Top 4, Top 5, Top 6, Top 7, Top 8, Top 9, Top 10
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000
0.000, 0.000, 0.000, 20.000, 20.000, 20.000, 20.000, 20.000, 20.000, 20.000
0.000, 0.000, 0.000, 16.667, 16.667, 16.667, 16.667, 16.667, 16.667, 16.667
0.000, 0.000, 0.000, 14.286, 14.286, 14.286, 14.286, 14.286, 14.286, 14.286
0.000, 0.000, 0.000, 12.500, 12.500, 12.500, 12.500, 12.500, 12.500, 12.500
0.000, 0.000, 0.000, 11.111, 11.111, 11.111, 11.111, 11.111, 11.111, 11.111
0.000, 0.000, 0.000, 10.000, 10.000, 10.000, 10.000, 10.000, 10.000, 10.000
0.000, 0.000, 0.000, 9.091, 9.091, 9.091, 9.091, 9.091, 9.091, 9.091
0.000, 0.000, 0.000, 8.333, 8.333, 8.333, 8.333, 8.333, 8.333, 8.333
0.000, 0.000, 0.000, 7.692, 7.692, 7.692, 7.692, 7.692, 7.692, 7.692
0.000, 0.000, 0.000, 7.143, 7.143, 7.143, 7.143, 7.143, 7.143, 7.143
0.000, 0.000, 0.000, 6.667, 6.667, 6.667, 6.667, 6.667, 6.667, 6.667
0.000, 0.000, 0.000, 6.250, 6.250, 6.250, 6.250, 6.250, 6.250, 6.250
0.000, 0.000, 0.000, 5.882, 5.882, 5.882, 5.882, 5.882, 5.882, 5.882
0.000, 0.000, 0.000, 5.556, 5.556, 5.556, 5.556, 5.556, 5.556, 5.556
0.000, 0.000, 0.000, 5.263, 5.263, 5.263, 5.263, 5.263, 5.263, 5.263
0.000, 0.000, 0.000, 5.000, 5.000, 5.000, 5.000, 5.000, 5.000, 5.000
0.000, 0.000, 0.000, 4.762, 4.762, 4.762, 4.762, 4.762, 4.762, 4.762
0.000, 0.000, 0.000, 4.545, 4.545, 4.545, 4.545, 4.545, 4.545, 4.545
0.000, 0.000, 0.000, 4.348, 4.348, 4.348, 4.348, 4.348, 4.348, 4.348
0.000, 0.000, 0.000, 4.167, 4.167, 4.167, 4.167, 4.167, 4.167, 4.167
0.000, 0.000, 0.000, 4.000, 4.000, 4.000, 4.000, 4.000, 4.000, 4.000
0.000, 0.000, 0.000, 3.846, 3.846, 3.846, 3.846, 3.846, 3.846, 3.846
0.000, 0.000, 0.000, 3.704, 3.704, 3.704, 3.704, 3.704, 3.704, 3.704
0.000, 0.000, 0.000, 3.571, 3.571, 3.571, 3.571, 3.571, 3.571, 3.571
0.000, 0.000, 0.000, 3.448, 3.448, 3.448, 3.448, 3.448, 3.448, 3.448
0.000, 0.000, 0.000, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333
0.000, 0.000, 0.000, 3.226, 3.226, 3.226, 3.226, 3.226, 3.226, 3.226
0.000, 0.000, 0.000, 3.125, 3.125, 3.125, 3.125, 3.125, 3.125, 3.125
0.000, 0.000, 0.000, 3.030, 3.030, 3.030, 3.030, 3.030, 3.030, 3.030
0.000, 0.000, 0.000, 2.941, 2.941, 2.941, 2.941, 2.941, 2.941, 2.941
0.000, 0.000, 0.000, 2.857, 2.857, 2.857, 2.857, 2.857, 2.857, 2.857
0.000, 0.000, 0.000, 2.778, 2.778, 2.778, 2.778, 2.778, 2.778, 2.778
0.000, 0.000, 0.000, 2.703, 2.703, 2.703, 2.703, 2.703, 2.703, 2.703
0.000, 0.000, 0.000, 2.632, 2.632, 2.632, 2.632, 2.632, 2.632, 2.632
0.000, 0.000, 0.000, 2.564, 2.564, 2.564, 2.564, 2.564, 2.564, 2.564
0.000, 0.000, 0.000, 2.500, 2.500, 2.500, 2.500, 2.500, 2.500, 2.500
0.000, 0.000, 0.000, 2.439, 2.439, 2.439, 2.439, 2.439, 2.439, 2.439
0.000, 0.000, 0.000, 2.381, 2.381, 2.381, 2.381, 2.381, 2.381, 2.381
0.000, 0.000, 0.000, 2.326, 2.326, 2.326, 2.326, 2.326, 2.326, 2.326
0.000, 0.000, 0.000, 2.273, 2.273, 2.273, 2.273, 2.273, 2.273, 2.273
0.000, 0.000, 0.000, 2.222, 2.222, 2.222, 2.222, 2.222, 2.222, 2.222
0.000, 0.000, 0.000, 2.174, 2.174, 2.174, 2.174, 2.174, 2.174, 2.174
0.000, 0.000, 0.000, 2.128, 2.128, 2.128, 2.128, 2.128, 2.128, 2.128
0.000, 0.000, 0.000, 2.083, 2.083, 2.083, 2.083, 2.083, 2.083, 2.083
0.000, 0.000, 0.000, 2.041, 2.041, 2.041, 2.041, 2.041, 2.041, 2.041
0.000, 0.000, 0.000, 2.000, 2.000, 2.000, 2.000, 2.000, 2.000, 2.000
0.000, 0.000, 0.000, 1.961, 1.961, 1.961, 1.961, 1.961, 1.961, 1.961
0.000, 0.000, 0.000, 1.923, 1.923, 1.923, 1.923, 1.923, 1.923, 1.923
0.000, 0.000, 0.000, 1.887, 1.887, 1.887, 1.887, 1.887, 1.887, 1.887

However, according to https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb, the top-1 accuracy can reach 76.8 but my attempt even cannot reach 1 in the end. Why this happens? Where I did wrong? Thanks!


回答1:


Please also check your category labels. In case wrong category labels have been used the results would be as you described.




回答2:


Check your model path, in your Python code, it is resnet_v2_101_299_frozen_quantize.tflite but you used a different one ResNet_V2_101.tflite in command line



来源:https://stackoverflow.com/questions/53734494/tensorflow-lite-resnet-example-model-gave-very-poor-result-during-validation-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!