将retrained_graph.pb转换为tflite文件后,模型分数不是很好。
我们按照以下步骤获取tflite文件:
Step1。收集数据图像(.jpg)并将其放入如下文件夹结构中
tf_files/cockroaches_photos/americancockroach/images
ex:tf_files/cockroaches_photos/americoncockroach/4.jpgStep2在训练之前,设置图像大小以及训练模型所需的架构。(inceptionV3/MobileNet)
设置图像大小:
IMAGE_SIZE=299设置inception_v3的架构:
ARCHITECTURE="inception_v3"Step3:使用默认的4000个时期重新训练模型:
python -m scripts.retrain --bottleneck_dir=tf_files/bottlenecks --model_dir=tf_files/models/"${ARCHITECTURE}" --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" --output_graph=tf_files/retrained_graph.pb --output_labels=tf_files/retrained_labels.txt --architecture="${ARCHITECTURE}" --image_dir=tf_files/i_cockr_photos在我的例子中,行尾的输出是:
INFO: tensorflow:Final test accuracy = 93.2% (N=44)Step4:修改脚本/label_image.py如下
input_height=299
input_width=299
input_layer=”Mul”
output_layer=”final_result”Step5:使用retrained_graph.pb对图像进行分类
python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=tf_files/i_cockr_photos/germancockroach/images.jpg输出:评估时间(1-image):0.816秒
德国蟑螂0.88213褐斑蟑螂0.0920959美国蟑螂0.024512澳大利亚蜘蛛0.000708872温室蟑螂0.000419116
Step6:优化模型
python -m tensorflow.python.tools.optimize_for_inference --input=tf_files/retrained_graph.pb --output=tf_files/optimized_graph.pb --input_format=TENSORFLOW_GRAPHDEF --output_format=TENSORFLOW_GRAPHDEF --input_shape=1,299,299,3 --input_names="Mul" --output_names="final_result"Step7:使用optimized_graph.pb验证优化后的模型:
python -m scripts.label_image --graph=tf_files/optimized_graph.pb --image=tf_files/i_cockr_photos/germancockroach/images.jpg评估时间(1张):0.713秒
germancockroach 0.882129
brownbandedcockroach 0.0920963
americancockroach 0.0245122
australianspiderbeetle 0.000708876
greenhousecockroach 0.000419114Step8:将模型转换为TFLite格式:(1)使用retrained_graph.pb
toco --input_file=/home/sudheer_sure/t_working/tf_files/retrained_graph.pb --output_file=/home/sudheer_sure/t_working/tf
_files/t_c_labs_retrained_graph.lite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,299,299,3 --input_array=Mul --output_array=final_result --inference_type=FLOAT --input_type=FLOAT(2)使用optimized_graph.pb
toco --input_file=/home/sudheer_sure/t_working/tf_files/optimized_graph.pb --output_file=/home/sudheer_sure/t_working/tf
_files/t_c_labs_optimized_graph.lite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,299,299,3 --input_array=Mul --output_array=final_result --inference_type=FLOAT --input_type=FLOAT(3)使用input_names & output_names参数的bazel & added转换tflite retrained_graph.pb:
sudo bazel-bin/tensorflow/contrib/lite/toco/toco '--input_format=TENSORFLOW_GRAPHDEF' '--input_file=/home/sudheer_sure/t_working/tf_files/retrained_graph.pb' '--output_format=TFLITE' '--output_file=/home/sudheer_sure/dummy/bazel_toco_retrain_inception_v3.lite' '--inference_type=FLOAT' '--inference_input_type=FLOAT' '--input_arrays=Mul' '--output_arrays=final_result' '--input_shapes=1,299,299,3' '--input_names=DecodeJpeg/contents' '--output_names=pool_3/_reshape'(4)使用input_names & output_names参数的bazel & added转换tflite optimizedd_graph.pb:
sudo bazel-bin/tensorflow/contrib/lite/toco/toco '--input_format=TENSORFLOW_GRAPHDEF' '--input_file=/home/sudheer_sure/t_working/tf_files/optimized_graph.pb' '--output_format=TFLITE' '--output_file=/home/sudheer_sure/dummy/bazel_toco_object_inception_v3.lite' '--inference_type=FLOAT' '--inference_input_type=FLOAT' '--input_arrays=Mul' '--output_arrays=final_result' '--input_shapes=1,299,299,3' '--input_names=DecodeJpeg/contents' '--output_names=pool_3/_reshape'在这里你可以比较我们在转换成tflite文件之前得到的分数,也就是直接从重新训练的图表和转换成tflite文件之后得到的分数
来自retarined_graph.pb的分数:
americancockroach 0.814701
greenhousecockroach 0.157736
germancockroach 0.0224954
brownbandedcockroach 0.00405233
australianspiderbeetle 0.000568162来自TensorFlow .lite文件的分数:
australianspiderbeetle, 0.012658036
confusedflourbeetle, 0.013540811
americancockroach, 0.033697817请让我知道需要做些什么来解决这个问题。
发布于 2018-11-26 22:54:13
我认为这是ImageClassifier.java中classifyFrame()下的applyFilter()已经让数字变得平滑了。尝试删除它,然后再次检查输出。请参阅this answer 。
https://stackoverflow.com/questions/49952165
复制相似问题