1、知识点
""" 验证码分析: 对图片进行分析: 1、分割识别 2、整体识别 输出:[3,5,7] -->softmax转为概率[0.04,0.16,0.8] ---> 交叉熵计算损失值 (目标值和预测值的对数) tf.argmax(预测值,2)验证码样例:[NAZP] [XCVB] [WEFW] ,都是字母的 """
2、将数据写入TFRecords
import tensorflow as tf import os os.environ[‘TF_CPP_MIN_LOG_LEVEL‘] = ‘2‘ FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string("tfrecords_dir", "./tfrecords/captcha.tfrecords", "验证码tfrecords文件") tf.app.flags.DEFINE_string("captcha_dir", "../data/Genpics/", "验证码图片路径") tf.app.flags.DEFINE_string("letter", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "验证码字符的种类") def dealwithlabel(label_str): # 构建字符索引 {0:‘A‘, 1:‘B‘......} num_letter = dict(enumerate(list(FLAGS.letter))) # 键值对反转 {‘A‘:0, ‘B‘:1......} letter_num = dict(zip(num_letter.values(), num_letter.keys())) print(letter_num) # 构建标签的列表 array = [] # 给标签数据进行处理[[b"NZPP"]......] for string in label_str: letter_list = []# [1,2,3,4] # 修改编码,b‘FVQJ‘到字符串,并且循环找到每张验证码的字符对应的数字标记 for letter in string.decode(‘utf-8‘): letter_list.append(letter_num[letter]) array.append(letter_list) # [[13, 25, 15, 15], [22, 10, 7, 10], [22, 15, 18, 9], [16, 6, 13, 10], [1, 0, 8, 17], [0, 9, 24, 14].....] print(array) # 将array转换成tensor类型 label = tf.constant(array) return label def get_captcha_image(): """ 获取验证码图片数据 :param file_list: 路径+文件名列表 :return: image """ # 构造文件名 filename = [] for i in range(6000): string = str(i) + ".jpg" filename.append(string) # 构造路径+文件 file_list = [os.path.join(FLAGS.captcha_dir, file) for file in filename] # 构造文件队列 file_queue = tf.train.string_input_producer(file_list, shuffle=False) # 构造阅读器 reader = tf.WholeFileReader() # 读取图片数据内容 key, value = reader.read(file_queue) # 解码图片数据 image = tf.image.decode_jpeg(value) image.set_shape([20, 80, 3]) # 批处理数据 [6000, 20, 80, 3] image_batch = tf.train.batch([image], batch_size=6000, num_threads=1, capacity=6000) return image_batch def get_captcha_label(): """ 读取验证码图片标签数据 :return: label """ file_queue = tf.train.string_input_producer(["../data/Genpics/labels.csv"], shuffle=False) reader = tf.TextLineReader() key, value = reader.read(file_queue) records = [[1], ["None"]] number, label = tf.decode_csv(value, record_defaults=records) # [["NZPP"], ["WKHK"], ["ASDY"]] label_batch = tf.train.batch([label], batch_size=6000, num_threads=1, capacity=6000) return label_batch def write_to_tfrecords(image_batch, label_batch): """ 将图片内容和标签写入到tfrecords文件当中 :param image_batch: 特征值 :param label_batch: 标签纸 :return: None """ # 转换类型 label_batch = tf.cast(label_batch, tf.uint8) print(label_batch) # 建立TFRecords 存储器 writer = tf.python_io.TFRecordWriter(FLAGS.tfrecords_dir) # 循环将每一个图片上的数据构造example协议块,序列化后写入 for i in range(6000): # 取出第i个图片数据,转换相应类型,图片的特征值要转换成字符串形式 image_string = image_batch[i].eval().tostring() # 标签值,转换成整型 label_string = label_batch[i].eval().tostring() # 构造协议块 example = tf.train.Example(features=tf.train.Features(feature={ "image": tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_string])), "label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label_string])) })) writer.write(example.SerializeToString()) # 关闭文件 writer.close() return None if __name__ == "__main__": # 获取验证码文件当中的图片 image_batch = get_captcha_image() # 获取验证码文件当中的标签数据 label = get_captcha_label() print(image_batch, label) with tf.Session() as sess: coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) # [b‘NZPP‘ b‘WKHK‘ b‘WPSJ‘ ..., b‘FVQJ‘ b‘BQYA‘ b‘BCHR‘] label_str = sess.run(label) print(label_str) # 处理字符串标签到数字张量 label_batch = dealwithlabel(label_str) print(label_batch) # 将图片数据和内容写入到tfrecords文件当中 write_to_tfrecords(image_batch, label_batch) coord.request_stop() coord.join(threads)
3、数据存在百度云(小白号)
4、标准代码
import tensorflow as tf FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string("captcha_dir", "./tfrecords/captcha.tfrecords", "验证码数据的路径") tf.app.flags.DEFINE_integer("batch_size", 100, "每批次训练的样本数") tf.app.flags.DEFINE_integer("label_num", 4, "每个样本的目标值数量") tf.app.flags.DEFINE_integer("letter_num", 26, "每个目标值取的字母的可能心个数") # 定义一个初始化权重的函数 def weight_variables(shape): w = tf.Variable(tf.random_normal(shape=shape, mean=0.0, stddev=1.0)) return w # 定义一个初始化偏置的函数 def bias_variables(shape): b = tf.Variable(tf.constant(0.0, shape=shape)) return b def read_and_decode(): """ 读取验证码数据API :return: image_batch, label_batch """ # 1、构建文件队列 file_queue = tf.train.string_input_producer([FLAGS.captcha_dir]) # 2、构建阅读器,读取文件内容,默认一个样本 reader = tf.TFRecordReader() # 读取内容 key, value = reader.read(file_queue) # tfrecords格式example,需要解析 features = tf.parse_single_example(value, features={ "image": tf.FixedLenFeature([], tf.string), "label": tf.FixedLenFeature([], tf.string), }) # 解码内容,字符串内容 # 1、先解析图片的特征值 image = tf.decode_raw(features["image"], tf.uint8) # 1、先解析图片的目标值 label = tf.decode_raw(features["label"], tf.uint8) # print(image, label) # 改变形状 image_reshape = tf.reshape(image, [20, 80, 3]) label_reshape = tf.reshape(label, [4]) print(image_reshape, label_reshape) # 进行批处理,每批次读取的样本数 100, 也就是每次训练时候的样本 image_batch, label_btach = tf.train.batch([image_reshape, label_reshape], batch_size=FLAGS.batch_size, num_threads=1, capacity=FLAGS.batch_size) print(image_batch, label_btach) return image_batch, label_btach def fc_model(image): """ 进行预测结果 :param image: 100图片特征值[100, 20, 80, 3] :return: y_predict预测值[100, 4 * 26] """ with tf.variable_scope("model"): # 将图片数据形状转换成二维的形状 image_reshape = tf.reshape(image, [-1, 20 * 80 * 3]) # 1、随机初始化权重偏置 # matrix[100, 20 * 80 * 3] * [20 * 80 * 3, 4 * 26] + [104] = [100, 4 * 26] weights = weight_variables([20 * 80 * 3, 4 * 26]) bias = bias_variables([4 * 26]) # 进行全连接层计算[100, 4 * 26] y_predict = tf.matmul(tf.cast(image_reshape, tf.float32), weights) + bias return y_predict def predict_to_onehot(label): """ 将读取文件当中的目标值转换成one-hot编码 :param label: [100, 4] [[13, 25, 15, 15], [19, 23, 20, 16]......] :return: one-hot """ # 进行one_hot编码转换,提供给交叉熵损失计算,准确率计算[100, 4, 26] label_onehot = tf.one_hot(label, depth=FLAGS.letter_num, on_value=1.0, axis=2) print(label_onehot) return label_onehot def captcharec(): """ 验证码识别程序 :return: """ # 1、读取验证码的数据文件 label_btch [100 ,4] image_batch, label_batch = read_and_decode() # 2、通过输入图片特征数据,建立模型,得出预测结果 # 一层,全连接神经网络进行预测 # matrix [100, 20 * 80 * 3] * [20 * 80 * 3, 4 * 26] + [104] = [100, 4 * 26] y_predict = fc_model(image_batch) # [100, 4 * 26] print(y_predict) # 3、先把目标值转换成one-hot编码 [100, 4, 26] y_true = predict_to_onehot(label_batch) # 4、softmax计算, 交叉熵损失计算 with tf.variable_scope("soft_cross"): # 求平均交叉熵损失 ,y_true [100, 4, 26]--->[100, 4*26] loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( labels=tf.reshape(y_true, [FLAGS.batch_size, FLAGS.label_num * FLAGS.letter_num]), logits=y_predict)) # 5、梯度下降优化损失 with tf.variable_scope("optimizer"): train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) # 6、求出样本的每批次预测的准确率是多少 三维比较 with tf.variable_scope("acc"): # 比较每个预测值和目标值是否位置(4)一样 y_predict [100, 4 * 26]---->[100, 4, 26] equal_list = tf.equal(tf.argmax(y_true, 2), tf.argmax(tf.reshape(y_predict, [FLAGS.batch_size, FLAGS.label_num, FLAGS.letter_num]), 2)) # equal_list 100个样本 [1, 0, 1, 0, 1, 1,..........] accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32)) # 定义一个初始化变量的op init_op = tf.global_variables_initializer() # 开启会话训练 with tf.Session() as sess: sess.run(init_op) # 定义线程协调器和开启线程(有数据在文件当中读取提供给模型) coord = tf.train.Coordinator() # 开启线程去运行读取文件操作 threads = tf.train.start_queue_runners(sess, coord=coord) # 训练识别程序 for i in range(5000): sess.run(train_op) print("第%d批次的准确率为:%f" % (i, accuracy.eval())) # 回收线程 coord.request_stop() coord.join(threads) return None if __name__ == "__main__": captcharec()
5、自写代码
# coding = utf-8 import tensorflow as tf from tensorflow.contrib.slim.python.slim.nets.inception_v3 import inception_v3_base import os """ 验证码分析: 对图片进行分析: 1、分割识别 2、整体识别 输出:[3,5,7] -->softmax转为概率[0.04,0.16,0.8] ---> 交叉熵计算损失值 (目标值和预测值的对数) tf.argmax(预测值,2) """ FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string("captcha_dir","./tfrecords/captcha.tfrecords","验证码数据路径") tf.app.flags.DEFINE_integer("batch_size",100,"读取批次") tf.app.flags.DEFINE_integer("label_num", 4, "每个样本的目标值数量") tf.app.flags.DEFINE_integer("letter_num", 26, "每个目标值取的字母的可能心个数") def weight_variable(shape): w = tf.Variable(tf.random_normal(shape=shape,mean=0.0,stddev=1.0,)) return w def bias_variable(shape): b = tf.Variable(tf.random_normal(shape=shape,mean=0.0,stddev=1.0)) return b def captcharec(): """ 验证码识别 :return: """ #1、读取验证码的数据文件 file_queue = tf.train.string_input_producer([FLAGS.captcha_dir]) #2、创建阅读器,解析example reader = tf.TFRecordReader() key ,value = reader.read(file_queue) features = tf.parse_single_example(value,features={ "image":tf.FixedLenFeature([],tf.string), "label": tf.FixedLenFeature([], tf.string) }) #解码操作 image = tf.decode_raw(features["image"],tf.uint8) label = tf.decode_raw(features["label"],tf.uint8) print(image,label) #修改形状 image_reshape = tf.reshape(image,[20,80,3]) label_reshape = tf.reshape(label, [4]) #进行批处理,每次读取100个样本 image_batch,label_batch = tf.train.batch([image_reshape,label_reshape],batch_size=100,num_threads=1,capacity=20) print(image_batch, label_batch) return image_batch,label_batch def fc_model(image_batch): #1、初始化权重和偏置 w = weight_variable([20*80*3,4*26]) b = bias_variable([4*26]) #模型 x [100,20*80*3] w [20*80*3,4] y_true [100,4] #对输入进行矩阵转换 image = tf.reshape(image_batch,[-1,20*80*3]) y_predict = tf.matmul(tf.cast(image,tf.float32),w) + b ############收集变量######## tf.summary.histogram("w",w) tf.summary.histogram("b",b) merged = tf.summary.merge_all() return y_predict,merged #[100,4] def predict_to_onehot(label_batch): y_true = tf.one_hot(label_batch,on_value=1.0,depth=26,axis=2) return y_true if __name__ == ‘__main__‘: image_batch, label_batch = captcharec() y_predict,merged_his =fc_model(image_batch) y_true = predict_to_onehot(label_batch) #计算交叉熵 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf.reshape(y_true,[100,4*26]),logits=y_predict)) #梯度下降优化 train_op = tf.train.GradientDescentOptimizer(0.001).minimize(loss) #准确率 equal_list = tf.equal(tf.argmax(y_true,2),tf.argmax(tf.reshape(y_predict,[100,4,26]),2)) accuracy = tf.reduce_mean(tf.cast(equal_list,tf.float32)) #####收集变量############### tf.summary.scalar("losses",loss) tf.summary.scalar("accuracy",accuracy) merged_scalar = tf.summary.merge_all() ############保存模型#### saver = tf.train.Saver() init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) fileWriter = tf.summary.FileWriter("./vc",graph=sess.graph) #创建线程协调器 coord = tf.train.Coordinator() #开启线程 threads = tf.train.start_queue_runners(sess,coord=coord) IS_TRAIN =1 # if os.path.exists("./vertifycode/checkpoint"): # IS_TRAIN = 0 if IS_TRAIN==1: #######训练模型############### # if os.path.exists("./vertifycode/checkpoint"): # saver.restore(sess, "./vertifycode/vertifycode_model") for i in range(2000): sess.run(train_op) summary_his = sess.run(merged_his) summary_scalar = sess.run(merged_scalar) fileWriter.add_summary(summary_scalar,i) fileWriter.add_summary(summary_his,i) print("训练第%d次的准确率为:%f" %(i,accuracy.eval())) #######保存模型############# saver.save(sess,"./vertifycode/vertifycode_model") else: ##########测试模型################## for i in range(10): saver.restore(sess, "./vertifycode/vertifycode_model") # print("第%d张图片的准确率为:%f" % ( # i, # tf.argmax(y_test, 2).eval(), # tf.argmax(y_predict,2).eval() # )) #停止线程 coord.request_stop() coord.join(threads)
原文地址:https://www.cnblogs.com/ywjfx/p/10940665.html
时间: 2024-10-14 07:51:52