ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.45k stars 16.28k forks source link

Export to saved_model keras raises NotImplementedError when trying to use the model #10429

Closed MatoFD closed 1 year ago

MatoFD commented 1 year ago

Search before asking

YOLOv5 Component

Export

Bug

Hi, I am trying to export the yolov5s model to Keras.

I have followed the guide on export. First I tried copying the command as it is suggested (python export.py --weights yolov5s.pt --include saved_model), but when trying to use it, it says the model is a '_UserObject' and doesn't have the attributes I expect from a keras model.

I found this issue that talks about it, and the solution was to add the --keras flag to the export command. Now the model, which was loaded with type <class 'keras.engine.functional.Functional'>, raises a "NotImplementedError" when trying to do model.to_json or saving the model.

I don't understand what is wrong with the model.

The model summary looks like this: Model: "model"


Layer (type) Output Shape Param # Connected to

input_1 (InputLayer) [(1, 640, 640, 3)] 0 []

tf_conv (TFConv) (1, 320, 320, 32) 3488 ['input_1[0][0]']

tf_conv_1 (TFConv) (1, 160, 160, 64) 18496 ['tf_conv[0][0]']

tfc3 (TFC3) (1, 160, 160, 64) 18624 ['tf_conv_1[0][0]']

tf_conv_7 (TFConv) (1, 80, 80, 128) 73856 ['tfc3[0][0]']

tfc3_1 (TFC3) (1, 80, 80, 128) 115200 ['tf_conv_7[0][0]']

tf_conv_15 (TFConv) (1, 40, 40, 256) 295168 ['tfc3_1[0][0]']

tfc3_2 (TFC3) (1, 40, 40, 256) 623872 ['tf_conv_15[0][0]']

tf_conv_25 (TFConv) (1, 20, 20, 512) 1180160 ['tfc3_2[0][0]']

tfc3_3 (TFC3) (1, 20, 20, 512) 1181184 ['tf_conv_25[0][0]']

tfsppf (TFSPPF) (1, 20, 20, 512) 656128 ['tfc3_3[0][0]']

tf_conv_33 (TFConv) (1, 20, 20, 256) 131328 ['tfsppf[0][0]']

tf_upsample (TFUpsample) (1, 40, 40, 256) 0 ['tf_conv_33[0][0]']

tf_concat (TFConcat) (1, 40, 40, 512) 0 ['tf_upsample[0][0]',
'tfc3_2[0][0]']

tfc3_4 (TFC3) (1, 40, 40, 256) 361216 ['tf_concat[0][0]']

tf_conv_39 (TFConv) (1, 40, 40, 128) 32896 ['tfc3_4[0][0]']

tf_upsample_1 (TFUpsample) (1, 80, 80, 128) 0 ['tf_conv_39[0][0]']

tf_concat_1 (TFConcat) (1, 80, 80, 256) 0 ['tf_upsample_1[0][0]',
'tfc3_1[0][0]']

tfc3_5 (TFC3) (1, 80, 80, 128) 90496 ['tf_concat_1[0][0]']

tf_conv_45 (TFConv) (1, 40, 40, 128) 147584 ['tfc3_5[0][0]']

tf_concat_2 (TFConcat) (1, 40, 40, 256) 0 ['tf_conv_45[0][0]',
'tf_conv_39[0][0]']

tfc3_6 (TFC3) (1, 40, 40, 256) 295680 ['tf_concat_2[0][0]']

tf_conv_51 (TFConv) (1, 20, 20, 256) 590080 ['tfc3_6[0][0]']

tf_concat_3 (TFConcat) (1, 20, 20, 512) 0 ['tf_conv_51[0][0]',
'tf_conv_33[0][0]']

tfc3_7 (TFC3) (1, 20, 20, 512) 1181184 ['tf_concat_3[0][0]']

tf_detect (TFDetect) ((1, 25200, 85), 229245 ['tfc3_5[0][0]',
) 'tfc3_6[0][0]',
'tfc3_7[0][0]']

================================================================================================== Total params: 7,225,885 Trainable params: 0 Non-trainable params: 7,225,885


Environment

OS - Ubuntu 18.04.6 LTS Yolov -

Conda environment: Python - 3.7.15 Tensorflow - 2.11.0

Minimal Reproducible Example

python export.py --weights yolov5s.pt --include saved_model --keras

import tensorflow as tf k_model = tf.keras.models.load_model("./yolov5s_saved_model") print(k_model.summary())

tf.keras.models.save_model(k_model, "my_h5_model.h5")

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 year ago

👋 Hello @MatoFD, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 year ago

@MatoFD hi, thanks for raising this bug report! I'm not a TF export and don't use Keras models myself, so I'm not sure what to say. We do CI on export and inference every 24 hours on every export format including saved_model, but not with the --keras flag. The --keras flag was added to maintain backwards compatibility after moving away from it as the default due to file size limitations on keras exports. I believe all TF exports now utilize TF1 and are free of file-size constraints imposed by TF2.

github-actions[bot] commented 1 year ago

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!