最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法

 更新時間:2018年02月07日 10:51:12   作者:tengxing007  
本篇文章主要介紹了Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

學(xué)習(xí)谷歌的深度學(xué)習(xí)終于有點眉目了,給大家分享我的Tensorflow學(xué)習(xí)歷程。

tensorflow的官方中文文檔比較生澀,數(shù)據(jù)集一直采用的MNIST二進(jìn)制數(shù)據(jù)集。并沒有過多講述怎么構(gòu)建自己的圖片數(shù)據(jù)集tfrecords。

流程是:制作數(shù)據(jù)集—讀取數(shù)據(jù)集—-加入隊列

先貼完整的代碼:

#encoding=utf-8
import os
import tensorflow as tf
from PIL import Image

cwd = os.getcwd()

classes = {'test','test1','test2'}
#制作二進(jìn)制數(shù)據(jù)
def create_record():
  writer = tf.python_io.TFRecordWriter("train.tfrecords")
  for index, name in enumerate(classes):
    class_path = cwd +"/"+ name+"/"
    for img_name in os.listdir(class_path):
      img_path = class_path + img_name
      img = Image.open(img_path)
      img = img.resize((64, 64))
      img_raw = img.tobytes() #將圖片轉(zhuǎn)化為原生bytes
      print index,img_raw
      example = tf.train.Example(
        features=tf.train.Features(feature={
          "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
          'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        }))
      writer.write(example.SerializeToString())
  writer.close()

data = create_record()

#讀取二進(jìn)制數(shù)據(jù)
def read_and_decode(filename):
  # 創(chuàng)建文件隊列,不限讀取的數(shù)量
  filename_queue = tf.train.string_input_producer([filename])
  # create a reader from file queue
  reader = tf.TFRecordReader()
  # reader從文件隊列中讀入一個序列化的樣本
  _, serialized_example = reader.read(filename_queue)
  # get feature from serialized example
  # 解析符號化的樣本
  features = tf.parse_single_example(
    serialized_example,
    features={
      'label': tf.FixedLenFeature([], tf.int64),
      'img_raw': tf.FixedLenFeature([], tf.string)
    }
  )
  label = features['label']
  img = features['img_raw']
  img = tf.decode_raw(img, tf.uint8)
  img = tf.reshape(img, [64, 64, 3])
  img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
  label = tf.cast(label, tf.int32)
  return img, label

if __name__ == '__main__':
  if 0:
    data = create_record("train.tfrecords")
  else:
    img, label = read_and_decode("train.tfrecords")
    print "tengxing",img,label
    #使用shuffle_batch可以隨機(jī)打亂輸入 next_batch挨著往下取
    # shuffle_batch才能實現(xiàn)[img,label]的同步,也即特征和label的同步,不然可能輸入的特征和label不匹配
    # 比如只有這樣使用,才能使img和label一一對應(yīng),每次提取一個image和對應(yīng)的label
    # shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的結(jié)果
    # Shuffle_batch構(gòu)建了一個RandomShuffleQueue,并不斷地把單個的[img,label],送入隊列中
    img_batch, label_batch = tf.train.shuffle_batch([img, label],
                          batch_size=4, capacity=2000,
                          min_after_dequeue=1000)

    # 初始化所有的op
    init = tf.initialize_all_variables()

    with tf.Session() as sess:
      sess.run(init)
      # 啟動隊列
      threads = tf.train.start_queue_runners(sess=sess)
      for i in range(5):
        print img_batch.shape,label_batch
        val, l = sess.run([img_batch, label_batch])
        # l = to_categorical(l, 12)
        print(val.shape, l)

制作數(shù)據(jù)集

#制作二進(jìn)制數(shù)據(jù)
def create_record():
  cwd = os.getcwd()
  classes = {'1','2','3'}
  writer = tf.python_io.TFRecordWriter("train.tfrecords")
  for index, name in enumerate(classes):
    class_path = cwd +"/"+ name+"/"
    for img_name in os.listdir(class_path):
      img_path = class_path + img_name
      img = Image.open(img_path)
      img = img.resize((28, 28))
      img_raw = img.tobytes() #將圖片轉(zhuǎn)化為原生bytes
      #print index,img_raw
      example = tf.train.Example(
        features=tf.train.Features(
          feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
          }
        )
      )
      writer.write(example.SerializeToString())
  writer.close()

TFRecords文件包含了tf.train.Example 協(xié)議內(nèi)存塊(protocol buffer)(協(xié)議內(nèi)存塊包含了字段 Features)。我們可以寫一段代碼獲取你的數(shù)據(jù), 將數(shù)據(jù)填入到Example協(xié)議內(nèi)存塊(protocol buffer),將協(xié)議內(nèi)存塊序列化為一個字符串, 并且通過tf.python_io.TFRecordWriter 寫入到TFRecords文件。

讀取數(shù)據(jù)集

#讀取二進(jìn)制數(shù)據(jù)
def read_and_decode(filename):
  # 創(chuàng)建文件隊列,不限讀取的數(shù)量
  filename_queue = tf.train.string_input_producer([filename])
  # create a reader from file queue
  reader = tf.TFRecordReader()
  # reader從文件隊列中讀入一個序列化的樣本
  _, serialized_example = reader.read(filename_queue)
  # get feature from serialized example
  # 解析符號化的樣本
  features = tf.parse_single_example(
    serialized_example,
    features={
      'label': tf.FixedLenFeature([], tf.int64),
      'img_raw': tf.FixedLenFeature([], tf.string)
    }
  )
  label = features['label']
  img = features['img_raw']
  img = tf.decode_raw(img, tf.uint8)
  img = tf.reshape(img, [64, 64, 3])
  img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
  label = tf.cast(label, tf.int32)
  return img, label

一個Example中包含F(xiàn)eatures,F(xiàn)eatures里包含F(xiàn)eature(這里沒s)的字典。最后,F(xiàn)eature里包含有一個 FloatList, 或者ByteList,或者Int64List

加入隊列

with tf.Session() as sess:
      sess.run(init)
      # 啟動隊列
      threads = tf.train.start_queue_runners(sess=sess)
      for i in range(5):
        print img_batch.shape,label_batch
        val, l = sess.run([img_batch, label_batch])
        # l = to_categorical(l, 12)
        print(val.shape, l)

這樣就可以的到和tensorflow官方的二進(jìn)制數(shù)據(jù)集了,

注意:

  1. 啟動隊列那條code不要忘記,不然卡死
  2. 使用的時候記得使用val和l,不然會報類型錯誤:TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
  3. 算交叉熵時候:cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits,labels)算交叉熵
  4. 最后評估的時候用tf.nn.in_top_k(logits,labels,1)選logits最大的數(shù)的索引和label比較
  5. cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))算交叉熵,所以label必須轉(zhuǎn)成one-hot向量

