tensorflow对象检测API:生成自定义数据集的TF记录
tensorflow object detection API: generate TF record of custom data set
我正在尝试用我自己的数据重新训练 tensorflow 对象检测 API
我用 labelImg 标记了我的图像,但是当我使用 tensorflow/models/research 中包含的脚本 create_pascal_tf_record.py 时,我遇到了一些错误,我真的不知道为什么会这样
python object_detection/dataset_tools/create_pascal_tf_record.py --data_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --label_map_path=/home/jim/Documents/tfAPI/workspace/training_cabbage/annotations/label_map.pbtxt --output_path=/home/jim/Desktop/cabbage_pascal.record --set=train --annotations_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --year=merged
Traceback (most recent call last):
File "object_detection/dataset_tools/create_pascal_tf_record.py", line 185, in <module>
tf.app.run()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "object_detection/dataset_tools/create_pascal_tf_record.py", line 167, in main
examples_list = dataset_util.read_examples_list(examples_path)
File "/home/jim/Documents/tfAPI/models/research/object_detection/utils/dataset_util.py", line 59, in read_examples_list
lines = fid.readlines()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 188, in readlines
self._preread_check()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 85, in _preread_check
compat.as_bytes(self.__name), 1024 * 512, status)
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: /home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/VOC2007/ImageSets/Main/aeroplane_train.txt; No such file or directory
火车文件夹包含 xml 和 jpg
注释文件夹包含我的 labelmap.pbtxt 用于我的自定义 class
并且我想在桌面上发布TF记录文件
似乎在我的图像和注释文件夹中找不到文件,但我不知道为什么
如果有人有想法,提前谢谢你
发生此错误是因为您使用了 PASCAL VOC 的代码,它需要特定的数据文件夹结构。基本上,您需要下载并解压 VOCdevkit 才能使脚本运行。正如用户 phd 指出的那样,您需要文件 VOC2007/ImageSets/Main/aeroplane_train.txt
.
我建议您编写自己的脚本来创建 tfrecords,这并不难。您只需要两个关键组件:
- 循环读取图像和注释的数据
- 一个将数据编码成
tf.train.Example
的函数。为此,您几乎可以重复使用 dict_to_tf_example
在循环内部,创建了 tf_example
,将其传递给 TFRecordWriter
:
writer.write(tf_example.SerializeToString())
好的,以备将来参考,这就是我将背景图像添加到数据集以允许模型对其进行训练的方式。
使用的函数来自:datitran/raccoon_dataset
- 生成 CSV 文件 ->
xml_to_csv.py
- 从 CSV 文件生成 TFRecord ->
generate_tfrecord.py
第一步 - 为它创建 XML 文件
背景图片示例XML文件
<annotation>
<folder>test/</folder>
<filename>XXXXXX.png</filename>
<path>your_path/test/XXXXXX.png</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>640</width>
<height>640</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
</annotation>
基本上你删除了整个<object>
(即没有注释)
第二步 - 生成 CSV 文件
使用 xml_to_csv.py
我只是添加了一点点变化,考虑没有任何注释(背景图像)的 XML 文件:
从原来的:
https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/xml_to_csv.py#L12-L22
我补充:
value = None
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
if value is None:
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
'-1',
'-1',
'-1',
'-1',
'-1'
)
xml_list.append(value)
如果 XML 文件中没有,我只是将负值添加到边界框的坐标中,背景图像就是这种情况,这在生成 TFRecords 时很有用.
第三步也是最后一步 - 生成 TFRecords
现在,在创建 TFRecords 时,如果相应的 row/image 具有负坐标,我只需将零值添加到记录中(以前,这甚至是不可能的)。
我补充:
for index, row in group.object.iterrows():
if int(row['xmin']) > -1:
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
else:
xmins.append(0)
xmaxs.append(0)
ymins.append(0)
ymaxs.append(0)
classes_text.append('something'.encode('utf8')) # this doe not matter for the background
classes.append(5000)
请注意,在 class_text
(else 语句)中,由于背景图像没有边界框,您可以将字符串替换为任何内容你会希望,对于背景案例,这不会出现在任何地方。
最后对于 classes
(else 语句)你只需要添加一个不属于你自己的数字标签 类.
对于那些想知道的人,我已经多次使用这个过程,并且目前适用于我的用例。
希望对您有所帮助。
我正在尝试用我自己的数据重新训练 tensorflow 对象检测 API 我用 labelImg 标记了我的图像,但是当我使用 tensorflow/models/research 中包含的脚本 create_pascal_tf_record.py 时,我遇到了一些错误,我真的不知道为什么会这样
python object_detection/dataset_tools/create_pascal_tf_record.py --data_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --label_map_path=/home/jim/Documents/tfAPI/workspace/training_cabbage/annotations/label_map.pbtxt --output_path=/home/jim/Desktop/cabbage_pascal.record --set=train --annotations_dir=/home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/ --year=merged
Traceback (most recent call last):
File "object_detection/dataset_tools/create_pascal_tf_record.py", line 185, in <module>
tf.app.run()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "object_detection/dataset_tools/create_pascal_tf_record.py", line 167, in main
examples_list = dataset_util.read_examples_list(examples_path)
File "/home/jim/Documents/tfAPI/models/research/object_detection/utils/dataset_util.py", line 59, in read_examples_list
lines = fid.readlines()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 188, in readlines
self._preread_check()
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/lib/io/file_io.py", line 85, in _preread_check
compat.as_bytes(self.__name), 1024 * 512, status)
File "/home/jim/.virtualenvs/enrouteDeepDroneTF/local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: /home/jim/Documents/tfAPI/workspace/training_cabbage/images/train/VOC2007/ImageSets/Main/aeroplane_train.txt; No such file or directory
火车文件夹包含 xml 和 jpg
注释文件夹包含我的 labelmap.pbtxt 用于我的自定义 class
并且我想在桌面上发布TF记录文件
似乎在我的图像和注释文件夹中找不到文件,但我不知道为什么 如果有人有想法,提前谢谢你
发生此错误是因为您使用了 PASCAL VOC 的代码,它需要特定的数据文件夹结构。基本上,您需要下载并解压 VOCdevkit 才能使脚本运行。正如用户 phd 指出的那样,您需要文件 VOC2007/ImageSets/Main/aeroplane_train.txt
.
我建议您编写自己的脚本来创建 tfrecords,这并不难。您只需要两个关键组件:
- 循环读取图像和注释的数据
- 一个将数据编码成
tf.train.Example
的函数。为此,您几乎可以重复使用dict_to_tf_example
在循环内部,创建了 tf_example
,将其传递给 TFRecordWriter
:
writer.write(tf_example.SerializeToString())
好的,以备将来参考,这就是我将背景图像添加到数据集以允许模型对其进行训练的方式。 使用的函数来自:datitran/raccoon_dataset
- 生成 CSV 文件 ->
xml_to_csv.py
- 从 CSV 文件生成 TFRecord ->
generate_tfrecord.py
第一步 - 为它创建 XML 文件
背景图片示例XML文件
<annotation>
<folder>test/</folder>
<filename>XXXXXX.png</filename>
<path>your_path/test/XXXXXX.png</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>640</width>
<height>640</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
</annotation>
基本上你删除了整个<object>
(即没有注释)
第二步 - 生成 CSV 文件
使用 xml_to_csv.py
我只是添加了一点点变化,考虑没有任何注释(背景图像)的 XML 文件:
从原来的:
https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/xml_to_csv.py#L12-L22
我补充:
value = None
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
if value is None:
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
'-1',
'-1',
'-1',
'-1',
'-1'
)
xml_list.append(value)
如果 XML 文件中没有,我只是将负值添加到边界框的坐标中,背景图像就是这种情况,这在生成 TFRecords 时很有用.
第三步也是最后一步 - 生成 TFRecords
现在,在创建 TFRecords 时,如果相应的 row/image 具有负坐标,我只需将零值添加到记录中(以前,这甚至是不可能的)。
我补充:
for index, row in group.object.iterrows():
if int(row['xmin']) > -1:
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
else:
xmins.append(0)
xmaxs.append(0)
ymins.append(0)
ymaxs.append(0)
classes_text.append('something'.encode('utf8')) # this doe not matter for the background
classes.append(5000)
请注意,在 class_text
(else 语句)中,由于背景图像没有边界框,您可以将字符串替换为任何内容你会希望,对于背景案例,这不会出现在任何地方。
最后对于 classes
(else 语句)你只需要添加一个不属于你自己的数字标签 类.
对于那些想知道的人,我已经多次使用这个过程,并且目前适用于我的用例。
希望对您有所帮助。