对刚入门深度学习的童鞋,这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数据集测试结果相关推荐

  1. 深度学习入门-基于Python的理论入门与实现源代码加mnist数据集下载推荐

    深度学习入门-基于Python的理论入门与实现源代码加mnist数据集下载推荐 书籍封面 1-图灵网站下载 书里也说了,可以图灵网站下载https://www.ituring.com.cn/book/ ...

  2. 基于深度学习的恶意样本行为检测(含源码) ----采用CNN深度学习算法对Cuckoo沙箱的动态行为日志进行检测和分类...

    from:http://www.freebuf.com/articles/system/182566.html 0×01 前言 目前的恶意样本检测方法可以分为两大类:静态检测和动态检测.静态检测是指并 ...

  3. 一文看尽深度学习中的20种卷积(附源码整理和论文解读)

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 引言 卷积,是卷积神经网络中最重要的组件之一.不同的卷积结构有着不一样的功能,但本质上都是用于提取特征 ...

  4. 2021-06-26一文看尽深度学习中的20种卷积(附源码整理和论文解读)

    卷积,是卷积神经网络中最重要的组件之一.不同的卷积结构有着不一样的功能,但本质上都是用于提取特征.比如,在传统图像处理中,人们通过设定不同的算子来提取诸如边缘.水平.垂直等固定的特征.而在卷积神经网络 ...

  5. 碉堡了!程序员用深度学习写了个老板探测器(附源码)

    如果上班的时候想放松一下,或者直说想偷偷懒,看点和工作无关的网页,这时候万一老板突然出现在背后,会不会感到很难堪呢? 有的浏览器设置了boss按键,手快的人还可以切换屏幕,不过总会显得不自然,而且经常 ...

  6. 52 个深度学习目标检测模型汇总,论文、源码一应俱全!(附链接)

    来源:AI有道 本文约2000字,建议阅读5分钟 本文给你总结52个深度学习检测模型. 标签:计算机视觉 目标检测作为计算机视觉中的一个重要分支,近些年来随着神经网络理论研究的深入和硬件 GPU 算力 ...

  7. 最全深度学习训练过程可视化工具(附github源码)

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 Datawhale干货 作者:Edison_G,来源:计算机视觉研究院 编辑丨极市平台 ...

  8. 52 个深度学习目标检测模型汇总,论文、源码一应俱全

    本文约2000字,建议阅读5分钟 本文给你总结52个深度学习检测模型. 标签:计算机视觉 目标检测作为计算机视觉中的一个重要分支,近些年来随着神经网络理论研究的深入和硬件 GPU 算力的大幅度提升,一 ...

  9. 目前最全:52 个深度学习目标检测模型汇总,论文、源码一应俱全!

    文章来源:https://zhuanlan.zhihu.com/p/115035951 作者:红色石头 目标检测作为计算机视觉中的一个重要分支,近些年来随着神经网络理论研究的深入和硬件 GPU 算力的 ...

最新文章

  1. CSS3——transform
  2. python编程,外星人飞船
  3. mysql清理 frm_通过.frm .ibd文件恢复MySQL数据
  4. LeetCode 524. 通过删除字母匹配到字典里最长单词(双指针)
  5. java 2和java有什么区别
  6. Matlab rand randn randint
  7. python str has no attribute_python – AttributeError(“’str’object has no attribute’read’”)...
  8. PreScan第三课:Sensors Model
  9. 暗黑破坏神不朽怎么在电脑上玩 暗黑破坏神不朽模拟器教程
  10. iphone开发每日一练【2011-10-21】
  11. UltraEdit如何激活
  12. 锐捷网络2019年秋招售前产品经理面试总结
  13. android 图片缩放工具,批量图片缩放软件下载-批量图片缩放 安卓版v1.3.1-PC6安卓网...
  14. 描写计算机教室的词语,形容教室环境布置的句子
  15. mysql 联合主键 自增_mysql联合主键,自增长(要区分数据库引擎)
  16. 一文读懂Apache Kylin(麒麟)
  17. Android Q 适配,看这篇就妥了
  18. 【编译原理】LL(1)语法分析器
  19. Markdown的使用之一:表格和公式
  20. 研究区域内测高卫星数据选取(pass)--以T/P-Jason1/2/3为例

热门文章

  1. matlab神经模糊推理系统
  2. 主题图标_Avada主题网站favicon图标设置详细图文教程
  3. 计算机系统的层次结构是缺一不可的吗,第1章 计算机组成与结构绪论.ppt
  4. 将String类型的Json字符串转化对象或对象数组
  5. C - 数据结构实验之查找三:树的种类统计(哈希树)
  6. 【Linux】5.linux下的export命令和环境变量
  7. Deep Learning for Computer Vision with Caffe and cuDNN
  8. Java集合框架之fastutil
  9. ArrayUtils使用详解
  10. Spring系列之一 Spring MVC