tenorflow(5)CNN

CNN理论

1、n个(28,28,1)的样本,通过第一层64个(3,3,1)卷积核卷积,得到64个特征图,即(28,28,64)
2、再通过maxpooling1,得到(14,14,64)的图像
3、再在(14,14,64)的图像的基础上,通过第二层128个(3,3,64)卷积核卷积,得到(14,14,128)的特征图
4 、再通过maxpooling2,得到(7,7,128)的图像
5、FC1: 将(7,7,128)的图像进行全连接。
6、FC2: softmax

# coding=utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import input_data

mnist = input_data.read_data_sets('data/', one_hot=True)
trainimg   = mnist.train.images
trainlabel = mnist.train.labels
testimg    = mnist.test.images
testlabel  = mnist.test.labels

print ("MNIST ready")

n_input  = 784
n_output = 10

weights  = {
    'wc1': tf.Variable(tf.random_normal([3,3,1,64],stddev=0.1)),
    'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128],stddev=0.1)),
    'wd1': tf.Variable(tf.random_normal([7*7*128, 1024],stddev=0.1)),
    'wd2': tf.Variable(tf.random_normal([1024,n_output],stddev=0.1))
}
biases   = {
    'bc1':tf.Variable(tf.random_normal([64],stddev=0.1)),
    'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)),
    'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)),
    'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1))
}
def plot(x1,y1,y2):
    plt.plot(x1, y1)
    plt.plot(x1, y2)
    plt.xlabel("step")
    plt.ylabel("acc")
    plt.text(1,0.8,'train:blue')
    plt.show()

def conv_basic(_input,_w,_b,_keepratio):
    # CONV LAYER 1
    #x_image = tf.reshape(x, [-1, 28, 28, 1])这里是将一组图像矩阵x重建为新的矩阵,该新矩阵的维数为(a,28,28,1),其中-1表示a由实际情况来定。 
    #例如,x是一组图像的矩阵(假设是50张,大小为56×56),
    #则执行x_image = tf.reshape(x, [-1, 28, 28, 1])可以计算a=50×56×56/28/28/1=200。即x_image的维数为(200,28,28,1)。
    _input_r = tf.reshape(_input,shape=[-1,28,28,1])
    # 1、卷积
    # strides[bitch_size,h,w,c]bitch_size,h,w,c代表各自的步长大小
    # padding
    # same:给图片自动填充0以满足滑窗计算
    # valid:滑窗到边界就结束,不会自动填充0
    _conv1 = tf.nn.conv2d(_input_r,_w['wc1'],strides=[1,1,1,1],padding='SAME')
    # 2、激活
    _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1,_b['bc1']))
    # 3、池化层
    _pool1 = tf.nn.max_pool(_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
    # 4、droput    _keepratio 神经元保留比例
    _pool_dr1 = tf.nn.dropout(_pool1,_keepratio)
    # CONV LAYER 2 与第一层相同的操作
    _conv2 = tf.nn.conv2d(_pool_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME')
    _conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2']))
    _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    _pool_dr2 = tf.nn.dropout(_pool2, _keepratio)
    # 转换成向量形式
    # ***一个图片724得到[1,1024]的向量即样本特征,此向量再与[1024,n_output]得到各个类的概率结果***
    _dense1 = tf.reshape(_pool_dr2,[-1,_w['wd1'].get_shape().as_list()[0]])
    # 全连接层+droput
    _fc1    = tf.nn.relu(tf.add(tf.matmul(_dense1,_w['wd1']),_b['bd1']))
    _fc_dr1= tf.nn.dropout(_fc1,_keepratio)
    _out    = tf.add(tf.matmul(_fc_dr1,_w['wd2']),_b['bd2'])

    out     = { 'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1, 'pool1_dr1': _pool_dr1,
            'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'dense1': _dense1,
            'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out
        }
    return out

print("CNN ready")


# 搭建计算框架
x = tf.placeholder(tf.float32,[None,n_input])
y = tf.placeholder(tf.float32,[None,n_output])
keepratio = tf.placeholder(tf.float32)

_pred = conv_basic(x,weights,biases,keepratio)['out']
cost  = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=_pred,labels=y))
optm  = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cost)
pred_ret = tf.arg_max(_pred,1)
_corr = tf.equal(tf.arg_max(_pred,1),tf.arg_max(y,1))
accr  = tf.reduce_mean(tf.cast(_corr,tf.float32))
init  = tf.global_variables_initializer()

save_step = 1
saver     = tf.train.Saver(max_to_keep=1)

print("Graph ready")

do_train = 1
sess = tf.Session()
sess.run(init)

trarning_epochs = 20
batch_size      = 50
step            = 3
x1 = []
plot_y1 =[]
plot_y2 =[]
epoch_num = 0

if do_train == 1 :
    for epoch in range(trarning_epochs):
        avg_cost = 0. # 每个epoch的平均误差
        # epoch_num = int(mnist.train.num_examples/batch_size)
        epoch_num = 20
        for i in range(epoch_num):
            # print ("training:%03d/%03d   %03d/%d"%(i,epoch_num,epoch,trarning_epochs))
            batch_xs,batch_yx = mnist.train.next_batch(batch_size)
            sess.run(optm,feed_dict={x:batch_xs,y:batch_yx,keepratio:0.8})
            avg_cost += sess.run(cost,feed_dict={x:batch_xs,y:batch_yx,keepratio:1.})/epoch_num

        if epoch % step == 0 :
            print ("epoch:%03d/%03d  cost:%.9f"%(epoch,epoch_num,avg_cost))
            train_acc = sess.run(accr,feed_dict={x:batch_xs,y:batch_yx,keepratio:1.})
            print ("train accuracy: %.3f"%(train_acc))
            test_xs, test_ys = mnist.train.next_batch(2000)
            test_acc = sess.run(accr, feed_dict={x: test_xs, y: test_ys, keepratio: 1.})
            print ("test accuracy: %.3f" % (test_acc))
            plot_y1.append(train_acc)
            plot_y2.append(test_acc)
            x1.append(len(plot_y1))

        if epoch % save_step == 0:
            saver.save(sess,"save/nets/cnn_mnist_basic.ckpt-"+str(epoch))
    plot(x1,plot_y1,plot_y2)
    print ("optimization finshed")


if do_train == 0:
    epoch =  trarning_epochs - 1
    saver.restore(sess,"save/nets/cnn_mnist_basic.ckpt-" + str(epoch))
    test_xs, test_ys = mnist.train.next_batch(1000)
    test_acc = sess.run(accr, feed_dict={x: test_xs, y: test_ys, keepratio: 1.})
    print ("test accuracy:%.3f"%(test_acc))

tenorflow(5)CNN_第1张图片

测试时候占用内存过多会导致错误:
terminate called after throwing an instance of ‘std::bad_alloc’

sess.run(test_accr,feed_dict={x:test.images.,y:test.labels,keepratio:0.8})

改成即可解决:

test_xs, test_ys = mnist.train.next_batch(2000)
test_acc = sess.run(accr, feed_dict={x: test_xs, y: test_ys, keepratio: 1.})

你可能感兴趣的:(tensorflow,cnn)