【问题标题】:How to get misclassified files in TF-Slim's eval_image_classifier.py?如何在 TF-Slim 的 eval_image_classifier.py 中获取错误分类的文件?
【发布时间】:2017-08-21 06:17:09
【问题描述】:

我正在使用 TF-Slim 附带的 script 来验证我的训练模型。它工作正常,但我想获得错误分类文件的列表。

该脚本使用https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/evaluation.py,但即使在那里我也找不到打印错误分类文件的任何选项。

我怎样才能做到这一点?

【问题讨论】:

    标签: tensorflow tf-slim


    【解决方案1】:

    在高层次上,你需要做 3 件事:

    1) 从数据加载器中获取您的文件名。如果您使用 tfrecords 中的 tf-slim 数据集,则文件名可能未存储在 tfrecord 中,因此您可能会不走运。但是,如果您使用 tf.WholeFileReader 直接从文件系统中使用图像文件,那么您可以获得形成批处理的文件名张量:

    def load_data():
        train_image_names = ... # list of filenames
        filename_queue = tf.train.string_input_producer(train_image_names)
        reader = tf.WholeFileReader()
        image_filename, image_file = reader.read(filename_queue)
        image = tf.image.decode_jpeg(image_file, channels=3)
    
        .... # load your labels from somewhere
    
        return image_filename, image, label
    
    
     # in your eval code
     image_fn, image, label = load_data()
    
     filenames, images, labels = tf.train.batch(
                                    [image_fn, image, label],
                                    batch_size=32,
                                    num_threads=2,
                                    capacity=100,
                                    allow_smaller_final_batch=True)
    

    2) 推理后用你的结果掩盖你的文件名张量:

    logits = my_network(images)
    preds = tf.argmax(logits, 1)
    mislabeled = tf.not_equal(preds, labels)
    mislabeled_filenames = tf.boolean_mask(filenames, mislabeled)
    

    3) 将所有这些放入您的 eval_op:

    eval_op = tf.Print(eval_op, [mislabeled_filenames])
    
    slim.evaluation.evaluate_once(
                            .... # other options 
                            eval_op=eval_op,
                            .... # other options)
    

    很遗憾,我没有设置来测试这个。让我知道它是否有效!

    【讨论】:

    • 感谢您的回答。当我使用来自 tfrecords 的 tf-slim 数据集时,我需要更改我的代码。有新信息我会尽快发布更新。
    • 我能够将您的方法转移到使用 tf-records !我会接受你的回答并添加我的 tf-record 解决方案。
    【解决方案2】:

    shadow chris 为我指明了正确的方向,因此我分享了我的解决方案,使其适用于 TF 记录数据集。

    为了更好地理解,我将我的代码与 TF-Slim 的花朵示例联系起来。

    1) 修改您的 dataset script 以在 TF 记录中存储文件名功能。

      keys_to_features = {
          'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
          'image/format': tf.FixedLenFeature((), tf.string, default_value='png'),
          'image/class/label': tf.FixedLenFeature(
              [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)),
          'image/filename': tf.FixedLenFeature((), tf.string, default_value=''),
      }
    
      items_to_handlers = {
          'image': slim.tfexample_decoder.Image(),
          'label': slim.tfexample_decoder.Tensor('image/class/label'),
          'filename': slim.tfexample_decoder.Tensor('image/filename'),
      }
    

    2) 给data utilimage_to_tfexample函数添加文件名参数

    它应该看起来像:

    def image_to_tfexample(image_data, image_format, height, width, class_id, filename):
     return tf.train.Example(features=tf.train.Features(feature={
          'image/encoded': bytes_feature(image_data),
          'image/format': bytes_feature(image_format),
          'image/class/label': int64_feature(class_id),
          'image/height': int64_feature(height),
          'image/width': int64_feature(width),
          'image/filename': bytes_feature(filename)
      }))
    

    3) 修改download and convert script 保存文件名

    用文件名输入你的 TF 记录。

        example = dataset_utils.image_to_tfexample(
            image_data, 'jpg', height, width, class_id, filenames[i])
    

    4) 在您的评估图中,将 imgs 错误分类为文件名

    我指的是eval_image_classifier.py

    使用 tf.train.batch 检索文件名:

    images, labels, filenames = tf.train.batch(
        [image, label, filename],
        batch_size=FLAGS.batch_size,
        num_threads=FLAGS.num_preprocessing_threads,
        capacity=5 * FLAGS.batch_size)
    

    获取错误分类的图像并将它们映射到文件名:

    predictions = tf.argmax(logits, 1)
    labels = tf.squeeze(labels)
    mislabeled = tf.not_equal(predictions, labels)
    mislabeled_filenames = tf.boolean_mask(filenames, mislabeled)
    

    打印:

    eval_op = tf.Print(eval_op, [mislabeled_filenames])
    
    slim.evaluation.evaluate_once(
                            .... # other options 
                            eval_op=eval_op,
                            .... # other options)
    

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 1970-01-01
      • 2018-07-14
      • 2020-03-22
      • 1970-01-01
      • 2018-07-11
      • 2017-02-05
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多