- from rknn.api import RKNN
- INPUT_SIZE = 64
- if __name__ == '__main__':
- # 创建RKNN执行对象
- rknn = RKNN()
- #初始化RKNN运行环境,指定host=’rk3399pro’,后续的操作将会运行在NPU上
- print('--> Init runtime environment')
- ret = rknn.init_runtime(host='rk3399pro')
- 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 255', 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='../digital_gesture_recognition/model_2500/digital_gesture.pb',
- inputs=['input_x'],
- outputs=['probability'],
- input_size_list=[[INPUT_SIZE, INPUT_SIZE, 3]])
- print('done')
- # 创建解析pb模型
- # do_quantization=False指定不进行量化
- # 量化会减小模型的体积和提升运算速度,但是会有精度的丢失
- print('--> Building model')
- rknn.build(do_quantization=False)
- print('done')
- # 导出保存rknn模型文件
- rknn.export_rknn('./digital_gesture.rknn')
- # Release RKNN Context
- rknn.release()
复制代码
- import numpy as np
- from PIL import Image
- from rknn.api import RKNN
- # 解析模型的输出,获得概率最大的手势和对应的概率
- def get_predict(probability):
- data = probability[0][0]
- data = data.tolist()
- max_prob = max(data)
- return data.index(max_prob), max_prob;
- def load_model():
- # 创建RKNN对象
- rknn = RKNN()
- # 载入RKNN模型
- print('-->loading model')
- rknn.load_rknn('./digital_gesture.rknn')
- print('loading model done')
- # 初始化RKNN运行环境
- print('--> Init runtime environment')
- ret = rknn.init_runtime(host='rk3399pro')
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- return rknn
- def predict(rknn):
- im = Image.open("../picture/6_7.jpg") # 加载图片
- im = im.resize((64, 64),Image.ANTIALIAS) # 图像缩放到64x64
- mat = np.asarray(im.convert('RGB')) # 转换成RGB格式
- outputs = rknn.inference(inputs=[mat]) # 运行推理,得到推理结果
- pred, prob = get_predict(outputs) # 将推理结果转化为可视信息
- print(prob)
- print(pred)
- if __name__=="__main__":
- rknn = load_model()
- predict(rknn)
复制代码
- # tensorflow 模型加载,加载的是pb模型,并且获得pb对应的graph进行使用。
- def load_model():
- with tf.gfile.GFile('./digital_gesture_recognition/model_2500/digital_gesture.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="") # 导入模型中的图到现在这个新的计算图中
- return graph
复制代码
- #RKNN模型加载,需要加载的是pb转换后的rknn模型,并且设置RKNN运行环境即可使用
- def load_model():
- # 创建RKNN对象
- rknn = RKNN()
- # 载入RKNN模型
- print('-->loading model')
- rknn.load_rknn('./digital_gesture.rknn')
- print('loading model done')
- # 初始化RKNN运行环境
- print('--> Init runtime environment')
- ret = rknn.init_runtime(host='rk3399pro')
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- return rknn
复制代码
- # 对于TensorFlow,输入图像数据必须为float32类型,且归一化的(1,64,64,3)形状的矩阵
- im = Image.open("dataset//new_pic//test6.jpg")
- im = im.resize((64, 64),Image.ANTIALIAS) # 图像缩放到64x64
- mat = np.asarray(im.convert('RGB')) # 转成RGB格式,此时数据类型为UINT8
- mat = mat.reshape(1,64,64,3) # TensorFlow需要的是(1,64,64,3)的float32类型数据
- mat = mat / 255. # 数据归一化
复制代码
- # 对于RKNN,图像的归一化和转float32,已经在模型转换时设置好,推理时会自动完成
- im = Image.open("dataset//new_pic//test6.jpg")
- im = im.resize((64, 64),Image.ANTIALIAS) # 图像缩放到64x64
- mat = np.asarray(im.convert('RGB')) # 转成RGB格式,此时数据类型为UINT8
复制代码
- # TensorFlow:需要输入(1,64,64,3)且进行归一后之后的float32类型矩阵
- mat = img_to_mat("./picture/0_10.jpg")
- x = graph.get_tensor_by_name("input_x:0") # 输入节点,RKNN转换模型时,input为"input_x"
- outlayer = graph.get_tensor_by_name("outlayer:0") # 输出节点,RKNN转换模型时,output为"outlayer"
- prob = graph.get_tensor_by_name("probability:0") # 输出节点,RKNN转换模型时,output为"probability"
- predict = graph.get_tensor_by_name("predict:0") # 输出节点,RKNN转换模型时,output为"predict"
复制代码
- # RKNN:需要的输入为(64, 64, 3)的UINT8类型矩阵,归一化和转float在模型转换时已定义好
- # 转换模型时,inputs和outputs列表可以tensor名得到,目前我们output只关心'probability',因此,outputs只设置成 outputs=['probability']
- rknn.load_tensorflow(tf_pb='../digital_gesture_recognition/model_2500/digital_gesture.pb',
- inputs=['input_x'],
- outputs=['probability'],
- input_size_list=[[INPUT_SIZE, INPUT_SIZE, 3]])
复制代码
- # Tensorflow使用run进行推理
- with tf.Session(graph=graph) as sess:
- np.set_printoptions(suppress=True)
- out, prob, pred = sess.run([outlayer, prob,predict],feed_dict={x:mat})
- print(out)
- print(prob)
- print(pred)
复制代码
- # RKNN使用inference进行推理
- outputs = rknn.inference(inputs=[image])
复制代码
我是流氓我怕谁 发表于 2019-1-11 15:39
adsfafdasdfFDSFDaf
user_one 发表于 2019-1-16 09:23
很棒很棒很棒很棒很棒很棒很棒很棒很棒很棒
someone9388 发表于 2019-2-21 12:01
你好 遇到一个错误, 没有HOST 参数名:
[toybrick@localhost rknn_test]$ python3 rknn_transfer.py
/usr ...
someone9388 发表于 2019-2-21 12:06
安装的是
rknn_toolkit-0.9.8-cp36-cp36m-linux_aarch64.whl
someone9388 发表于 2019-2-21 12:01
你好 遇到一个错误, 没有HOST 参数名:
[toybrick@localhost rknn_test]$ python3 rknn_transfer.py
/usr ...
linuxsky 发表于 2019-2-16 09:06
请问下这个能做到 几帧呢? 整个任务处理时间
someone9388 发表于 2019-2-21 12:01
你好 遇到一个错误, 没有HOST 参数名:
[toybrick@localhost rknn_test]$ python3 rknn_transfer.py
/usr ...
15875649641 发表于 2019-5-28 13:45
您好 请问能提供
tensorflow-1.8.0-cp36-cp36m-linux_aarch64.whl
rknn-toolkit-0.9.7-cp36-cp36m-linux_aa ...
troy 发表于 2019-5-28 17:26
wiki上面有安装流程,而且最新发布的固件在帖子上有发布,现在最新的已经是rknn-toolkit 1.0.0了 ...
gwill 发表于 2019-6-14 22:09
请问有百度云的下载链接吗?github下载一直失败!
someone9388 发表于 2019-2-21 12:01
你好 遇到一个错误, 没有HOST 参数名:
[toybrick@localhost rknn_test]$ python3 rknn_transfer.py
/usr ...
瓜瓜瓜 发表于 2019-7-12 15:20
运行python3 rknn_transfer.py 报错,请问怎么解决?
[root@localhost rknn_test]# python3 rknn_transfer ...
瓜瓜瓜 发表于 2019-7-12 17:29
请问跑手势识别的demo的时候,预测标签一直是0,然后概率一直是-65504.0,请问是怎么回事? ...
1074292224 发表于 2019-6-28 11:36
运行rknn_transfer.py时报错:
--> Init runtime environment
E Model is not loaded yet, this interface ...
瓜瓜瓜 发表于 2019-7-12 15:20
运行python3 rknn_transfer.py 报错,请问怎么解决?
[root@localhost rknn_test]# python3 rknn_transfer ...
hisping 发表于 2019-8-1 15:54
rknn_test.zip已更新,可以在rknn-toolkit 1.0.0运行
lyc202019 发表于 2019-8-13 11:12
no module named 'rknn'.
这个错误怎么解决?
troy 发表于 2019-8-13 14:43
按照wiki,安装下开发环境
lyc202019 发表于 2019-8-13 16:24
Could not connect to any X display
这个问题怎么解决?
troy 发表于 2019-8-13 17:15
必须要在界面下才能显示,命令行登录是不能显示的,就会报错。
- Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
- E Catch exception when building RKNN model!
- E Traceback (most recent call last):
- E File "rknn/api/rknn_base.py", line 737, in rknn.api.rknn_base.RKNNBase.build
- E File "rknn/api/rknn_base.py", line 1644, in rknn.api.rknn_base.RKNNBase._quantize2
- E File "rknn/base/RKNNlib/app/medusa/quantization.py", line 105, in rknn.base.RKNNlib.app.medusa.quantization.Quantization.run
- E File "rknn/base/RKNNlib/app/medusa/quantization.py", line 44, in rknn.base.RKNNlib.app.medusa.quantization.Quantization._run_quantization
- E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 129, in rknn.base.RKNNlib.app.medusa.workspace.Workspace.run
- E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 99, in rknn.base.RKNNlib.app.medusa.workspace.Workspace._setup_graph
- E File "rknn/base/RKNNlib/app/medusa/workspace.py", line 100, in rknn.base.RKNNlib.app.medusa.workspace.Workspace._setup_graph
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 274, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 278, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 305, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 331, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
- E File "rknn/base/RKNNlib/RKNNnetbuilder.py", line 336, in rknn.base.RKNNlib.RKNNnetbuilder.RKNNNetBuilder.build_layer
- E File "rknn/base/RKNNlib/layer/RKNNlayer.py", line 287, in rknn.base.RKNNlib.layer.RKNNlayer.RKNNLayer.compute_tensor
- E File "rknn/base/RKNNlib/layer/dropout.py", line 20, in rknn.base.RKNNlib.layer.dropout.Dropout.compute_out_tensor
- E File "/home/toybrick/.local/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
- E return func(*args, **kwargs)
- E File "/home/toybrick/.local/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 4170, in dropout
- E return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)
- E File "/home/toybrick/.local/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 4219, in dropout_v2
- E "range [0, 1), got %g" % rate)
- E ValueError: rate must be a scalar tensor or a float in the range [0, 1), got 1
- --> Building model
复制代码
- from rknn.api import RKNN
- INPUT_SIZE = 64
- if __name__ == '__main__':
- model_file_path = '../digital_gesture_recognition/model_2500/digital_gesture.pb'
-
- rknn = RKNN()
-
- print("--> Init rknn enviroment")
-
- # ret = rknn.init_runtime()
-
- rknn.load_tensorflow(tf_pb=model_file_path,
- inputs=['input_x'],
- outputs=['probability'],
- input_size_list=[[INPUT_SIZE, INPUT_SIZE, 3]])
- print("--> Model Loaded")
-
- rknn.config(channel_mean_value='0 0 0 255', reorder_channel='0 1 2')
- print("--> Init config")
-
- rknn.build(do_quantization=True, dataset='./dataset_64x64.txt')
- # rknn.build(do_quantization=False)
- print('--> Building model')
-
- rknn.export_rknn('./digital_gesture.rknn')
- rknn.release()
-
- print('Done')
复制代码
ddcat1991 发表于 2020-2-18 21:42
你好,我在设置 do_quantization=True 转换上述手势识别的pb模型的时候, 产生如下错误。 (如果使用do_quant ...
jackson 发表于 2021-5-13 20:06
我运行了这个demo,错误率好高啊
hiCrystal 发表于 2021-7-30 14:54
设备是rv1126
- sudo v4l2-ctl -d /devideo19 --set-fmt-video=width=1920,height=1080,pixelformat=NV12 --stream-mmap=3 --stream-to=/tmp/NV12_1920_1080.out --stream-count=3 --stream-skip=10 --stream-poll
复制代码
troy 发表于 2021-8-2 10:00
可以尝试一下v4l2命令对video节点进行抓图,当前TB-rv1126D开发板,video0节点是typec投屏输入,video19 ...
hiCrystal 发表于 2021-8-2 11:33
这个命令在哪执行呀?命令的详细使用说明有文档吗?
troy 发表于 2021-8-2 11:44
这个命令在板子上执行,v4l2 --help可以获取帮助信息,或者百度可以查找到。这个是开源的工具,不是我们 ...
zx123 发表于 2021-8-11 18:07
请问下../digital_gesture_recognition/model_2500/digital_gesture.pb这个数据模型在哪里可以获得 ...
troy 发表于 2021-8-11 19:33
可以自己训练。
troy 发表于 2021-5-14 08:49
这个例程主要是实验性质的,模型比较简单,错误率搞是正常的。主要用于演示整个数据采集,数据处理,数据 ...
hiCrystal 发表于 2021-7-30 14:54
设备是rv1126
zx123 发表于 2021-8-13 21:54
请问下你有这个训练的数据模型吗
hiCrystal 发表于 2021-8-2 12:43
好的,谢谢!我先试一下。
troy 发表于 2021-8-2 11:44
这个命令在板子上执行,v4l2 --help可以获取帮助信息,或者百度可以查找到。这个是开源的工具,不是我们 ...
AI_XIEQF 发表于 2021-10-13 22:43
请问rv1126使用usb摄像头使用什么命令实现呢?我没有mipi的摄像头
ai开发者 发表于 2021-11-6 10:26
E Traceback (most recent call last):
E File "rknn\base\RKNNlib\converter\convert_tf.py", line 1179 ...
yyyy 发表于 2022-2-11 16:56
我运行为什么卡住了,一直显示Done但是就是没有摄像头的画面,是配套的usb摄像头 ...
luffy 发表于 2022-6-2 13:33
RK3399上运行,摄像头打不开怎么回事啊
yyyy 发表于 2022-2-11 16:56
我运行为什么卡住了,一直显示Done但是就是没有摄像头的画面,是配套的usb摄像头 ...
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |