TensorFlow实现VGG网络

# -*- coding: utf-8 -*-


from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import os 

os.environ["CUDA_VISIBLE_DEVICES"] = "0"#指定GPU

#tensorflow基于mnist实现VGG11
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)


#定义权重初始化函数
def weights_variable(shape):
    w = tf.truncated_normal(shape,stddev = 0.1)
    return tf.Variable(w)

#定义偏置初始化函数
def bias_variable(shape):
    b = tf.constant(0.1,shape = shape)
    return tf.Variable(b)

#定义卷积
def conv2d(x,w):
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')


if __name__ == '__main__':
    
    #定于输入、输出占位符
    x = tf.placeholder(tf.float32, [None, 784])
    y_ = tf.placeholder(tf.float32, [None, 10])

    #调整x的大小
    x_image = tf.reshape(x, [-1,28,28,1])

    with tf.variable_scope("conv_1"):
        W_conv1 = weights_variable([3, 3, 1, 64])
        b_conv1 = bias_variable([64])
        h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
        
    with tf.variable_scope("conv_2"):
        W_conv2 = weights_variable([3, 3, 64, 64])
        b_conv2 = bias_variable([64])
        h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
    with tf.variable_scope("pool_1"):
        h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
        
    with tf.variable_scope("conv_3"):
        
        W_conv3 = weights_variable([3, 3, 64, 128])
        b_conv3 = bias_variable([128])
        h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
    
    with tf.variable_scope("conv_4"):
        
        W_conv4 = weights_variable([3, 3, 128, 128])
        b_conv4 = bias_variable([128])
        h_conv4 = tf.nn.relu(conv2d(h_conv3, W_conv4) + b_conv4)
        
    with tf.variable_scope("pool_2"):
        h_pool4= tf.nn.max_pool(h_conv4, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
    
    with tf.variable_scope("conv_5"):
        
        W_conv5 = weights_variable([3, 3, 128, 256])
        b_conv5 = bias_variable(shape=[256])
        h_conv5 = tf.nn.relu(conv2d(h_pool4, W_conv5) + b_conv5)
    
    with tf.variable_scope("conv_6"):
        
        W_conv6 = weights_variable([3, 3, 256, 256])
        b_conv6 = bias_variable(shape=[256])
        h_conv6 = tf.nn.relu(conv2d(h_conv5, W_conv6) + b_conv6)
    
    with tf.variable_scope("conv_7"):
        
        W_conv7 = weights_variable([3, 3, 256, 256])
        b_conv7 = bias_variable(shape=[256])
        h_conv7 = tf.nn.relu(conv2d(h_conv6, W_conv7) + b_conv7)
        
    
    with tf.variable_scope("conv_8"):
      
        W_conv8 = weights_variable([3, 3, 256,256])
        b_conv8 = bias_variable(shape=[512])
        h_conv8 = tf.nn.relu(conv2d(h_conv7, W_conv8) + b_conv8)
    
    with tf.variable_scope("pool_3"):
        
        h_pool8 = tf.nn.max_pool(h_conv8, ksize=[1, 2, 2, 1],strides=[1, 1, 1, 1], padding='SAME')
        
    
    with tf.variable_scope("fc_1"):
        
        W_fc1 = weights_variable([7*7*256,1024])
        b_fc1 = bias_variable([1024])
        
        #对h_pool8数据进行铺平
        h_pool2_flat = tf.reshape(h_pool8, [-1, 7*7*256])
        #进行relu计算,matmul表示(wx+b)计算
        h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
        keep_prob = tf.placeholder(tf.float32)
        h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
        
    with tf.variable_scope("fc_2"):
        
        W_fc2 = weights_variable([1024,1024])
        b_fc2 = bias_variable([1024])
        h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
        h_fc2_drop = tf.nn.dropout(h_fc2, keep_prob)
    
    with tf.variable_scope("fc_3"):
        
        W_fc3 = weights_variable([1024,10])
        b_fc3 = bias_variable([10])
        y_conv = tf.matmul(h_fc2_drop, W_fc3) + b_fc3
    
    #在这里通过tf.nn.softmax_cross_entropy_with_logits函数可以对y_conv完成softmax计算,同时计算交叉熵损失函数
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
    
    #定义训练目标以及加速优化器
    train_step = tf.train.AdamOptimizer(1e-3).minimize(loss)
    #计算准确率
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    #记录loss,accuracy变化曲线
    tf.summary.scalar("losses",loss)
    tf.summary.scalar("acc",accuracy)
    #高纬度变量收集
    tf.summary.histogram('weightes',W_conv1)
    tf.summary.histogram('biases',b_conv1)
    
    
    saver = tf.train.Saver()
    #初始化变量
    init_op = tf.global_variables_initializer()
    
    #定义一个合并变量的op
    merged = tf.summary.merge_all()
    
    #开启一个会话
    with tf.Session() as sess:
        
        #变量初始化
        sess.run(init_op)
        
        #建立events文件,然后写入
        filewriter = tf.summary.FileWriter('./tensorboard/',graph=sess.graph)
        #迭代2000次
        for i in range(20000):
            
            #batch设置为10
            batch = mnist.train.next_batch(10)
            train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
            
            #写入每步训练的值
            summary = sess.run(merged,feed_dict={x:batch[0], y_:batch[1], keep_prob: 0.5})
            filewriter.add_summary(summary,i)
            
            if i % 100 == 0:
                train_accuracy = accuracy.eval(feed_dict={
                        x:batch[0], y_: batch[1], keep_prob: 1.0})
                print("step %d, training accuracy %.3f"%(i, train_accuracy))
            
                #在验证集上的准确度
                #print("validation accuracy %g" % accuracy.eval(feed_dict={
                        #x: mnist.validation.images, y_: mnist.validation.labels, keep_prob: 0.5}))
    
        #保存模型
        save_path = saver.save(sess, "./model/save_net.ckpt")
    
        print("test accuracy %g"%accuracy.eval(feed_dict={
                x: mnist.test.images[:3000], y_: mnist.test.labels[:3000], keep_prob: 1.0}))

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI追随者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值