上面我们已经得到了模型文件,接下来我们需要验证我们的网络是否准确:
给出验证的代码(需要改动的是模型的路径,这里使用测试集test.tfrecord):
# coding: utf-8
import tensorflow as tf
from PIL import Image
from nets import nets_factory
import matplotlib.pyplot as plt
# In[2]:
# 不同字符数量
CHAR_SET_LEN = 40
# 图片高度
IMAGE_HEIGHT = 60
# 图片宽度
IMAGE_WIDTH = 160
# 批次
BATCH_SIZE = 1
# tfrecord文件存放路径
TFRECORD_FILE = "D:/微云同步/医学/验证码识别/使用一个标签来训练/test.tfrecords"
# placeholder
x = tf.placeholder(tf.float32, [None, 224, 224])
y = tf.placeholder(tf.float32, [None])
# 学习率
lr = tf.Variable(0.6, dtype=tf.float32)
# 从tfrecord读出数据
def read_and_decode(filename):
# 根据文件名生成一个队列
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
# 返回文件名和文件
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64)})
# 获取图片数据
image = tf.decode_raw(features['image'], tf.uint8)
# tf.train.shuffle_batch必须确定shape
image2 = tf.reshape(image, [224, 224])
image = tf.reshape(image, [224, 224])
# 图片预处理
image = tf.cast(image, tf.float32) / 255.0
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
# 获取label
label= tf.cast(features['label'], tf.float64)
return image,image2, label
# 获取图片数据和标签
image,image2,label = read_and_decode(TFRECORD_FILE)
# 使用shuffle_batch可以随机打乱
image_batch, image_raw,label_batch = tf.train.shuffle_batch(
[image,image2,label], batch_size=BATCH_SIZE,
capacity=50000, min_after_dequeue=10000, num_threads=1)
# 定义网络结构
train_network_fn = nets_factory.get_network_fn(
'alexnet_v2',
num_classes=CHAR_SET_LEN,
weight_decay=0.0005,
is_training=True)
with tf.Session() as sess:
# inputs: a tensor of size [batch_size, height, width, channels]
X = tf.reshape(x, [BATCH_SIZE, 224, 224, 1])
# 数据输入网络得到输出值
logits, end_points = train_network_fn(X)
logits0 = tf.slice(logits, [0, 0], [-1, 10])
logits1 = tf.slice(logits, [0, 10], [-1, 10])
logits2 = tf.slice(logits, [0, 20], [-1, 10])
logits3 = tf.slice(logits, [0, 30], [-1, 10])
predict0 = tf.argmax(logits0, 1)
predict1 = tf.argmax(logits1, 1)
predict2 = tf.argmax(logits2, 1)
predict3 = tf.argmax(logits3, 1)
qian = y / 1000 % 10
bai = y / 100 % 10
shi = y / 10 % 10
ge = y % 10
one_hot_labels0 = tf.one_hot(indices=tf.cast(qian, tf.int32), depth=10)
one_hot_labels1 = tf.one_hot(indices=tf.cast(bai, tf.int32), depth=10)
one_hot_labels2 = tf.one_hot(indices=tf.cast(shi, tf.int32), depth=10)
one_hot_labels3 = tf.one_hot(indices=tf.cast(ge, tf.int32), depth=10)
label_40 = tf.concat([one_hot_labels0, one_hot_labels1, one_hot_labels2, one_hot_labels3], axis=1)
# 初始化
sess.run(tf.global_variables_initializer())
# 调用模型
saver = tf.train.Saver()
saver.restore(sess, 'D:/微云同步/医学/验证码识别/使用一个标签来训练/-13000')
# 创建一个协调器,管理线程
coord = tf.train.Coordinator()
# 启动QueueRunner, 此时文件名队列已经进队
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(5):
# 获取一个批次的数据和标签
b_image,image_raw2,b_label = sess.run([image_batch,image_raw,label_batch])
#print(b_label.shape)
img = Image.fromarray(image_raw2[0], 'L')
plt.imshow(img)
plt.axis('off')
plt.show()
# 打印标签
print('label:',b_label)
# 优化模型
label0, label1, label2, label3 = sess.run([predict0, predict1, predict2, predict3], feed_dict={x: b_image})
# 打印预测值
print('predict:', label0, label1, label2, label3)
# result = sess.run(logits, feed_dict={x: b_image, y: b_label})
# print('result:',result)
# 通知其他线程关闭
coord.request_stop()
# 其他所有线程关闭之后,这一函数才能返回
coord.join(threads)
运行之后的结果图,效果还是不错的: