【问题标题】:The following classes have no ground truth examples以下类没有基本事实示例
【发布时间】:2019-02-08 08:54:57
【问题描述】:

我已经在数据集上训练了来自 zoo 的 ssd_mobilenet_v1_coco 模型,其中包含约 25000 个 48x48 像素的交通标志图片:

训练过程看起来不错(从 ~15.5 开始下降到 0.0135): 但是当我使用包含 ~7k 图片的测试数据集运行 eval.py 时: 最后我看到错误:以下类没有基本事实示例[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67]

使用该脚本从 csv 生成的记录:

    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

import sys
sys.path.append("C:\\Users\\Jekoc\\Desktop\\TRAINING\\rus\\models-master\\research\\")
sys.path.append("C:\\Users\\Jekoc\\Desktop\\TRAINING\\rus\\models-master\\research\\object_detection\\utils")

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 != 0:
        return row_label
    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_png = fid.read()
    encoded_png_io = io.BytesIO(encoded_png)
    image = Image.open(encoded_png_io)
    width, height = image.size
    filename = group.filename.encode('utf8')
    image_format = b'png'#changed from jpg to png
    xmins = []
    xmaxs = []
    ymins = []
    ymaxs = []
    classes_text = []
    classes = []

    for index, row in group.object.iterrows():
        xmins.append(0 / width)
        xmaxs.append(48 /width) # size is 48x48px so xmaxs=1
        ymins.append(0 /height)
        ymaxs.append(48 /height) # size is 48x48px so ymaxs=1
        classes_text.append(str(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_png),
        '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()

我能用它做什么?提前致谢

【问题讨论】:

    标签: python tensorflow image-recognition object-detection-api


    【解决方案1】:

    您的数据有问题,即 int 类标签。我不知道你为什么要为分类任务使用对象检测框架,但要使用它,你需要准备在类名和整数 ID 之间建立对应关系的标签映射。见this example。

    顺便说一句,这在你的代码中:

    # TO-DO replace this with label map
    def class_text_to_int(row_label):
        if row_label != 0:
            return row_label
        else:
            None
    

    所以,你需要:

    1. 为您的数据集创建标签映射。查看示例here。
    2. 从中创建一个 label_dict:label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)
    3. 用它来获取整数ID:classes.append(label_map_dict[row['class']])

    【讨论】:

    • 我使用结构为 |filename,class| 的 csv 文件生成我的记录|000000.png,67| (例如)并且所有这些类都在我的标签映射中: item { id: 1 name: '1.1' } 该标签映射在模型配置文件中用于训练和评估。我使用对象检测 api,因为模型训练是我的应用程序通过摄像头识别交通标志的第一步。顺便说一句,我不确定那一行'classes.append(label_map dict[row['class']])',因为标签映射的例子都没有'行',尤其是名称为'class'的行。
    猜你喜欢
    • 2020-05-11
    • 2018-12-18
    • 2019-09-18
    • 2019-08-31
    • 1970-01-01
    • 2013-08-22
    • 2020-06-03
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多