tensorflow编写vggnet16二分类

#本程序将神经网络进一步加深,但是问题是变动神经网络太过于麻烦,还需要不断改进。
#https://blog.csdn.net/u013921430/article/details/80339337
#动态调整激活函数感觉比较苦难,每一层都是relu
import os
from cv2 import cv2 as cv
import tensorflow as tf 
import matplotlib.pyplot as plt
import numpy as np
import threading
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2"

def read_tfRecord(file_tfRecord):     #输入是.tfrecords文件地址
    queue = tf.train.string_input_producer([file_tfRecord])
    reader = tf.TFRecordReader()
    _,serialized_example = reader.read(queue)
    features = tf.parse_single_example(
            serialized_example,
            features={
          'image_raw':tf.FixedLenFeature([], tf.string),   
          'label':tf.FixedLenFeature([], tf.int64)
                    }
            )
    image = tf.decode_raw(features['image_raw'],tf.uint8)
    image = tf.reshape(image,[256*256*3])
    image = tf.cast(image, tf.float32)
    image = tf.cast(image, tf.float32) * (1./ 255) - 0.5
   # image = tf.image.per_image_standardization(image)
    label = tf.cast(features['label'], tf.int64)
    one_hot_labels = tf.one_hot(indices=label,depth=2, on_value=1, off_value=0, axis=-1, dtype=tf.int32, name="one-hot")
    one_hot_labels=tf.cast(one_hot_labels,tf.float32)
    return image,one_hot_labels


if __name__ == '__main__':
    
    outputdir1 = "/home/xiaozhen/Desktop/UCMerced1"
    outputdir2 = "/home/xiaozhen/Desktop/UCMerced2" 
    traindata1,trainlabel1 = read_tfRecord(outputdir1+".tfrecords")
    traindata2,trainlabel2 = read_tfRecord(outputdir2+".tfrecords")
    image_batch1,label_batch1 = tf.train.shuffle_batch([traindata1,trainlabel1],
                                            batch_size=20,capacity=200,min_after_dequeue = 10) 
    image_batch2,label_batch2 = tf.train.shuffle_batch([traindata2,trainlabel2],
                                            batch_size=20,capacity=200,min_after_dequeue = 10) 

def conv_op(input_op,name,kh,kw,n_out,dh,dw,p):
    #Kw:filter宽  kh:filter高   n—out:输出维数   dh:strides步数    dw:strides
    n_in=input_op.get_shape()[-1].value    ##获得倒数第一个维度的大小,也就是图像通道数目的大小
    
    with tf.name_scope(name) as scope:
        kernel=tf.get_variable(scope+"w",shape=[kh,kw,n_in,n_out],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer_conv2d())  #初始化权重矩阵
        
        bias_init_val=tf.constant(0.0,shape=[n_out],dtype=tf.float32)
        biases=tf.Variable(bias_init_val,trainable=True,name='b')
        
        activation=tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_op,kernel,(1,dh,dw,1),padding='SAME'),biases),name=scope)
        
        p+=[kernel,biases]
        
        return activation

def mpool_op(input_op,name,kh,kw,dh,dw):
    
    return tf.nn.max_pool(input_op,ksize=[1,kh,kw,1],strides=[1,dh,dw,1],padding='SAME',name=name)

def fc_op(input_op,name,n_out,p):
    n_in=input_op.get_shape()[-1].value  #只能是tensor类型, tfshape()可以是tensor、list、array
    
    with tf.name_scope(name) as scope:
        kernel=tf.get_variable(scope+"w",shape=[n_in,n_out],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer_conv2d())
        
        biases=tf.Variable(tf.constant(0.1,shape=[n_out],dtype=tf.float32),trainable=True,name='b')
        
        activation=tf.nn.relu_layer(input_op,kernel,biases,name=scope)  #activation = tf.nn.relu(tf.matmul(x, weights) + biases, name=None)
        
        
        p+=[kernel,biases]
        
        return activation

