tensorflow-yolov4-tflite icon indicating copy to clipboard operation
tensorflow-yolov4-tflite copied to clipboard

Convert to tflite&Run tflite model:ValueError: Shapes incompatible

Open atomrun39 opened this issue 3 years ago • 13 comments

First I use darknet to train the yolov4-tiny model, it has two detection classes, and the input image size is 224. The trained weight is first converted to tensorflow, and then converted to tflite. But shape incompatible error appears when running the tflite model, please see below for specific errors.

using:python detect.py --weights ./checkpoints/yolov4-224-fp16.tflite --size 224 --model yolov4 --image ./data/49.jpg --framework tflite

physical GPU (device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5) [{'name': 'input_1', 'index': 0, 'shape': array([ 1, 224, 224, 3], dtype=int32), 'shape_signature': array([ -1, 224, 224, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] [{'name': 'Identity', 'index': 103, 'shape': array([ 1, 7, 7, 21], dtype=int32), 'shape_signature': array([-1, 7, 7, 21], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'Identity_1', 'index': 104, 'shape': array([ 1, 14, 14, 21], dtype=int32), 'shape_signature': array([-1, 14, 14, 21], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] Traceback (most recent call last): File "detect.py", line 90, in app.run(main) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/absl/app.py", line 300, in run _run_main(main, args) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/absl/app.py", line 251, in _run_main sys.exit(main(argv)) File "detect.py", line 61, in main boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.4, input_shape=tf.constant([input_size, input_size])) File "/home/jiaoda/PycharmProjects/tensorflow-yolov4-tflite/core/yolov4.py", line 308, in filter_boxes class_boxes = tf.boolean_mask(box_xywh, mask) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper return target(*args, **kwargs) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1803, in boolean_mask_v2 return boolean_mask(tensor, mask, name, axis) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper return target(*args, **kwargs) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1730, in boolean_mask shape_tensor[axis:axis + ndims_mask].assert_is_compatible_with(shape_mask) File "/home/jiaoda/anaconda3/envs/tf36/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 1134, in assert_is_compatible_with raise ValueError("Shapes %s and %s are incompatible" % (self, other)) ValueError: Shapes (1, 7, 7) and (1, 14, 14) are incompatible

atomrun39 avatar Sep 26 '20 16:09 atomrun39

Me too! How to fix it ? Please

w840401 avatar Sep 29 '20 06:09 w840401

I've got almost the same error : tensorflow.python.framework.errors_impl.InvalidArgumentError: Determined shape must either match input shape along split_dim exactly if fully specified, or be less than the size of the input along split_dim if not fully specified. Got: 4 [Op:SplitV] name: split Does anyone found a workaround ?

tomkisiela avatar Oct 02 '20 15:10 tomkisiela

Me too! How to fix it ? Please

drahmad89 avatar Oct 05 '20 11:10 drahmad89

I reinstalled the repo and recreate the conda environement and it fixed the error.

tomkisiela avatar Oct 06 '20 07:10 tomkisiela

I followed this tutorial and it succeeded. https://colab.research.google.com/drive/1OKzbccsdA40sJ67gqmNyblWuEMCfP5zx?usp=sharing#scrollTo=dvN2-COJ9w4k

atomrun39 avatar Oct 12 '20 08:10 atomrun39

if you're running tiny model, set the flag --tiny true python detect.py --weights ./checkpoints/yolov4-224-fp16.tflite --size 224 --model yolov4 --image ./data/49.jpg --framework tflite --tiny true

kaunghtetsan275 avatar Nov 04 '20 09:11 kaunghtetsan275

I've got almost the same error : tensorflow.python.framework.errors_impl.InvalidArgumentError: Determined shape must either match input shape along split_dim exactly if fully specified, or be less than the size of the input along split_dim if not fully specified. Got: 4 [Op:SplitV] name: split Does anyone found a workaround ?

I meet the same problem. How do you solve it?

My command:

python detect.py --weights ./checkpoints/yolov4-416-tiny-tf.tflite --size 416 --model yolov4 --image ./data/toast.jpg --framework tflite --tiny true

And my error: [{'name': 'serving_default_input_1:0', 'index': 0, 'shape': array([ 1, 416, 416, 3]), 'shape_signature': array([ -1, 416, 416, 3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] [{'name': 'StatefulPartitionedCall:1', 'index': 211, 'shape': array([ 1, 2535, 2]), 'shape_signature': array([ 1, -1, 2]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:0', 'index': 232, 'shape': array([ 1, 2535, 4]), 'shape_signature': array([ 1, -1, 4]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}] Traceback (most recent call last): File "detect.py", line 90, in <module> app.run(main) File "C:\Users\s1074\AppData\Roaming\Python\Python38\site-packages\absl\app.py", line 303, in run _run_main(main, args) File "C:\Users\s1074\AppData\Roaming\Python\Python38\site-packages\absl\app.py", line 251, in _run_main sys.exit(main(argv)) File "detect.py", line 61, in main boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size])) File "D:\yolov4\training\tensorflow-yolov4-tflite-master\core\yolov4.py", line 301, in filter_boxes box_xy, box_wh = tf.split(class_boxes, (2, 2), axis=-1) File "C:\Users\s1074\anaconda3\lib\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper return target(*args, **kwargs) File "C:\Users\s1074\anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2145, in split return gen_array_ops.split_v( File "C:\Users\s1074\anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 10094, in split_v _ops.raise_from_not_ok_status(e, name) File "C:\Users\s1074\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 6897, in raise_from_not_ok_status six.raise_from(core._status_to_exception(e.code, message), None) File "<string>", line 3, in raise_from tensorflow.python.framework.errors_impl.InvalidArgumentError: Determined shape must either match input shape along split_dim exactly if fully specified, or be less than the size of the input along split_dim if not fully specified. Got: 4 [Op:SplitV] name: split

piggychu0w0 avatar Jun 08 '21 08:06 piggychu0w0

@tomkisiela @atomrun39 @kaunghtetsan275 @w840401 @drahmad89 Hi, everyone I also meet the problem image Did anyone solve the problem? please give me some adivce, tks

KuoEuran avatar Jun 28 '21 08:06 KuoEuran

Got the same issue, I removed flag --tiny true and it worked...

python detect.py --weights ./tflite_yolov3/int8.tflite --size 416 --model yolov3 --image ./images/test.jpg --framework tflite

Ashokcharu avatar Aug 23 '21 11:08 Ashokcharu

Still not working for me. Same issue "ValueError: Shapes (1, 13, 13) and (1, 26, 26) are incompatible"

abhi-84 avatar Dec 09 '21 06:12 abhi-84

Still not working for me. Same issue "ValueError: Shapes (1, 13, 13) and (1, 26, 26) are incompatible"

Have you solved it? I have encountered the same problem. Looking forward to your reply, thanks!

Hanseyyyy avatar Dec 21 '21 08:12 Hanseyyyy

Still not working for me. Same issue "ValueError: Shapes (1, 13, 13) and (1, 26, 26) are incompatible"

Have you solved it? I have encountered the same problem. Looking forward to your reply, thanks!

Refer this https://github.com/hunglc007/tensorflow-yolov4-tflite/issues/413

abhi-84 avatar Dec 21 '21 08:12 abhi-84

Hi guys. I encountered the same issue in the evaluate.py script with Yolov4. It seems the order of the outputs, pred[0] and pred[1] is random. This leads to a problem with the filter_boxes function if you are unlucky, 50:50 chance ;). For some people using tiny if normal does not work and vice versa does works (you will see why in the code snippet below). To fix it, you need to change these lines in the detect.py or evaluate.py file whichever one you are trying to run.

        if FLAGS.model == 'yolov4' and FLAGS.tiny == True:
            boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25)
        else:
            boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25)

So if you face this error and need the tiny model, swap pred[1] and pred[0] inside the if-statement. If you face this error and need the normal model swap pred[0] and pred[1] inside the else-statement.

thias15 avatar Feb 21 '22 22:02 thias15