首页 > 解决方案 > raise ValueError('boxlists 的所有元素都应该是 BoxList 对象')

问题描述

我是 tensorflow 的新手,我试图使用对象检测 API 来训练面部识别模型,但我被困在这部分

raise ValueError('all elements of boxlists should be BoxList objects')
ValueError: all elements of boxlists should be BoxList objects

我检查了 csv 文件,一切似乎都井井有条,但我无法解决问题

filename    width   height  class   xmin    ymin    xmax    ymax
img_591.jpg 450 431 Face    187 43  351 279
img_265.jpg 449 305 Face    61  20  149 154
img_265.jpg 449 305 Face    157 87  211 171
img_265.jpg 449 305 Face    298 47  382 187
img_423.jpg 449 450 Face    196 46  314 220
img_490.jpg 370 450 Face    111 24  179 132
img_17676.jpg   364 450 Face    1   1   57  74
img_17676.jpg   364 450 Face    62  13  162 171
img_228.jpg 409 450 Face    70  52  136 162
img_228.jpg 409 450 Face    258 10  332 128
img_402.jpg 450 311 Face    328 30  366 82
img_402.jpg 450 311 Face    78  39  162 197
img_402.jpg 450 311 Face    418 42  442 74
img_769.jpg 318 450 Face    24  11  148 207
img_769.jpg 318 450 Face    192 67  296 245
img_581.jpg 317 450 Face    85  86  225 292
img_723.jpg 348 449 Face    82  56  260 298
img_821.jpg 341 450 Face    82  57  274 347
img_610.jpg 410 294 Face    143 24  257 184
img_610.jpg 410 294 Face    1   88  79  202
img_610.jpg 410 294 Face    327 55  410 181
img_610.jpg 410 294 Face    120 108 192 204
img_610.jpg 410 294 Face    278 110 334 192
img_610.jpg 410 294 Face    19  40  93  142
img_1116.jpg    450 318 Face    149 29  259 193
img_1116.jpg    450 318 Face    365 94  385 122
img_19238.jpg   450 338 Face    45  36  129 158
img_19238.jpg   450 338 Face    325 103 411 235
img_660.jpg 386 450 Face    126 55  248 243
img_607.jpg 293 450 Face    106 10  188 134
img_3708.jpg    450 322 Face    80  2   208 176
img_3708.jpg    450 322 Face    299 24  403 194
img_511.jpg 449 320 Face    83  29  179 179
img_511.jpg 449 320 Face    240 30  314 142
Use `tf.data.Dataset.batch(..., drop_remainder=True)`.
<object_detection.core.box_list.BoxList object at 0x122d9d208>
Traceback (most recent call last):
  File "object_detection/model_main.py", line 112, in <module>
    tf.app.run()
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "object_detection/model_main.py", line 108, in main
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 471, in train_and_evaluate
    return executor.run()
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 610, in run
    return self.run_local()
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/training.py", line 711, in run_local
    saving_listeners=saving_listeners)
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 354, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1207, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1237, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "/Users/gllow/venv/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py", line 1195, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "/Users/gllow/Desktop/GL/Tensorflow/models/research/object_detection/model_lib.py", line 302, in model_fn
    features[fields.InputDataFields.true_image_shape])
  File "/Users/gllow/Desktop/GL/Tensorflow/models/research/object_detection/meta_architectures/ssd_meta_arch.py", line 578, in predict
    im_width=image_shape[2]))
  File "/Users/gllow/Desktop/GL/Tensorflow/models/research/object_detection/core/box_list_ops.py", line 565, in concatenate
    raise ValueError('all elements of boxlists should be BoxList objects')
ValueError: all elements of boxlists should be BoxList objects

"""

""" 用法:#来自 tensorflow/models/ #创建训练数据:python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=train.record #创建测试数据:python generate_tfrecord.py --csv_input=data /test_labels.csv --output_path=test.record """ 来自未来的进口部门 来自未来的进口 print_function 从未来的进口 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, OrderedDict

flags = tf.app.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


# TO-DO replace this with label map
def class_text_to_int(row_label):
    if row_label == 'Face':
        return 1
    else:
        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.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

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

    for index, 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(_):
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
    path = os.path.join(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())

    writer.close()
    output_path = os.path.join(os.getcwd(), FLAGS.output_path)
    print('Successfully created the TFRecords: {}'.format(output_path))


if __name__ == '__main__':
    tf.app.run()

"""

标签: tensorflowobject-detection-api

解决方案


推荐阅读