hhk7734 / tensorflow-yolov4

YOLOv4 Implemented in Tensorflow 2.
MIT License
136 stars 75 forks source link

Yolov4 tiny conversion to tflite #10

Closed vladimirhorvat closed 4 years ago

vladimirhorvat commented 4 years ago

Hi, thanks for this! I used AlexeyAB yolov4-tiny cfg, trained for 1 class. When trying to convert weights to tflite I get:

2020-07-16 08:15:29.339406: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 13962 MB memory) -> physical GPU (device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5)
loc(callsite("YOLOv4Tiny/PANetTiny/up_sampling2d/resize/ResizeBilinear"("/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py":2245:0) at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py":927:0 at callsite("/usr/local/lib/python3.6/dist-packages/yolov4/model/neck.py":236:0 at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py":309:0 at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py":927:0 at callsite("/usr/local/lib/python3.6/dist-packages/yolov4/model/yolov4.py":110:0 at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py":309:0 at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py":927:0 at callsite("/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py":132:0 at "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py":441:0)))))))))): error: 'tf.ResizeBilinear' op is neither a custom op nor a flex op
error: failed while converting: 'main'
Ops that can be supported by the flex runtime (enabled via setting the -emit-select-tf-ops flag): ResizeBilinear.
Traceback (most recent call last):
  File "/usr/local/bin/toco_from_protos", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/lite/toco/python/toco_from_protos.py", line 93, in main
    app.run(main=execute, argv=[sys.argv[0]] + unparsed)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/usr/local/lib/python2.7/dist-packages/absl/app.py", line 300, in run
    _run_main(main, args)
  File "/usr/local/lib/python2.7/dist-packages/absl/app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/lite/toco/python/toco_from_protos.py", line 56, in execute
    enable_mlir_converter)
Exception: /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/convolutional.py:2245:9: error: 'tf.ResizeBilinear' op is neither a custom op nor a flex op
        interpolation=self.interpolation)
        ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927:19: note: called from
                  outputs = call_fn(cast_inputs, *args, **kwargs)
                  ^
/usr/local/lib/python3.6/dist-packages/yolov4/model/neck.py:236:9: note: called from
        x1 = self.upSampling18(x1)
        ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:309:7: note: called from
      return func(*args, **kwargs)
      ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927:19: note: called from
                  outputs = call_fn(cast_inputs, *args, **kwargs)
                  ^
/usr/local/lib/python3.6/dist-packages/yolov4/model/yolov4.py:110:9: note: called from
        x = self.panet_tiny(x)
        ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py:309:7: note: called from
      return func(*args, **kwargs)
      ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927:19: note: called from
                  outputs = call_fn(cast_inputs, *args, **kwargs)
                  ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saving_utils.py:132:7: note: called from
      outputs = model(inputs, training=False)
      ^
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py:441:9: note: called from
        return weak_wrapped_fn().__wrapped__(*args, **kwds)
        ^
<unknown>:0: error: failed while converting: 'main'
Ops that can be supported by the flex runtime (enabled via setting the -emit-select-tf-ops flag): ResizeBilinear.
hhk7734 commented 4 years ago

Can you share your python script?

vladimirhorvat commented 4 years ago

Oh, I used just your example: from yolov4.tf import YOLOv4 yolo = YOLOv4(tiny=True) yolo.classes = "/content/TensorFlow-2.x-YOLOv3/model_data/license_plate_names.txt" yolo.make_model() yolo.load_weights("/mydrive/yolo/backup/yolov4-tiny-obj_final.weights", weights_type="yolo") yolo.save_as_tflite("yolov4.tflite")

yolov4-tiny-obj_final.weights: https://drive.google.com/file/d/1-7W6dL49bu2u0gZTT_7Ooi9HvyEymoKb/view?usp=sharing yolov4-tiny-obj.cfg: https://drive.google.com/file/d/1wHyP2k6tW6qotuepE_tPsTLqrg3mG2HJ/view?usp=sharing

license_plate_names.txt contains just: custom

hhk7734 commented 4 years ago
python3 -m pip show yolov4

What is your version?

vladimirhorvat commented 4 years ago

0.16.0

hhk7734 commented 4 years ago

test.py

from yolov4.tf import YOLOv4

yolo = YOLOv4(tiny=True)

yolo.classes = {0:"custom"}

yolo.make_model()
yolo.load_weights("/home/hhk7734/Downloads/yolov4-tiny-obj_final.weights", weights_type="yolo")

yolo.save_as_tflite("yolov4.tflite")
python3 test.py

it successfully converted.

python 3.6.9 version tensorflow 2.2.0 version

hhk7734 commented 4 years ago
import tensorflow as tf
from yolov4.tf import YOLOv4

yolo = YOLOv4(tiny=True)

yolo.classes = {0:"custom"}

yolo.make_model()
yolo.load_weights("/home/hhk7734/Downloads/yolov4-tiny-obj_final.weights", weights_type="yolo")

converter = tf.lite.TFLiteConverter.from_keras_model(yolo.model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                                       tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
with tf.io.gfile.GFile("yolov4.tflite", "wb") as fd:
    fd.write(tflite_model)

Please try this

vladimirhorvat commented 4 years ago

It worked! Thanks!

hhk7734 commented 4 years ago

Thanks for testing the code :)