简体   繁体   中英

tensorflow lite: error when converting retrained graph model to lite format

Followed Steps

Step1- Clone the git repository:

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:

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

Step3- set the image size and architecture:

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

Step4- Retrain the model

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

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

daisy 0.725841 dandelion 0.200525 tulips 0.0411526 roses 0.0318613 sunflowers 0.000619742

Step6:Optimize the model

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

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

daisy 0.725845 dandelion 0.200523 tulips 0.0411517 roses 0.031861 sunflowers 0.00061973

Step8- Convert to model to TFlite format

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

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

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.

Source: 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. If that is the case and you are getting this error in Android Studio, you should have a look at your ImageClassifier.java file.

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. If you set those two values to 224, that should solve the problem.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM