tensorflow實現卷積神經網絡

本文是在參考文獻2的基礎上進行擴展和改進的,並將代碼進行註釋,方便學習。並將每層的size進行簡要註釋

參數設定

N_CLASSES = 5
IMG_W = 208
IMG_H = 208
BATCH_SIZE = 8
CAPACITY = 64
MAX_STEP = 200
learning_rate = 0.0001

(1)獲取數據

def get_files(file_dir):
    A5 = []
    label_A5 = []
    A6 = []
    label_A6 = []
    SEG = []
    label_SEG = []
    SUM = []
    label_SUM = []
    LTAX1 = []
    label_LTAX1 = []
    for file in os.listdir(file_dir):
        name = file.split(sep='.')
        if name[0]=='A5':
            A5.append(file_dir+file)
            label_A5.append(0)
        elif name[0] == 'A6':
            A6.append(file_dir+file)
            label_A6.append(1)
        elif name[0]=='LTAX1':
            LTAX1.append(file_dir+file)
            label_LTAX1.append(2)
        elif name[0] == 'SEG':
            SEG.append(file_dir+file)
            label_SEG.append(3)
        else:
            SUM.append(file_dir+file)
            label_SUM.append(4)
    
    image_list = np.array(A5+A6+LTAX1+SEG+SUM)
    label_list = np.array(label_A5+label_A6+label_LTAX1+label_SEG+label_SUM)
    
    #隨機打亂順序
    temp = np.array([image_list,label_list])    #轉換爲二維數組
    temp = temp.T
    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

返回一個image和標籤的列表。

def get_batch(image,label,image_W,image_H,batch_size,capacity):
    image = tf.cast(image,tf.string)    #轉換image
    label = tf.cast(label,tf.int32)
    
    input_queue = tf.train.slice_input_producer([image,label])
    
    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0])
    
    image = tf.image.decode_jpeg(image_contents,channels=3)
    
    image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
    image = tf.image.per_image_standardization(image)
    image_batch,label_batch = tf.train.batch([image,label],batch_size = batch_size,num_threads=16,capacity = capacity)
    
    label_batch = tf.reshape(label_batch,[batch_size])
    
    return image_batch,label_batch
將image,label轉換爲兩個batch,方便tensorflow進行計算。

(2)神經網絡模型

大致模型如下所示,有2個卷積層和池化層,通過上述的計算我們將得到我們想要的特徵,然後,把池化後的所有節點進行平鋪進行全連接層的計算。


def inference(images, batch_size, n_classes):
    # conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
    with tf.variable_scope("conv1") as scope:
        weights = tf.get_variable("weights",
                                    shape=[3, 3, 3, 16],
                                    dtype=tf.float32,
                                    initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 3,out size 16
        biases = tf.get_variable("biases",
                                    shape=[16],
                                    dtype=tf.float32,
                                    initializer=tf.constant_initializer(0.1)) #biases 16
        tf.initialize_all_variables()    #初始化所有變量
        conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding="SAME")    #卷積開始計算,‘SAME’自動加zero padding
        pre_activation = tf.nn.bias_add(conv, biases)    #求和
        conv1 = tf.nn.relu(pre_activation, name="conv1") #output size 208x208x16
        print('images.shape\weights.shape',images.shape,weights.shape)
        print('conv1.shape',conv1.shape)
    # pool1 && norm1
    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") #pool 3x3, stride 2x2
        norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1')   #池化後,歸一化
        print('norm1.shape',norm1.shape)
    # conv2
    with tf.variable_scope("conv2") as scope:
        weights = tf.get_variable("weights",
                                    shape=[3, 3, 16, 32],
                                    dtype=tf.float32,
                                    initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 16, out size 32
        biases = tf.get_variable("biases",
                                shape=[32],
                                dtype=tf.float32,
                                initializer=tf.constant_initializer(0.1))
        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")    #output 104x104x32
        print('norm1.shape\weights.shape',norm1.shape,weights.shape)
        print('conv2.shape',conv2.shape)
    # pool2 && norm2
    with tf.variable_scope("pooling2_lrn") as scope:
        pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                                padding="SAME", name="pooling2")
        norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
                            beta=0.75, name='norm2')

    # full-connect1 
    with tf.variable_scope("fc1") as scope:
        reshape = tf.reshape(norm2, shape=[batch_size, -1])    
        dim = reshape.get_shape()[1].value  #shape 1,52x52x32,將所有節點平鋪得到的矩陣
        print(reshape.shape)
        weights = tf.get_variable("weights",
                                  shape=[dim, 120],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[120],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1")    #激活函數,relu(0,x)
        #dropout1 = tf.nn.dropout(fc1, keep_prob = 0.5, name='dropout1')    #防止過擬合的函數,長用來連接層
            
    # full_connect2
    with tf.variable_scope("fc2") as scope:
        weights = tf.get_variable("weights",
                                  shape=[120, 120],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[120],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        fc2 = tf.nn.relu(tf.matmul(fc1, weights) + biases, name="fc2")    #
        #dropout2 = tf.nn.dropout(fc2, keep_prob = 0.5, name='dropout2')
        
    # softmax
    with tf.variable_scope("softmax_linear") as scope:
        weights = tf.get_variable("weights",
                                  shape=[120, n_classes],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[n_classes],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        softmax_linear = tf.add(tf.matmul(fc2, weights), biases, name="softmax_linear")    #利用常用的softmax線性函數進行模型預測
    return softmax_linear

(3)訓練模型

計算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

進行模型的迭代訓練

def trainning(loss, learning_rate):
    with tf.name_scope("optimizer"):
        #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #adam優化算法
        #optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)  #梯度下降方法
        #global_step = tf.Variable(0, name="global_step", trainable=False)
        #train_op = optimizer.minimize(loss, global_step=global_step)
        train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)    #利用adam進行模型迭代,最小loss
    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)    #記錄一下accuracy數據,方便查看
    return accuracy

進行整體的迭代訓練

def run_training():
    train_path = 'D:/picture/train/'
    logs_train_path = 'D:/picture/log/'
    train,train_label = get_files(train_path)
    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_path,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 %  50 == 0:    #打印模型
                print('Step %d,train loss = %.2f,train occuracy = %.2f%%'%(step,tra_loss,tra_acc))
                summary_str = sess.run(summary_op)
                print(summary_op)
                train_writer.add_summary(summary_str,step)
                
            if step % 200 ==0 or (step +1) == MAX_STEP:    #保存模型
                checkpoint_path = os.path.join(logs_train_path,'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()

運行run_training()    即可完成模型的訓練

(4)測試模型

得到一個img的numpy

def get_one_image(img_dir):
    image = Image.open(img_dir)
    image = image.resize([208, 208])    #將一個img重採樣轉換爲特定大小的numpy
    image_arr = np.array(image)
    return image_arr

進行測試

def test(test_file):
    log_dir = 'D:/picture/log/'    #加載模型
    image_arr = get_one_image(test_file)
    
    with tf.Graph().as_default():
        image = tf.cast(image_arr, tf.float32)
        image = tf.image.per_image_standardization(image)
        image = tf.reshape(image, [1,208, 208, 3])
        print(image.shape)
        p = inference(image,1,5)
        logits = tf.nn.softmax(p)
        x = tf.placeholder(tf.float32,shape = [208,208,3])
        saver = tf.train.Saver()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(log_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')
            else:
                print('No checkpoint')
            prediction = sess.run(logits, feed_dict={x: image_arr})
            max_index = np.argmax(prediction) 
            print('預測的標籤爲:')
            print(max_index)
            print('預測的結果爲:')
            print(prediction)
            if max_index==0:
                print('This is a LTAX with possibility %.6f' %prediction[:, 0])
            elif max_index == 1:
                print('This is a SUM with possibility %.6f' %prediction[:, 1])
            elif max_index == 2:
                print('This is a A5 with possibility %.6f' %prediction[:, 2])
            elif max_index == 3:
                print('This is a A6 with possibility %.6f' %prediction[:, 3])
            else :
                print('This is a SEG with possibility %.6f' %prediction[:, 4])

(5)打印圖片

def print_img_train(train_path,IMG_W,IMG_H,BATCH_SIZE,CAPACITY):
    image_list,label_list = get_files(train_path)
    image_batch,label_batch = get_batch(image_list,label_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)

    with tf.Session() as sess:
        i=0
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord = coord)
        try:
            while not coord.should_stop() and i<1:
                img,label = sess.run([image_batch,label_batch])
                
                for j in np.arange(BATCH_SIZE):
                    print('label: %d'%label[j])
                    plt.imshow(img[j,:,:,:])
                    plt.show()
                i+=1
        except tf.errors.OutOfRangeError:
            print('done!')
        finally:
            coord.request_stop()
        coord.join(threads)
test_path = 'D:/picture/test/'    #測試路徑,以及打印整個圖片的預測值

def read_test_img(test_path):
    test_list = [test_path+x for x in os.listdir(test_path)]
    for file_path in test_list:
        print(file_path)
        test(file_path)

利用上述神經網絡實現花卉的圖片分類。

數據集:http://download.tensorflow.org/example_images/flower_photos.tgz

具體代碼如下:

import os
import numpy as np
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt

train_path = 'D:/flower_photos/data/'

def get_files(train_path):
    daisy = []
    label_daisy = []
    dandelion = []
    label_dandelion = []
    sunflowers = []
    label_sunflowers = []
    tulips = []
    label_tulips = []
    roses = []
    label_roses = []
    for file in os.listdir(train_path):
        print(file)
        for file_dir in os.listdir(train_path+file):
            if file=='daisy':
                daisy.append(train_path+file+'/'+file_dir)
                label_daisy.append(0)
            elif file == 'dandelion':
                dandelion.append(train_path+file+'/'+file_dir)
                label_dandelion.append(1)
            elif file=='roses':
                roses.append(train_path+file+'/'+file_dir)
                label_roses.append(2)
            elif file == 'sunflowers':
                sunflowers.append(train_path+file+'/'+file_dir)
                label_sunflowers.append(3)
            else:
                tulips.append(train_path+file+'/'+file_dir)
                label_tulips.append(4)
    
    image_list = np.array(daisy+dandelion+roses+sunflowers+tulips)
    label_list = np.array(label_daisy+label_dandelion+label_roses+label_sunflowers+label_tulips)
    
    #隨機打亂順序
    temp = np.array([image_list,label_list])    #轉換爲二維數組
    temp = temp.T
    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 = tf.cast(image,tf.string)
    label = tf.cast(label,tf.int32)
    
    input_queue = tf.train.slice_input_producer([image,label])
    
    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0])
    
    image = tf.image.decode_jpeg(image_contents,channels=3)
    
    image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
    image = tf.image.per_image_standardization(image)
    image_batch,label_batch = tf.train.batch([image,label],batch_size = batch_size,num_threads=16,capacity = capacity)
    
    label_batch = tf.reshape(label_batch,[batch_size])
    
    return image_batch,label_batch
    
def print_img_train(train_path,IMG_W,IMG_H,BATCH_SIZE,CAPACITY):
    image_list,label_list = get_files(train_path)
    image_batch,label_batch = get_batch(image_list,label_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)

    with tf.Session() as sess:
        i=0
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord = coord)
        try:
            while not coord.should_stop() and i<1:
                img,label = sess.run([image_batch,label_batch])
                
                for j in np.arange(BATCH_SIZE):
                    print('label: %d'%label[j])
                    plt.imshow(img[j,:,:,:])
                    plt.show()
                i+=1
        except tf.errors.OutOfRangeError:
            print('done!')
        finally:
            coord.request_stop()
        coord.join(threads)
    

def inference(images, batch_size, n_classes):
    # conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
    with tf.variable_scope("conv1") as scope:
        weights = tf.get_variable("weights",
                                    shape=[3, 3, 3, 16],
                                    dtype=tf.float32,
                                    initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 3,out size 16
        biases = tf.get_variable("biases",
                                    shape=[16],
                                    dtype=tf.float32,
                                    initializer=tf.constant_initializer(0.1)) #biases 16
        tf.initialize_all_variables()
        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="conv1") #output size 208x208x16
        print('images.shape\weights.shape',images.shape,weights.shape)
        print('conv1.shape',conv1.shape)
    # pool1 && norm1
    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") #pool 3x3, stride 2x2
        norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1')   #歸一化
        print('norm1.shape',norm1.shape)
    # conv2
    with tf.variable_scope("conv2") as scope:
        weights = tf.get_variable("weights",
                                    shape=[3, 3, 16, 32],
                                    dtype=tf.float32,
                                    initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 16, out size 32
        biases = tf.get_variable("biases",
                                shape=[32],
                                dtype=tf.float32,
                                initializer=tf.constant_initializer(0.1))
        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")    #output 104x104x32
        print('norm1.shape\weights.shape',norm1.shape,weights.shape)
        print('conv2.shape',conv2.shape)
    # pool2 && norm2
    with tf.variable_scope("pooling2_lrn") as scope:
        pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                                padding="SAME", name="pooling2")
        norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
                            beta=0.75, name='norm2')

    # full-connect1
    with tf.variable_scope("fc1") as scope:
        reshape = tf.reshape(norm2, shape=[batch_size, -1])
        dim = reshape.get_shape()[1].value  #shape 1,52x52x32,平鋪得到的矩陣
        print(reshape.shape)
        weights = tf.get_variable("weights",
                                  shape=[dim, 120],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[120],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1")
        dropout1 = tf.nn.dropout(fc1, keep_prob = 0.3, name='dropout1')
        
    # full_connect2
    with tf.variable_scope("fc2") as scope:
        weights = tf.get_variable("weights",
                                  shape=[120, 120],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[120],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        fc2 = tf.nn.relu(tf.matmul(dropout1, weights) + biases, name="fc2")
        dropout2 = tf.nn.dropout(fc2, keep_prob = 0.3, name='dropout2')
        
    # softmax
    with tf.variable_scope("softmax_linear") as scope:
        weights = tf.get_variable("weights",
                                  shape=[120, n_classes],
                                  dtype=tf.float32,
                                  initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
        biases = tf.get_variable("biases",
                                 shape=[n_classes],
                                 dtype=tf.float32,
                                 initializer=tf.constant_initializer(0.1))
        softmax_linear = tf.add(tf.matmul(dropout2, weights), biases, name="softmax_linear")
    return softmax_linear

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


def trainning(loss, learning_rate):
    with tf.name_scope("optimizer"):
        #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #adam優化算法
        #optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)  #梯度下降方法
        #global_step = tf.Variable(0, name="global_step", trainable=False)
        #train_op = optimizer.minimize(loss, global_step=global_step)
        train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
    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

N_CLASSES = 5
IMG_W = 208
IMG_H = 208
BATCH_SIZE = 100
CAPACITY = 64
MAX_STEP = 2000
learning_rate = 0.0001
def split_train_test(temp_train,temp_train_label):
    ratio = 0.8
    s = np.int(len(temp_train)*ratio)
    train = temp_train[:s]
    train_label = temp_train_label[:s]
    test = temp_train[s:]
    test_label = temp_train_label[s:]
    return train,train_label,test,test_label

def run_training():
    train_path = 'D:/flower_photos/data/'
    logs_train_path = 'D:/flower_photos/log/'
    temp_train,temp_train_label = get_files(train_path)
    ratio = 0.8
    s = np.int(len(temp_train)*ratio)
    train = temp_train[:s]
    train_label = temp_train_label[:s]
    test = temp_train[s:]
    test_label = temp_train_label[s:]
    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_path,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 %  50 == 0:
                print('Step %d,train loss = %.2f,train occuracy = %.2f%%'%(step,tra_loss,tra_acc))
                summary_str = sess.run(summary_op)
                print(summary_op)
                train_writer.add_summary(summary_str,step)
                
            if step % 200 ==0 or (step +1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_path,'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()
    return test, test_label

def get_one_image(img_dir):
    image = Image.open(img_dir)
    image = image.resize([208, 208])
    image_arr = np.array(image)
    return image_arr

def test(test_file):
    log_dir = 'D:/flower_photos/log/'
    image_arr = get_one_image(test_file)
    
    with tf.Graph().as_default():
        image = tf.cast(image_arr, tf.float32)
        image = tf.image.per_image_standardization(image)
        image = tf.reshape(image, [1,208, 208, 3])
        print(image.shape)
        p = inference(image,1,5)
        logits = tf.nn.softmax(p)
        x = tf.placeholder(tf.float32,shape = [208,208,3])
        saver = tf.train.Saver()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(log_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')
            else:
                print('No checkpoint')
            prediction = sess.run(logits, feed_dict={x: image_arr})
            max_index = np.argmax(prediction) 
            print('預測的標籤爲:')
            print(max_index)
            print('預測的結果爲:')
            print(prediction)
            if max_index==0:
                print('This is a roses with possibility %.6f' %prediction[:, 0])
            elif max_index == 1:
                print('This is a tulips with possibility %.6f' %prediction[:, 1])
            elif max_index == 2:
                print('This is a daisy with possibility %.6f' %prediction[:, 2])
            elif max_index == 3:
                print('This is a dandelion with possibility %.6f' %prediction[:, 3])
            else :
                print('This is a sunflowers with possibility %.6f' %prediction[:, 4])
            return max_index
            
test_data, test_label = run_training()
#test_path = 'D:/picture/test/'
#print(test_data,test_label)

def read_test_img(test_data, test_label):
    num = 0
    for i in range(len(test_data)):
        if test(test_data[i]) == int(test_label[i]):
            num += 1
    print(len(test_data),num)
read_test_img(test_data, test_label)



參考文獻:

https://blog.csdn.net/Missayaaa/article/details/79119839

https://blog.csdn.net/qq_30159351/article/details/52641644

https://blog.csdn.net/lenbow/article/details/52218551 常用函數

https://blog.csdn.net/marsjhao/article/details/72630147 交叉熵

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章