tensorflow-yolov3
tensorflow-yolov3 copied to clipboard
Anyone tried to convert the model to tflite? I tried but had error...
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!
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
Thanks for the explanation @BeautyMess . Any progress in replacing tf.newaxis ? I would be interested as well.
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 👍
dear @WeiyiLi and @BeautyMess :+1: are you able now to convert the model to tflite? thanks in advance.
@kerolos Yes. Check the README https://github.com/peace195/tensorflow-lite-yolo-v3
@WeiyiLi I encountered the same error when I converted .pb to .tflite file.Have you found any solutions?
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?
@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.
Hi @rubencardenes,Did you convert tflite successfully?
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 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.
@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!
This issue has not been solved yet?
@rubencardenes Have you found any solution ?