728x90
출처
rknn-toolkit2 소스다운로드
orangepi@orangepi-desktop:~$ cd Llama/
orangepi@orangepi-desktop:~/Llama$ source .venv/bin/activate
(.venv) orangepi@orangepi-desktop:~/Llama$ git clone https://github.com/rockchip-linux/rknn-toolkit2.git
(.venv) orangepi@orangepi-desktop:~/Llama$ cd rknn-toolkit2/rknn-toolkit2/examples/darknet/yolov3_416x416/
onnx 파일 복사
(.venv) orangepi@orangepi-desktop:~/Llama/rknn-toolkit2/rknn-toolkit2/examples/darknet/yolov3_416x416$ cp ~/Llama/onnx_tflite_yolov3/weights/export.onnx yolov3.onnx
test3588.py 소스
import numpy as np
import cv2
from rknn.api import RKNN
from yolov3_utils import yolov3_post_process, draw, download_yolov3_weight
GRID0 = 13
GRID1 = 26
GRID2 = 52
LISTSIZE = 85
SPAN = 3
if __name__ == '__main__':
# Model from https://pjreddie.com/darknet/yolo/
# The yolov3.cfg changed the width & height from 608 to 416 on the basis of
# https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg
MODEL_PATH = './yolov3.cfg'
WEIGHT_PATH = './yolov3.weights'
RKNN_MODEL_PATH = './yolov3_416.rknn'
im_file = './dog_bike_car_416x416.jpg'
DATASET = './dataset.txt'
# Download yolov3.weight
# download_yolov3_weight(WEIGHT_PATH)
# Create RKNN object
rknn = RKNN(verbose=True)
# Pre-process config
print('--> Config model')
rknn.config(mean_values=[0, 0, 0], std_values=[255, 255, 255], target_platform='rk3588')
print('done')
# Load model
print('--> Loading model')
# ret = rknn.load_darknet(model=MODEL_PATH, weight=WEIGHT_PATH)
ret = rknn.load_onnx(model='./yolov3.onnx')
if ret != 0:
print('Load model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=True, dataset=DATASET)
if ret != 0:
print('Build model failed!')
exit(ret)
print('done')
# Export rknn model
print('--> Export rknn model')
ret = rknn.export_rknn(RKNN_MODEL_PATH)
if ret != 0:
print('Export rknn model failed!')
exit(ret)
print('done')
# Set inputs
img = cv2.imread(im_file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.expand_dims(img, 0)
# 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], data_format='nhwc')
print('done')
print('Number of outputs:', len(outputs))
for i, out in enumerate(outputs):
print(f' output[{i}] shape:', np.array(out).shape)
# YOLOv3 only needs 3 detection heads -> outputs[1], outputs[2], outputs[3]
head0 = np.array(outputs[1]) # (1,3,13,13,85)
head1 = np.array(outputs[2]) # (1,3,26,26,85)
head2 = np.array(outputs[3]) # (1,3,52,52,85)
# Remove batch dimension
head0 = head0[0] # (3,13,13,85)
head1 = head1[0] # (3,26,26,85)
head2 = head2[0] # (3,52,52,85)
# Transpose to match yolov3_post_process format: (H,W,3,85)
input0 = np.transpose(head0, (1, 2, 0, 3))
input1 = np.transpose(head1, (1, 2, 0, 3))
input2 = np.transpose(head2, (1, 2, 0, 3))
input_data = [input0, input1, input2]
# Postprocess
boxes, classes, scores = yolov3_post_process(input_data)
# Draw results
image = cv2.imread(im_file)
if boxes is not None:
draw(image, boxes, scores, classes)
print("Save results to result.jpg!")
cv2.imwrite("result.jpg", image)
rknn.release()
실행
(.venv) orangepi@orangepi-desktop:~/Llama/rknn-toolkit2/rknn-toolkit2/examples/darknet/yolov3_416x416$ python test3588.py
I rknn-toolkit2 version: 2.3.2
--> Config model
done
--> Loading model
I Loading : 100%|███████████████████████████████████████████████| 150/150 [00:00<00:00, 3641.08it/s]
done
--> Building model
D base_optimize ...
D base_optimize done.
D
D fold_constant ...
D RKNN: [23:39:23.038] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
D RKNN: [23:39:23.038] Network Layer Information Table
D RKNN: [23:39:23.038] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
D RKNN: [23:39:23.038] ID OpType DataType Target InputShape OutputShape Cycles(DDR/NPU/Total) RW(KB) FullName
D RKNN: [23:39:23.038] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
D RKNN: [23:39:23.038] 0 InputOperator INT8 CPU \ (1,3,416,416) 0/0/0 0 InputOperator:input.1
D RKNN: [23:39:23.038] 1 ConvLeakyRelu INT8 NPU (1,3,416,416),(32,3,3,3),(32) (1,32,416,416) 256271/1557504/1557504 511 Conv:/module_list.0/Conv2d/Conv
D RKNN: [23:39:23.038] 2 ConvLeakyRelu INT8 NPU (1,32,416,416),(64,32,3,3),(64) (1,64,208,208) 351976/778752/778752 5426 Conv:/module_list.1/Conv2d/Conv
D RKNN: [23:39:23.038] 3 ConvLeakyRelu INT8 NPU (1,64,208,208),(32,64,1,1),(32) (1,32,208,208) 175685/173056/175685 2706 Conv:/module_list.2/Conv2d/Conv
D RKNN: [23:39:23.038] 4 ConvLeakyReluAdd INT8 NPU (1,32,208,208),(64,32,3,3),(64),... (1,64,208,208) 293447/778752/778752 4074 Conv:/module_list.3/Conv2d/Conv
D RKNN: [23:39:23.038] 5 ConvLeakyRelu INT8 NPU (1,64,208,208),(128,64,3,3),(128) (1,128,104,104) 178748/778752/778752 2777 Conv:/module_list.5/Conv2d/Conv
D RKNN: [23:39:23.038] 6 ConvLeakyRelu INT8 NPU (1,128,104,104),(64,128,1,1),(64) (1,64,104,104) 88162/86528/88162 1360 Conv:/module_list.6/Conv2d/Conv
D RKNN: [23:39:23.038] 7 ConvLeakyReluAdd INT8 NPU (1,64,104,104),(128,64,3,3),(128),... (1,128,104,104) 149484/778752/778752 2101 Conv:/module_list.7/Conv2d/Conv
D RKNN: [23:39:23.038] 8 ConvLeakyRelu INT8 NPU (1,128,104,104),(64,128,1,1),(64) (1,64,104,104) 88162/86528/88162 1360 Conv:/module_list.9/Conv2d/Conv
D RKNN: [23:39:23.038] 9 ConvLeakyReluAdd INT8 NPU (1,64,104,104),(128,64,3,3),(128),... (1,128,104,104) 149484/778752/778752 2101 Conv:/module_list.10/Conv2d/Conv
D RKNN: [23:39:23.038] 10 ConvLeakyRelu INT8 NPU (1,128,104,104),(256,128,3,3),(256) (1,256,52,52) 100349/778752/778752 1642 Conv:/module_list.12/Conv2d/Conv
D RKNN: [23:39:23.038] 11 ConvLeakyRelu INT8 NPU (1,256,52,52),(128,256,1,1),(128) (1,128,52,52) 45326/86528/86528 709 Conv:/module_list.13/Conv2d/Conv
D RKNN: [23:39:23.038] 12 ConvLeakyReluAdd INT8 NPU (1,128,52,52),(256,128,3,3),(256),... (1,256,52,52) 85716/778752/778752 1304 Conv:/module_list.14/Conv2d/Conv
D RKNN: [23:39:23.038] 13 ConvLeakyRelu INT8 NPU (1,256,52,52),(128,256,1,1),(128) (1,128,52,52) 45326/86528/86528 709 Conv:/module_list.16/Conv2d/Conv
D RKNN: [23:39:23.038] 14 ConvLeakyReluAdd INT8 NPU (1,128,52,52),(256,128,3,3),(256),... (1,256,52,52) 85716/778752/778752 1304 Conv:/module_list.17/Conv2d/Conv
D RKNN: [00:30:59.983] --------------------------------------------------------------------------------------------+---------------------------------
D RKNN: [00:30:59.985] ----------------------------------------
D RKNN: [00:30:59.985] Total Internal Memory Size: 30002.4KB
D RKNN: [00:30:59.985] Total Weight Memory Size: 171666KB
D RKNN: [00:30:59.985] ----------------------------------------
D RKNN: [00:30:59.985] <<<<<<<< end: rknn::RKNNMemStatisticsPass
I rknn building done.
done
--> Export rknn model
done
--> Init runtime environment
I Target is None, use simulator!
done
--> Running model
I GraphPreparing : 100%|████████████████████████████████████████| 220/220 [00:00<00:00, 3847.87it/s]
I SessionPreparing : 100%|███████████████████████████████████████| 220/220 [00:01<00:00, 188.20it/s]
done
Number of outputs: 4
output[0] shape: (1, 10647, 85)
output[1] shape: (1, 3, 13, 13, 85)
output[2] shape: (1, 3, 26, 26, 85)
output[3] shape: (1, 3, 52, 52, 85)
class score xmin, ymin, xmax, ymax
--------------------------------------------------
dog 0.998 [ 66, 166, 174, 391]
bicycle 0.991 [ 58, 89, 314, 314]
truck 0.950 [ 258, 64, 374, 119]
Save results to result.jpg!
실행결과

728x90