一级目录
二级目录
三级目录
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(hppy)
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)运行结果截屏