def inference_op(input_op,keep_prob):
    p=[]
    
    conv1_1=conv_op(input_op,name="conv1_1",kh=3,kw=3,n_out=64,dh=1,dw=1,p=p)
    
    conv1_2=conv_op(conv1_1,name="conv1_2",kh=3,kw=3,n_out=64,dh=1,dw=1,p=p)
    
    pool1=mpool_op(conv1_2,name="pool1",kh=2,kw=2,dh=2,dw=2)
 
    conv2_1=conv_op(pool1,name="conv2_1",kh=3,kw=3,n_out=128,dh=1,dw=1,p=p)
    
    conv2_2=conv_op(conv2_1,name="conv2_2",kh=3,kw=3,n_out=128,dh=1,dw=1,p=p)
    
    pool2=mpool_op(conv2_2,name="pool2",kh=2,kw=2,dh=2,dw=2)
    
    conv3_1=conv_op(pool2,name="conv3_1",kh=3,kw=3,n_out=256,dh=1,dw=1,p=p)
    
    conv3_2=conv_op(conv3_1,name="conv3_2",kh=3,kw=3,n_out=256,dh=1,dw=1,p=p)
    
    conv3_3=conv_op(conv3_2,name="conv3_3",kh=3,kw=3,n_out=256,dh=1,dw=1,p=p)
    
    pool3=mpool_op(conv3_3,name="pool3",kh=2,kw=2,dh=2,dw=2)
    
    conv4_1=conv_op(pool3,name="conv4_1",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    conv4_2=conv_op(conv4_1,name="conv4_2",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    conv4_3=conv_op(conv4_2,name="conv4_3",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    pool4=mpool_op(conv4_3,name="pool4",kh=2,kw=2,dh=2,dw=2)
    
    conv5_1=conv_op(pool4,name="conv5_1",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    conv5_2=conv_op(conv5_1,name="conv5_2",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    conv5_3=conv_op(conv5_2,name="conv5_3",kh=3,kw=3,n_out=512,dh=1,dw=1,p=p)
    
    pool5=mpool_op(conv5_3,name="pool5",kh=2,kw=2,dh=2,dw=2)
    
    
    shp=pool5.get_shape() #得到一个元组
    
    #shp[0]是batch_size
    flattened_shape=shp[1].value*shp[2].value*shp[3].value
    resh1=tf.reshape(pool5,shape=[-1,flattened_shape],name="resh1")
    
    fc6=fc_op(resh1,"fc6",128,p=p)
    fc6_drop=tf.nn.dropout(fc6,keep_prob,name="fc6_drop")
    
    #fc7=fc_op(fc6_drop,"fc7",4096,p=p)
    #fc7_drop=tf.nn.dropout(fc7,keep_prob,name="fc7_drop")
    
    fc8=fc_op(fc6_drop,"fc8",2,p=p)
    softmax=tf.nn.softmax(fc8)  
    #predictions=tf.argmax(softmax,1)
 
#返回是概率最大的分类    
    return softmax


with tf.name_scope("Input_layer"):
    x=tf.placeholder("float",[None,256*256*3],name="x")
    x_image=tf.reshape(x,[-1,256,256,3])

with tf.name_scope("keep_prob"):
    keep_probb=tf.placeholder("float")

y_predict=inference_op(x_image,keep_probb)
with tf.name_scope("optimizer"):
    y_label = tf.placeholder("float",[None, 2])
#cross_entropy = -tf.reduce_sum(y_*tf.log(y))
    cross_entropy= -tf.reduce_mean(y_label * tf.log(tf.clip_by_value(y_predict,1e-10,1.0)))
    tf.summary.scalar('cross_entropy', cross_entropy)  #加入tensorboard中便于显示
    #loss_function=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_predict,labels=y_label))
    optimizer=tf.train.AdadeltaOptimizer(learning_rate=0.1).minimize(cross_entropy)
with tf.name_scope("evaluate_model"):
    correct_prediction=tf.equal(tf.arg_max(y_label,1),tf.arg_max(y_predict,1))
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
    tf.summary.scalar('accuracy', accuracy)
#train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
saver = tf.train.Saver(max_to_keep=2)   #只保存最后两个模型
merged = tf.summary.merge_all()    #使用tensorboard必备
Epochs=20
trainEpochs=Epochs-1
batchSize=20
loss_list=[]
epoch_list=[]
accuracy_list=[]
sess=tf.Session()
writer = tf.summary.FileWriter('/home/xiaozhen/Desktop/logs',tf.get_default_graph())   #写入计算图

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord = coord)
sess.run(tf.global_variables_initializer())
batch_x2,batch_y2=sess.run([image_batch2,label_batch2])
try:
    while not coord.should_stop():

        for epoch in range(10000):
            for i in range(8):
                batch_x,batch_y=sess.run([image_batch1,label_batch1])
                summary,_=sess.run([merged,optimizer],feed_dict={x:batch_x,y_label:batch_y,keep_probb:0.8}) #训练
            writer.add_summary(summary,epoch)
            loss,acc=sess.run([cross_entropy,accuracy],feed_dict={x:batch_x2,y_label:batch_y2,keep_probb:1.0})#验证
            epoch_list.append(epoch)
            loss_list.append(loss)
            accuracy_list.append(acc)
            print("-----------------------------------------------------------")
            print("Train Epoch:","%02d"%(epoch+1),"Loss=","{:.9f}".format(loss),"Accuracy=",acc)
            saver.save(sess, "model_conv/my-model", global_step=epoch)    #存储模型
            print ("save the model")
            print("------------------------------------------------------------")
            if epoch>=trainEpochs:
                coord.request_stop()


except tf.errors.OutOfRangeError:  
    print ('Done training -- epoch limit reached')  
finally:  
            # When done, ask the threads to stop. 请求该线程停止  
    coord.request_stop()  
            # And wait for them to actually do it. 等待被指定的线程终止  
 
    coord.join(threads)


-----------------------------------------------------------
Train Epoch: 01 Loss= 0.348828018 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 02 Loss= 0.348073244 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 03 Loss= 0.347468823 Accuracy= 0.35
W0224 23:09:28.552989 140279480149824 deprecation.py:323] From /home/xiaozhen/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py:960: remove_checkpoint (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
Instructions for updating:
Use standard file APIs to delete files with this prefix.
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 04 Loss= 0.346792281 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 05 Loss= 0.347110569 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 06 Loss= 0.346143782 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 07 Loss= 0.348147035 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 08 Loss= 0.345978916 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 09 Loss= 0.349924773 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 10 Loss= 0.346459389 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 11 Loss= 0.342494786 Accuracy= 0.85
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 12 Loss= 0.342151791 Accuracy= 0.5
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 13 Loss= 0.342804581 Accuracy= 0.4
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 14 Loss= 0.355247229 Accuracy= 0.35
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 15 Loss= 0.386807263 Accuracy= 0.4
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 16 Loss= 0.226710230 Accuracy= 1.0
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 17 Loss= 0.350952327 Accuracy= 0.65
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 18 Loss= 0.126959860 Accuracy= 0.95
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 19 Loss= 0.086159639 Accuracy= 1.0
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 20 Loss= 0.054284729 Accuracy= 1.0
save the model
------------------------------------------------------------
-----------------------------------------------------------
Train Epoch: 21 Loss= 0.033515219 Accuracy= 1.0
save the model
------------------------------------------------------------
Done training -- epoch limit reached

tensorflow编写vggnet16二分类_第1张图片

你可能感兴趣的:(tensorflow编写vggnet16二分类)