實例2:將圖片文件夾下的圖片轉(zhuǎn)存tfrecords的數(shù)據(jù)集。

############################################################################################ 
#!/usr/bin/python2.7 
# -*- coding: utf-8 -*- 
#Author : zhaoqinghui 
#Date  : 2016.5.10 
#Function: image convert to tfrecords  
############################################################################################# 
 
import tensorflow as tf 
import numpy as np 
import cv2 
import os 
import os.path 
from PIL import Image 
 
#參數(shù)設(shè)置 
############################################################################################### 
train_file = 'train.txt' #訓(xùn)練圖片 
name='train'   #生成train.tfrecords 
output_directory='./tfrecords' 
resize_height=32 #存儲圖片高度 
resize_width=32 #存儲圖片寬度 
############################################################################################### 
def _int64_feature(value): 
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 
 
def _bytes_feature(value): 
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 
 
def load_file(examples_list_file): 
  lines = np.genfromtxt(examples_list_file, delimiter=" ", dtype=[('col1', 'S120'), ('col2', 'i8')]) 
  examples = [] 
  labels = [] 
  for example, label in lines: 
    examples.append(example) 
    labels.append(label) 
  return np.asarray(examples), np.asarray(labels), len(lines) 
 
def extract_image(filename, resize_height, resize_width): 
  image = cv2.imread(filename) 
  image = cv2.resize(image, (resize_height, resize_width)) 
  b,g,r = cv2.split(image)     
  rgb_image = cv2.merge([r,g,b])    
  return rgb_image 
 
def transform2tfrecord(train_file, name, output_directory, resize_height, resize_width): 
  if not os.path.exists(output_directory) or os.path.isfile(output_directory): 
    os.makedirs(output_directory) 
  _examples, _labels, examples_num = load_file(train_file) 
  filename = output_directory + "/" + name + '.tfrecords' 
  writer = tf.python_io.TFRecordWriter(filename) 
  for i, [example, label] in enumerate(zip(_examples, _labels)): 
    print('No.%d' % (i)) 
    image = extract_image(example, resize_height, resize_width) 
    print('shape: %d, %d, %d, label: %d' % (image.shape[0], image.shape[1], image.shape[2], label)) 
    image_raw = image.tostring() 
    example = tf.train.Example(features=tf.train.Features(feature={ 
      'image_raw': _bytes_feature(image_raw), 
      'height': _int64_feature(image.shape[0]), 
      'width': _int64_feature(image.shape[1]), 
      'depth': _int64_feature(image.shape[2]), 
      'label': _int64_feature(label) 
    })) 
    writer.write(example.SerializeToString()) 
  writer.close() 
 
def disp_tfrecords(tfrecord_list_file): 
  filename_queue = tf.train.string_input_producer([tfrecord_list_file]) 
  reader = tf.TFRecordReader() 
  _, serialized_example = reader.read(filename_queue) 
  features = tf.parse_single_example( 
    serialized_example, 
 features={ 
     'image_raw': tf.FixedLenFeature([], tf.string), 
     'height': tf.FixedLenFeature([], tf.int64), 
     'width': tf.FixedLenFeature([], tf.int64), 
     'depth': tf.FixedLenFeature([], tf.int64), 
     'label': tf.FixedLenFeature([], tf.int64) 
   } 
  ) 
  image = tf.decode_raw(features['image_raw'], tf.uint8) 
  #print(repr(image)) 
  height = features['height'] 
  width = features['width'] 
  depth = features['depth'] 
  label = tf.cast(features['label'], tf.int32) 
  init_op = tf.initialize_all_variables() 
  resultImg=[] 
  resultLabel=[] 
  with tf.Session() as sess: 
    sess.run(init_op) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
    for i in range(21): 
      image_eval = image.eval() 
      resultLabel.append(label.eval()) 
      image_eval_reshape = image_eval.reshape([height.eval(), width.eval(), depth.eval()]) 
      resultImg.append(image_eval_reshape) 
      pilimg = Image.fromarray(np.asarray(image_eval_reshape)) 
      pilimg.show() 
    coord.request_stop() 
    coord.join(threads) 
    sess.close() 
  return resultImg,resultLabel 
 
def read_tfrecord(filename_queuetemp): 
  filename_queue = tf.train.string_input_producer([filename_queuetemp]) 
  reader = tf.TFRecordReader() 
  _, serialized_example = reader.read(filename_queue) 
  features = tf.parse_single_example( 
    serialized_example, 
    features={ 
     'image_raw': tf.FixedLenFeature([], tf.string), 
     'width': tf.FixedLenFeature([], tf.int64), 
     'depth': tf.FixedLenFeature([], tf.int64), 
     'label': tf.FixedLenFeature([], tf.int64) 
   } 
  ) 
  image = tf.decode_raw(features['image_raw'], tf.uint8) 
  # image 
  tf.reshape(image, [256, 256, 3]) 
  # normalize 
  image = tf.cast(image, tf.float32) * (1. /255) - 0.5 
  # label 
  label = tf.cast(features['label'], tf.int32) 
  return image, label 
 
def test(): 
  transform2tfrecord(train_file, name , output_directory, resize_height, resize_width) #轉(zhuǎn)化函數(shù)   
  img,label=disp_tfrecords(output_directory+'/'+name+'.tfrecords') #顯示函數(shù) 
  img,label=read_tfrecord(output_directory+'/'+name+'.tfrecords') #讀取函數(shù) 
  print label 
 
if __name__ == '__main__': 
  test() 

這樣就可以得到自己專屬的數(shù)據(jù)集.tfrecords了  ,它可以直接用于tensorflow的數(shù)據(jù)集。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python最常用的20 個包總結(jié)

    Python最常用的20 個包總結(jié)

    這篇文章主要介紹了Python最常用的20 個包總結(jié),在平時使用Python的過程中,需要用到很多有用的包,今天就來盤點一下常用的包,需要的朋友可以參考下
    2023-04-04
  • python內(nèi)置函數(shù)之slice案例詳解

    python內(nèi)置函數(shù)之slice案例詳解

    這篇文章主要介紹了python內(nèi)置函數(shù)之slice案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • python實現(xiàn)自動售貨機(jī)

    python實現(xiàn)自動售貨機(jī)

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)自動售貨機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 如何讓PyQt5中QWebEngineView與JavaScript交互

    如何讓PyQt5中QWebEngineView與JavaScript交互

    這篇文章主要介紹了如何讓PyQt5中QWebEngineView與JavaScript交互,幫助大家更好的理解和學(xué)習(xí)PyQt5框架,感興趣的朋友可以了解下
    2020-10-10
  • pycharm導(dǎo)入源碼的具體步驟

    pycharm導(dǎo)入源碼的具體步驟

    在本篇內(nèi)容里小編給大家整理了關(guān)于pycharm導(dǎo)入源碼的具體步驟,有需要的朋友們可以參考學(xué)習(xí)下。
    2020-08-08
  • 使用Python實現(xiàn)優(yōu)雅生成假數(shù)據(jù)

    使用Python實現(xiàn)優(yōu)雅生成假數(shù)據(jù)

    Faker是一個Python包,開源的GITHUB項目,主要用來創(chuàng)建偽數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了Python如何使用Faker生成假數(shù)據(jù),感興趣的小伙伴可以了解下
    2023-12-12
  • Python發(fā)展史及網(wǎng)絡(luò)爬蟲

    Python發(fā)展史及網(wǎng)絡(luò)爬蟲

    Python 是一個高層次的結(jié)合了解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言。這篇文章給大家介紹了python發(fā)展史及網(wǎng)絡(luò)爬蟲知識,感興趣的朋友跟隨小編一起看看吧
    2019-06-06
  • Python中的類與對象之描述符詳解

    Python中的類與對象之描述符詳解

    這篇文章主要介紹了Python中的描述符詳解,屬于Python學(xué)習(xí)過程中類與對象的基本知識,需要的朋友可以參考下
    2015-03-03
  • Python導(dǎo)出數(shù)據(jù)到Excel可讀取的CSV文件的方法

    Python導(dǎo)出數(shù)據(jù)到Excel可讀取的CSV文件的方法

    這篇文章主要介紹了Python導(dǎo)出數(shù)據(jù)到Excel可讀取的CSV文件的方法,設(shè)計Python操作Excel的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python osgeo庫安裝失敗問題的解決方案

    Python osgeo庫安裝失敗問題的解決方案

    Osgeo是一個支持開源地理空間數(shù)據(jù)處理的基金會,我們可以在python中使用“osgeo”庫來訪問其提供的高效地理空間數(shù)據(jù),在PyCharm安裝osgeo庫的時候,顯示報錯,所以本文記錄一下解決這個麻煩的步驟,需要的朋友可以參考下
    2025-04-04

最新評論

青田县| 镇平县| 保德县| 广平县| 大同县| 马边| 孝义市| 齐河县| 镇江市| 汉寿县| 昌邑市| 武安市| 宁都县| 茌平县| 永定县| 三门峡市| 莱阳市| 临汾市| 门源| 南漳县| 卫辉市| 临海市| 凌源市| 松江区| 凤台县| 仪征市| 东港市| 彭州市| 广元市| 永昌县| 承德县| 彭泽县| 永寿县| 黑河市| 宝清县| 佛冈县| 宁安市| 福贡县| 鞍山市| 扬州市| 太和县|