Toybrick

RKNN转换MNIST手写体识别问题

yxtest

新手上路

积分
36
楼主
发表于 2019-6-28 13:53:34    查看: 7453|回复: 5 | [复制链接]    打印 | 只看该作者
本帖最后由 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 325x5x1滤波器 --> 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 645x5x32滤波器 --> 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
回复

使用道具 举报

jefferyzhang

版主

积分
12942
沙发
发表于 2019-7-8 14:32:20 | 只看该作者
代码太长了没办法帮你调试,建议你可以按以下思路调试:
1. rknn的层默认未量化是FP16,精度和TF默认FP32有一点点区别,可以试着修改其中一个精度和另外一个 一致看下
2. 单层调试,把out设置到第一层输出、第二层输出、第三层... 以此类推,和tf的结果进行比较,查看是哪一层开始不一致的。
3. rknn的config归一指标要写 "0 0 0 1",不能不写
回复

使用道具 举报

Ningbo

新手上路

积分
33
板凳
发表于 2019-8-8 16:59:27 | 只看该作者
jefferyzhang 发表于 2019-7-8 14:32
代码太长了没办法帮你调试,建议你可以按以下思路调试:
1. rknn的层默认未量化是FP16,精度和TF默认FP32有 ...
  1. --> config model
  2. done
  3. --> Loading model
  4. done
  5. --> Building model
  6. done
  7. --> Export RKNN model
  8. done
  9. --> Init runtime environment
  10. done
  11. --> Running model
  12. lenet
  13. -----TOP 5-----
  14. [2]: 0.59814453125
  15. [6]: 0.274169921875
  16. [3]: 0.037811279296875
  17. [1]: 0.0258941650390625
  18. [8]: 0.02532958984375

  19. done
  20. --> Begin evaluate model performance
  21. ========================================================================
  22.                                Performance                              
  23. ========================================================================
  24. Layer ID    Name                                         Time(us)
  25. 8           convolution.relu.pooling.layer2_2            15
  26. 9           convolution.relu.pooling.layer2_2            95
  27. 10          fullyconnected.relu.layer_3                  86
  28. 11          fullyconnected.relu.layer_3                  7
  29. Total Time(us): 203
  30. FPS(800MHz): 4926.11
  31. ========================================================================

  32. done
复制代码

最后一层输出
  1. [1.7805099e-03 2.5894165e-02 5.9814453e-01 3.7811279e-02 1.2107849e-02
  2. 2.2293091e-02 2.7416992e-01 2.4204254e-03 2.5329590e-02 1.3971329e-04]       
复制代码


用的caffe框架,图片为0.jgp,推理一直不正确
回复

使用道具 举报

Ningbo

新手上路

积分
33
地板
发表于 2019-8-9 11:08:29 | 只看该作者

没错哦,是28*28

hisping 发表于 2019-8-9 09:28
你转模型时input_size_list=[[28, 28, 1]]才对吧,试一试
  1. name: "LeNet"
  2. layer {
  3.   name: "input"
  4.   type: "Input"
  5.   top: "data"
  6.   input_param {
  7.     shape {
  8.       dim: 64
  9.       dim: 1
  10.       dim: 28
  11.       dim: 28
  12.     }
  13.   }
  14. }
复制代码
回复

使用道具 举报

Ningbo

新手上路

积分
33
5#
发表于 2019-8-9 16:29:56 | 只看该作者
标准网络模型就是28*28,本身输入就是28*28图片。
问题不在于这里,已经解决了。
谢谢啦
回复

使用道具 举报

jiajia1990

中级会员

积分
448
6#
发表于 2019-8-20 16:35:03 | 只看该作者
Ningbo 发表于 2019-8-9 16:29
标准网络模型就是28*28,本身输入就是28*28图片。
问题不在于这里,已经解决了。
谢谢啦 ...

你好,问题在哪里?我做的也是预测的和实际不对,看着预测结果差不多,你的问题怎么解决的啊?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

产品中心 购买渠道 开源社区 Wiki教程 资料下载 关于Toybrick


快速回复 返回顶部 返回列表