|
本帖最后由 yxtest 于 2019-6-28 13:59 编辑
用TF的CNN实现MNIST手写体识别,将模型保存为pb文件后,用TF加pb进行预测后实现预期效果,而用转换为rknn后的文件进行预测时,结果值出入较大。
TF实现MNIST手写体识别(存储为pb,并加裁pb进行预测)代码:
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from tensorflow.python.framework import graph_util
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")
def train(mnist):
# 使用占位符
x = tf.placeholder(tf.float32, [None, 784], name='input_x') # x为特征
y_ = tf.placeholder(tf.float32, [None, 10], name='y_tag') # y_为label
# 卷积中将1x784转换为28x28x1 [-1,,,]代表样本数量不变 [,,,1]代表通道数
x_image = tf.reshape(x, [-1, 28, 28, 1], name='reshape')
# 第一个卷积层 [5, 5, 1, 32]代表 卷积核尺寸为5x5,1个通道,32个不同卷积核s
# 创建滤波器权值-->加偏置-->卷积-->池化
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 28x28x1 与32个5x5x1滤波器 --> 28x28x32
h_pool1 = max_pool_2x2(h_conv1) # 28x28x32 -->14x14x32
# 第二层卷积层 卷积核依旧是5x5 通道为32 有64个不同的卷积核
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) # 14x14x32 与64个5x5x32滤波器 --> 14x14x64
h_pool2 = max_pool_2x2(h_conv2) # 14x14x64 --> 7x7x64
# h_pool2的大小为7x7x64 转为1-D 然后做FC层
W_fc1 = weight_variable([7 * 7 * 64, 500])
b_fc1 = bias_variable([500])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) # 7x7x64 --> 1x3136
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # FC层传播 3136 --> 1024
# 使用Dropout层减轻过拟合,通过一个placeholder传入keep_prob比率控制
# 在训练中,我们随机丢弃一部分节点的数据来减轻过拟合,预测时则保留全部数据追求最佳性能
# keep_prob = tf.placeholder(tf.float32, name='kp')
keep_prob = tf.placeholder_with_default(1.0, shape=(), name="keep_prob")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# 将Dropout层的输出连接到一个Softmax层,得到最后的概率输出
W_fc2 = weight_variable([500, 10]) # MNIST只有10种输出可能
b_fc2 = bias_variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2, name='probability')
# 定义损失函数,依旧使用交叉熵 同时定义优化器 learning rate = 1e-4
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 定义评测准确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1), name='probability')
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver(max_to_keep=1)
# 开始训练
with tf.Session() as sess:
init_op = tf.global_variables_initializer() # 初始化所有变量
sess.run(init_op)
STEPS = 10000
for i in range(STEPS):
# next_batch函数,它可以从所有的训练数据中读取一小部分作为一个训练batch
batch = mnist.train.next_batch(50)
if i % 1000 == 0:
train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print('step %d,training accuracy %g' % (i, train_accuracy))
sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
saver.save(sess, './ckpt/mnist.ckpt')
# 将训练好的模型保存为.pb文件,方便在Android studio中使用
output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph_def,
output_node_names=['probability'])
# ’wb’中w代表写文件,b代表将数据以二进制方式写入文件。
with tf.gfile.FastGFile('./mnist.pb', mode='wb') as f:
f.write(output_graph_def.SerializeToString())
acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
print("test accuracy %g" % acc)
def load_pb_one(images, labels):
"""
调用pb模型进行预测
:param images:
:param labels:
:return:
"""
print('******************')
print(images.shape)
print(labels)
graph = tf.Graph()
with graph.as_default():
output_graph_def = tf.GraphDef()
with open('./mnist.pb', "rb") as f:
output_graph_def.ParseFromString(f.read())
tensors = tf.import_graph_def(output_graph_def, name="")
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
input_1 = sess.graph.get_tensor_by_name("input_x:0")
output = sess.graph.get_tensor_by_name("probability:0")
predict_y = sess.run(output, feed_dict={input_1: images})
print("predict pb y:", predict_y)
if __name__ == '__main__':
mnist = input_data.read_data_sets("./data", one_hot=True) # 载入数据集
train(mnist)
images, labels = mnist.test.next_batch(1)
print(labels)
load_pb_one(images, labels)
将pb文件转为rknn文件
from tensorflow.examples.tutorials.mnist import input_data
from rknn.api import RKNN
INPUT_SIZE = 784
if __name__ == '__main__':
# 创建RKNN执行对象
rknn = RKNN(verbose=True, verbose_file='./mobilenet_build.log')
rknn.config()
print('--> Loading model')
rknn.load_tensorflow(tf_pb='./mnist.pb',
inputs=['input_x'],
outputs=['probability'],
input_size_list=[[INPUT_SIZE]])
print('done')
# 创建解析pb模型
print('--> Building model')
rknn.build(do_quantization=False)
print('done')
# 导出保存rknn模型文件
rknn.export_rknn('./mnist.rknn')
# Release RKNN Context
rknn.release()
用rknn模型预测出来的结果:
import numpy as np
from PIL import Image
from tensorflow.examples.tutorials.mnist import input_data
from rknn.api import RKNN
def load_model():
# 创建RKNN对象
rknn = RKNN()
# 载入RKNN模型
print('-->loading model')
rknn.load_rknn('./mnist.rknn')
print('loading model done')
# 初始化RKNN运行环境
print('--> Init runtime environment')
# ret = rknn.init_runtime(target='rk3399pro')
ret = rknn.init_runtime()
if ret != 0:
print('Init runtime environment failed')
exit(ret)
print('done')
return rknn
def predict(rknn):
mnist = input_data.read_data_sets("./data", one_hot=True) # 载入数据集
images, labels = mnist.test.next_batch(1)
print(labels)
print(images.shape)
outputs = rknn.inference(inputs=[images]) # 运行推理,得到推理结果
print(outputs)
if __name__ == "__main__":
rknn = load_model()
predict(rknn)
rknn.release()
为什么预测出来的softmax值变为下面的结果[0. , 0.25, 0.25, 0. , 0. , 0.25, 0. , 0.25, 0. , 0. ],
照理说应该为下面这样的结果啊[1.5930017e-09 2.3001310e-09 2.0898119e-09 6.1176152e-06 1.1426837e-08 9.9999332e-01 2.1268575e-07 2.2138165e-09 2.3590802e-07 1.5638408e-07].
希望有人能来解答一下。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|