tensorflow---读取数据集

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={
                                           'image':tf.FixedLenFeature([],tf.string),
                                           'label':tf.FixedLenFeature([],tf.int64)
                                       })

    images = tf.decode_raw(features['image'],tf.uint8)
    images = tf.reshape(images,[144,144,3])
    images = tf.cast(images,tf.float32)
    label = tf.cast(features['label'],tf.int32)

    images ,labels = tf.train.shuffle_batch([images,label],batch_size=10,capacity=4,min_after_dequeue=3)

    return images,labels

if __name__ == '__main__':
    filename = 'D:/项目实现/compare_insightface/data/lfw_one.tfrecord'

    images,labels = read_and_decode(filename)

    with tf.Session() as sess:
        start_time = time.time()
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)

        for i in range(300):
            image_raw,labels_raw = sess.run([images,labels])

            print(image_raw.shape,labels_raw.shape)
        end_time = time.time()
        print('the time is',end_time - start_time)
        coord.request_stop()
        coord.join(threads)

2.使用dataset

def parse_function(example_proto):
    features = {
        'image':tf.FixedLenFeature([],tf.string),
        'label':tf.FixedLenFeature([],tf.int64)
    }

    features = tf.parse_single_example(example_proto,features)

    img = tf.decode_raw(features['image'],tf.uint8)
    img = tf.reshape(img,shape=(144,144,3))
    r,g,b = tf.split(img,num_or_size_splits=3,axis=-1)
    img = tf.concat([b,g,r],axis=-1)
    label = tf.cast(features['label'],tf.int64)
    return img,label



filename = 'D:/项目实现/compare_insightface/data/lfw_one.tfrecord'

dataset = tf.data.TFRecordDataset(filename)
dataset = dataset.map(parse_function)
dataset = dataset.shuffle(buffer_size=10)
dataset = dataset.batch(10).repeat(2)  #数据集重复使用2次
iterator = dataset.make_initializable_iterator()

next_element = iterator.get_next()

with tf.Session() as sess:
    sess.run(iterator.initializer)
    start_time = time.time()
    for i in range(300):
        images_batch,labels_batch = sess.run(next_element)
        print(images_batch[0,0,0,0],labels_batch[0])
    end_time = time.time()

    print('the time is ',end_time - start_time)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值