optional arguments:
-h, --help show this help message and exit
-x XML_DIR, --xml_dir XML_DIR
Path to the folder where the input .xml files are stored.
-l LABELS_PATH, --labels_path LABELS_PATH
Path to the labels (.pbtxt) file.
-o OUTPUT_PATH, --output_path OUTPUT_PATH
Path of output TFRecord (.record) file.
-i IMAGE_DIR, --image_dir IMAGE_DIR
Path to the folder where the input image files are stored. Defaults to the same directory as XML_DIR.
-c CSV_PATH, --csv_path CSV_PATH
Path of output .csv file. If none provided, then no file will be written.
"""
import os
import glob
import pandas as pd
import io
import xml.etree.ElementTree as ET
import argparse
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
import tensorflow.compat.v1 as tf
from PIL import Image
from object_detection.utils import dataset_util, label_map_util
from collections import namedtuple
Initiate argument parser
parser = argparse.ArgumentParser(
description="Sample TensorFlow XML-to-TFRecord converter")
parser.add_argument("-x",
"--xml_dir",
help="Path to the folder where the input .xml files are stored.",
type=str)
parser.add_argument("-l",
"--labels_path",
help="Path to the labels (.pbtxt) file.", type=str)
parser.add_argument("-o",
"--output_path",
help="Path of output TFRecord (.record) file.", type=str)
parser.add_argument("-i",
"--image_dir",
help="Path to the folder where the input image files are stored. "
"Defaults to the same directory as XML_DIR.",
type=str, default=None)
parser.add_argument("-c",
"--csv_path",
help="Path of output .csv file. If none provided, then no file will be "
"written.",
type=str, default=None)
args = parser.parse_args()
if args.image_dir is None:
args.image_dir = args.xml_dir
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
writer = tf.python_io.TFRecordWriter(args.output_path)
path = os.path.join(args.image_dir)
examples = xml_to_csv(args.xml_dir)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
print('Successfully created the TFRecord file: {}'.format(args.output_path))
if args.csv_path is not None:
examples.to_csv(args.csv_path, index=None)
print('Successfully created the CSV file: {}'.format(args.csv_path))
Traceback (most recent call last):
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 168, in
tf.app.run()
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 158, in main
tf_example = create_tf_example(group, path)
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 132, in create_tf_example
classes.append(class_text_to_int(row['class']))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 101, in class_text_to_int
return label_map_dict[row_label]
KeyError: 'w'
Traceback (most recent call last):
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 168, in
tf.app.run()
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 158, in main
tf_example = create_tf_example(group, path)
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 132, in create_tf_example
classes.append(class_text_to_int(row['class']))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 101, in class_text_to_int
return label_map_dict[row_label]
KeyError: 'w'
i use this code for TF recorde
""" Sample TensorFlow XML-to-TFRecord converter
usage: generate_tfrecord.py [-h] [-x XML_DIR] [-l LABELS_PATH] [-o OUTPUT_PATH] [-i IMAGE_DIR] [-c CSV_PATH]
optional arguments: -h, --help show this help message and exit -x XML_DIR, --xml_dir XML_DIR Path to the folder where the input .xml files are stored. -l LABELS_PATH, --labels_path LABELS_PATH Path to the labels (.pbtxt) file. -o OUTPUT_PATH, --output_path OUTPUT_PATH Path of output TFRecord (.record) file. -i IMAGE_DIR, --image_dir IMAGE_DIR Path to the folder where the input image files are stored. Defaults to the same directory as XML_DIR. -c CSV_PATH, --csv_path CSV_PATH Path of output .csv file. If none provided, then no file will be written. """
import os import glob import pandas as pd import io import xml.etree.ElementTree as ET import argparse
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1) import tensorflow.compat.v1 as tf from PIL import Image from object_detection.utils import dataset_util, label_map_util from collections import namedtuple
Initiate argument parser
parser = argparse.ArgumentParser( description="Sample TensorFlow XML-to-TFRecord converter") parser.add_argument("-x", "--xml_dir", help="Path to the folder where the input .xml files are stored.", type=str) parser.add_argument("-l", "--labels_path", help="Path to the labels (.pbtxt) file.", type=str) parser.add_argument("-o", "--output_path", help="Path of output TFRecord (.record) file.", type=str) parser.add_argument("-i", "--image_dir", help="Path to the folder where the input image files are stored. " "Defaults to the same directory as XML_DIR.", type=str, default=None) parser.add_argument("-c", "--csv_path", help="Path of output .csv file. If none provided, then no file will be " "written.", type=str, default=None)
args = parser.parse_args()
if args.image_dir is None: args.image_dir = args.xml_dir
label_map = label_map_util.load_labelmap(args.labels_path) label_map_dict = label_map_util.get_label_map_dict(label_map)
def xml_to_csv(path): """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas dataframe.
def class_text_to_int(row_label): return label_map_dict[row_label]
def split(df, group): data = namedtuple('data', ['filename', 'object']) gb = df.groupby(group) return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path): with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) image = Image.open(encoded_jpg_io) width, height = image.size
def main(_):
if name == 'main': tf.app.run()
whene i execute it by those commands
!python {SCRIPTS_PATH + '/generate_tfrecord.py'} -x {IMAGE_PATH + '/train'} -l {ANNOTATION_PATH + '/label_map.pbtxt'} -o {ANNOTATION_PATH + '/train.record'} !python {SCRIPTS_PATH + '/generate_tfrecord.py'} -x{IMAGE_PATH + '/test'} -l {ANNOTATION_PATH + '/label_map.pbtxt'} -o {ANNOTATION_PATH + '/test.record'}
i get those errors
Traceback (most recent call last): File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 168, in
tf.app.run()
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 158, in main
tf_example = create_tf_example(group, path)
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 132, in create_tf_example
classes.append(class_text_to_int(row['class']))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 101, in class_text_to_int
return label_map_dict[row_label]
KeyError: 'w'
Traceback (most recent call last):
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 168, in
tf.app.run()
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 303, in run
_run_main(main, args)
File "C:\Users\PC.conda\envs\tensorflow\lib\site-packages\absl\app.py", line 251, in _run_main
sys.exit(main(argv))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 158, in main
tf_example = create_tf_example(group, path)
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 132, in create_tf_example
classes.append(class_text_to_int(row['class']))
File "C:\Users\PC\Documents\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py", line 101, in class_text_to_int
return label_map_dict[row_label]
KeyError: 'w'
what should i do