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

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是tensorflow实现vggnet16结构的python代码: ```python import tensorflow as tf # 定义vggnet16结构 def VGG16(inputs, num_classes=1000, is_training=True, dropout_keep_prob=0.5, spatial_squeeze=True, scope='vgg_16'): with tf.variable_scope(scope, 'vgg_16', [inputs]) as sc: end_points_collection = sc.name + '_end_points' # 使用 collection 将所有的端点收集起来,方便最后输出 with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], outputs_collections=end_points_collection): net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') net = slim.max_pool2d(net, [2, 2], scope='pool1') net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') net = slim.max_pool2d(net, [2, 2], scope='pool2') net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3') net = slim.max_pool2d(net, [2, 2], scope='pool3') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4') net = slim.max_pool2d(net, [2, 2], scope='pool4') net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5') net = slim.max_pool2d(net, [2, 2], scope='pool5') # 将卷积层输出的结果转化为全连接层的输入格式,以便后面的全连接层对全部的features做预测 net = slim.flatten(net) net = slim.fully_connected(net, 4096, scope='fc6') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout6') net = slim.fully_connected(net, 4096, scope='fc7') net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout7') logits = slim.fully_connected(net, num_classes, activation_fn=None, scope='fc8') end_points = slim.utils.convert_collection_to_dict(end_points_collection) if spatial_squeeze: logits = tf.squeeze(logits, [1, 2], name='fc8/squeezed') end_points = {sc.name + '/fc8': logits} return logits, end_points ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值