0

我正在学习 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,top-1的准确率可以达到 76.8,但我的尝试最终甚至达不到 1。为什么会发生这种情况?我哪里做错了?谢谢!

4

2 回答 2

0

另请检查您的类别标签。如果使用了错误的类别标签,结果将如您所述。

于 2018-12-12T19:05:04.820 回答
0

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

于 2018-12-13T15:15:55.280 回答