FLAG问题:absl.flags._exceptions.UnrecognizedFlagError:未知的命令行标志“模式”

问题描述 投票:0回答:1

我正在尝试使用以下代码获取 tfrecord 文件,并且我打印了代码的多个部分以尝试解决问题,但是;但是,我总是收到相同的消息“absl.flags._exceptions.UnrecognizedFlagError:未知的命令行标志'模式'”。

为什么不打印标志

感谢您的帮助。

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple
from numpy import split

csv_input = "C:\\Users\\Documents\\Research\\ShortCut\\Model_B\\PTrain_labels.csv"
output_path = "C:\\Users\\Documents\\OutPutPath\\Output.tfrecord"
image_dir = "C:\\Users\\Documents\\Research\\ShortCut\\Model_B\\Base"

flags = tf.compat.v1.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', '', 'Path to images')
FLAGS = flags.FLAGS

print("csv_input flag:", FLAGS.csv_input)
print("output_path flag:", FLAGS.output_path)
print("image_dir flag:", FLAGS.image_dir)


def class_text_to_int(row_label):
        if row_label == "M":
            return 1
        elif row_label == "J":
            return 2
        else:
            return None

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.io.gfile.GFile(os.path.join(path, 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

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for _, row in group.object.iterrows():
        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']))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
       'image/height': dataset_util.int64_feature(height),
       'image/width': dataset_util.int64_feature(width),
       'image/filename': dataset_util.bytes_feature(filename),
       'image/source_id': dataset_util.bytes_feature(filename),
       'image/encoded': dataset_util.bytes_feature(encoded_jpg),
       'image/format': dataset_util.bytes_feature(image_format),
       'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
       'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
       'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
       'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
       'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
       'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example

def main(_):
    # Print the values of csv_input, output_path, and image_dir
    print("csv_input:", csv_input)
    print("output_path:", output_path)
    print("image_dir:", image_dir)

    print("csv_input:", FLAGS.csv_input)
    print("output_path:", FLAGS.output_path)
    print("image_dir:", FLAGS.image_dir)

    writer = tf.io.TFRecordWriter(FLAGS.output_path)
    path = FLAGS.image_dir
    examples = pd.read_csv(FLAGS.csv_input)
    grouped = split(examples, 'filename')
    for group in grouped:
        tf_example = create_tf_example(group, path)
        writer.write(tf_example.SerializeToString())

    print("Writing TFRecord to:", FLAGS.output_path)

    writer.close()
    print('Successfully created the TFRecords: {}'.format(FLAGS.output_path))
    print('Successfully created the TFRecords: {}'.format(FLAGS.output_path))

if __name__ == '__main__':
    try:
        tf.compat.v1.app.run()
    except Exception as e:
        # Print any error messages that occur during script execution
        print("An error occurred:", str(e))

python tensorflow pycharm object-detection flags
1个回答
0
投票

尝试一下。我删除了一堆你不需要的东西。

您有

from numpy import split
,但随后提供了您自己的
split
副本。有很多
__future__
的东西你并不需要。您不需要命令行参数,因为您正在对文件和目录进行硬编码。我实际上已经检查了这里的处理过程,当然我没有你的数据,但是看看这是否能让你克服第一个障碍。

import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple

csv_input = "C:\\Users\\Documents\\Research\\ShortCut\\Model_B\\PTrain_labels.csv"
output_path = "C:\\Users\\Documents\\OutPutPath\\Output.tfrecord"
image_dir = "C:\\Users\\Documents\\Research\\ShortCut\\Model_B\\Base"


def class_text_to_int(row_label):
        if row_label == "M":
            return 1
        elif row_label == "J":
            return 2
        else:
            return None

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.io.gfile.GFile(os.path.join(path, 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

    filename = group.filename.encode('utf8')
    image_format = b'jpg'
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for _, row in group.object.iterrows():
        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']))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
       'image/height': dataset_util.int64_feature(height),
       'image/width': dataset_util.int64_feature(width),
       'image/filename': dataset_util.bytes_feature(filename),
       'image/source_id': dataset_util.bytes_feature(filename),
       'image/encoded': dataset_util.bytes_feature(encoded_jpg),
       'image/format': dataset_util.bytes_feature(image_format),
       'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
       'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
       'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
       'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
       'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
       'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example

def main(_):
    # Print the values of csv_input, output_path, and image_dir
    print("csv_input:", csv_input)
    print("output_path:", output_path)
    print("image_dir:", image_dir)

    writer = tf.io.TFRecordWriter(output_path)
    path = image_dir
    examples = pd.read_csv(csv_input)
    grouped = split(examples, 'filename')
    for group in grouped:
        tf_example = create_tf_example(group, path)
        writer.write(tf_example.SerializeToString())

    print("Writing TFRecord to:", output_path)

    writer.close()
    print('Successfully created the TFRecords: {}'.format(output_path))

if __name__ == '__main__':
    tf.compat.v1.app.run()
© www.soinside.com 2019 - 2024. All rights reserved.