亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

將自己的數(shù)據(jù)集制作成TFRecord格式教程

 更新時(shí)間:2020年02月17日 09:33:51   作者:v1_vivian  
今天小編就為大家分享一篇將自己的數(shù)據(jù)集制作成TFRecord格式教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在使用TensorFlow訓(xùn)練神經(jīng)網(wǎng)絡(luò)時(shí),首先面臨的問(wèn)題是:網(wǎng)絡(luò)的輸入

此篇文章,教大家將自己的數(shù)據(jù)集制作成TFRecord格式,feed進(jìn)網(wǎng)絡(luò),除了TFRecord格式,TensorFlow也支持其他格

式的數(shù)據(jù),此處就不再介紹了。建議大家使用TFRecord格式,在后面可以通過(guò)api進(jìn)行多線程的讀取文件隊(duì)列。

1. 原本的數(shù)據(jù)集

此時(shí),我有兩類圖片,分別是xiansu100,xiansu60,每一類中有10張圖片。

2.制作成TFRecord格式

tfrecord會(huì)根據(jù)你選擇輸入文件的類,自動(dòng)給每一類打上同樣的標(biāo)簽。如在本例中,只有0,1 兩類,想知道文件夾名與label關(guān)系的,可以自己保存起來(lái)。

#生成整數(shù)型的屬性
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]))
 
#制作TFRecord格式
def createTFRecord(filename,mapfile):
 class_map = {}
 data_dir = '/home/wc/DataSet/traffic/testTFRecord/'
 classes = {'xiansu60','xiansu100'}
 #輸出TFRecord文件的地址
 
 writer = tf.python_io.TFRecordWriter(filename)
 
 for index,name in enumerate(classes):
  class_path=data_dir+name+'/'
  class_map[index] = name
  for img_name in os.listdir(class_path):
   img_path = class_path + img_name #每個(gè)圖片的地址
   img = Image.open(img_path)
   img= img.resize((224,224))
   img_raw = img.tobytes()   #將圖片轉(zhuǎn)化成二進(jìn)制格式
   example = tf.train.Example(features = tf.train.Features(feature = {
    'label':_int64_feature(index),
    'image_raw': _bytes_feature(img_raw)
   }))
   writer.write(example.SerializeToString())
 writer.close()
 
 txtfile = open(mapfile,'w+')
 for key in class_map.keys():
  txtfile.writelines(str(key)+":"+class_map[key]+"\n")
 txtfile.close()

此段代碼,運(yùn)行完后會(huì)產(chǎn)生生成的.tfrecord文件。

3. 讀取TFRecord的數(shù)據(jù),進(jìn)行解析,此時(shí)使用了文件隊(duì)列以及多線程

#讀取train.tfrecord中的數(shù)據(jù)
def read_and_decode(filename): 
 #創(chuàng)建一個(gè)reader來(lái)讀取TFRecord文件中的樣例
 reader = tf.TFRecordReader()
 #創(chuàng)建一個(gè)隊(duì)列來(lái)維護(hù)輸入文件列表
 filename_queue = tf.train.string_input_producer([filename], shuffle=False,num_epochs = 1)
 #從文件中讀出一個(gè)樣例,也可以使用read_up_to一次讀取多個(gè)樣例
 _,serialized_example = reader.read(filename_queue)
#  print _,serialized_example
 
 #解析讀入的一個(gè)樣例,如果需要解析多個(gè),可以用parse_example
 features = tf.parse_single_example(
 serialized_example,
 features = {'label':tf.FixedLenFeature([], tf.int64),
    'image_raw': tf.FixedLenFeature([], tf.string),})
 #將字符串解析成圖像對(duì)應(yīng)的像素?cái)?shù)組
 img = tf.decode_raw(features['image_raw'], tf.uint8)
 img = tf.reshape(img,[224, 224, 3]) #reshape為128*128*3通道圖片
 img = tf.image.per_image_standardization(img)
 labels = tf.cast(features['label'], tf.int32)
 return img, labels

4. 將圖片幾個(gè)一打包,形成batch

def createBatch(filename,batchsize):
 images,labels = read_and_decode(filename)
 
 min_after_dequeue = 10
 capacity = min_after_dequeue + 3 * batchsize
 
 image_batch, label_batch = tf.train.shuffle_batch([images, labels], 
              batch_size=batchsize, 
              capacity=capacity, 
              min_after_dequeue=min_after_dequeue
              )
 
 label_batch = tf.one_hot(label_batch,depth=2)
 return image_batch, label_batch

5.主函數(shù)

if __name__ =="__main__":
 #訓(xùn)練圖片兩張為一個(gè)batch,進(jìn)行訓(xùn)練,測(cè)試圖片一起進(jìn)行測(cè)試
 mapfile = "/home/wc/DataSet/traffic/testTFRecord/classmap.txt"
 train_filename = "/home/wc/DataSet/traffic/testTFRecord/train.tfrecords"
#  createTFRecord(train_filename,mapfile)
 test_filename = "/home/wc/DataSet/traffic/testTFRecord/test.tfrecords"
#  createTFRecord(test_filename,mapfile)
 image_batch, label_batch = createBatch(filename = train_filename,batchsize = 2)
 test_images,test_labels = createBatch(filename = test_filename,batchsize = 20)
 with tf.Session() as sess:
  initop = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
  sess.run(initop)
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess = sess, coord = coord)
 
  try:
   step = 0
   while 1:
    _image_batch,_label_batch = sess.run([image_batch,label_batch])
    step += 1
    print step
    print (_label_batch)
  except tf.errors.OutOfRangeError:
   print (" trainData done!")
   
  try:
   step = 0
   while 1:
    _test_images,_test_labels = sess.run([test_images,test_labels])
    step += 1
    print step
 #     print _image_batch.shape
    print (_test_labels)
  except tf.errors.OutOfRangeError:
   print (" TEST done!")
  coord.request_stop()
  coord.join(threads)

此時(shí),生成的batch,就可以feed進(jìn)網(wǎng)絡(luò)了。

以上這篇將自己的數(shù)據(jù)集制作成TFRecord格式教程就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python網(wǎng)絡(luò)爬蟲(chóng)的基本原理解析

    Python網(wǎng)絡(luò)爬蟲(chóng)的基本原理解析

    如果要獲取網(wǎng)絡(luò)上數(shù)據(jù),我們要給爬蟲(chóng)一個(gè)網(wǎng)址(程序中通常叫URL),爬蟲(chóng)發(fā)送一個(gè)HTTP請(qǐng)求給目標(biāo)網(wǎng)頁(yè)的服務(wù)器,服務(wù)器返回?cái)?shù)據(jù)給客戶端(也就是我們的爬蟲(chóng)),爬蟲(chóng)再進(jìn)行數(shù)據(jù)解析、保存等一系列操作,需要的朋友可以參考下
    2023-05-05
  • python將處理好的圖像保存到指定目錄下的方法

    python將處理好的圖像保存到指定目錄下的方法

    今天小編就為大家分享一篇python將處理好的圖像保存到指定目錄下的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 使用python批量生成insert語(yǔ)句的方法

    使用python批量生成insert語(yǔ)句的方法

    很多時(shí)候需要造數(shù)據(jù),大量的插入數(shù)據(jù),本文介紹了使用python批量生成insert語(yǔ)句的方法,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • python中翻譯功能translate模塊實(shí)現(xiàn)方法

    python中翻譯功能translate模塊實(shí)現(xiàn)方法

    在本篇文章中小編給各位整理了一篇關(guān)于python中翻譯功能translate模塊實(shí)現(xiàn)方法,有需要的朋友們可以參考下。
    2020-12-12
  • python三種數(shù)據(jù)結(jié)構(gòu)及13種創(chuàng)建方法總結(jié)

    python三種數(shù)據(jù)結(jié)構(gòu)及13種創(chuàng)建方法總結(jié)

    拿Python來(lái)說(shuō),數(shù)據(jù)結(jié)構(gòu)的概念也是超級(jí)重要,不同的數(shù)據(jù)結(jié)構(gòu),有著不同的函數(shù),供我們調(diào)用,接下來(lái),我們分別來(lái)介紹字符串、列表、字典的創(chuàng)建方法
    2021-09-09
  • pytest測(cè)試框架+allure超詳細(xì)教程

    pytest測(cè)試框架+allure超詳細(xì)教程

    這篇文章主要介紹了pytest測(cè)試框架+allure超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-11-11
  • Python中的各種裝飾器詳解

    Python中的各種裝飾器詳解

    這篇文章主要介紹了Python中的各種裝飾器詳解,Python裝飾器分兩部分,一是裝飾器本身的定義,一是被裝飾器對(duì)象的定義,本文分別講解了各種情況下的裝飾器,需要的朋友可以參考下
    2015-04-04
  • 詳解Python中的__new__、__init__、__call__三個(gè)特殊方法

    詳解Python中的__new__、__init__、__call__三個(gè)特殊方法

    用雙下劃線包圍的特殊方法在Python中又被成為魔術(shù)方法,類似于C++等語(yǔ)言中的構(gòu)造函數(shù),這里我們就來(lái)詳解Python中的__new__、__init__、__call__三個(gè)特殊方法:
    2016-06-06
  • Python自動(dòng)化運(yùn)維和部署項(xiàng)目工具Fabric使用實(shí)例

    Python自動(dòng)化運(yùn)維和部署項(xiàng)目工具Fabric使用實(shí)例

    Fabric是一個(gè)Python庫(kù),只要目標(biāo)機(jī)器支持ssh訪問(wèn),就可以借助fabric來(lái)進(jìn)行遠(yuǎn)程操作(如在host1上對(duì)host2遠(yuǎn)程運(yùn)行shell命令),顯然,由于fabric是個(gè)Python package,故其它Python package都可以被import到fabric特有的fabfile.py腳本中
    2016-09-09
  • Python實(shí)現(xiàn)的下載網(wǎng)頁(yè)源碼功能示例

    Python實(shí)現(xiàn)的下載網(wǎng)頁(yè)源碼功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的下載網(wǎng)頁(yè)源碼功能,涉及Python基于http請(qǐng)求與響應(yīng)實(shí)現(xiàn)的網(wǎng)頁(yè)源碼讀取功能相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06

最新評(píng)論