- model = Darknet(opt.model_def, img_size=opt.img_size)
- model.load_state_dict(torch.load("./checkpoints/custom_yolov3_ckpt_900.pth", map_location=torch.device('cpu')))
- model.eval()
- trace_model = torch.jit.trace(model, torch.Tensor(1,3,448,448))
- trace_model.save('./yoloV3.pt')
- np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
- model_path = './yoloV3.pt'
- input_size_list = [[3, 448, 448]]
- # Create RKNN object
- rknn = RKNN()
- print('--> config model')
- rknn.config(channel_mean_value='0. 0. 0. 255.', reorder_channel='0 1 2')
- print('done')
- # Load pytorch model
- print('--> Loading model')
- ret = rknn.load_pytorch(model=model_path, input_size_list=input_size_list)
- if ret != 0:
- print('Load pytorch model failed!')
- exit(ret)
- print('done')
- # Build model
- print('--> Building model')
- # 不量化,结果基本一致
- ret = rknn.build(do_quantization=False, dataset='./valid.txt')
- if ret != 0:
- print('Build pytorch failed!')
- exit(ret)
- print('done')
- # Export rknn model
- print('--> Export RKNN model')
- ret = rknn.export_rknn('./yoloV3.rknn')
- if ret != 0:
- print('Export yoloV3.rknn failed!')
- exit(ret)
- print('done')
- ret = rknn.load_rknn('./yoloV3.rknn')
- # Set inputs
- img = cv2.imread('./data/custom/images/train.jpg')
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- # 归一化
- img = np.array(img, dtype=np.float32)
- img = np.array((img-img.min()) / img.ptp())
- # print("img: ", img)
- # init runtime environment
- print('--> Init runtime environment')
- ret = rknn.init_runtime()
- if ret != 0:
- print('Init runtime environment failed')
- exit(ret)
- print('done')
- # Inference
- print('--> Running model')
- outputs = rknn.inference(inputs=[img])
- print('done')
- # # perf
- print('--> Begin evaluate model performance')
- perf_results = rknn.eval_perf(inputs=[img])
- print('done')
- rknn.release()
复制代码
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |