parent
3ec93ac29b
commit
763d9210f5
@ -0,0 +1,127 @@
|
||||
# -*- coding=UTF-8 -*-
|
||||
import sys
|
||||
import os
|
||||
import random
|
||||
import cv2
|
||||
import math
|
||||
import time
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
import linecache
|
||||
import string
|
||||
import skimage
|
||||
import imageio
|
||||
# 输入数据
|
||||
import input_data
|
||||
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
|
||||
# 定义网络超参数
|
||||
learning_rate = 0.001
|
||||
training_iters = 200000
|
||||
batch_size = 64
|
||||
display_step = 20
|
||||
# 定义网络参数
|
||||
n_input = 784 # 输入的维度
|
||||
n_classes = 10 # 标签的维度
|
||||
dropout = 0.8 # Dropout 的概率
|
||||
# 占位符输入
|
||||
x = tf.placeholder(tf.types.float32, [None, n_input])
|
||||
y = tf.placeholder(tf.types.float32, [None, n_classes])
|
||||
keep_prob = tf.placeholder(tf.types.float32)
|
||||
# 卷积操作
|
||||
def conv2d(name, l_input, w, b):
|
||||
return tf.nn.relu(tf.nn.bias_add( \
|
||||
tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b) \
|
||||
, name=name)
|
||||
# 最大下采样操作
|
||||
def max_pool(name, l_input, k):
|
||||
return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], \
|
||||
strides=[1, k, k, 1], padding='SAME', name=name)
|
||||
# 归一化操作
|
||||
def norm(name, l_input, lsize=4):
|
||||
return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)
|
||||
# 定义整个网络
|
||||
def alex_net(_X, _weights, _biases, _dropout):
|
||||
_X = tf.reshape(_X, shape=[-1, 28, 28, 1]) # 向量转为矩阵
|
||||
# 卷积层
|
||||
conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])
|
||||
# 下采样层
|
||||
pool1 = max_pool('pool1', conv1, k=2)
|
||||
# 归一化层
|
||||
norm1 = norm('norm1', pool1, lsize=4)
|
||||
# Dropout
|
||||
norm1 = tf.nn.dropout(norm1, _dropout)
|
||||
|
||||
# 卷积
|
||||
conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])
|
||||
# 下采样
|
||||
pool2 = max_pool('pool2', conv2, k=2)
|
||||
# 归一化
|
||||
norm2 = norm('norm2', pool2, lsize=4)
|
||||
# Dropout
|
||||
norm2 = tf.nn.dropout(norm2, _dropout)
|
||||
|
||||
# 卷积
|
||||
conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])
|
||||
# 下采样
|
||||
pool3 = max_pool('pool3', conv3, k=2)
|
||||
# 归一化
|
||||
norm3 = norm('norm3', pool3, lsize=4)
|
||||
# Dropout
|
||||
norm3 = tf.nn.dropout(norm3, _dropout)
|
||||
|
||||
# 全连接层,先把特征图转为向量
|
||||
dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]])
|
||||
dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1')
|
||||
# 全连接层
|
||||
dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2')
|
||||
# Relu activation
|
||||
# 网络输出层
|
||||
out = tf.matmul(dense2, _weights['out']) + _biases['out']
|
||||
return out
|
||||
|
||||
# 存储所有的网络参数
|
||||
weights = {
|
||||
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64])),
|
||||
'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),
|
||||
'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),
|
||||
'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])),
|
||||
'wd2': tf.Variable(tf.random_normal([1024, 1024])),
|
||||
'out': tf.Variable(tf.random_normal([1024, 10]))
|
||||
}
|
||||
biases = {
|
||||
'bc1': tf.Variable(tf.random_normal([64])),
|
||||
'bc2': tf.Variable(tf.random_normal([128])),
|
||||
'bc3': tf.Variable(tf.random_normal([256])),
|
||||
'bd1': tf.Variable(tf.random_normal([1024])),
|
||||
'bd2': tf.Variable(tf.random_normal([1024])),
|
||||
'out': tf.Variable(tf.random_normal([n_classes]))
|
||||
}
|
||||
# 构建模型
|
||||
pred = alex_net(x, weights, biases, keep_prob)
|
||||
# 定义损失函数和学习步骤
|
||||
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
|
||||
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
|
||||
# 测试网络
|
||||
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
|
||||
# 初始化所有的共享变量
|
||||
init = tf.initialize_all_variables()
|
||||
# 开启一个训练
|
||||
with tf.Session() as sess:
|
||||
sess.run(init)
|
||||
step = 1
|
||||
# Keep training until reach max iterations
|
||||
while step * batch_size < training_iters:
|
||||
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
|
||||
# 获取批数据
|
||||
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
|
||||
if step % display_step == 0:
|
||||
# 计算精度
|
||||
acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
|
||||
# 计算损失值
|
||||
loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
|
||||
print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)
|
||||
step += 1
|
||||
print "Optimization Finished!"
|
||||
# 计算测试精度
|
||||
print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})
|
@ -0,0 +1,24 @@
|
||||
|
||||
nexts = [0]*100
|
||||
x = '<img class="ui image bqppsearch lazy" data-original="'
|
||||
|
||||
def KMPinit():
|
||||
i,j,m = 0,-1,len(x)
|
||||
nexts[0]=-1
|
||||
while i<m:
|
||||
while j!=-1 and x[i]!=x[j]: j=nexts[j]
|
||||
nexts[i+1]=j+1
|
||||
i,j=i+1,j+1
|
||||
|
||||
def findWord(y):
|
||||
i,j,ans,n,m=0,0,[],len(y),len(x)
|
||||
while i<n:
|
||||
while j!=-1 and y[i]!=x[j]: j=nexts[j]
|
||||
i,j=i+1,j+1
|
||||
if j==m:
|
||||
to = i+1
|
||||
while y[to:to+7]!='\" title': to=to+1
|
||||
if y[to-3:to]=='jpg' and y[i]=='h' :ans.append(y[i:to])
|
||||
j=nexts[j]
|
||||
return ans
|
||||
|
@ -0,0 +1,68 @@
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
def weight_variable(shape):
|
||||
initial = tf.truncated_normal(shape,stddev=0.1)
|
||||
return tf.Variable(initial)
|
||||
|
||||
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')
|
||||
|
||||
|
||||
# 预处理
|
||||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
|
||||
xs = tf.placeholder(tf.float32,[None,784])
|
||||
ys = tf.placeholder(tf.float32,[None,10])
|
||||
keep_prob = tf.placeholder(tf.float32)
|
||||
|
||||
# 第一层卷积
|
||||
W_conv1 = weight_variable([5,5,1,32])
|
||||
b_conv1 = bias_variable([32])
|
||||
x_image = tf.reshape(xs,[-1,28,28,1])
|
||||
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)
|
||||
|
||||
# 第一层全连接层
|
||||
W_fc1 = weight_variable([7*7*64,1024])
|
||||
b_fc1 = bias_variable([1024])
|
||||
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
|
||||
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
|
||||
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
|
||||
|
||||
# 第二层全连接层
|
||||
W_fc2 = weight_variable([1024,10])
|
||||
b_fc2 = bias_variable([10])
|
||||
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
|
||||
|
||||
# 训练模型
|
||||
cross_entropy = -tf.reduce_sum(ys*tf.log(y_conv))
|
||||
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
|
||||
|
||||
# 概率计算
|
||||
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(ys,1))
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
|
||||
|
||||
init = tf.initialize_all_variables()
|
||||
sess = tf.Session()
|
||||
sess.run(init)
|
||||
|
||||
for i in range(2001):
|
||||
batch = mnist.train.next_batch(50)
|
||||
sess.run(train_step,feed_dict={xs:batch[0],ys:batch[1],keep_prob:0.5})
|
||||
|
||||
if i%100==0:
|
||||
tests = mnist.test.next_batch(200)
|
||||
print(sess.run(accuracy,feed_dict={xs:tests[0],ys:tests[1],keep_prob:1.0}))
|
@ -0,0 +1,26 @@
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
|
||||
|
||||
xs = tf.placeholder(tf.float32,[None,784])
|
||||
ys = tf.placeholder(tf.float32,[None,10])
|
||||
|
||||
Weight = tf.Variable(tf.zeros([784,10]))
|
||||
biases = tf.Variable(tf.zeros([10]))
|
||||
y = tf.nn.softmax(tf.matmul(xs,Weight)+biases)
|
||||
|
||||
loss = -tf.reduce_sum(ys*tf.log(y))
|
||||
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
|
||||
|
||||
init = tf.initialize_all_variables()
|
||||
sess = tf.Session()
|
||||
sess.run(init)
|
||||
|
||||
for step in range(10000):
|
||||
batch = mnist.train.next_batch(100)
|
||||
sess.run(train,feed_dict={xs:batch[0],ys:batch[1]})
|
||||
if step%50==0:
|
||||
correct_prediction = tf.equal(tf.arg_max(ys,1),tf.arg_max(y,1))
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
|
||||
print(sess.run(accuracy,feed_dict={xs:mnist.test.images,ys:mnist.test.labels}))
|
@ -0,0 +1,26 @@
|
||||
import tensorflow as tf
|
||||
from tensorflow.examples.tutorials.mnist import input_data
|
||||
|
||||
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
|
||||
|
||||
xs = tf.placeholder(tf.float32,[None,784])
|
||||
ys = tf.placeholder(tf.float32,[None,10])
|
||||
|
||||
Weight = tf.Variable(tf.zeros([784,10]))
|
||||
biases = tf.Variable(tf.zeros([10]))
|
||||
y = tf.nn.softmax(tf.matmul(xs,Weight)+biases)
|
||||
|
||||
loss = -tf.reduce_sum(ys*tf.log(y))
|
||||
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
|
||||
|
||||
init = tf.initialize_all_variables()
|
||||
sess = tf.Session()
|
||||
sess.run(init)
|
||||
|
||||
for step in range(10000):
|
||||
batch = mnist.train.next_batch(100)
|
||||
sess.run(train,feed_dict={xs:batch[0],ys:batch[1]})
|
||||
if step%50==0:
|
||||
correct_prediction = tf.equal(tf.arg_max(ys,1),tf.arg_max(y,1))
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
|
||||
print(sess.run(accuracy,feed_dict={xs:mnist.test.images,ys:mnist.test.labels}))
|
@ -0,0 +1,24 @@
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
PATH = 'D:/tensorflow/未处理的训练源/'
|
||||
PATH2 = 'D:/tensorflow/train/'
|
||||
|
||||
def resize_image(img_path,save_path):
|
||||
total = 0
|
||||
try:
|
||||
for pic in os.listdir(img_path):
|
||||
path = img_path+pic
|
||||
image = Image.open(path).convert('L')
|
||||
new_pic = image.resize((60,60),Image.ANTIALIAS)
|
||||
new_path = save_path+str(total)+'.jpg'
|
||||
new_pic.save(new_path,quality=100)
|
||||
total = total+1
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
if __name__ == '__main__':
|
||||
for page in range(1,6):
|
||||
print("During deal with %d"%(page))
|
||||
resize_image(PATH+str(page)+'/',PATH2+str(page)+'/')
|
@ -0,0 +1,22 @@
|
||||
import requests
|
||||
import KMP
|
||||
|
||||
url = 'https://fabiaoqing.com/search/search/keyword/%E4%BA%8C%E6%AC%A1%E5%85%83%E5%A6%B9%E5%AD%90/type/bq/page/'
|
||||
store_path = 'D://tensorflow//萌妹子//'
|
||||
|
||||
|
||||
KMP.KMPinit()
|
||||
total = 0
|
||||
file = store_path
|
||||
for page in range(1,20):
|
||||
pages = url+str(page)+'.html'
|
||||
response = requests.get(pages)
|
||||
Word = KMP.findWord(response.text)
|
||||
num = len(Word)
|
||||
for step in range(num):
|
||||
saver = requests.get(Word[step]).content
|
||||
hander=open(store_path+str(total)+'.jpg','wb')
|
||||
hander.write(saver)
|
||||
hander.close()
|
||||
total = total+1
|
||||
print("At Page :%d %d %d"%(page,num,total))
|
@ -0,0 +1,39 @@
|
||||
import os
|
||||
import time
|
||||
import random
|
||||
import shutil
|
||||
from captcha.image import ImageCaptcha
|
||||
|
||||
CHAR_SET = ['0','1','2','3','4','5','6','7','8','9']
|
||||
CHAR_SET_LEN = 10
|
||||
CAPTCHA_LEN = 4
|
||||
|
||||
|
||||
CAPTCHA_IMAGE_PATH = 'D:/tensorflow/captcha/images/'
|
||||
TEST_IMAGE_PATH = 'D:/tensorflow/captcha/test/'
|
||||
TEST_CASE = 200
|
||||
|
||||
def generate_captcha_image(PATH):
|
||||
image = ImageCaptcha()
|
||||
for i in range(10):
|
||||
for j in range(10):
|
||||
for k in range(10):
|
||||
for z in range(10):
|
||||
cap_text = CHAR_SET[i]+CHAR_SET[j]+CHAR_SET[k]+CHAR_SET[z]
|
||||
image.write(cap_text,PATH+cap_text+'.jpg')
|
||||
print("Finish Write!")
|
||||
|
||||
def generate_test_image():
|
||||
fileNameList = []
|
||||
for filePath in os.listdir(CAPTCHA_IMAGE_PATH):
|
||||
captcha_name = filePath.split('/')[-1]
|
||||
fileNameList.append(captcha_name)
|
||||
random.seed(time.time())
|
||||
random.shuffle(fileNameList)
|
||||
for i in range(TEST_CASE):
|
||||
name = fileNameList[i]
|
||||
shutil.move(CAPTCHA_IMAGE_PATH + name, TEST_IMAGE_PATH + name)
|
||||
|
||||
if __name__ == '__main__':
|
||||
generate_captcha_image(CAPTCHA_IMAGE_PATH)
|
||||
generate_test_image()
|
@ -0,0 +1,57 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
import os
|
||||
|
||||
flower_dict = {0:'小熊猫',1:'滑稽',2:'萌妹子',3:'小坏坏',4:'小黄鸡'}
|
||||
path = 'C:/Users/yuanshao/Desktop/test/'
|
||||
|
||||
w=100
|
||||
h=100
|
||||
c=3
|
||||
|
||||
def read_one_image(path):
|
||||
image = Image.open(path).convert('RGB')
|
||||
img = image.resize((w, h), Image.ANTIALIAS)
|
||||
return np.asarray(img)
|
||||
|
||||
def preprocess():
|
||||
count = 0
|
||||
for file in os.listdir(path):
|
||||
new_name = os.path.join(path, str(count))
|
||||
os.rename(os.path.join(path, file),new_name)
|
||||
count += 1
|
||||
for file in os.listdir(path):
|
||||
ori_name = path+file
|
||||
os.rename(ori_name,ori_name+'.jpg')
|
||||
|
||||
with tf.Session() as sess:
|
||||
PATH = cate=[path+x for x in os.listdir(path)]
|
||||
data = []
|
||||
pic = []
|
||||
preprocess()
|
||||
for i in range(len(PATH)):
|
||||
picture = path+str(i)+'.jpg';
|
||||
print(picture)
|
||||
pic.append(picture)
|
||||
data.append(read_one_image(picture))
|
||||
|
||||
saver = tf.train.import_meta_graph('D:/tensorflow/saver/model.ckpt.meta')
|
||||
saver.restore(sess,tf.train.latest_checkpoint('D:/tensorflow/saver/'))
|
||||
|
||||
graph = tf.get_default_graph()
|
||||
x = graph.get_tensor_by_name("x:0")
|
||||
|
||||
logits = graph.get_tensor_by_name("logits_eval:0")
|
||||
|
||||
classification_result = sess.run(logits,feed_dict={x:data})
|
||||
|
||||
#打印出预测矩阵
|
||||
print(classification_result)
|
||||
#打印出预测矩阵每一行最大值的索引
|
||||
print(tf.argmax(classification_result,1).eval())
|
||||
output = []
|
||||
output = tf.argmax(classification_result,1).eval()
|
||||
for i in range(len(output)):
|
||||
print("第",i,"张图片预测:"+flower_dict[output[i]])
|
||||
|
@ -0,0 +1,209 @@
|
||||
import tensorflow as tf
|
||||
from captcha.image import ImageCaptcha
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from PIL import Image
|
||||
import random
|
||||
|
||||
number=['0','1','2','3','4','5','6','7','8','9']
|
||||
#alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
|
||||
#ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
|
||||
|
||||
def random_captcha_text(char_set=number,captcha_size=4):
|
||||
captcha_text=[]
|
||||
for i in range(captcha_size):
|
||||
c=random.choice(char_set)
|
||||
captcha_text.append(c)
|
||||
return captcha_text
|
||||
|
||||
def gen_captcha_text_image():
|
||||
image=ImageCaptcha()
|
||||
captcha_text=random_captcha_text()
|
||||
captcha_text=''.join(captcha_text)
|
||||
captcha=image.generate(captcha_text)
|
||||
captcha_image=Image.open(captcha)
|
||||
captcha_image=np.array(captcha_image)
|
||||
return captcha_text,captcha_image
|
||||
|
||||
|
||||
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 text2vec(text):
|
||||
text_len = len(text)
|
||||
if text_len > max_captcha:
|
||||
raise ValueError('验证码最长4个字符')
|
||||
|
||||
vector = np.zeros(max_captcha * char_set_len)
|
||||
|
||||
def char2pos(c):
|
||||
if c == '_':
|
||||
k = 62
|
||||
return k
|
||||
k = ord(c) - 48
|
||||
if k > 9:
|
||||
k = ord(c) - 55
|
||||
if k > 35:
|
||||
k = ord(c) - 61
|
||||
if k > 61:
|
||||
raise ValueError('No Map')
|
||||
return k
|
||||
|
||||
for i, c in enumerate(text):
|
||||
idx = i * char_set_len + char2pos(c)
|
||||
vector[idx] = 1
|
||||
return vector
|
||||
|
||||
|
||||
def get_next_batch(batch_size=128):
|
||||
batch_x=np.zeros([batch_size,image_height*image_width])
|
||||
batch_y=np.zeros([batch_size,max_captcha*char_set_len])
|
||||
|
||||
def wrap_gen_captcha_text_and_image():
|
||||
while True:
|
||||
text, image = gen_captcha_text_image()
|
||||
if image.shape == (60, 160, 3):
|
||||
return text, image
|
||||
|
||||
for i in range(batch_size):
|
||||
text, image = wrap_gen_captcha_text_and_image()
|
||||
image = convert2gray(image)
|
||||
|
||||
batch_x[i, :] = image.flatten() / 255
|
||||
batch_y[i, :] = text2vec(text)
|
||||
|
||||
return batch_x, batch_y
|
||||
|
||||
def cnn_structure(w_alpha=0.01, b_alpha=0.1):
|
||||
x = tf.reshape(X, shape=[-1, image_height, image_width, 1])
|
||||
|
||||
|
||||
wc1=tf.get_variable(name='wc1',shape=[3,3,1,32],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
|
||||
#wc1 = tf.Variable(w_alpha * tf.random_normal([3, 3, 1, 32]))
|
||||
bc1 = tf.Variable(b_alpha * tf.random_normal([32]))
|
||||
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, wc1, strides=[1, 1, 1, 1], padding='SAME'), bc1))
|
||||
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
conv1 = tf.nn.dropout(conv1, keep_prob)
|
||||
|
||||
wc2=tf.get_variable(name='wc2',shape=[3,3,32,64],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
|
||||
# wc2 = tf.Variable(w_alpha * tf.random_normal([3, 3, 32, 64]))
|
||||
bc2 = tf.Variable(b_alpha * tf.random_normal([64]))
|
||||
conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, wc2, strides=[1, 1, 1, 1], padding='SAME'), bc2))
|
||||
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
conv2 = tf.nn.dropout(conv2, keep_prob)
|
||||
|
||||
wc3=tf.get_variable(name='wc3',shape=[3,3,64,128],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
|
||||
#wc3 = tf.Variable(w_alpha * tf.random_normal([3, 3, 64, 128]))
|
||||
bc3 = tf.Variable(b_alpha * tf.random_normal([128]))
|
||||
conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, wc3, strides=[1, 1, 1, 1], padding='SAME'), bc3))
|
||||
conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
conv3 = tf.nn.dropout(conv3, keep_prob)
|
||||
|
||||
|
||||
wd1=tf.get_variable(name='wd1',shape=[8*20*128,1024],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
|
||||
#wd1 = tf.Variable(w_alpha * tf.random_normal([7*20*128,1024]))
|
||||
bd1 = tf.Variable(b_alpha * tf.random_normal([1024]))
|
||||
dense = tf.reshape(conv3, [-1, wd1.get_shape().as_list()[0]])
|
||||
dense = tf.nn.relu(tf.add(tf.matmul(dense, wd1), bd1))
|
||||
dense = tf.nn.dropout(dense, keep_prob)
|
||||
|
||||
wout=tf.get_variable('name',shape=[1024,max_captcha * char_set_len],dtype=tf.float32,initializer=tf.contrib.layers.xavier_initializer())
|
||||
#wout = tf.Variable(w_alpha * tf.random_normal([1024, max_captcha * char_set_len]))
|
||||
bout = tf.Variable(b_alpha * tf.random_normal([max_captcha * char_set_len]))
|
||||
out = tf.add(tf.matmul(dense, wout), bout)
|
||||
return out
|
||||
|
||||
def train_cnn():
|
||||
output=cnn_structure()
|
||||
cost=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=output,labels=Y))
|
||||
optimizer=tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
|
||||
predict=tf.reshape(output,[-1,max_captcha,char_set_len])
|
||||
max_idx_p = tf.argmax(predict, 2)
|
||||
max_idx_l = tf.argmax(tf.reshape(Y, [-1, max_captcha, char_set_len]), 2)
|
||||
correct_pred = tf.equal(max_idx_p, max_idx_l)
|
||||
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
|
||||
|
||||
saver=tf.train.Saver()
|
||||
|
||||
with tf.Session() as sess:
|
||||
init = tf.global_variables_initializer()
|
||||
sess.run(init)
|
||||
step = 0
|
||||
while True:
|
||||
batch_x, batch_y = get_next_batch(100)
|
||||
_, cost_= sess.run([optimizer, cost], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75})
|
||||
print(step, cost_)
|
||||
if step % 10 == 0:
|
||||
batch_x_test, batch_y_test = get_next_batch(100)
|
||||
acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})
|
||||
print(step, acc)
|
||||
if acc > 0.99:
|
||||
saver.save(sess, "./model/crack_capcha.model", global_step=step)
|
||||
break
|
||||
step += 1
|
||||
|
||||
|
||||
def crack_captcha(captcha_image):
|
||||
output = cnn_structure()
|
||||
|
||||
saver = tf.train.Saver()
|
||||
with tf.Session() as sess:
|
||||
saver.restore(sess, "./model/crack_capcha.model-1200")
|
||||
|
||||
predict = tf.argmax(tf.reshape(output, [-1, max_captcha, char_set_len]), 2)
|
||||
text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1.})
|
||||
text = text_list[0].tolist()
|
||||
return text
|
||||
|
||||
if __name__=='__main__':
|
||||
train=1
|
||||
if train==0:
|
||||
text,image=gen_captcha_text_image()
|
||||
print("验证码大小:",image.shape)#(60,160,3)
|
||||
|
||||
image_height=60
|
||||
image_width=160
|
||||
max_captcha=len(text)
|
||||
print("验证码文本最长字符数",max_captcha)
|
||||
char_set=number
|
||||
char_set_len=len(char_set)
|
||||
|
||||
X = tf.placeholder(tf.float32, [None, image_height * image_width])
|
||||
Y = tf.placeholder(tf.float32, [None, max_captcha * char_set_len])
|
||||
keep_prob = tf.placeholder(tf.float32)
|
||||
train_cnn()
|
||||
|
||||
if train == 1:
|
||||
image_height = 60
|
||||
image_width = 160
|
||||
char_set = number
|
||||
char_set_len = len(char_set)
|
||||
|
||||
text, image = gen_captcha_text_image()
|
||||
|
||||
f = plt.figure()
|
||||
ax = f.add_subplot(111)
|
||||
ax.text(0.1, 0.9, text, ha='center', va='center', transform=ax.transAxes)
|
||||
plt.imshow(image)
|
||||
|
||||
# plt.show()
|
||||
|
||||
max_captcha = len(text)
|
||||
image = convert2gray(image)
|
||||
image = image.flatten() / 255
|
||||
|
||||
X = tf.placeholder(tf.float32, [None, image_height * image_width])
|
||||
Y = tf.placeholder(tf.float32, [None, max_captcha * char_set_len])
|
||||
keep_prob = tf.placeholder(tf.float32)
|
||||
|
||||
predict_text = crack_captcha(image)
|
||||
print("正确: {} 预测: {}".format(text, predict_text))
|
||||
|
||||
|
||||
plt.show()
|
Loading…
Reference in new issue