1. 卷积神经网络基本操作
import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt
import random
def readimg(file):
image=plt.imread(file)
image=image/255
return image
images=[]
labels=[]
path=r'E:\ana\envs\tf14\day12\data3'
for f in os.listdir(path):
img=readimg(path+'\\'+f)
images.append(img)
ones=np.eye(3)
labels.append(ones[int(f[0])])
print(labels)
print(images)
image_data=np.array(images)
label_data=np.array(labels)
print(image_data.shape)
print(label_data.shape)
np.random.seed(3)
m=image_data.shape[0]
order=np.random.permutation(m)
imgarr=image_data[order]
y_one_hot=label_data[order]
total=imgarr.shape[0]
print(total)
g=0
def next_batch(size):
global g
xb=imgarr[g:g+size]
yb=y_one_hot[g:g+size]
g=g+size
return xb,yb
x=tf.placeholder(tf.float32,shape=[None,32,32,3])
y=tf.placeholder(tf.float32,shape=[None,3])
W1 = tf.Variable(tf.random_normal([3, 3, 3, 32]))
L1 = tf.nn.conv2d(x, W1, strides=[1, 1, 1, 1], padding='SAME')
L1 = tf.nn.relu(L1)
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
dim = L2.get_shape()[1].value * L2.get_shape()[2].value * L2.get_shape()[3].value
L2_flat = tf.reshape(L2,[-1, dim])
W3 = tf.get_variable("W3", shape=[dim, 3], initializer=tf.contrib.layers.xavier_initializer())
b = tf.Variable(tf.random_normal([3]))
logits = tf.matmul(L2_flat, W3) + b
cost=tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,labels=y)
optimiter=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess=tf.Session()
sess.run(tf.global_variables_initializer())
print('开始学习')
batch_size=100
train_count=16
train_test=0.9
for epo in range(train_count):
avg_cost=0
g=0
total_batch=int(total*train_test/batch_size)
for i in range(total_batch):
batch_x,batch_y=next_batch(batch_size)
cost_val,_=sess.run([cost,optimiter],feed_dict={x:batch_x,y:batch_y})
avg_cost=avg_cost+cost_val/total_batch
acc = sess.run(accuracy, feed_dict={x: imgarr[int(total * train_test):], y: y_one_hot[int(total * train_test):]})
print(epo,avg_cost,acc)
acc_v=sess.run(accuracy,feed_dict={x:imgarr[int(total*train_test):],y:y_one_hot[int(total*train_test):]})
print(acc_v)
print('结束学习')
r=random.randint(0,int(total*train_test)-1)
label=sess.run(tf.argmax(y_one_hot[r:r+1],1))
predict=sess.run(tf.argmax(logits,1),feed_dict={x:imgarr[r:r+1]})
print(label)
print(predict)
2.卷积神经网络实现验证码
import tensorflow as tf
import matplotlib.pyplot as plt
import os
import numpy as np
import random
from PIL import Image
tf.set_random_seed(123)
train_num=1000
test_num=100
img_height=60
img_width=160
char_num = 4
characters =range(10)
print(characters)
labellen = char_num * len(characters)
def label2vec(label):
"""
:param label: 1327
[0,1,0,0,0,0,0,0,0,0,
0,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,0] """
label_vec=np.zeros(labellen)
for i,num in enumerate(label):
xb=i*len(characters)+int(num)
label_vec[xb]=1
return label_vec
def convert2gray(img):
if len(img.shape)>2:
r,g,b=img[:,:,0],img[:,:,1],img[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
else:
return img
def get_all_files(file_path,num):
image_list=[]
label_list=[]
i=0
for ep in os.listdir(file_path):
img=Image.open(file_path+'\\'+ep)
img=convert2gray(np.array(img))
img=np.array(img)/255
image_list.append(img.reshape([img_height,img_width,1]))
label=os.path.splitext(os.path.split(ep)[1])[0]
label_list.append(label2vec(label))
i+=1
if i>=num:
pass
return np.array(image_list),np.array(label_list)
train_dir=r'E:\ana\envs\tf14\day12\vcode_data\train'
test_dir=r'E:\ana\envs\tf14\day12\vcode_data\test'
imgarr,y_one_hot=get_all_files(train_dir,train_num)
imgarr_test,y_test=get_all_files(test_dir,test_num)
g=0
def next_batch(size):
global g
xb=imgarr[g:g+size]
yb=y_one_hot[g:g+size]
g=g+size
return xb,yb
x=tf.placeholder(tf.float32,shape=[None,60,160,1])
y=tf.placeholder(tf.float32,shape=[None,labellen])
keep_pro=tf.placeholder(tf.float32)
with tf.name_scope('cov1'):
w1=tf.Variable(tf.random_normal([3,3,1,32]))
l1=tf.nn.conv2d(x,w1,strides=[1,1,1,1],padding='SAME')
l1=tf.nn.relu(l1)
l1=tf.nn.max_pool(l1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
l1=tf.nn.dropout(l1,keep_pro)
with tf.name_scope('cov2'):
w2=tf.Variable(tf.random_normal([3,3,32,64]))
l2=tf.nn.conv2d(l1,w2,strides=[1,1,1,1],padding='SAME')
l2=tf.nn.relu(l2)
l2=tf.nn.max_pool(l2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
l2=tf.nn.dropout(l2,keep_pro)
dim=l2.get_shape()[1].value*l2.get_shape()[2].value*l2.get_shape()[3].value
l2_flat=tf.reshape(l2,[-1,dim])
w3=tf.Variable(tf.random_normal([dim,100],stddev=0.01))
b3=tf.Variable(tf.random_normal([100]))
logit1=tf.nn.relu(tf.matmul(l2_flat,w3)+b3)
w4=tf.Variable(tf.random_normal([100,labellen],stddev=0.01))
b4=tf.Variable(tf.random_normal([labellen]))
logit2=tf.matmul(logit1,w4)+b4
cost=tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=logit2)
optimiter=tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
y_true=tf.reshape(y,[-1,4,10])
y_predict=tf.reshape(logit2,[-1,4,10])
y_true=tf.argmax(y_true,2)
y_predict=tf.argmax(y_predict,2)
accurancy=tf.reduce_mean(tf.cast(tf.equal(y_true,y_predict),tf.float32))
sess=tf.Session()
sess.run(tf.global_variables_initializer())
batch_size=100
batch_count=15
print('开始学习')
for ep in range(batch_count):
avg_cost=0
g=0
total_batch=int(train_num/batch_size)
for i in range(total_batch):
batch_x,batch_y=next_batch(batch_size)
cost_val,_=sess.run([cost,optimiter],feed_dict={x:batch_x,y:batch_y,keep_pro:0.7})
avg_cost+=cost_val/total_batch
print(ep,avg_cost)
acc=sess.run(accurancy,feed_dict={x:imgarr_test,y:y_test,keep_pro:0.7})
print(acc)
print('学习完成')
r = int(random.randint(0, test_num - 1))
print("标签: ", sess.run(tf.argmax(y_test[r:r + 1].reshape(4, 10), 1)))
pre = sess.run([tf.argmax(tf.reshape(logit2, [4, 10]), 1)], feed_dict={x: imgarr_test[r:r + 1],keep_pro:0.7})
print("预测: ", pre)