xajlzz 2018-07-26 03:58 采纳率: 0%
浏览 1601
已结题

关于Tensorflow的TFRecords读取问题

生成TFRecords核心代码(图片处理成224 * 224 * 3)

 with tf.Session() as sess:
        for i in range(len(img_path_0)):
            # 获得图片的路径和类型
            img_path = img_path_0[i]
            label = label_0[i]


            # 读取图片
            image = tf.gfile.FastGFile(img_path, 'rb').read()
            # 解码图片(如果是 png 格式就使用 decode_png)
            image = tf.image.decode_jpeg(image)
            image_size = 224
                        # 图像预处理
            image = ima_preprocess.preprocess_for_train(image, image_size, image_size)
            # 转换数据类型
            image = tf.image.convert_image_dtype(image, dtype=tf.float32)
            # resize 224 * 224 * 3
            image = tf.image.resize_images(image, [width, height], method=0)
            # 执行 op: image
            image = sess.run(image)
            #print(image)
            # print(image.shape)
            # plt.imshow(image)
            # plt.show()

            # 将其图片矩阵转换成 tostring,tobytes
            image_raw = image.tostring()

            # 将数据整理成 TFRecord 需要的数据结构
            example = tf.train.Example(features=tf.train.Features(feature={
                'image_raw': _bytes_feature(image_raw),
                'label': _int64_feature(label),
                'height': _int64_feature(height),
                'width': _int64_feature(width),
                'channels': _int64_feature(channels),
            }))

            # 写 TFRecord
            writer.write(example.SerializeToString())
            print(i, label)

    writer.close()

在读取TFRecords的时候,由于之前使用tostring,导致使用代码

 decode_image = tf.decode_raw(features['image'], tf.uint8)

产生的矩阵比原来大了4倍,即 224 * 224 * 3 * 4
没办法使用之后的reshape[224, 224, 3]

请问,tf下有什么函数可以把TFRecords内的数据decode到原始的图片的矩阵??

  • 写回答

1条回答 默认 最新

  • devmiao 2018-07-26 15:20
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?