实例介绍
【实例截图】
│ 8.png
│ checkpoint
│ demo01_ce.py
│ demo02_tfdemo.py
│ demo03_mnist.py
│ demo04_mnist_test.py
│ mnist_model.ckpt.data-00000-of-00001
│ mnist_model.ckpt.index
│ mnist_model.ckpt.meta
│ 神经网络分类模型_初始.png
│ 神经网络分类模型_结果.png
│
└─MNIST_data
t10k-images-idx3-ubyte.gz
t10k-images.idx3-ubyte
t10k-labels-idx1-ubyte.gz
t10k-labels.idx1-ubyte
train-images-idx3-ubyte.gz
train-images.idx3-ubyte
train-labels-idx1-ubyte.gz
train-labels.idx1-ubyte
【核心代码】
import tensorflow as tf
import cv2 as cv
import numpy as np
#生成权重
def weight_variable(shape):
initial = tf.random_normal(shape, stddev=0.1)
return tf.Variable(initial)
#生成b
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#卷积层
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
x_image = tf.reshape(x, [-1,28,28,1])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) b_fc1)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1, W_fc2) b_fc2)
saver = tf.train.Saver()
with tf.Session() as sess:
# 加载模型参数
saver.restore(sess, 'mnist_model.ckpt')
print('Variables restored.')
# 读入测试图片
original = cv.imread('8.png')
# 图片转灰度
gray = cv.cvtColor(original, cv.COLOR_BGR2GRAY)
# 把灰度小于等于10的=0,大于10的=1(纯 黑色像素 值为 0, 纯 白色像素值为 1)
gray[gray<=10] = 0
gray[gray>10] = 1
# 修改灰度图片的像素改为28x28, 并且转为二维数组
gray = cv.resize(gray, (28, 28)).reshape(1, 28*28)
# 喂数据(把gray代入方程获取测试结果)
pred_y = sess.run(y_conv, feed_dict={x:gray})
# 取预测结果的最大值的索引就是真实预测结果
print(pred_y.argmax())
# from tensorflow.examples.tutorials.mnist import input_data
# import tensorflow as tf
# mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# batch = mnist.test.next_batch(5000)
# test_x = batch[0]
# test_y = batch[1]
# pred_y = sess.run(y_conv, feed_dict={x:test_x})
# for pred, label in zip(
# np.argmax(pred_y, axis=1),
# np.argmax(test_y, axis=1)):
# print(pred, '<-', label)
#
# print((np.argmax(pred_y, axis=1) == np.argmax(test_y, axis=1)).sum() / 50)
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论