學習TensorFlow的基礎教程一般都會接觸到入門實驗--手寫數字識別(MNIST),當我們學習完這個實驗後就會想着能不能自己去做個一個CNN(卷積神經網絡)模型來訓練自己的圖像集呢,於是基於此想法可以通過MNIST延伸加深TensorFlow的學習和理解。網上有很多圖像分類的例子,做爲新手我建議先去閱讀別人的模型,然後在此基礎上去修改,以爲如果自己去從頭開始
做的話一旦出錯,你很難找出問題的解決辦法,當然有問題可以激發你學習的動力,但一旦解決不了容易讓你崩潰。接下來進入話題,貓狗識別,大致流程如下:
- 獲取訓練集及測試集(圖像)
- 構建CNN模型
- 訓練模型,優化參數
- 測試模型
以上是所有圖像分類一致的處理過程
首先訓練集及測試集,在互聯網上搜索下載了一些貓和狗的圖片,然後把這些圖片讀入到名稱隊列中,每次往隊列中取一定數量的圖片來訓練
圖1 訓練集--貓
圖2 訓練集--狗
圖像獲取及處理過程爲:首先把圖像放置在文件夾 train_image 下面,train_image下面再放置兩類訓練集文件夾0和1,0代表貓,1代表狗,代碼中的def get_files(file_path):函數返回的是file_path下面每一類的訓練圖像路徑及標籤值(image_list, label_list),代碼中的def get_batches(image, label, resize_w, resize_h, batch_size, capacity):是通過訓練圖像的路徑來加載訓練圖像到名稱隊列,每次獲取一批次的訓練圖像到內存隊列中,這個過程包含了圖像裁剪,把圖像處理成合適的尺寸,其中用到兩個重要的函數:queue = tf.train.slice_input_producer([image, label]) 和 image_batch, label_batch = tf.train.batch([image, label], batch_size = batch_size,num_threads = 64, capacity = capacity),其中tf.train.batch是從內存隊列中取出圖像數據,每次取batch_size,關於這兩個重要的函數,網上有說明可以參考:https://blog.csdn.net/dcrmg/article/details/79776876 和 http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/reading_data.html,下面貼出圖像獲取及處理相關代碼:
def get_files(file_path):
class_train = []
label_train = []
for train_class in os.listdir(file_path):
for pic_name in os.listdir(file_path + train_class):
class_train.append(file_path + train_class + '/' + pic_name)
label_train.append(train_class)
temp = np.array([class_train, label_train])
temp = temp.transpose()
np.random.shuffle(temp)
image_list = list(temp[:,0])
label_list = list(temp[:,1])
# class is 1 2 3 4 5
label_list = [int(i) for i in label_list]
return image_list, label_list
def get_batches(image, label, resize_w, resize_h, batch_size, capacity):
image = tf.cast(image, tf.string)
label = tf.cast(label, tf.int64)
queue = tf.train.slice_input_producer([image, label])
label = queue[1]
image_temp = tf.read_file(queue[0])
image = tf.image.decode_jpeg(image_temp, channels = 3)
#resize image
image = tf.image.resize_image_with_crop_or_pad(image, resize_w, resize_h)
image = tf.image.per_image_standardization(image)
image_batch, label_batch = tf.train.batch([image, label], batch_size = batch_size,
num_threads = 64,
capacity = capacity)
images_batch = tf.cast(image_batch, tf.float32)
labels_batch = tf.reshape(label_batch, [batch_size])
return images_batch, labels_batch
接下來是模型構建,卷積層+全連接層,最後將得分通過softmax進行分類,得到每一類的概率
def inference(images, batch_size, n_classes):
#
# conv1
#
with tf.variable_scope('conv1') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 3, 64], stddev=1.0, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[64]),
name='biases', dtype=tf.float32)
conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(pre_activation, name=scope.name)
# pooling1
#
with tf.variable_scope('pooling1_lrn') as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pooling1')
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
# conv2
with tf.variable_scope('conv2') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[3, 3, 64, 16], stddev=0.1, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[16]),
name='biases', dtype=tf.float32)
conv = tf.nn.conv2d(norm1, weights, strides=[1, 1, 1, 1], padding='SAME')
pre_activation = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(pre_activation, name='conv2')
# pooling2
with tf.variable_scope('pooling2_lrn') as scope:
norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2')
pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME', name='pooling2')
# fc3
with tf.variable_scope('local3') as scope:
reshape = tf.reshape(pool2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value
weights = tf.Variable(tf.truncated_normal(shape=[dim, 128], stddev=0.005, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
name='biases', dtype=tf.float32)
local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name)
# fc4
with tf.variable_scope('local4') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[128, 128], stddev=0.005, dtype=tf.float32),
name='weights', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[128]),
name='biases', dtype=tf.float32)
local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4')
# dropout
# with tf.variable_scope('dropout') as scope:
# drop_out = tf.nn.dropout(local4, 0.8)
with tf.variable_scope('softmax_linear') as scope:
weights = tf.Variable(tf.truncated_normal(shape=[128, n_classes], stddev=0.005, dtype=tf.float32),
name='softmax_linear', dtype=tf.float32)
biases = tf.Variable(tf.constant(value=0.1, dtype=tf.float32, shape=[n_classes]),
name='biases', dtype=tf.float32)
softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear')
return softmax_linear
# -----------------------------------------------------------------------------
# cal loss
def losses(logits, labels):
with tf.variable_scope('loss') as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels,
name='xentropy_per_example')
loss = tf.reduce_mean(cross_entropy, name='loss')
tf.summary.scalar(scope.name + '/loss', loss)
return loss
# --------------------------------------------------------------------------
# loss
# loss learning_rate
# train_op
def trainning(loss, learning_rate):
with tf.name_scope('optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
global_step = tf.Variable(0, name='global_step', trainable=False)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
# -----------------------------------------------------------------------
def evaluation(logits, labels):
with tf.variable_scope('accuracy') as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
tf.summary.scalar(scope.name + '/accuracy', accuracy)
return accuracy
接下來是訓練過程,
train,train_label = get_files('/home/jyf/jyf/python/PeopleRecong/train_image/')
train_batch, train_label_batch = get_batches(train, train_label, 64, 64, 10, 20)
train_logits = model.inference(train_batch, 10, 2)
train_loss = model.losses(train_logits, train_label_batch)
train_op = model.trainning(train_loss, 0.001)
train_acc = model.evaluation(train_logits, train_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(LOG_DIR, sess.graph)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
try:
for step in np.arange(1000):
if coord.should_stop():
break
_, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])
if step % 10 == 0:
print('Step %d, train loss=%.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc))
summary_str = sess.run(summary_op)
train_writer.add_summary(summary_str, step)
if (step + 1) == 1000:
checkpoint_path = os.path.join(CHECK_POINT_DIR, 'model_ckpt')
saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
print ('Done training')
finally:
coord.request_stop()
coord.join(threads)
接下來是測試過程,加載模型,輸出測試結果
CHECK_POINT_DIR = '/home/jyf/jyf/python/PeopleRecong/modelsave'
def evaluate_one_image(image_array):
with tf.Graph().as_default():
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 64,64,3])
logit = model.inference(image, 1, 2)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[64,64,3])
saver = tf.train.Saver()
with tf.Session() as sess:
print ('Reading checkpoints...')
ckpt = tf.train.get_checkpoint_state(CHECK_POINT_DIR)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loading success, global_step is %s' %global_step)
else:
print ('No checkpoint file found')
prediction = sess.run(logit, feed_dict = {x:image_array})
max_index = np.argmax(prediction)
print (prediction)
if max_index == 0:
result = ('this is cat rate: %.6f, result prediction is [%s]' %(prediction[:,0],','.join(str(i) for i in prediction[0])))
else:
result = ('this is dog rate: %.6f, result prediction is [%s]' %(prediction[:,1],','.join(str(i) for i in prediction[0])))
return result
if __name__ == '__main__':
image = Image.open('/home/jyf/jyf/python/PeopleRecong/4.jpg')
plt.imshow(image)
plt.show()
image = image.resize([64,64])
image = np.array(image)
print evaluate_one_image(image)
測試結果:
訓練過程的損失值曲線:
訓練精度曲線圖如下:
實驗的完整代碼已經上傳到CSDN下載:https://download.csdn.net/download/jiangyingfeng/10610662