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

原文链接:https://blog.csdn.net/lovelyaiq/article/details/78709826

  通过前面几节的总结,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.<span class="hljs-built_in">read</span>(filename_queue)
<span class="hljs-comment"># image:存储图像中的原始数据</span>
<span class="hljs-comment"># label该样本所对应的标签</span>
<span class="hljs-comment"># width,height,channel</span>
features = tf.parse_single_example(serialized_example,features={
    <span class="hljs-string">'image'</span> : tf.FixedLenFeature([],tf.<span class="hljs-keyword">string</span>),
    <span class="hljs-string">'label'</span>: tf.FixedLenFeature([], tf.int64),
    <span class="hljs-string">'width'</span>: tf.FixedLenFeature([], tf.int64),
    <span class="hljs-string">'heigth'</span>: tf.FixedLenFeature([], tf.int64),
    <span class="hljs-string">'channel'</span>: tf.FixedLenFeature([], tf.int64)
})

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

<span class="hljs-comment"># shuffle_batch中的参数</span>
min_after_dequeue = <span class="hljs-number">1000</span>
batch_size = <span class="hljs-number">100</span>
capacity = min_after_dequeue + <span class="hljs-number">3</span>*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)

<span class="hljs-operator">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
    <span class="hljs-comment"># 变量初始化</span>
    tf.global_variables_initializer().run()
    <span class="hljs-comment"># 线程初始化和启动</span>
    coord = tf.train.Coordinator()
    theads = tf.train.start_queue_runners(sess=sess,coord=coord)

    <span class="hljs-keyword">for</span> i <span class="hljs-operator">in</span> range(STEPS):
        sess.run(train_step)
    <span class="hljs-comment"># 停止所有线程</span>
    coord.request_stop()
    coord.join(threads)</code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li></ul></pre>            </div>
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值