tensorflow-yolov3 icon indicating copy to clipboard operation
tensorflow-yolov3 copied to clipboard

Anyone tried to convert the model to tflite? I tried but had error...

Open WeiyiLi opened this issue 5 years ago • 14 comments

Hello,

I followed your steps to freeze the ckpt model to frozen graph .pb and then I use toco --graph_def_file=yolov3_coco.pb --output_file=yolov3.tflite --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --input_shape=1,1,1,1 --input_array=input/input_data --output_array=pred_sbbox/concat_2,pred_mbbox/concat_2,pred_lbbox/concat_2 --input_data_type=FLOAT --inference_type=FLOAT --allow_custom_ops to convert it to tflite. However, it has error tensorflow/lite/toco/graph_transformations/resolve_strided_slice_attributes.cc:95] Check failed: start_indices_size <= num_input_axes (2 vs. 1)StridedSlice op requires no more than 1 start indices . Does anyone know how to convert it to tflite? Thanks!

WeiyiLi avatar Jun 25 '19 18:06 WeiyiLi

I tried the converting operation using a different yolov3 code and the same error occurred.

I guess it was caused by "tf.newaxis" referring to https://github.com/tensorflow/tensorflow/blob/5a142145f86377ed1ffc1ac19d1d6d6853f8f355/tensorflow/lite/toco/graph_transformations/propagate_fixed_sizes.cc#L1662.

The tf.newaxis is implemented in strided_slice with the parameter new_axis_mask that is not supported according to the code, so I think the reason is that.

I am trying to replace the tf.newaxis and share the idea if you have better ideas.

And there is a useful issue for this problem, which solved the error caused by shrink_axis_mask: https://github.com/tensorflow/tensorflow/issues/19260

BeautyMess avatar Jul 05 '19 09:07 BeautyMess

Thanks for the explanation @BeautyMess . Any progress in replacing tf.newaxis ? I would be interested as well.

pauqilaz avatar Sep 06 '19 10:09 pauqilaz

Here is the solution: https://github.com/peace195/tensorflow-lite-yolo-v3

The .pb should be in right format (SavedModel).

Please try it. I would appreciate if you give me a star for this project 👍

peace195 avatar Oct 20 '19 16:10 peace195

dear @WeiyiLi and @BeautyMess :+1: are you able now to convert the model to tflite? thanks in advance.

kerolos avatar Nov 08 '19 08:11 kerolos

@kerolos Yes. Check the README https://github.com/peace195/tensorflow-lite-yolo-v3

peace195 avatar Nov 08 '19 08:11 peace195

@WeiyiLi I encountered the same error when I converted .pb to .tflite file.Have you found any solutions?

Lemonfmr avatar Jan 17 '20 08:01 Lemonfmr

Hi @peace95, your code is nice, but it helps the conversion from offitial YOLOv3 Darknet implementation, as I understand. Has anyone managed to convert the YunYang1994 implementation to tflite?

rubencardenes avatar Mar 11 '20 15:03 rubencardenes

@kerolos Sorry. I reply to you so late. I have done the conversion last year. I can't directly upload my code because of our company's security rule. The key point of the conversion is using "tf.expand_dims" instead of "tf.newaxis" in the code, because the tflite didn't support the op then. And I don't know whether to support that op now. And you can relpy to me here if meeting any problem while doing the conversion. Now I will keep online and watch the notification.

BeautyMess avatar Apr 04 '20 09:04 BeautyMess

Hi @rubencardenes,Did you convert tflite successfully?

zxj11838 avatar Apr 21 '20 06:04 zxj11838

Hi @zxj11838 , I advanced a bit. As suggested by @BeautyMess changing tf.newaxis with tf.expand solves the error:

 ... start_indices_size <= num_input_axes (2 vs. 1)StridedSlice op requires no more than 1 start indices

So, specifically, one has to change these lines of code

y = tf.tile(tf.range(output_size, dtype=tf.int32)[:, tf.newaxis], [1, output_size])
x = tf.tile(tf.range(output_size, dtype=tf.int32)[tf.newaxis, :], [output_size, 1])

xy_grid = tf.concat([x[:, :, tf.newaxis], y[:, :, tf.newaxis]], axis=-1)
xy_grid = tf.tile(xy_grid[tf.newaxis, :, :, tf.newaxis, :], [batch_size, 1, 1, anchor_per_scale, 1])
xy_grid = tf.cast(xy_grid, tf.float32)

into these ones:

y = tf.tile(tf.expand_dims(tf.range(output_size, dtype=tf.int32), -1), [1, output_size])
x = tf.tile(tf.expand_dims(tf.range(output_size, dtype=tf.int32), 0), [output_size, 1])

xy_grid = tf.concat([tf.expand_dims(x, -1), tf.expand_dims(y, -1)], axis=-1)
xy_grid = tf.tile(tf.expand_dims(tf.expand_dims(xy_grid, 0), 3), [batch_size, 1, 1, anchor_per_scale, 1])
xy_grid = tf.cast(xy_grid, tf.float32)

And then, the conversion goes through and tflite model is obtained. But my excitement didn't last too long because when I run the tflite model, at inference I got is this error:

RuntimeError: tensorflow/lite/kernels/strided_slice.cc StridedSlice op only supports 1D-4D input arrays.Node number 151 (STRIDED_SLICE) failed to prepare.

still haven't got time to investigate how to solve that problem. Any ideas?

rubencardenes avatar Apr 21 '20 08:04 rubencardenes

@rubencardenes Thank you for your reply! I saw some related discussions in this link:https://github.com/YunYang1994/tensorflow-yolov3/issues/232 The corresponding project is:https://github.com/panguxiaoshen/tiny_yolov3 The corresponding file should be:tensorflow/src/core/yolov3_tiny.py I want to try.

zxj11838 avatar Apr 21 '20 09:04 zxj11838

@rubencardenes
About this error:

RuntimeError: tensorflow/lite/kernels/strided_slice.cc StridedSlice op only supports 1D-4D input arrays.Node number 151 (STRIDED_SLICE) failed to prepare.

It happened to me, too. And the issue provided by @zxj11838 gives the right solution. The discussion is in Chinese, so I give my explanation in English.

In my retropection, I remember the 3D input causes this error. The oringinal input is 4D like [batchsize, Height, Weight, Channel] but the code will reduce the dimension of 'batchsize'. As a result, the input becomes 3D format. Unfrotunately, the 3D input causes an error. And the solution is direct and simple. You can just expand the dimension before where the error occurs using tf.expand just like you have done.

I have no code on my hand now, so that's my memory with probable mistakes. But I think the main idea is useful. May you all success!

BeautyMess avatar Apr 21 '20 16:04 BeautyMess

This issue has not been solved yet?

giorgiomondauto avatar Jan 16 '21 19:01 giorgiomondauto

@rubencardenes Have you found any solution ?

Kapil-23 avatar Mar 08 '22 14:03 Kapil-23