|
1 生成pb(参数随机初始化,不影响前向运算):
- <blockquote>import tensorflow as tf
2pb转换rknn代码
- from rknn.api import RKNN
- if __name__ == '__main__':
- # 创建RKNN执行对象
- rknn = RKNN()
- #初始化RKNN运行环境,指定host=’rk3399pro’,后续的操作将会运行在NPU上
- print('--> Init runtime environment')
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- # 配置模型输入,用于NPU对数据输入的预处理
- # channel_mean_value='0 0 0 255',那么模型推理时,将会对RGB数据做如下转换
- # (R - 0)/255, (G - 0)/255, (B - 0)/255。推理时,RKNN模型会自动做均值和归一化处理
- # reorder_channel=’0 1 2’用于指定是否调整RBG顺序,设置成0 1 2即按输入的RGB顺序不做调整
- # reorder_channel=’2 1 0’表示交换0和2通道,如果输入是RGB,将会被调整为BGR。如果是BGR将会被
- # 调整为RGB
- rknn.config(channel_mean_value='0 0 0 1', reorder_channel='0 1 2')
- # 加载TensorFlow模型
- # tf_pb='../digital_gesture_recognition/model_2500/digital_gesture.pb'指定待转换的TensorFlow模型
- # inputs指定模型中的输入节点
- # outputs指定模型中输出节点
- # input_size_list指定模型输入的大小
- print('--> Loading model')
- rknn.load_tensorflow(tf_pb='test.pb',
- inputs=['input_node'],
- outputs=['logits'],
- input_size_list=[[1, 1000, 1]])
- print('done')
- # 创建解析pb模型
- # do_quantization=False指定不进行量化
- # 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
- print('--> Building model')
- rknn.build(do_quantization=False)
- print('done')
- # 导出保存rknn模型文件
- rknn.export_rknn('test.rknn')
- # Release RKNN Conte
- rknn.release()
3分别使用rknn和pb进行推理
- import numpy as np
- import tensorflow as tf
- from rknn.api import RKNN
- def load_rknn():
- # 创建RKNN对象
- rknn = RKNN()
- # 载入RKNN模型
- # print('-->loading rknn model')
- rknn.load_rknn('./out1/frozen_model.rknn')
- # print('loading rknn model done')
- # 初始化RKNN运行环境
- # print('--> Init runtime environment')
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('rknn prepare done\n')
- return rknn
- def load_pb():
- with tf.gfile.GFile('test.pb', "rb") as f: # 读取模型数据
- graph_def = tf.GraphDef()
- graph_def.ParseFromString(f.read()) # 得到模型中的计算图和数据
- with tf.Graph().as_default() as graph: # 这里的Graph()要有括号,不然会报TypeError
- tf.import_graph_def(graph_def, name = "") # 导入模型中的图到现在这个新的计算图中
- print("pb prepare done\n")
- return graph
- def rknn_infer(data,rknn):
- outputs = rknn.inference(inputs=[data],data_type="float32")
- return outputs
- def pb_infer(data,graph):
- x = graph.get_tensor_by_name("input_node:0") # 输入节点,RKNN转换模型时,input为"input_x"
- y = graph.get_tensor_by_name("logits:0")
- with tf.Session(graph = graph) as sess:
- yout= sess.run([y], feed_dict = {x: data})
- return yout
- def prepare_data():
- data = np.ones(1000,dtype=np.int32)
- data1 = np.reshape(data,(1,1000,1))
- print(data1.dtype)
- data2 = np.reshape(data,(1,1,1000,1))
- return data1,data2
- if __name__=="__main__":
- data1,data2 = prepare_data()
- rknn = load_rknn()
- graph = load_pb()
- out1 = rknn_infer(data1,rknn)
- out2 = pb_infer(data2,graph)
- # print(np.sum(out2[0],axis=-1))
- np.set_printoptions(suppress = True)
- print("the rknn output is :{}\n".format(out1))
- print("the pb output is :{}\n".format(out2))
以上三步,完成所有过程,返回结果,但是相差很大。
输出:
- the rknn output is :[array([[-0.00941467, 0.00408554, 0.00399399, -0.01940918, 0.00273895,
- 0.00243187, -0.01450348, 0.00104523, 0.00271606, 0.00832367,
- 0.00605774, 0.00070429]], dtype=float32)]
- the pb output is :[array([[-0.02569406, 0.01786735, 0.00751904, -0.02548516, -0.02158996,
- 0.01359443, -0.00974836, -0.01726884, 0.02188211, 0.02064164,
- -0.00169315, -0.00375702]], dtype=float32)]
代码是.txt改成py就可以运行
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|