TF支持3種文件讀取:
1.直接把數據保存到變量中
2.佔位符配合feed_dict使用
3. QueueRunner(TF中特有的)html
文件讀取流程(多線程 + 隊列)
1)構造文件名隊列(先讀取文件名到隊列,方便快速讀取文件)
file_queue = tf.train.string_input_producer(string_tensor,shuffle=True)
2)讀取與解碼(讀取器根據文件名讀取數據,以一個樣本爲一個單位進行讀取,返回一個實例reader)
文本(一行文字):
讀取:tf.TextLineReader()
解碼:tf.decode_csv()
圖片(一張圖片):
讀取:tf.WholeFileReader()
解碼:
tf.image.decode_jpeg(contents) # 將jpeg編碼的圖片解碼爲unit8
tf.image.decode_png(contents) # 將png編碼的圖片解碼爲unit8
二進制(固定數量字節):
讀取:tf.FixedLengthRecordReader(record_bytes)
解碼:tf.decode_raw()
TFRecords類型(TF自定義的)
讀取:tf.TFRecordReader()
說明:讀取器有一個通用的方法read(),返回一個元組java
key, value = 讀取器.read(file_queue)
key:文件名(指明文件在哪一個文件名中)
value:一個樣本
3)批處理隊列
tf.train.batch(tensors, batch_size, num_threads = 1, capacity = 32, name=None)
4)手動開啓線程
隊列操做對象:tf.train.QueueRunner()
開啓會話:
tf.train.start_queue_runners(sess=None, coord=None)git
一、構造文件名隊列數組
二、讀取與解碼網絡
三、批處理多線程
線程操做dom
圖像基本知識
0 文本特徵處理
文本 -->利用特徵詞(某個單詞,可統計數量Count,可統計重要程度TF-IDF) -> 二維數組shape(n_samples行,m_features列)
字典 –> one-hot編碼 -> 二維數組
圖片 像素值(組成圖片的最基本單位就是像素)
1 圖片三要素(長度+寬度+通道數)
黑白圖、灰度圖
一個通道
黑[0, 255]白
彩色圖
三個通道
一個像素點 由三個通道值構成,每個通道是一個灰度圖,3個組合在一塊兒就是一個彩色圖的像素點
R [0, 255]
G [0, 255]
B [0, 255]
2 TensorFlow中表示圖片
Tensor對象
指令名稱、形狀、類型
shape = [height, width, channel]
3 圖片特徵值處理
[samples, features]
爲何要縮放圖片到統一大小?
1)每個樣本特徵數量要同樣多
2)縮小圖片的大小
tf.image.resize_images(images, size)
4 數據格式
存儲:uint8
訓練:float32函數
一、圖片三要素(長+寬+通道數)測試
點擊查看圖片的某一個像素點(左:灰度圖, 右:彩色圖)優化
二、張量(相似ndarray)形狀--》3D
三、圖片特徵值處理
iris數據集: 150個樣本 4個特徵值 目標值
圖片數據集:1個樣本有500*500*3個特徵(像素點) + 每一個圖片的特徵值不同 –》因此須要進行大小轉換
四、數據格式
狗圖片讀取Demo
import tensorflow as tf import os os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' def read_picture_dog(file_list): """ 讀取狗圖片案例 :return: """ # 一、構造文件名隊列 file_queue = tf.train.string_input_producer(file_list) # 二、讀取與解碼 # 讀取 reader = tf.WholeFileReader() key, value = reader.read(file_queue) print("key:\n", key) print("value:\n", value) # 解碼 image_decoded = tf.image.decode_jpeg(value) print("image_decoded:\n", image_decoded) # 三、圖像形狀、類型修改 # 將圖片縮放到同一個大小 image_resized = tf.image.resize_images(image_decoded, [200, 200]) # 到此僅肯定了長和寬,未肯定通道 print("image_resized_before:\n", image_resized) # 更新靜態形狀 image_resized.set_shape([200, 200, 3]) # 第三個的通道必定是3 print("image_resized_after:\n", image_resized) # 四、批處理隊列 image_batch = tf.train.batch([image_resized], batch_size=100, num_threads=2, capacity=100) print("image_batch:\n", image_batch) # 五、開始會話 with tf.Session() as sess: # key_new, value_new = sess.run([key, value]) # print('key_new\n', key_new) # 開啓單線程的狀況下,會阻塞,由於只有一個queue現場,而前面已經運行中 # print('value_new\n', value_new) # 開啓線程 # ->構造線程協調器 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # 運行 filename, sample, image, n_image = sess.run([key, value, image_resized, image_batch]) print("filename:\n", filename) print("sample:\n", sample) print("image:\n", image) print("n_image:\n", n_image) coord.request_stop() coord.join(threads) # 回收線程 if __name__ == "__main__": # 0、構造文件路徑 + 文件名列表 filename = os.listdir('F:\\linear') file_list = [ os.path.join('F:\\linear', x) for x in filename ] print('文件名列表:', filename) print('文件路徑:', file_list) read_picture_dog(file_list)
CIFAR-10數據集由10個類中的60000個32x32彩色圖像組成,每一個類有6000個圖像。有50000個訓練圖像和10000個測試圖像。
數據集分爲五個訓練批次和一個測試批次,每一個批次有10000個圖像。測試批次包含來自每一個類別的1000個隨機選擇的圖像。訓練批次以隨機順序包含剩餘圖像,可是一些訓練批次可能包含來自一個類別的更多圖像而不是另外一個類別。在他們之間,訓練批次包含來自每一個班級的5000個圖像。
http://www.cs.toronto.edu/~kriz/cifar.html
每3072個字節構成一個樣本 1個目標值 + 3072個字節
demo:
import tensorflow as tf import os os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3' class Cifar(): def __init__(self): # 設置圖像大小 self.width = 32 self.height = 32 self.channel = 3 # 設置圖像字節數 self.image = self.height * self.width * self.channel # 一個圖的字節大小 self.label = 1 # 標籤字節數(範圍0-9) self.sample = self.image + self.label # 一個樣本的大小 = 一個標籤 + 一個圖 def read_binary(self, file_list): # 一、 構建文件名隊列 file_queue = tf.train.string_input_producer(file_list) # 二、 文件讀取與解碼 # 讀取 reader = tf.FixedLengthRecordReader(self.sample) key, value = reader.read(file_queue) print('key\n', key) # Tensor("ReaderReadV2:0", shape=(), dtype=string) print('value\n', value) # 解碼 image_decoded = tf.decode_raw(value, tf.uint8) print("image_decoded:\n", image_decoded) # 一階 Tensor("DecodeRaw:0", shape=(?,), dtype=uint8) # 切片操做 label = tf.slice(image_decoded, [0], [self.label]) image = tf.slice(image_decoded, [self.label], [self.image]) print("label:\n", label) # Tensor("Slice:0", shape=(1,), dtype=uint8) ,1維, print("image:\n", image) # Tensor("Slice_1:0", shape=(3072,), dtype=uint8), 1維,3072字節 # 調整圖像的形狀 image_reshaped = tf.reshape(image, [self.channel, self.height, self.width]) print("image_reshaped:\n", image_reshaped) # 改變階數, Tensor("Reshape:0", shape=(3, 32, 32), dtype=uint8) # 三維數組的轉置(原數組的順序不符合 TF的要求,轉換後維) image_transposed = tf.transpose(image_reshaped, [1, 2, 0]) print("image_transposed:\n", image_transposed) # 調整圖像類型 # image_cast = tf.cast(image_transposed, tf.float32) # 三、構造批處理隊列 image_batch, label_batch = tf.train.batch([image_transposed, label], batch_size=100, num_threads=2, capacity=100) # 開啓會話 with tf.Session() as sess: # 開啓線程 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) key_new, value_new, image_decoded, label_new, image_new, image_reshaped_new,\ image_transposed_new,image_batch_new, label_batch_new = sess.run([key, value, image_decoded, label, image, image_reshaped, image_transposed, image_batch, label_batch]) print('key_new\n', key_new) # Tensor("ReaderReadV2:0", shape=(), dtype=string) print('value_new\n', value_new) # b'\x03\x9e\x9f\xa5\xa6\xa0 print('image_decoded\n', image_decoded) # [3 158 159 ... 124 129 110] --》 3是類別,158,159是目標和特徵值,需切片 print('label_new\n', label_new) # [6] print('image_new\n', image_new) # [ 59 43 50 ... 140 84 72] print('image_reshaped_new\n', image_reshaped_new) print('image_transposed_new\n', image_transposed_new) print('image_batch_new\n', image_batch_new) print('label_batch_new\n', label_batch_new) coord.request_stop() coord.join(threads) # 回收線程 return None if __name__ == "__main__": filename = os.listdir('F:\\linear') file_list = [ os.path.join('F:\\linear', x) for x in filename if x[-3:] == 'bin' ] print('filename\n', filename) print('file_list\n', file_list) cifar = Cifar() cifar.read_binary(file_list)
Softmax迴歸
softmax特色:神經網絡的輸出變成了機率輸出
交叉熵損失
logits: 線性值+權重的線性值
Mnist手寫數字識別案例
一、數據集介紹
一、特徵值:
特徵值就是咱們的圖片(28*28個像素),圖片就是以28*28*1的形式存儲,根據書寫的軌跡進行編碼(即把數字1用0.1~1.0的數字進行標識,徹底筆直用1,稍微偏一點用0.5標識)。一個手寫識別圖片包含圖片和它對應的標籤,設置圖片爲xs,標籤爲ys。樣本數共55000
二、目標值
Minist的每一個圖片都有對應的標籤,0~9之間的數字表示圖像中繪製的數字,利用one-hot編碼,共[55000,10]個樣本
三、Mnist數據獲取API
Mnist手寫數字的識別:
圖片數字1 --》 輸入特徵值28*28*1 --》每一個特徵都有權重值,進行加權求和(y=w1x1+w2x2+b) -->獲取logits -->進行softmax進行分類
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data def full_connection(): """ 用全鏈接對手寫數字進行識別 :return: """ # 1)準備數據 mnist = input_data.read_data_sets("F:\mnist_data", one_hot=True) # 用佔位符定義真實數據 X = tf.placeholder(dtype=tf.float32, shape=[None, 784]) y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10]) # 2)構造模型 - 全鏈接 # [None, 784] * W[784, 10] + Bias = [None, 10] weights = tf.Variable(initial_value=tf.random_normal(shape=[784, 10], stddev=0.01)) bias = tf.Variable(initial_value=tf.random_normal(shape=[10], stddev=0.1)) y_predict = tf.matmul(X, weights) + bias # 3)構造損失函數 loss_list = tf.nn.softmax_cross_entropy_with_logits(logits=y_predict, labels=y_true) loss = tf.reduce_mean(loss_list) # 4)優化損失 # optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss) optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss) # 5)增長準確率計算 bool_list = tf.equal(tf.argmax(y_true, axis=1), tf.argmax(y_predict, axis=1)) accuracy = tf.reduce_mean(tf.cast(bool_list, tf.float32)) # 初始化變量 init = tf.global_variables_initializer() # 開啓會話 with tf.Session() as sess: # 初始化變量 sess.run(init) # 開始訓練 for i in range(50): # 獲取真實值 image, label = mnist.train.next_batch(50) _, loss_value, accuracy_value = sess.run([optimizer, loss, accuracy], feed_dict={X: image, y_true: label}) print("第%d次的損失爲%f,準確率爲%f" % (i+1, loss_value, accuracy_value)) return None if __name__ == "__main__": full_connection()
===