如何测试kmodel在kpu上的推理帧率

Viewed 186

这里给出一段代码,测试视觉任务下,kpu对某一模型的推理帧率。先读入一帧图片,将图片resize成模型输入的分辨率,然后转换成tensor设置给模型,然后使用kpu.run()进行推理,测试推理帧率。

import os
import ujson
import nncase_runtime as nn
import ulab.numpy as np
import image
import sys
import gc
import time

# 加载kmodel
kmodel_path="/sdcard/best.kmodel"
input_w=640
input_h=640

# 初始化kpu
kpu=nn.kpu()
kpu.load_kmodel(kmodel_path)

# 创建一个空的输入tensor,并将其设置为模型的第0个输入
input_data = np.ones((1,3,input_h,input_w),dtype=np.uint8)
kpu_input_tensor = nn.from_numpy(input_data)
kpu.set_input_tensor(0, kpu_input_tensor)

# 读取一张图片,并将其transpose成chw数据
img_path="/data/test.jpg"
img_data = image.Image(img_path).to_rgb888()
img_hwc=img_data.to_numpy_ref()
shape=img_hwc.shape
img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2]))
img_tmp_trans = img_tmp.transpose().copy()
img_chw=img_tmp_trans.reshape((shape[2],shape[0],shape[1]))

# 初始化ai2d预处理,并配置ai2d resize预处理,预处理输入分辨率为图片分辨率,输出分辨率模型输入的需求分辨率,实现image->preprocess->model的过程
ai2d=nn.ai2d()
ai2d.set_dtype(nn.ai2d_format.NCHW_FMT, nn.ai2d_format.NCHW_FMT, np.uint8, np.uint8)
ai2d.set_resize_param(True,nn.interp_method.tf_bilinear, nn.interp_mode.half_pixel)
ai2d_builder = ai2d.build([1,3,img_chw.shape[1],img_chw.shape[2]], [1,3,input_h,input_w])
ai2d_input_tensor = nn.from_numpy(img_chw)
# 运行ai2d,将输出直接送到kpu_input_tensor
ai2d_builder.run(ai2d_input_tensor, kpu_input_tensor)

# 测试推理帧率
fps = time.clock()
while True:
    os.exitpoint()
    try:
        fps.tick()
        kpu.run()
        print(fps.fps())
    except KeyboardInterrupt as e:
        print("user stop: ", e)
        break
    except BaseException as e:
        print(f"Exception {e}")
        break

这个例子可以帮助用户测试yolo不同输入分辨率下模型的推理性能。

1 Answers