Error when converting xml files to tfrecord files
Asked Answered
U

4

6

I am following the TensorFlow 2 Object Detection API Tutorial on a Macbook

Here's what I got when running the given script for converting xmls to TFrecords

Traceback (most recent call last):
  File "generate_tfrecord.py", line 62, in <module>
    label_map_dict = label_map_util.get_label_map_dict(label_map)
  File "/usr/local/lib/python3.8/site-packages/object_detection/utils/label_map_util.py", line 164, in get_label_map_dict
    label_map = load_labelmap(label_map_path)
  File "/usr/local/lib/python3.8/site-packages/object_detection/utils/label_map_util.py", line 133, in load_labelmap
    label_map_string = fid.read()
  File "/usr/local/lib/python3.8/site-packages/tensorflow/python/lib/io/file_io.py", line 116, in read
    self._preread_check()
  File "/usr/local/lib/python3.8/site-packages/tensorflow/python/lib/io/file_io.py", line 78, in _preread_check
    self._read_buf = _pywrap_file_io.BufferedInputStream(
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. tensorflow.python._pywrap_file_io.BufferedInputStream(arg0: str, arg1: int)

Invoked with: item {
  name: "cat"
  id: 1
}
, 524288

My label map file contains the following

item {
    id: 1
    name: 'cat'
}
Unpen answered 6/9, 2020 at 11:47 Comment(2)
Can you add your label map file in your question?Auriculate
@YuexinXu please mark beryllite 's answer as accepted.Holloman
S
17

It seems the problem can be resolved by replacing

label_map = label_map_util.load_labelmap(args.labels_path)
label_map_dict = label_map_util.get_label_map_dict(label_map)

as

label_map_dict = label_map_util.get_label_map_dict(args.labels_path)
Streptothricin answered 16/9, 2020 at 9:10 Comment(2)
This worked for me as well. can you please explain why the other version didn't work?Abseil
Thanks, it's not only for mac, it's solved my error for windows also.Vauban
T
2

I've came across the same error and found a workaround.

Remove the lines:

label_map = label_map_util.load_labelmap(args.labels_path)
label_map_dict = label_map_util.get_label_map_dict(label_map)

And change the def class_text_to_int according to your label map like this:

def class_text_to_int(row_label):
    if row_label == 'cat':
        return 1

Now everything should be working fine.

Thermocouple answered 9/9, 2020 at 14:5 Comment(0)
C
1

In generate_tfrecord.py file comment line 61 & 62

label_map = label_map_util.load_labelmap(args.labels_path)
label_map_dict = label_map_util.get_label_map_dict(label_map)

Insert below code

label_map_dict = label_map_util.get_label_map_dict(args.labels_path)

Now save and re-run. It should create TFRecord file successfully.

Carolacarolan answered 21/4, 2022 at 14:15 Comment(0)
B
-1

Make sure you clone object detection files from tensorflow directory on github: https://github.com/tensorflow/models

I had cloned theses files from different github directories and then realized they are not the same, other directories have many issues

Box answered 1/4, 2021 at 13:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.