简体   繁体   English

tensorflow lite:将重新训练的图模型转换为lite格式时出错

[英]tensorflow lite: error when converting retrained graph model to lite format

Followed Steps 遵循的步骤

Step1- Clone the git repository: 步骤1-克隆git仓库:

git clone https://github.com/googlecodelabs/tensorflow-for-poets-2

cd tensorflow-for-poets-2

Step2- Download the training images or gather the custom images: 步骤2-下载训练图像或收集自定义图像:

curl http://download.tensorflow.org/example_images/flower_photos.tgz \
    | tar xz -C tf_files

Step3- set the image size and architecture: 步骤3-设置图像大小和体系结构:

IMAGE_SIZE=224
ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"

Step4- Retrain the model 步骤4-重新训练模型

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/flower_photos

Step5- Using retrained model check the classify image 步骤5-使用重新训练的模型检查分类图像

python -m scripts.label_image \
--graph=tf_files/retrained_graph.pb\ -- 
image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg

Evaluation time (1-image): 0.281s 评估时间(1张图像):0.281s

daisy 0.725841 dandelion 0.200525 tulips 0.0411526 roses 0.0318613 sunflowers 0.000619742 雏菊0.725841蒲公英0.200525郁金香0.0411526玫瑰0.0318613向日葵0.000619742

Step6:Optimize the model 步骤6:优化模型

IMAGE_SIZE=224
toco \
  --input_file=tf_files/retrained_graph.pb \
  --output_file=tf_files/optimized_graph.pb \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TENSORFLOW_GRAPHDEF \
  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \
  --input_array=input \
  --output_array=final_result

Step7- Verify the optimized model of classifying image 步骤7-验证分类图像的优化模型

python -m scripts.label_image \
--graph=tf_files/optimized_graph.pb \
--image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg

Evaluation time (1-image): 0.126s 评估时间(1张图像):0.126s

daisy 0.725845 dandelion 0.200523 tulips 0.0411517 roses 0.031861 sunflowers 0.00061973 雏菊0.725845蒲公英0.200523郁金香0.0411517玫瑰0.031861向日葵0.00061973

Step8- Convert to model to TFlite format 步骤8-将模型转换为TFlite格式

IMAGE_SIZE=224
toco \
  --input_file=tf_files/retrained_graph.pb \
  --output_file=tf_files/optimized_graph.lite \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TFLITE \
  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \
  --input_array=input \
  --output_array=final_result \
  --inference_type=FLOAT \
  --input_type=FLOAT

Still getting the issue of 0-th input should have 602112 bytes, but found 150528 bytes 仍然遇到第0个输入的问题,应该有602112字节,但是找到了150528字节

Please give a better solution to overcome/achieve this issue to solve 请提供更好的解决方案来克服/解决此问题

Been trying to do this all morning, with 1.9 and above (and possibly 1.8 too, haven't tested.) you need to drop the --input_format field, and change the --input_file param to --graph_def_file 整天都在尝试使用1.9及更高版本(可能还有1.8,尚未测试)进行此操作。您需要删除--input_format字段,并将--input_file参数更改为--graph_def_file

So you end up with a command that looks a bit like: 因此,您最终得到的命令看起来像:

toco \
  --graph_def_file=tf_files/retrained_graph.pb \
  --output_file=tf_files/optimized_graph.lite \
  --output_format=TFLITE \
  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \
  --input_array=input \
  --output_array=final_result \
  --inference_type=FLOAT \
  --inference_input_type=FLOAT

I was then able to complete the poets example and get my tflite file to work on android. 然后,我能够完成诗人的示例,并使我的tflite文件在android上工作。

Source: https://github.com/googlecodelabs/tensorflow-for-poets-2/issues/68 来源: https//github.com/googlecodelabs/tensorflow-for-poets-2/issues/68

I assume you are trying to use your model in the android-app that comes with Tensorflow for Poets. 我假设您正在尝试在Tensorflow for Poets随附的android-app中使用您的模型。 If that is the case and you are getting this error in Android Studio, you should have a look at your ImageClassifier.java file. 如果是这种情况,并且您在Android Studio中遇到此错误,则应该查看ImageClassifier.java文件。

My guess is that your static final int DIM_IMG_SIZE_X and static final int DIM_IMG_SIZE_Y are not the same value as your IMG_SIZE. 我的猜测是,您的静态最终int DIM_IMG_SIZE_X和静态最终int DIM_IMG_SIZE_Y与您的IMG_SIZE值不同。 If you set those two values to 224, that should solve the problem. 如果将这两个值设置为224,则应该可以解决问题。

Hope this helps! 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Tensorflow 模型转换为 tensorflow-lite (.tflite) 格式的问题 - Problem converting Tensorflow model to tensorflow-lite (.tflite) format Tensorflow Lite iOS相机示例不适用于重新训练的MobileNet模型 - Tensorflow Lite iOS Camera example does not work with retrained MobileNet model 将 ONNX model 转换为 TensorFlow Lite - Converting ONNX model to TensorFlow Lite 将 tensorflow/keras 模型转换为 tensorflow lite 模型时出现的问题 - Issues when converting tensorflow/keras model to tensorflow lite model 如何修复“ TOCO失败。 检查失败:将冻结的图形转换为tensorflow_lite模型时,dim> = 1(0对1)”错误 - How to fix “TOCO failed. Check failed: dim >= 1 (0 vs. 1)” error while converting a frozen graph into a tensorflow_lite model 将 tensorflow 模型转换为 tensorflow lite = ValueError: bad marshal data (unknown type code) 时出错 - Getting Error while converting tensorflow model to tensorflow lite = ValueError: bad marshal data (unknown type code) 将 Keras 模型权重和架构转换为 TensorFlow Lite 模型 - Converting Keras Model Weights and Architecture to TensorFlow Lite Model 在 TF2 中将 Keras RNN 模型转换为 TensorFlow Lite 模型 - Converting Keras RNN model to TensorFlow Lite model in TF2 TensorFlow MTCNN 模型可以转换成 TensorFlow Lite 格式吗? - Can the TensorFlow MTCNN model converted to the TensorFlow Lite format? 将 Tensorflow 模型转换为 Tensorflow Lite - Convert Tensorflow model into Tensorflow Lite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM