Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法
學(xué)習(xí)谷歌的深度學(xué)習(xí)終于有點(diǎn)眉目了,給大家分享我的Tensorflow學(xué)習(xí)歷程。
tensorflow的官方中文文檔比較生澀,數(shù)據(jù)集一直采用的MNIST二進(jìn)制數(shù)據(jù)集。并沒(méi)有過(guò)多講述怎么構(gòu)建自己的圖片數(shù)據(jù)集tfrecords。
流程是:制作數(shù)據(jù)集—讀取數(shù)據(jù)集—-加入隊(duì)列
先貼完整的代碼:
#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)建文件隊(duì)列,不限讀取的數(shù)量 filename_queue = tf.train.string_input_producer([filename]) # create a reader from file queue reader = tf.TFRecordReader() # reader從文件隊(duì)列中讀入一個(gè)序列化的樣本 _, serialized_example = reader.read(filename_queue) # get feature from serialized example # 解析符號(hào)化的樣本 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才能實(shí)現(xiàn)[img,label]的同步,也即特征和label的同步,不然可能輸入的特征和label不匹配 # 比如只有這樣使用,才能使img和label一一對(duì)應(yīng),每次提取一個(gè)image和對(duì)應(yīng)的label # shuffle_batch返回的值就是RandomShuffleQueue.dequeue_many()的結(jié)果 # Shuffle_batch構(gòu)建了一個(gè)RandomShuffleQueue,并不斷地把單個(gè)的[img,label],送入隊(duì)列中 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) # 啟動(dòng)隊(duì)列 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)存塊序列化為一個(gè)字符串, 并且通過(guò)tf.python_io.TFRecordWriter 寫入到TFRecords文件。
讀取數(shù)據(jù)集
#讀取二進(jìn)制數(shù)據(jù) def read_and_decode(filename): # 創(chuàng)建文件隊(duì)列,不限讀取的數(shù)量 filename_queue = tf.train.string_input_producer([filename]) # create a reader from file queue reader = tf.TFRecordReader() # reader從文件隊(duì)列中讀入一個(gè)序列化的樣本 _, serialized_example = reader.read(filename_queue) # get feature from serialized example # 解析符號(hào)化的樣本 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
一個(gè)Example中包含F(xiàn)eatures,F(xiàn)eatures里包含F(xiàn)eature(這里沒(méi)s)的字典。最后,F(xiàn)eature里包含有一個(gè) FloatList, 或者ByteList,或者Int64List
加入隊(duì)列
with tf.Session() as sess: sess.run(init) # 啟動(dòng)隊(duì)列 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ù)集了,
注意:
- 啟動(dòng)隊(duì)列那條code不要忘記,不然卡死
- 使用的時(shí)候記得使用val和l,不然會(huì)報(bào)類型錯(cuò)誤:TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.
- 算交叉熵時(shí)候:cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits,labels)算交叉熵
- 最后評(píng)估的時(shí)候用tf.nn.in_top_k(logits,labels,1)選logits最大的數(shù)的索引和label比較
- cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))算交叉熵,所以label必須轉(zhuǎn)成one-hot向量
實(shí)例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 #存儲(chǔ)圖片高度 resize_width=32 #存儲(chǔ)圖片寬度 ############################################################################################### 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ù)集。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python按要求從多個(gè)txt文本中提取指定數(shù)據(jù)的代碼示例
本文給大家介紹了Python如何按要求從多個(gè)txt文本中提取指定數(shù)據(jù),遍歷文件夾并從中找到文件名稱符合我們需求的多個(gè).txt格式文本文件,文中有相關(guān)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12Python使用jsonpath-rw模塊處理Json對(duì)象操作示例
這篇文章主要介紹了Python使用jsonpath-rw模塊處理Json對(duì)象操作,結(jié)合實(shí)例形式分析了Python使用requests與response處理json的方法,并給出了jsonpath_rw模塊操作json對(duì)象的基本示例,需要的朋友可以參考下2018-07-07用Python selenium實(shí)現(xiàn)淘寶搶單機(jī)器人
今天給大家?guī)?lái)的是關(guān)于Python實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞著用Python selenium實(shí)現(xiàn)淘寶搶單機(jī)器人展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python簡(jiǎn)潔優(yōu)雅的推導(dǎo)式示例詳解
這篇文章主要給大家介紹了關(guān)于Python簡(jiǎn)潔優(yōu)雅的推導(dǎo)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04利用Python進(jìn)行音頻信號(hào)處理和音樂(lè)生成的代碼示例
隨著計(jì)算機(jī)技術(shù)的快速發(fā)展,音頻信號(hào)處理和音樂(lè)生成逐漸成為了Python程序員們的關(guān)注點(diǎn),本文將介紹如何利用Python進(jìn)行音頻信號(hào)處理和音樂(lè)生成,包括基本概念、常用庫(kù)的使用以及實(shí)際的代碼示例,需要的朋友可以參考下2024-06-06python實(shí)現(xiàn)購(gòu)物車小程序
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)購(gòu)物車小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02python+django+sql學(xué)生信息管理后臺(tái)開發(fā)
這篇文章主要為大家詳細(xì)介紹了python+django+sql學(xué)生信息管理后臺(tái)開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01