3

我一直在尝试 TFLite 来提高 Android 上的检测速度,但奇怪的是我的 .tflite 模型现在几乎只检测到 1 个类别。

我已经对重新训练移动网络后得到的 .pb 模型进行了测试,结果很好,但由于某种原因,当我将其转换为 .tflite 时,检测就差了……

对于再训练,我使用了来自Tensorflow 的 retrain.py 文件,用于诗人 2

我正在使用以下命令来重新训练、优化推理并将模型转换为 tflite:

python retrain.py \
--image_dir ~/tf_files/tw/ \
--tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/feature_vector/1 \
--output_graph ~/new_training_dir/retrainedGraph.pb \
-–saved_model_dir ~/new_training_dir/model/ \
--how_many_training_steps 500 

sudo toco \
--input_file=retrainedGraph.pb \
--output_file=optimized_retrainedGraph.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TENSORFLOW_GRAPHDEF \
--input_shape=1,224,224,3 \
--input_array=Placeholder \
--output_array=final_result \

sudo toco \
--input_file=optimized_retrainedGraph.pb \
--input_format=TENSORFLOW_GRAPHDEF \
--output_format=TFLITE \
--output_file=retrainedGraph.tflite \
--inference_type=FLOAT \
--inference_input_type=FLOAT \
--input_arrays=Placeholder \
--output_array=final_result \
--input_shapes=1,224,224,3

我在这里做错什么了吗?准确性的损失从何而来?

4

2 回答 2

4

我在尝试将 .pb 模型转换为 .lite 时遇到了同样的问题。

事实上,我的准确率会从 95 下降到 30!

原来我犯的错误不是在将 .pb 转换为 .lite 的过程中,也不是在执行此操作的命令中。但实际上是在加载图像并对其进行预处理之前将其传递到精简模型并使用推断

interpreter.invoke()

命令。

您看到的以下代码就是我所说的预处理:

test_image=cv2.imread(file_name)
test_image=cv2.resize(test_image,(299,299),cv2.INTER_AREA)
test_image = np.expand_dims((test_image)/255, axis=0).astype(np.float32)
interpreter.set_tensor(input_tensor_index, test_image)
interpreter.invoke()
digit = np.argmax(output()[0])
#print(digit)
prediction=result[digit]

正如你所看到的,一旦使用“imread()”读取图像,就会对图像进行两个关键的命令/预处理:

i) 图像的大小应调整为训练期间使用的输入图像/张量的“input_height”和“input_width”值。在我的情况下(inception-v3),“input_height”和“input_width”都是299。(阅读该值的模型文档或在您用于训练或重新训练模型的文件中查找该变量

ii) 上述代码中的下一条命令是:

test_image = np.expand_dims((test_image)/255, axis=0).astype(np.float32)

我从“公式”/模型代码中得到了这个:

test_image = np.expand_dims((test_image-input_mean)/input_std, axis=0).astype(np.float32)

阅读文档显示,对于我的架构 input_mean = 0 和 input_std = 255。

当我对我的代码进行上述更改时,我得到了预期的准确性(90%)。

希望这可以帮助。

于 2019-10-27T21:40:40.193 回答
0

请在 GitHub https://github.com/tensorflow/tensorflow/issues上提交问题并在此处添加链接。还请添加更多关于您重新训练最后一层的详细信息。

于 2018-07-06T17:31:26.763 回答