本人在學(xué)習(xí)完用MNIST數(shù)據(jù)集訓(xùn)練簡(jiǎn)單的MLP、自編碼器抽兆、CNN后,想著自己能不能做一個(gè)數(shù)據(jù)集冈钦,并用卷積神經(jīng)網(wǎng)絡(luò)訓(xùn)練郊丛,所以在網(wǎng)上查了一下資料李请,發(fā)現(xiàn)可以使用標(biāo)準(zhǔn)的TFrecords格式。但是厉熟,遇到了問(wèn)題导盅,制作好的TFrecords的數(shù)據(jù)集,運(yùn)行的時(shí)候報(bào)錯(cuò)揍瑟,網(wǎng)上沒(méi)有找到相關(guān)的方法白翻。后來(lái)我自己找了個(gè)方法解決了。如果有人有更好的方法绢片,可以交流一下滤馍。
1. 準(zhǔn)備數(shù)據(jù)
我準(zhǔn)備的是貓和狗兩個(gè)類(lèi)別的圖片,分別存放在D盤(pán)data/train文件夾下,如下圖:
測(cè)試數(shù)據(jù)放在D盤(pán)data/test文件夾下底循,如下圖:
2. 制作tfrecords文件
代碼起名為make_own_data.py
tfrecord會(huì)根據(jù)你選擇輸入文件的類(lèi)巢株,自動(dòng)給每一類(lèi)打上同樣的標(biāo)簽。注意:要分別給訓(xùn)練和測(cè)試數(shù)據(jù)制作一個(gè)tfrecords文件熙涤。
代碼如下:
# -*- coding: utf-8 -*-
"""
@author: caokai
"""
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
#cwd='./data/train/'
cwd='./data/test/'
classes={'dog','cat'} #人為設(shè)定2類(lèi)
#writer= tf.python_io.TFRecordWriter("dog_and_cat_train.tfrecords") #要生成的文件
writer= tf.python_io.TFRecordWriter("dog_and_cat_test.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 #每一個(gè)圖片的地址
img=Image.open(img_path)
img= img.resize((128,128))
print(np.shape(img))
img_raw=img.tobytes()#將圖片轉(zhuǎn)化為二進(jìn)制格式
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]))
})) #example對(duì)象對(duì)label和image數(shù)據(jù)進(jìn)行封裝
writer.write(example.SerializeToString()) #序列化為字符串
writer.close()
這樣就‘狗’和‘貓’的圖片打上了兩類(lèi)數(shù)據(jù)0和1阁苞,并且文件儲(chǔ)存為dog_and_cat_train.tfrecords和dog_and_cat_test.tfrecords,你會(huì)發(fā)現(xiàn)自己的python代碼所在的文件夾里有了這兩個(gè)文件祠挫。
3. 讀取tfrecords文件
將圖片和標(biāo)簽讀出那槽,圖片reshape為128x128x3。
讀取代碼單獨(dú)作為一個(gè)文件等舔,起名為ReadMyOwnData.py
代碼如下:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 15 22:55:47 2017
tensorflow : read my own dataset
@author: caokai
"""
import numpy as np
import tensorflow as tf
def read_and_decode(filename): # 讀入tfrecords
filename_queue = tf.train.string_input_producer([filename],shuffle=True)#生成一個(gè)queue隊(duì)列
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)#返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})#將image數(shù)據(jù)和label取出來(lái)
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [128, 128, 3]) #reshape為128*128的3通道圖片
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中拋出img張量
label = tf.cast(features['label'], tf.int32) #在流中拋出label張量
return img, label
4. 使用卷積神經(jīng)網(wǎng)絡(luò)訓(xùn)練
這一部分Python代碼起名為dog_and_cat_train.py
4.1 定義好卷積神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)
要把我們讀取文件的ReadMyOwnData導(dǎo)入骚灸,這邊權(quán)重初始化使用的是tf.truncated_normal,兩次卷積操作慌植,兩次最大池化甚牲,激活函數(shù)ReLU,全連接層涤浇,最后y_conv是softmax輸出的二類(lèi)問(wèn)題鳖藕。損失函數(shù)用交叉熵,優(yōu)化算法Adam只锭。
卷積部分代碼如下:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 15 17:44:58 2017
@author: caokai
"""
import tensorflow as tf
import numpy as np
import ReadMyOwnData
epoch = 15
batch_size = 20
def one_hot(labels,Label_class):
one_hot_label = np.array([[int(i == int(labels[j])) for i in range(Label_class)] for j in range(len(labels))])
return one_hot_label
#initial weights
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.02)
return tf.Variable(initial)
#initial bias
def bias_variable(shape):
initial = tf.constant(0.0, shape=shape)
return tf.Variable(initial)
#convolution layer
def conv2d(x,W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
#max_pool layer
def max_pool_4x4(x):
return tf.nn.max_pool(x, ksize=[1,4,4,1], strides=[1,4,4,1], padding='SAME')
x = tf.placeholder(tf.float32, [batch_size,128,128,3])
y_ = tf.placeholder(tf.float32, [batch_size,2])
#first convolution and max_pool layer
W_conv1 = weight_variable([5,5,3,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x, W_conv1) + b_conv1)
h_pool1 = max_pool_4x4(h_conv1)
#second convolution and max_pool layer
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_4x4(h_conv2)
#變成全連接層著恩,用一個(gè)MLP處理
reshape = tf.reshape(h_pool2,[batch_size, -1])
dim = reshape.get_shape()[1].value
W_fc1 = weight_variable([dim, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(reshape, W_fc1) + b_fc1)
#dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024,2])
b_fc2 = bias_variable([2])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
#損失函數(shù)及優(yōu)化算法
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
img, label = ReadMyOwnData.read_and_decode("dog_and_cat_train.tfrecords")
img_test, label_test = ReadMyOwnData.read_and_decode("dog_and_cat_test.tfrecords")
#使用shuffle_batch可以隨機(jī)打亂輸入
img_batch, label_batch = tf.train.shuffle_batch([img, label],
batch_size=batch_size, capacity=2000,
min_after_dequeue=1000)
img_test, label_test = tf.train.shuffle_batch([img_test, label_test],
batch_size=batch_size, capacity=2000,
min_after_dequeue=1000)
init = tf.initialize_all_variables()
t_vars = tf.trainable_variables()
print(t_vars)
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
batch_idxs = int(2314/batch_size)
for i in range(epoch):
for j in range(batch_idxs):
val, l = sess.run([img_batch, label_batch])
l = one_hot(l,2)
_, acc = sess.run([train_step, accuracy], feed_dict={x: val, y_: l, keep_prob: 0.5})
print("Epoch:[%4d] [%4d/%4d], accuracy:[%.8f]" % (i, j, batch_idxs, acc) )
val, l = sess.run([img_test, label_test])
l = one_hot(l,2)
print(l)
y, acc = sess.run([y_conv,accuracy], feed_dict={x: val, y_: l, keep_prob: 1})
print(y)
print("test accuracy: [%.8f]" % (acc))
coord.request_stop()
coord.join(threads)
4.2 訓(xùn)練
訓(xùn)練過(guò)程中顯示訓(xùn)練誤差,最后會(huì)顯示在測(cè)試集上的誤差蜻展。
如果有更好更高效的讀入tfrecords數(shù)據(jù)集并訓(xùn)練CNN的方法喉誊,可以交流一下。
轉(zhuǎn)載請(qǐng)注明出處纵顾!
參考:
TensorFlow(二)制作自己的TFRecord數(shù)據(jù)集 讀取伍茄、顯示及代碼詳解
test