Tensorflow项目(5)—卷积网络手写数字识别

卷积网络手写数字识别

一级目录

二级目录

三级目录

import numpy as np
import tensorflow as tf
import PIL
from PIL import Image
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding=‘utf-8’)

def binarize_image(img,threshold):
img=numpy.array(img)
image=binarize_array(img,threshold)
return image

def binarize_array(numpy_array,threshold=200):
for i in range(len(numpy_array)):
for j in range(len(numpy_array[0])):
if numpy_array[i][j]>threshold:
numpy_array[i][j]=255

		else:
			numpy_array[i][j]=0
	return numpy_array

def crop_pic(img,px,py,p):
wp=(img.size[0])
hp=(img.size[1])
cxp=int(wppx)
cyp=int(hp
py)
L0=wp
if wp>hp:
L0=hp
L0=int(L0*p)
x0=int(cxp-L0/2)
y0=int(cyp-L0/2)
x1=int(cxp+L0/2)
y1=int(cyp+L0/2)
img1=img.crop(x0,y0,x1,y1)
return img1
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.04)
return tf.Variable(initial)

def bias_variable(shape):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial)
#定义 输入 占位符
x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.float32,[None,10])
x_image=tf.reshape(x,[-1,28,28,1])
#1卷积层
W_conv1=weight_variable([5,5,1,32])
b_conv1=bias_variable([32])
h_conv1=tf.nn.conv2d(x_image,W_conv1,strides=[1,1,1,1],padding=‘SAME’)
h_conv1=tf.nn.relu(h_conv1+b_conv1)
#1池化层
h_pool1=tf.nn.max_pool(h_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding=‘SAME’)
#2卷积层
W_conv2=weight_variable([5,5,32,64])
b_conv2=bias_variable([64])
h_conv2=tf.nn.conv2d(h_pool1,W_conv2,strides=[1,1,1,1],padding=‘SAME’)
h_conv2=tf.nn.relu(h_conv2+b_conv2)
h_pool2=tf.nn.max_pool(h_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding=‘SAME’)
h_pool2_flat=tf.reshape(h_pool2,[-1,7764])
W_fc1=weight_variable([7764,1024])
b_fc1=bias_variable([1024])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
#训练集的dropout层
keep_prob=0.5
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#输出层
W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
hout=tf.matmul(h_fc1_drop,W_fc2)+b_fc2
y=tf.nn.softmax(hout)
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#测试集的dropout层
h_fc1_drop=h_fc1
yt=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)*keep_prob+b_fc2)
#损失函数
correct_prediction_test=tf.equal(tf.argmax(yt,1),tf.argmax(y_,1))
accuracy_test=tf.reduce_mean(-tf.cast(correct_prediction_test,tf.float32))
cross_entropy_test=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(yt),reduction_indices=[1]))
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess=tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
saver=tf.train.Saver()
saver.restore(sess,’/data/ConvMNIST.cpk’)

nk=0
while nk==0:
a=input(‘按任意键继续,q键退出:’)
if a ==‘q’:
break
fil=input(‘输入数字土拍你文件名数字(输入0-9任意一个数字):’)
im=Image.open(’/data/testnum/num’+fil+‘JPG’)
kk=1
for p in (0.95,0.90):
im2=crop_pic(im,0.5,0.5,p)
im2=im2.convert(‘L’)
im2=im2.resize((28,28),PIL.Image.ANTIALIAS)
imar=np.array(im2)

	imarr=np.reshape(imar,[784])
	thres=(imarr.max()*0.45+imarr.min()*0.55)
	imar=binarize_array(imar,thres)
	x0=1-np.reshape(imar,[1,784])/imar.max()
	h1=sess.run(y,feed_dict={x:x0})
	xx1=np.sum(-h1*np.log(h1),axis=1)
	print(np.argmax(h1[0,:]))
	x1=1.0-x0
	h2=sess.run(y,feed_dict={x:X1})
	xx2=np.sum(-h2*np.log(h2),axis=1)	
	if kk==1:
		xx0=xx1
		h=h1
		imar_final=imar
	if xx0>xx1:
		h=h1
		xx0=xx1
		imar_final=imar
		kk==kk+1
	print('0:%g'%h[0,0])
	print('1:%g'%h[0,1])
	print('2:%g'%h[0,2])
	print('3:%g'%h[0,3])
	print('4:%g'%h[0,4])
	print('5:%g'%h[0,5])
	print('6:%g'%h[0,6])
	print('7:%g'%h[0,7])
	print('8:%g'%h[0,8])
	print('9:%g'%h[0,9])
	k=np.argmax(h[0,:])
	print('这是%g,置信度为%f'%(k,h[0,k]))
	(2)运行结果截屏

在这里插入图片描述

TensorFlow中实现卷积神经网络(Convolutional Neural Network, CNN)用于手写数字识别是一个常见的机器学习任务。以下是一个简单的例子,使用MNIST数据集,它包含了60,000个训练样本和10,000个测试样本: ```python import tensorflow as tf from tensorflow.keras import datasets, layers, models # 加载MNIST数据集 (train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data() # 数据预处理 train_images = train_images / 255.0 test_images = test_images / 255.0 train_images = train_images[..., tf.newaxis] test_images = test_images[..., tf.newaxis] # 添加通道维度 # 构建CNN模型 model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.Flatten()) # 展平卷积层 model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10)) # 输出层,10个类别对应数字0-9 # 编译模型 model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # 训练模型 history = model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels)) # 测试模型性能 test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print(f'Test accuracy: {test_acc}') # 相关问题-- 1. 这段代码如何利用了CNN的特点? 2. 模型的优化器和损失函数是如何选择的? 3. 为什么需要对训练数据进行归一化处理?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值