我正在使用 TF-Slim 附带的脚本来验证我的训练模型。它工作正常,但我想获得错误分类文件的列表。
该脚本使用https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/evaluation.py但即使在那里我也找不到打印错误分类文件的任何选项。
我怎样才能做到这一点?
我正在使用 TF-Slim 附带的脚本来验证我的训练模型。它工作正常,但我想获得错误分类文件的列表。
该脚本使用https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/slim/python/slim/evaluation.py但即使在那里我也找不到打印错误分类文件的任何选项。
我怎样才能做到这一点?
在高层次上,您需要做 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)
不幸的是,我没有设置来测试这个。让我知道它是否有效!
shadow chris 为我指明了正确的方向,因此我分享了我的解决方案,以使其适用于 TF 记录数据集。
为了更好地理解,我将我的代码与 TF-Slim 的花朵示例联系起来。
1) 修改您的数据集脚本以在 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) 将 filename 参数添加到data util的image_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)修改下载和转换脚本以保存文件名
使用文件名提供您的 TF 记录。
example = dataset_utils.image_to_tfexample(
image_data, 'jpg', height, width, class_id, filenames[i])
4)在您的评估地图中,将 imgs 错误分类为文件名
使用 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)
获取错误分类的 img 并将它们映射到文件名:
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)