【深度学习】2个经典的练手CNN源码与MNIST数据集测试结果
生活随笔
收集整理的這篇文章主要介紹了
【深度学习】2个经典的练手CNN源码与MNIST数据集测试结果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對剛入門深度學習的童鞋,這2個簡單的工程可快速入門。建議手敲一遍,可快速熟悉代碼和CNN的實現流程。
#1、導入相關庫 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import input_data#2、加載數據集 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")#3、定義權重和偏置 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)) }#4、定義CNN層 def conv_basic(_input, _w, _b, _keepratio):# INPUT_input_r = tf.reshape(_input, shape=[-1, 28, 28, 1])# CONV LAYER 1_conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME')#_mean, _var = tf.nn.moments(_conv1, [0, 1, 2])#_conv1 = tf.nn.batch_normalization(_conv1, _mean, _var, 0, 1, 0.0001)_conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1']))_pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')_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')#_mean, _var = tf.nn.moments(_conv2, [0, 1, 2])#_conv2 = tf.nn.batch_normalization(_conv2, _mean, _var, 0, 1, 0.0001)_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)# VECTORIZE_dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]])# FULLY CONNECTED LAYER 1_fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1']))_fc_dr1 = tf.nn.dropout(_fc1, _keepratio)# FULLY CONNECTED LAYER 2_out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2'])# RETURNout = { '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") #5、定義會話,初始化 a = tf.Variable(tf.random_normal([3,3,1,64], stddev=0.1)) print(a) a = tf.Print(a, [a], "a: ") init = tf.global_variables_initializer() sess = tf.Session() sess.run(init)#print (help(tf.nn.conv2d)) #print (help(tf.nn.max_pool))x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) keepratio = tf.placeholder(tf.float32)#functions _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.001).minimize(cost) _corr = tf.equal(tf.argmax(_pred,1), tf.argmax(y,1)) accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) init = tf.global_variables_initializer()#save print("GRAAPH ready")sess = tf.Session() sess.run(init)training_epochs = 15 batch_size = 50 display_step = 1 for epoch in range(training_epochs):avg_cost = 0.#total_batch = int(mnist.train.num_examples/batch_size)total_batch = 10# Loop over all batchesfor i in range(total_batch):batch_xs, batch_ys = mnist.train.next_batch(batch_size)# Fit training using batch datasess.run(optm, feed_dict={x: batch_xs, y: batch_ys, keepratio:0.7})# Compute average lossavg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})/total_batch# Display logs per epoch stepif epoch % display_step == 0: print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})print (" Training accuracy: %.3f" % (train_acc))#test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.})#print (" Test accuracy: %.3f" % (test_acc))print ("OPTIMIZATION FINISHED")?
#from tensorflow.examples.tutorials.mnist import input_data import input_datamnist = input_data.read_data_sets("MNIST_data/", reshape=False) X_train, y_train = mnist.train.images, mnist.train.labels X_validation, y_validation = mnist.validation.images, mnist.validation.labels X_test, y_test = mnist.test.images, mnist.test.labelsassert(len(X_train) == len(y_train)) assert(len(X_validation) == len(y_validation)) assert(len(X_test) == len(y_test))print() print("Image Shape: {}".format(X_train[0].shape)) print() print("Training Set: {} samples".format(len(X_train))) print("Validation Set: {} samples".format(len(X_validation))) print("Test Set: {} samples".format(len(X_test)))#Visualize Data import numpy as np# Pad images with 0s X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant') X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant') X_test = np.pad(X_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')print("Updated Image Shape: {}".format(X_train[0].shape))import random import numpy as np import matplotlib.pyplot as plt %matplotlib inlineindex = random.randint(0, len(X_train)) image = X_train[index].squeeze()plt.figure(figsize=(1,1)) plt.imshow(image, cmap="gray") print(y_train[index])from sklearn.utils import shuffle X_train, y_train = shuffle(X_train, y_train)import tensorflow as tf EPOCHS = 10 BATCH_SIZE = 128#SOLUTION: Implement LeNet-5 from tensorflow.contrib.layers import flatten def LeNet(x): # Arguments used for tf.truncated_normal, randomly defines variables for the weights and biases for each layermu = 0sigma = 0.1# SOLUTION: Layer 1: Convolutional. Input = 32x32x1. Output = 28x28x6.conv1_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 1, 6), mean = mu, stddev = sigma))conv1_b = tf.Variable(tf.zeros(6))conv1 = tf.nn.conv2d(x, conv1_W, strides=[1, 1, 1, 1], padding='VALID') + conv1_b# SOLUTION: Activation.conv1 = tf.nn.relu(conv1)# SOLUTION: Pooling. Input = 28x28x6. Output = 14x14x6.conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')# SOLUTION: Layer 2: Convolutional. Output = 10x10x16.conv2_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean = mu, stddev = sigma))conv2_b = tf.Variable(tf.zeros(16))conv2 = tf.nn.conv2d(conv1, conv2_W, strides=[1, 1, 1, 1], padding='VALID') + conv2_b# SOLUTION: Activation.conv2 = tf.nn.relu(conv2)# SOLUTION: Pooling. Input = 10x10x16. Output = 5x5x16.conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')# SOLUTION: Flatten. Input = 5x5x16. Output = 400.fc0 = flatten(conv2)# SOLUTION: Layer 3: Fully Connected. Input = 400. Output = 120.fc1_W = tf.Variable(tf.truncated_normal(shape=(400, 120), mean = mu, stddev = sigma))fc1_b = tf.Variable(tf.zeros(120))fc1 = tf.matmul(fc0, fc1_W) + fc1_b# SOLUTION: Activation.fc1 = tf.nn.relu(fc1)# SOLUTION: Layer 4: Fully Connected. Input = 120. Output = 84.fc2_W = tf.Variable(tf.truncated_normal(shape=(120, 84), mean = mu, stddev = sigma))fc2_b = tf.Variable(tf.zeros(84))fc2 = tf.matmul(fc1, fc2_W) + fc2_b# SOLUTION: Activation.fc2 = tf.nn.relu(fc2)# SOLUTION: Layer 5: Fully Connected. Input = 84. Output = 10.fc3_W = tf.Variable(tf.truncated_normal(shape=(84, 10), mean = mu, stddev = sigma))fc3_b = tf.Variable(tf.zeros(10))logits = tf.matmul(fc2, fc3_W) + fc3_breturn logitsx = tf.placeholder(tf.float32, (None, 32, 32, 1)) y = tf.placeholder(tf.int32, (None)) one_hot_y = tf.one_hot(y, 10)#Training Pipeline rate = 0.001 logits = LeNet(x) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits) loss_operation = tf.reduce_mean(cross_entropy) optimizer = tf.train.AdamOptimizer(learning_rate = rate) training_operation = optimizer.minimize(loss_operation)#Model Evaluation correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1)) accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) saver = tf.train.Saver()def evaluate(X_data, y_data):num_examples = len(X_data)total_accuracy = 0sess = tf.get_default_session()for offset in range(0, num_examples, BATCH_SIZE):batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})total_accuracy += (accuracy * len(batch_x))return total_accuracy / num_exampleswith tf.Session() as sess:sess.run(tf.global_variables_initializer())num_examples = len(X_train)print("Training...")print()for i in range(EPOCHS):X_train, y_train = shuffle(X_train, y_train)for offset in range(0, num_examples, BATCH_SIZE):end = offset + BATCH_SIZEbatch_x, batch_y = X_train[offset:end], y_train[offset:end]sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})validation_accuracy = evaluate(X_validation, y_validation)print("EPOCH {} ...".format(i+1))print("Validation Accuracy = {:.3f}".format(validation_accuracy))print()saver.save(sess, './lenet')print("Model saved")#Evaluate the Model with tf.Session() as sess:saver.restore(sess, tf.train.latest_checkpoint('.'))test_accuracy = evaluate(X_test, y_test)print("Test Accuracy = {:.3f}".format(test_accuracy))總結
以上是生活随笔為你收集整理的【深度学习】2个经典的练手CNN源码与MNIST数据集测试结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C++】error C2512: 'A
- 下一篇: 【C/C++】将二个有序数组合并