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

hm2xizp9  于 2023-10-20  发布在  PyCharm
关注(0)|答案(1)|浏览(198)

我试图得到tfrecord文件使用以下代码,我打印多个部分的代码试图得到的问题,但;然而,我总是得到相同的消息“absl.flags._exceptions.UnrecognizedFlagError:未知的命令行标志“mode”。
为什么没有印国旗
谢谢你的帮助

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))
jdgnovmf

jdgnovmf1#

给予这个试试。我把你不需要的东西都删掉了。
您有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()

相关问题