【问题标题】:Converting grayscale to RGB in tfrecord在 tfrecord 中将灰度转换为 RGB
【发布时间】:2018-04-25 08:15:47
【问题描述】:

我有一个灰度图像数据集,我想使用 sdd-mobilenet 检查点来训练我的对象检测。 将灰度图像转换为 RGB 的正确方法是什么,我可以将我的数据集转换为 tfrecord? 这是我使用的代码(注意注释部分对我不起作用)

with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
    encoded_jpg = fid.read()
# rgb_image = tf.image.grayscale_to_rgb(
#     tf.image.encode_jpeg(encoded_jpg),
#     name=None
# )
encoded_jpg_io = io.BytesIO(encoded_jpg)
encoded_jpg_io = tf.stack([encoded_jpg_io, encoded_jpg_io, encoded_jpg_io], axis=-1)
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/channels': dataset_util.int64_feature(),
    '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

【问题讨论】:

    标签: python tensorflow object-detection tfrecord


    【解决方案1】:

    我尝试了不同的方法,最终得到了答案(不仅转换为 tfrecords,还包括训练和对象检测本身)。

    如果数据集只包含灰度图像,Tensorflow 对象检测只需要定义通道数为 3。因此,唯一需要的更改是在代码中的训练特征中添加'image/channels': dataset_util.int64_feature(3) .完全不需要使用 cv2.COLOR_GRAY2BGR 或 tf.image.grayscale_to_rgb 将灰度转换为 RGB。

    使用这些方法转换图像最终会出现以下错误: outofrangeerror FIFOQueue '_3_prefetch_queue' is closed and has insufficient elements (requested 1, current size 0) 或者 OP_REQUIRES failed at iterator_ops.cc:891 : Invalid argument: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] 训练期间。

    为避免任何额外工作,请确保您使用的是 jpg 图片。如果您有其他格式,例如 bmp,请将它们转换为 jpg。请注意,更改文件扩展名不是转换。您必须使用您喜欢的任何工具对其进行转换。

    【讨论】:

      【解决方案2】:

      为什么 'image/channels': dataset_util.int64_feature(3) 有效而无效

      'image/channels': dataset_util.int64_feature(1) 因为您传递的是具有 1 个颜色通道的灰度图像?

      【讨论】:

      • 正如我所说,我想使用 sdd-mobilenet 检查点以避免从头开始训练。 ssd-mobilnet 使用 RGB 图像(3 个通道)进行训练。当然我可以只使用 1 个通道创建 tfrecord,但是在开始训练后,它会吐出错误。
      • 我正在努力从原始 maxtrix 形式直接输入我的数据,而不是有损压缩 jpeg。但到目前为止,我还没有弄清楚如何在运行 train.py 时停止错误。
      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 2010-10-15
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 2016-10-23
      相关资源
      最近更新 更多