hunglc007 / tensorflow-yolov4-tflite

YOLOv4, YOLOv4-tiny, YOLOv3, YOLOv3-tiny Implemented in Tensorflow 2.0, Android. Convert YOLO v4 .weights tensorflow, tensorrt and tflite
https://github.com/hunglc007/tensorflow-yolov4-tflite
MIT License
2.25k stars 1.24k forks source link

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

Open atomrun39 opened 4 years ago

atomrun39 commented 4 years ago

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

w840401 commented 4 years ago

Me too! How to fix it ? Please

tomkisiela commented 4 years ago

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 ?

drahmad89 commented 4 years ago

Me too! How to fix it ? Please

tomkisiela commented 4 years ago

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

atomrun39 commented 4 years ago

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

kaunghtetsan275 commented 4 years ago

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

piggychu0w0 commented 3 years ago

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

KuoEuran commented 3 years ago

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

Ashokcharu commented 3 years ago

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

abhi-84 commented 2 years ago

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

Hanseyyyy commented 2 years ago

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!

abhi-84 commented 2 years ago

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

thias15 commented 2 years ago

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.