|
rknn不仅支持单张图片推理,还支持多图同时进行推理,以mnist的模型为例,这里把batch_size设为3:
- def common_transfer(pb_name,export_name):
- ret = 0
- #看具体log 传入verbose=True
- rknn = RKNN()
- #灰度图无需此步操作
- # rknn.config(channel_mean_value='', reorder_channel='')
- print('--> Loading model')
- ret = rknn.load_tensorflow(
- tf_pb='./mnist_frozen_graph.pb',
- inputs=['x'],
- outputs=['y_conv'],
- input_size_list=[[28,28,1]])
- if ret != 0:
- print('load_tensorflow error')
- rknn.release()
- return ret
- print('done')
- print('--> Building model')
- rknn.build(rknn_batch_size=3,do_quantization=False)
- print('done')
- # 导出保存rknn模型文件
- rknn.export_rknn('./mnist.rknn')
- # Release RKNN Context
- rknn.release()
- return ret
然后进行推理,推理的结果会把三张图片的结果合并在一个list中,需要我们自己将其分割开:
- def predict(rknn,length):
- acc_count = 0
- mat = []
- for i in range(length):
- im = mnist.test.images[i]
- # im = Image.open("../MNIST_data/mnist_test/test_%d.jpg"%i) # 加载图片
- # im = im.resize((28,28),Image.ANTIALIAS)
- # im = np.asarray(im)
- im = im.reshape(28,28,1)
- mat.append(im)
- if len(mat) == 3:
- mat = np.array(mat).reshape(3,28,28,1)
- outputs = rknn.inference(inputs=[mat])
- outputs1 = outputs[0][0][0:10]
- outputs2 = outputs[0][0][10:20]
- outputs3 = outputs[0][0][20:]
- outputs = [[[outputs1]],[[outputs2]],[[outputs3]]]
- j = 2
- for output in outputs:
- pred, prob = get_predict(output)
- if i == 2:
- print(output)
- print(prob)
- print(pred)
- if list(mnist.test.labels[i-j]).index(1) == pred:
- acc_count += 1
- j -= 1
- mat = []
- else:
- pass
- if i ==100 or i ==500 or i ==1000 or i ==10000:
- result = float(acc_count)/i
- print('result%d:'%i,result)
- result = float(acc_count)/length
- print('result:',result)
最终其结果和单张推理的结果是相同的.
|
|