Tensorflow学习笔记-输入数据处理框架

  通过前面几节的总结,Tensorflow关于TFRecord格式文件的处理、模型的训练的架构为:
  1、获取文件列表、创建文件队列:http://blog.csdn.net/lovelyaiq/article/details/78711944
  2、图像预处理:http://blog.csdn.net/lovelyaiq/article/details/78716325
  3、合成Batch:http://blog.csdn.net/lovelyaiq/article/details/78727189
  4、设计损失函数、梯度下降算法:http://blog.csdn.net/lovelyaiq/article/details/78616736

Created with Raphaël 2.1.0 获取输入文件列表 创建输入文件队列 从文件队列读取数据 整理成Batch作为神经网络的输入 设计损失函数 选择梯度下降法 训练

  对应的代码流程如下:

    # 创建文件列表,并通过文件列表来创建文件队列。在调用输入数据处理流程前,需要统一
    # 所有的原始数据格式,并将它们存储到TFRecord文件中
    # match_filenames_once 获取符合正则表达式的所有文件
    files = tf.train.match_filenames_once('path/to/file-*-*')
    # 将文件列表生成文件队列
    filename_queue = tf.train.string_input_producer(files,shuffle=True)

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    # image:存储图像中的原始数据
    # label该样本所对应的标签
    # width,height,channel
    features = tf.parse_single_example(serialized_example,features={
        'image' : tf.FixedLenFeature([],tf.string),
        'label': tf.FixedLenFeature([], tf.int64),
        'width': tf.FixedLenFeature([], tf.int64),
        'heigth': tf.FixedLenFeature([], tf.int64),
        'channel': tf.FixedLenFeature([], tf.int64)
    })

    image, label = features['image'], features['label']
    width, height = features['width'], features['height']
    channel = features['channel']
    # 将原始图像数据解析出像素矩阵,并根据图像尺寸还原糖图像。
    decode_image = tf.decode_raw(image)
    decode_image.set_shape([width,height,channel])
    # 神经网络的输入大小
    image_size = 299
    # 对图像进行预处理操作,比对亮度、对比度、随机裁剪等操作
    distorted_image = propocess_train(decode_image,image_size,None)

    # shuffle_batch中的参数
    min_after_dequeue = 1000
    batch_size = 100
    capacity = min_after_dequeue + 3*batch_size
    image_batch,label_batch = tf.train.shuffle_batch([distorted_image,label],
                                                     batch_size=batch_size,capacity=capacity,
                                                     min_after_dequeue=min_after_dequeue)

    logit = inference(image_batch)
    loss = cal_loss(logit,label_batch)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    with tf.Session() as sess:
        # 变量初始化
        tf.global_variables_initializer().run()
        # 线程初始化和启动
        coord = tf.train.Coordinator()
        theads = tf.train.start_queue_runners(sess=sess,coord=coord)

        for i in range(STEPS):
            sess.run(train_step)
        # 停止所有线程
        coord.request_stop()
        coord.join(threads)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值