yolov5
yolov5 copied to clipboard
when export yolov5 model to pb format, load the pb model occur ERROR
Search before asking
- [X] I have searched the YOLOv5 issues and discussions and found no similar questions.
Question
when export yolov5 model to pb format, load the pb model occur ERROR, and my command is
python .\export.py --data xxx\coco128.yaml --weights xxx\best.pt --include pb
when use tensorflow loading the exported pb model, something wrong occur:
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 432, in new_func
return func(*args, **kwargs)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 663, in import_graph_def
ops.set_shapes_for_outputs(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2501, in set_shapes_for_outputs
return _set_shapes_for_outputs(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2474, in _set_shapes_for_outputs
shapes = shape_func(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2404, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 668, in _call_cpp_shape_fn_impl
v = tensor_util.constant_value(op.inputs[idx])
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 807, in constant_value
ret = _ConstantValue(tensor, partial)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 672, in _ConstantValue
return MakeNdarray(tensor.op.get_attr("value"))
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 595, in MakeNdarray
return np.fromiter(tensor.int_val, dtype=dtype).reshape(shape)
ValueError: cannot reshape array of size 0 into shape (1,)
Is that means when export yolo model , the img_size parameter is need to be assigned? But my code (ussing TF) is batch_size=1, has nothing to do with the size of the image entered. How to solute this wrong and what I need to do
Additional
No response
👋 Hello @ybshaw, 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 a minimum reproducible example to help us debug it.
If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.
Requirements
Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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):
-
Notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
-
Docker Image. See Docker Quickstart Guide
Status
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.
Introducing YOLOv8 🚀
We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!
Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.
Check out our YOLOv8 Docs for details and get started with:
pip install ultralytics
@ybshaw hello! It seems like you're encountering an error when trying to load a .pb
model exported from YOLOv5. The error message indicates there might be an issue with the shape inference during the import of the graph definition in TensorFlow.
To help you better, could you please provide the following additional information:
- The version of TensorFlow you are using.
- The exact command you used to export the model (ensure there are no typos and that all paths are correct).
- Confirm if you have successfully exported other formats (like ONNX or TorchScript) for this model.
In the meantime, please ensure that your environment is set up according to the requirements specified in the YOLOv5 documentation and that you are using a compatible version of TensorFlow for the export process.
If you have not already, you might also want to check the export.py
script for any recent updates or changes that could affect the export process.
Once you provide the additional details, we'll be in a better position to assist you. Thank you for reaching out, and we appreciate your contribution to the YOLOv5 community! 🚀
@ybshaw hello! It seems like you're encountering an error when trying to load a
.pb
model exported from YOLOv5. The error message indicates there might be an issue with the shape inference during the import of the graph definition in TensorFlow.To help you better, could you please provide the following additional information:
- The version of TensorFlow you are using.
- The exact command you used to export the model (ensure there are no typos and that all paths are correct).
- Confirm if you have successfully exported other formats (like ONNX or TorchScript) for this model.
In the meantime, please ensure that your environment is set up according to the requirements specified in the YOLOv5 documentation and that you are using a compatible version of TensorFlow for the export process.
If you have not already, you might also want to check the
export.py
script for any recent updates or changes that could affect the export process.Once you provide the additional details, we'll be in a better position to assist you. Thank you for reaching out, and we appreciate your contribution to the YOLOv5 community! 🚀
Thanks for your reply, additional information are as following:
- TensorFlow verion:
1.6.0
(cpu version) - export command:
python .\export.py --data xxx\cfg\coco128.yaml --weights xxx\my_best.pt --include pb
- I confirmed that I have successfully exported the pb model
when loading the exported pb model with following code, something errors encountered, the error is from the method tf.import_graph_def(graph_def, name="")
with tf.gfile.GFile(self.trained_model, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="")
and detail error as following:
tf.import_graph_def(graph_def, name="")
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 432, in new_func
return func(*args, **kwargs)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 663, in import_graph_def
ops.set_shapes_for_outputs(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2501, in set_shapes_for_outputs
return _set_shapes_for_outputs(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2474, in _set_shapes_for_outputs
shapes = shape_func(op)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2404, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 668, in _call_cpp_shape_fn_impl
v = tensor_util.constant_value(op.inputs[idx])
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 807, in constant_value
ret = _ConstantValue(tensor, partial)
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 672, in _ConstantValue
return MakeNdarray(tensor.op.get_attr("value"))
File "/home/apprun/program/python/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 595, in MakeNdarray
return np.fromiter(tensor.int_val, dtype=dtype).reshape(shape)
ValueError: cannot reshape array of size 0 into shape (1,)
@ybshaw, thank you for providing the additional details. It appears that you are using TensorFlow version 1.6.0, which is quite outdated. YOLOv5's export functionality is designed to work with TensorFlow 2.x, as there have been significant changes and improvements in TensorFlow since version 1.x.
Here are a few steps you can take to resolve the issue:
-
Upgrade TensorFlow: Update your TensorFlow to a 2.x version, as YOLOv5's export scripts are compatible with the newer versions of TensorFlow. This should hopefully resolve compatibility issues with the
.pb
model. -
Check TensorFlow Compatibility: Ensure that the version of TensorFlow you upgrade to is compatible with the rest of your environment and dependencies.
-
Re-export the Model: After upgrading TensorFlow, re-export your YOLOv5 model to the
.pb
format using theexport.py
script. -
Load the Model with Updated Code: TensorFlow 1.x code is not compatible with TensorFlow 2.x without using the compatibility module (
tf.compat.v1
). If you upgrade to TensorFlow 2.x, you will need to update your code to use the new APIs or enable compatibility mode. -
Review the Export Script: If you continue to encounter issues, review the
export.py
script for any potential updates or changes that might affect the export process.
Please try these steps and let us know if the issue persists. We're here to help! 🌟
👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.
For additional resources and information, please see the links below:
- Docs: https://docs.ultralytics.com
- HUB: https://hub.ultralytics.com
- Community: https://community.ultralytics.com
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 YOLO 🚀 and Vision AI ⭐