tensorflow实战系列(二)TFRecordReader

前面写了TFRecordWriter的生成。这次写TFRecordReader。

代码附上:

def read_and_decode(filename):
    #根据文件名生成一个队列
    filename_queue = tf.train.string_input_producer([filename])

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)   #返回文件名和文件
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'label': tf.FixedLenFeature([], tf.int64),
                                           'img_raw' : tf.FixedLenFeature([], tf.string),
                                       })

    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [224, 224, 3])
 #    img = tf.reshape(img, [39, 39, 3])
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
    label = tf.cast(features['label'], tf.int32)
    print img,label
    return img, label

这里我碰到了一个非常奇怪的问题,困扰了我大半天。百思不得其解。

问题的报错是:读入没有任何问题,在把读入的数据输入tensorflow中训练模型的时候,前50次都是好的,

然后就开始报错:

img = tf.reshape(img, [224, 224, 3])

输入的tensor是200704,而期望的tensor是150528

200704=224*224*4,

150528=224*224*3.是不是通道数不对?我用opencv读入后打印出来的通道数都是3。

真是奇怪的问题。

后来把原始图片中的png和jpeg格式的图片删除了。重新生成TFRecord。没有报错了

估计还是图片的底层属性问题

原文地址:https://www.cnblogs.com/whu-zeng/p/6293589.html