Tensorflow-CNN

#coding=utf-8

import numpy as np;
import  tensorflow as tf;

from   tensorflow.examples.tutorials.mnist import input_data;

import  matplotlib.pyplot as plt;



np.set_printoptions(suppress=True);

drop_out = 0.5;
batch_size = 100;

train_epochs = 100;
learn_rate = 0.001;


#权重初始化
def weight_init(shape):
    weight = tf.truncated_normal(shape,stddev=0.1,dtype=tf.float32)  #正态分布
    return tf.Variable(weight);

#偏执初始化
def bias_init(shape):
    bias = tf.random_normal(shape,dtype=tf.float32)
    return tf.Variable(bias);

#随机选取数据集
def get_random_batch_size(examples,size):
    start = np.random.randint(0,examples-size)
    return (start,start+size);

#全连接层参数初始话
def fch_init(layer1,layer2,const=1):
    min = -const*(6.0/(layer1+layer2));
    max = -min;
    weight  = tf.random_uniform([layer1,layer2],minval=min,maxval=max,dtype=tf.float32)
    return tf.Variable(weight)

#卷积
def conv2d(x_images,conv):
    return tf.nn.conv2d(x_images,conv,strides=[1,1,1,1],padding='SAME');

#池化,消除冗余

def max_pool2x2(x_images): #shape: [-1,28,28,16]
    return tf.nn.max_pool(x_images,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME');


x_images = tf.placeholder(tf.float32,[None,784],name='input_images');
y = tf.placeholder(tf.float32,[None,10],name='input_labels');

#w_conv1初始化
w_conv1= weight_init([3,3,1,16]); #3*3的卷积核,深度为1,第一层抓取16个特征feature map
b_conv1 = bias_init([16]);

x = tf.reshape(x_images,[-1,28,28,1]);

r1_conv1 = conv2d(x,w_conv1)+b_conv1;
r1_relu  = tf.nn.relu(r1_conv1); # 非线处理提高拟合能力
r1_max_pool = max_pool2x2(r1_relu);

w_conv2 = weight_init([3,3,16,32])
b_conv2 = bias_init([32])

r2_conv2 = conv2d(r1_max_pool,w_conv2) + b_conv2
r2_relu = tf.nn.relu(r2_conv2);
r2_max_pool = max_pool2x2(r2_relu); # -1*7*7*32

#全连接

f1_input = tf.reshape(r2_max_pool,[-1,7*7*32])
w_fch1 = fch_init(7*7*32,512)
b_fch1 = bias_init([512])

r_fch1 = tf.nn.relu(tf.matmul(f1_input,w_fch1) + b_fch1)

r_fch1  = tf.nn.dropout(r_fch1,drop_out);

w_fch2 = fch_init(512,10);
b_fch2 = bias_init([10]);

y_out = tf.matmul(r_fch1,w_fch2) + b_fch2;

y_out = tf.nn.softmax(y_out,name='y_out');

#定义损失函数
cross_entry  = tf.reduce_mean(tf.reduce_sum(-y*tf.log(y_out)))
optimizer = tf.train.AdamOptimizer(learn_rate).minimize(cross_entry);

#定义准确率

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_out,1));  # [True,False,True]
accuracy  = tf.reduce_mean(tf.cast(correct_prediction,dtype=tf.float32))

#加载mnist数据集
data_path='/Users/chenlinzhong/Documents/code/ML/MNIST_data';
mnist = input_data.read_data_sets(data_path,False,one_hot=True);


n_examples = mnist.train.num_examples
total_batch = int(n_examples/batch_size);

init = tf.global_variables_initializer();

with tf.Session() as sess:
    sess.run(init)
    Cost=[];
    Accuracy=[]
    for i in range(train_epochs):
        for j in range(100):
            start,end = get_random_batch_size(n_examples,batch_size)
            batch_x = mnist.train.images[start:end]
            batch_y = mnist.train.labels[start:end]
            _,c,accur = sess.run([optimizer,cross_entry,accuracy],feed_dict={x_images:batch_x,y:batch_y})
            Cost.append(c)
            Accuracy.append(accur)
            print i,j,c,accur
        if(accur>=0.96):
            break;

    #打印代价函数曲线
    fg1,ax= plt.subplots(figsize=(10,7))
    plt.plot(Cost)
    ax.set_xlabel('Epochs');
    ax.set_ylabel('Cost');
    plt.title('Cross lost');
    plt.grid();
    plt.show();

    #准确率
    fg1,ax= plt.subplots(figsize=(10,7))
    plt.plot(Accuracy)
    ax.set_xlabel('Epochs');
    ax.set_ylabel('Accuracy');
    plt.title('Train Accuracy Rate');
    plt.grid();
    plt.show();

    #各层特征可视化,第一层
    input_image = mnist.train.images[15:16]
    r1_relu = sess.run(r1_relu,feed_dict={x_images:input_image}) # 1*28*28*16
    r1_tranpose = sess.run(tf.transpose(r1_relu,[3,0,1,2]))
    fig,ax = plt.subplots(nrows=1,ncols=16,figsize=(16,1))
    for i in range(16):
        ax[i].imshow(r1_tranpose[i][0])

    plt.title('Conv 16*28*28')
    plt.show()

    #池化后的特征
    r1_max_pool = sess.run(r1_max_pool,feed_dict={x_images:input_image})
    print 'max_pool 1';
    print r1_max_pool.shape
    r1_max_pool_tranpose = sess.run(tf.transpose(r1_max_pool,[3,0,1,2]))
    fig,ax=plt.subplots(nrows=1,ncols=16,figsize=(16,1))
    for i in range(16):
        ax[i].imshow(r1_max_pool_tranpose[i][0])
    plt.title('Max pool 16*14*14')
    plt.show()

    r2_relu  = sess.run(r2_relu,feed_dict={x_images:input_image})
    r2_relu_tranpose = sess.run(tf.transpose(r2_relu,[3,0,1,2]))
    fig,ax = plt.subplots(nrows=1,ncols=32,figsize=(32,1))
    for i in range(32):
        ax[i].imshow(r2_relu_tranpose[i][0])
    plt.title('Conv2 32*14*14')
    plt.show()

    r2_max_pool = sess.run(r2_max_pool,feed_dict={x_images:input_image})
    r2_max_pool_tranpose = sess.run(tf.transpose(r2_max_pool,[3,0,1,2]))
    fig,ax = plt.subplots(nrows=1,ncols=32,figsize=(32,1))
    for i in range(32):
        ax[i].imshow(r2_max_pool_tranpose[i][0])
    plt.title('Max pool2 32*7*7')
    plt.show()

    print sess.run(y_out,feed_dict={x_images:input_image})
    print mnist.train.labels[15:16]

    #保存模型
    saver = tf.train.Saver()
    saver.save(sess, '/Users/chenlinzhong/Documents/code/ML/Model/my-test-model')

还原模型

#coding=utf-8

#使用保存好的模型

import tensorflow as tf;
from tensorflow.examples.tutorials.mnist import  input_data;
import numpy as np;

np.set_printoptions(suppress=True)


#加载mnist数据集
data_path='/Users/chenlinzhong/Documents/code/ML/MNIST_data';
mnist = input_data.read_data_sets(data_path,False,one_hot=True);

with tf.Session() as sess:
    server = tf.train.import_meta_graph('/Users/chenlinzhong/Documents/code/ML/Model/my-test-model.meta');
    server.restore(sess,tf.train.latest_checkpoint('/Users/chenlinzhong/Documents/code/ML/Model'))
    graph = tf.get_default_graph();
    x = graph.get_tensor_by_name('input_images:0')
    y = graph.get_tensor_by_name('input_labels:0')
    feed_dict={x:mnist.train.images[15:16],y:mnist.train.labels[15:16]}
    y_out = graph.get_tensor_by_name('y_out:0')
    print sess.run(tf.argmax(sess.run(y_out,feed_dict),1))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值