使用jupyter實現貓和狗的分類

沙雅雲發表於2018-12-12

這篇部落格是參照一個pycharm來寫的,其實改的也不多,但是自己跑出來了
主要包括,讀取資料,定義模型,訓練,儲存模型,提取模型進行分類

1,訓練資料的讀取

#x訓練資料的讀取
import tensorflow as tf
import numpy as np
import os

獲取檔案,從一個特定的目錄中獲取

#獲取檔案的路徑
def get_files(file_dir):
    cats=[]
    label_cats=[]
    dogs=[]
    label_dogs=[]
    #載入路徑並且寫入標籤值
    for file in os.listdir(file_dir):
        name=file.split(sep='.')
        if name[0]=='cat':
            cats.append(file_dir+file)
            label_cats.append(0)
        else:
            dogs.append(file_dir+file)
            label_dogs.append(1)
    print("There are %d cats\nThere are %d dogs" % (len(cats), len(dogs)))
    #打亂檔案的順序
    image_list=np.hstack((cats,dogs))#將貓和狗的圖片整個一起
    label_list=np.hstack((label_cats,label_dogs))
    temp=np.array([image_list,label_list])
    temp=temp.transpose()
    np.random.shuffle(temp)#使用這種方式將他們打亂
    image_list=list(temp[:,0])#第一列是影象
    label_list=list(temp[:,1])#第二列是標籤
    label_list=[int(i) for i in label_list]
    
    return image_list,label_list

#定義生成批次的函式

