tensorflow / tensorflow/models
ImportError: cannot import name 'convert_to_tensor_v1_with_dispatch' from 'tensorflow.python.framework.ops'
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.7k
- Forks
- 44.8k
- PR merge metrics
- No merged PRs in 30d
Description
Prerequisites
Please answer the following questions for yourself before submitting an issue.
- [x ] I am using the latest TensorFlow Model Garden release and TensorFlow 2.
- [x ] I am reporting the issue to the correct repository. (Model Garden official or research directory)
- [x ] I checked to make sure that this issue has not already been filed.
1. The entire URL of the file you are using
https://github.com/tensorflow/models/tree/master/research/object_detection/utils
2. Describe the bug
I was using Tensorflow 2.2 in Colab and planning to train ResNet50 SSD model from scratch.
I was writing a function to extract the number of classes from self-created label map pbtxt file. Then when I tried to run the function, it reported the import error.
3. Steps to reproduce
Here is the code for the function:
def get_num_classes(pbtxt_fname):
from object_detection.utils import label_map_util
label_map = label_map_util.load_labelmap(pbtxt_fname)
categories = label_map_util.convert_label_map_to_categories(
label_map, max_num_classes=90, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
return len(category_index.keys())
Here is the content of the label_map.pbtxt file:
item {
id: 1
name: 'apple'
}
item {
id: 2
name: 'banana'
}
item {
id: 3
name: 'orange'
}
When I ran:
num_classes = get_num_classes('label_map.pbtxt')
The following error appears:
/usr/local/lib/python3.7/dist-packages/tensorflow/_api/v2/compat/v1/compat/v1/init.py in ()
158 from tensorflow.python.framework.ops import container
159 from tensorflow.python.framework.ops import control_dependencies
--> 160 from tensorflow.python.framework.ops import convert_to_tensor_v1_with_dispatch as convert_to_tensor
161 from tensorflow.python.framework.ops import device
162 from tensorflow.python.framework.ops import disable_eager_execution
ImportError: cannot import name 'convert_to_tensor_v1_with_dispatch' from 'tensorflow.python.framework.ops' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py)
4. Expected behavior
I expect the function to return 3 in this case.
5. Additional context
Include any logs that would be helpful to diagnose the problem.
6. System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
- Mobile device name if the issue happens on a mobile device:
- TensorFlow installed from (source or binary):
- TensorFlow version (use command below): v2.2.0-rc4-8-g2b96f3662b 2.2.0
- Python version:Python 3.7.10
- Bazel version (if compiling from source):
- GCC/Compiler version (if compiling from source):
- CUDA/cuDNN version: cuda_11.0_bu.TC445_37.28845127_0
- GPU model and memory:GPU in Colab with Colab pro
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.