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

来源:互联网 发布:什么软件个人房源多 编辑:程序博客网 时间:2024/05/20 21:47

  通过前面几节的总结,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)
原创粉丝点击