#生成大小相同的批次
def get_batch(image,label,image_W,image_H,batch_size,capacity):
   # image, label: 要生成batch的影象和標籤list
    # image_W, image_H: 圖片的寬高
    # batch_size: 每個batch有多少張圖片
    # capacity: 佇列容量
    # return: 影象和標籤的batch
    # 將python.list型別轉換成tf能夠識別的格式
    image=tf.cast(image,tf.string)
    label=tf.cast(label,tf.int32)
    #生成佇列
    input_queue=tf.train.slice_input_producer([image,label])
    image_contents=tf.read_file(input_queue[0])
    label=input_queue[1]
    image=tf.image.decode_jpeg(image_contents,channels=3)#
    # 統一圖片大小read_file()讀取圖片之後,要按照圖片格式進行解碼。本例程中訓練資料是jpg格式的,所以使用decode_jpeg()解碼器,如果是其他格式,就要用其他解碼器,具
    # 視訊方法
    # image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
    # 我的方法
    image = tf.image.resize_images(image, [image_H, image_W], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    image = tf.cast(image, tf.float32)
    # 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)

    # 這行多餘?
    # label_batch = tf.reshape(label_batch, [batch_size])

    return image_batch, label_batch

2定義模型,就是定義一個網路結構

#生成大小相同的批次
def get_batch(image,label,image_W,image_H,batch_size,capacity):
   # image, label: 要生成batch的影象和標籤list
    # image_W, image_H: 圖片的寬高
    # batch_size: 每個batch有多少張圖片
    # capacity: 佇列容量
    # return: 影象和標籤的batch
    # 將python.list型別轉換成tf能夠識別的格式
    image=tf.cast(image,tf.string)
    label=tf.cast(label,tf.int32)
    #生成佇列
    input_queue=tf.train.slice_input_producer([image,label])
    image_contents=tf.read_file(input_queue[0])
    label=input_queue[1]
    image=tf.image.decode_jpeg(image_contents,channels=3)#
    # 統一圖片大小read_file()讀取圖片之後,要按照圖片格式進行解碼。本例程中訓練資料是jpg格式的,所以使用decode_jpeg()解碼器,如果是其他格式,就要用其他解碼器,具
    # 視訊方法
    # image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H)
    # 我的方法
    image = tf.image.resize_images(image, [image_H, image_W], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    image = tf.cast(image, tf.float32)
    # 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)

    # 這行多餘?
    # label_batch = tf.reshape(label_batch, [batch_size])

    return image_batch, label_batch

#定義損失函式,訓練函式,評估函式

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
#是將稀疏表示的label與輸出層計算出來結果做對比,然後因為訓練的時候是16張圖片一個batch,
#所以再用tf.reduce_mean求一下平均值,就得到了這個batch的平均loss

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

3開始訓練

import os
import numpy as np
import tensorflow as tf
# import input_data
# import model

N_CLASSES = 2
IMG_H = 208
IMG_W = 208
BATCH_SIZE = 32
CAPACITY = 2000
MAX_STEP = 15000
learning_rate = 0.0001



train_dir = "data/train/"
logs_train_dir = "log/"
train, train_label = get_files(train_dir)
train_batch, train_label_batch = get_batch(train,
                                            train_label,
                                            IMG_W,
                                            IMG_H,
                                            BATCH_SIZE,
                                            CAPACITY)
train_logits = inference(train_batch, BATCH_SIZE, N_CLASSES)
train_loss = losses(train_logits, train_label_batch)
train_op = trainning(train_loss, learning_rate)
train_acc = evaluation(train_logits, train_label_batch)

summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_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(MAX_STEP):
        if coord.should_stop():
             break
        _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

        if step % 100 == 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 % 2000 == 0 or (step + 1) == MAX_STEP:
            checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
            saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
    print("Done training -- epoch limit reached.")
finally:
    coord.request_stop()

coord.join(threads)
sess.close()

4評估模型,載入圖片,進行測試

import os
import numpy as np
import tensorflow as tf
# import input_data
# import model

N_CLASSES = 2
IMG_H = 208
IMG_W = 208
BATCH_SIZE = 32
CAPACITY = 2000
MAX_STEP = 15000
learning_rate = 0.0001



train_dir = "data/train/"
logs_train_dir = "log/"
train, train_label = get_files(train_dir)
train_batch, train_label_batch = get_batch(train,
                                            train_label,
                                            IMG_W,
                                            IMG_H,
                                            BATCH_SIZE,
                                            CAPACITY)
train_logits = inference(train_batch, BATCH_SIZE, N_CLASSES)
train_loss = losses(train_logits, train_label_batch)
train_op = trainning(train_loss, learning_rate)
train_acc = evaluation(train_logits, train_label_batch)

summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_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(MAX_STEP):
        if coord.should_stop():
             break
        _, tra_loss, tra_acc = sess.run([train_op, train_loss, train_acc])

        if step % 100 == 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 % 2000 == 0 or (step + 1) == MAX_STEP:
            checkpoint_path = os.path.join(logs_train_dir, "model.ckpt")
            saver.save(sess, checkpoint_path, global_step=step)
except tf.errors.OutOfRangeError:
    print("Done training -- epoch limit reached.")
finally:
    coord.request_stop()

coord.join(threads)
sess.close()

執行結果是一張圖片:
在這裡插入圖片描述
載入模型進行測試


train_dir = "data/train/"
train, train_label = get_files(train_dir)
image_array = get_one_image(train)

with tf.Graph().as_default():
    BATCH_SIZE = 1
    N_CLASSES = 2

    image = tf.cast(image_array, tf.float32)
    image = tf.reshape(image, [1, 208, 208, 3])
    logit = inference(image, BATCH_SIZE, N_CLASSES)
    logit = tf.nn.softmax(logit)

    x = tf.placeholder(tf.float32, shape=[208, 208, 3])

    logs_train_dir = "log/"
    saver = tf.train.Saver()

    with tf.Session() as sess:
        print("Reading checkpoints...")
        ckpt = tf.train.get_checkpoint_state(logs_train_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)
        if max_index == 0:
            print("This is a cat with possibility %.6f" % prediction[:, 0])
        else:
            print("This is a dog with possibility %.6f" % prediction[:, 1])



在這裡插入圖片描述

參考文獻

基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(2) - Sual - CSDN部落格 https://blog.csdn.net/qq_16137569/article/details/72830964

基於TensorFlow的Cats vs. Dogs(貓狗大戰)實現和詳解(1) - Sual - CSDN部落格 https://blog.csdn.net/qq_16137569/article/details/72802387

相關文章