Decoding tfrecord with tfslim

蹲街弑〆低调 提交于 2019-11-30 15:31:50

Thanks to pudae, the problem is solved. I was needed to use:

 image_data = tf.gfile.FastGFile(filenames[i], 'rb').read()

Instead of this for loading data. That works perfectly now.

 img = load_image(filenames[i])
 image_data = tf.compat.as_bytes(img.tostring())

According to the error, I think the problem is that you use an image decoder for array data (decoded data) because you saved decoded data when creating TFRecords. Maybe you have noticed, when you are not using slim, you use tf.decode_raw to decode the data. But when you use slim, the 'image/format': tf.FixedLenFeature((), tf.string, default_value='raw') is not used and by default, slim will use image decoder.

I believe you use the code in slim/data, where format_key = 'image/format' is you need. So, like this:

keys_to_features = {
    'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
    'image/format': tf.FixedLenFeature((), tf.string, default_value='raw'),
    'image/class/label': tf.FixedLenFeature(
        [1], tf.int64, default_value=tf.zeros([1], dtype=tf.int64)),
}

items_to_handlers = {
    'image': tfexample_decoder.Image(
      image_key = 'image/encoded',
      format_key = 'image/format',
    'label': tfexample_decoder.Tensor('image/class/label'),
}

decoder = tfexample_decoder.TFExampleDecoder(
    keys_to_features, items_to_handlers)

But I am not sure this can solve your problem perfectly because I can't reproduce your work in my machine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!