Tensorflow Lite:ResNet示例模型在使用ImageNet进行验证期间给出了非常差的结果

时间:2018-12-12 00:43:40

标签: tensorflow tensorflow-lite resnet imagenet

我正在研究tensorflow lite。我从https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/models.md#image-classification-float-models下载了ResNet冻结图 ResNet_V2_101

然后我跟随https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb将冻结的图转换为Lite模型和量化Lite模型。

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) 

然后我跟随https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/accuracy/ilsvrc使用台式机上的ImageNet验证数据集(50000张图像)评估其准确性。

但是,当我跑步

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

并检查输出accuracy_output.txt。准确性很差。我可以在50000张图像中捕获一些结果。

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

但是,根据https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb,前1位的准确度可以达到76.8,但我的尝试甚至最终都无法达到1。 为什么会这样?我哪里做错了?谢谢!

2 个答案:

答案 0 :(得分:0)

还请检查类别标签。如果使用了错误的类别标签,结果将与您描述的一样。

答案 1 :(得分:0)

在您的Python代码中检查模型路径,它是resnet_v2_101_299_frozen_quantize.tflite,但是您在命令行中使用了另一个ResNet_V2_101.tflite