'Error when converting xml files to tfrecord files

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'
}


Solution 1:[1]

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)

Solution 2:[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.

Solution 3:[3]

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.

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 beryllite
Solution 2 Ian
Solution 3 Kawsikan
Solution 4 Husam Alhwadi