tfrecord读取数据踩得坑

#coding=utf-8

import json
import numpy as np

import tensorflow as tf

from IPython import embed


file_queue = tf.train.string_input_producer(
        ['test.tfrecord'],
        shuffle=False,
        num_epochs=1
)
reader = tf.TFRecordReader()
_, serialized = reader.read(file_queue)

contexts, features = tf.parse_single_sequence_example(
    serialized,
    context_features={
        "id": tf.FixedLenFeature([], tf.string),
        "labels": tf.VarLenFeature(tf.int64)
        },
    sequence_features={
        "rgb": tf.FixedLenSequenceFeature([], tf.string)
        }
)
conf = tf.ConfigProto()
conf.gpu_options.allow_growth = True
#shit = tf.train.shuffle_batch(
#    [contexts, features],
#    batch_size=1,
#    capacity=10 + 3,
#    min_after_dequeue=10)
    

with open('completion.json', 'r') as f:
    comple_dict = json.load(f)

miss = 0
hit = 0

with tf.Session(config=conf) as sess:
    sess.run(tf.local_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
        while not coord.should_stop():
            while True:
                ctx_v, feats_v = sess.run([contexts, features])
                if str(ctx_v['id'][:-4], encoding='utf-8') in comple_dict:
                    hit += 1
                else:
                    miss += 1
                print('hit: %d, miss: %d' % (hit, miss))
    except tf.errors.OutOfRangeError as e:
        print(e)
        print('finish')


embed()

1)如果使用了Num_epochs=1,必须得tf.local_variables_initializer(),不然会一开始就outofrangeerror

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值