初始化仓库

This commit is contained in:
haotian 2025-08-15 10:24:30 +08:00
commit 23afbdd1cf
12 changed files with 629 additions and 0 deletions

19
001导出onnx模型_0.py Normal file
View File

@ -0,0 +1,19 @@
from ultralytics import YOLO
'''
yolo官方导出的onnx模型转换为rknn模型后并不能使用.
'''
# 加载模型
model = YOLO("/home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt")
# 导出 ONNX关键参数
model.export(
format="onnx",
imgsz=(640, 640), # 固定输入尺寸
opset=12, # ONNX 算子版本 >=12
simplify=True, # 简化模型
dynamic=False, # 禁用动态轴
nms=False, # 移除输出端的 NMS 层RKNN 不支持)
)
# yolo导出的模型

19
001导出onnx模型_1.py Normal file
View File

@ -0,0 +1,19 @@
from ultralytics import YOLO
import torch
# 加载模型时禁用数据验证
model = YOLO("/home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt", task="detect")
# 手动设置模型为推理模式
model.model.eval()
# 导出 ONNX
dummy_input = torch.randn(1, 3, 640, 640)
torch.onnx.export(
model.model, # 使用 model.model 访问底层 PyTorch 模型
dummy_input,
"yolov8m.onnx",
input_names=["input"],
output_names=["output"],
opset_version=11,
)

12
002验证ONNX模型.py Normal file
View File

@ -0,0 +1,12 @@
import onnxruntime as ort
import numpy as np
sess = ort.InferenceSession("/home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.onnx")
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
# 模拟输入BCHW 格式)
input_data = np.random.randint(0, 255, (1, 3, 640, 640), dtype=np.uint8)
outputs = sess.run([output_name], {input_name: input_data.astype(np.float32)})
print("输出形状:", outputs[0].shape) # 应为 [1, 84, 8400]84=80类+4坐标

58
003ONNX转RKNN.py Normal file
View File

@ -0,0 +1,58 @@
from rknn.api import RKNN
import cv2
import numpy as np
# 初始化 RKNN
rknn = RKNN()
# 配置参数(关键!)
rknn.config(
target_platform="rk3588", # 根据实际芯片型号修改
mean_values=[[0, 0, 0]], # YOLOv8 输入为 0-255无需归一化
std_values=[[255, 255, 255]], # 输入数据除以 255即 0-1 范围)
# quant_img_RGB2BGR=True,
optimization_level=3, # 最高优化级别
)
# 加载 ONNX
ret = rknn.load_onnx(model="/home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.onnx")
assert ret == 0, "加载 ONNX 失败!"
# 转换模型
ret = rknn.build(
do_quantization=False, # 启用量化
# dataset="dataset.txt", # 校准数据路径
)
assert ret == 0, "转换 RKNN 失败!"
# 导出 RKNN
ret = rknn.export_rknn("/home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.rknn")
assert ret == 0, "导出 RKNN 失败!"
# Set inputs
img = cv2.imread('/home/admin-root/haotian/rk3588/pytorch模型转rknn/images/bus.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img.resize((3, 640, 640))
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=['nchw'])
np.save('./tflite_mobilenet_v1_0.npy', outputs[0])
print(len(outputs))
print('done')
rknn.release()

58
004验证RKNN模型.py Normal file
View File

@ -0,0 +1,58 @@
from rknn.api import RKNN
import cv2
import numpy as np
# Create RKNN object
rknn = RKNN(verbose=True)
# Pre-process config
print('--> Config model')
rknn.config(mean_values=[128, 128, 128], std_values=[128, 128, 128], target_platform='rk3566')
print('done')
# Load model (from https://www.tensorflow.org/lite/guide/hosted_models?hl=zh-cn)
print('--> Loading model')
ret = rknn.load_tflite(model='mobilenet_v1_1.0_224.tflite')
if ret != 0:
print('Load model failed!')
exit(ret)
print('done')
# Build model
print('--> Building model')
ret = rknn.build(do_quantization=True, dataset='./dataset.txt')
if ret != 0:
print('Build model failed!')
exit(ret)
print('done')
# Export rknn model
print('--> Export rknn model')
ret = rknn.export_rknn('./mobilenet_v1.rknn')
if ret != 0:
print('Export rknn model failed!')
exit(ret)
print('done')
# Set inputs
img = cv2.imread('./dog_224x224.jpg')
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'])
np.save('./tflite_mobilenet_v1_0.npy', outputs[0])
show_outputs(outputs)
print('done')
rknn.release()

47
005推理.py Normal file
View File

@ -0,0 +1,47 @@
import cv2
import numpy as np
from rknnlite import RKNNLite
# 初始化 RKNN
rknn = RKNNLite()
rknn.load_rknn("yolov8n.rknn")
rknn.init_runtime(core_mask=RKNNLite.NPU_CORE_0)
def preprocess(image):
# 调整尺寸并转换为 BCHW 格式
img = cv2.resize(image, (640, 640))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR 转 RGB根据模型配置
img = img.transpose(2, 0, 1)[np.newaxis, ...].astype(np.uint8) # HWC -> BCHW
return img
# 加载测试图像
img = cv2.imread("test.jpg")
input_data = preprocess(img)
# 推理
outputs = rknn.inference(inputs=[input_data])[0] # 输出形状 [1, 84, 8400]
# 后处理(示例:提取检测框)
def postprocess(output, conf_thresh=0.5):
# 输出格式: [batch, 84, 8400]
# 84 = 4坐标 + 80类别
boxes = []
for i in range(output.shape[2]):
scores = output[0, 4:84, i]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_thresh:
cx, cy, w, h = output[0, :4, i]
# 转换为 x1,y1,x2,y2 格式
x1 = int((cx - w/2) * 640)
y1 = int((cy - h/2) * 640)
x2 = int((cx + w/2) * 640)
y2 = int((cy + h/2) * 640)
boxes.append([x1, y1, x2, y2, confidence, class_id])
return boxes
# 执行后处理
detections = postprocess(outputs)
print("检测结果:", detections)
rknn.release()

BIN
models/yolov8m.onnx Normal file

Binary file not shown.

104
runs/detect/train/args.yaml Normal file
View File

@ -0,0 +1,104 @@
task: detect
mode: train
model: /home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt
data: coco.yaml
epochs: 100
time: null
patience: 100
batch: 16
imgsz: 640
save: true
save_period: -1
cache: false
device: null
workers: 8
project: null
name: train
exist_ok: false
pretrained: true
optimizer: auto
verbose: true
seed: 0
deterministic: true
single_cls: false
rect: false
cos_lr: false
close_mosaic: 10
resume: false
amp: true
fraction: 1.0
profile: false
freeze: null
multi_scale: false
overlap_mask: true
mask_ratio: 4
dropout: 0.0
val: true
split: val
save_json: false
conf: null
iou: 0.7
max_det: 300
half: false
dnn: false
plots: true
source: null
vid_stride: 1
stream_buffer: false
visualize: false
augment: false
agnostic_nms: false
classes: null
retina_masks: false
embed: null
show: false
save_frames: false
save_txt: false
save_conf: false
save_crop: false
show_labels: true
show_conf: true
show_boxes: true
line_width: null
format: torchscript
keras: false
optimize: false
int8: false
dynamic: false
simplify: true
opset: null
workspace: null
nms: false
lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 7.5
cls: 0.5
dfl: 1.5
pose: 12.0
kobj: 1.0
nbs: 64
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
bgr: 0.0
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0
copy_paste_mode: flip
auto_augment: randaugment
erasing: 0.4
cfg: null
tracker: botsort.yaml
save_dir: runs/detect/train

View File

@ -0,0 +1,104 @@
task: detect
mode: train
model: /home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt
data: coco.yaml
epochs: 100
time: null
patience: 100
batch: 16
imgsz: 640
save: true
save_period: -1
cache: false
device: null
workers: 8
project: null
name: train2
exist_ok: false
pretrained: true
optimizer: auto
verbose: true
seed: 0
deterministic: true
single_cls: false
rect: false
cos_lr: false
close_mosaic: 10
resume: false
amp: true
fraction: 1.0
profile: false
freeze: null
multi_scale: false
overlap_mask: true
mask_ratio: 4
dropout: 0.0
val: true
split: val
save_json: false
conf: null
iou: 0.7
max_det: 300
half: false
dnn: false
plots: true
source: null
vid_stride: 1
stream_buffer: false
visualize: false
augment: false
agnostic_nms: false
classes: null
retina_masks: false
embed: null
show: false
save_frames: false
save_txt: false
save_conf: false
save_crop: false
show_labels: true
show_conf: true
show_boxes: true
line_width: null
format: torchscript
keras: false
optimize: false
int8: false
dynamic: false
simplify: true
opset: null
workspace: null
nms: false
lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 7.5
cls: 0.5
dfl: 1.5
pose: 12.0
kobj: 1.0
nbs: 64
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
bgr: 0.0
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0
copy_paste_mode: flip
auto_augment: randaugment
erasing: 0.4
cfg: null
tracker: botsort.yaml
save_dir: runs/detect/train2

View File

@ -0,0 +1,104 @@
task: detect
mode: train
model: /home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt
data: coco.yaml
epochs: 100
time: null
patience: 100
batch: 16
imgsz: 640
save: true
save_period: -1
cache: false
device: null
workers: 8
project: null
name: train3
exist_ok: false
pretrained: true
optimizer: auto
verbose: true
seed: 0
deterministic: true
single_cls: false
rect: false
cos_lr: false
close_mosaic: 10
resume: false
amp: true
fraction: 1.0
profile: false
freeze: null
multi_scale: false
overlap_mask: true
mask_ratio: 4
dropout: 0.0
val: true
split: val
save_json: false
conf: null
iou: 0.7
max_det: 300
half: false
dnn: false
plots: true
source: null
vid_stride: 1
stream_buffer: false
visualize: false
augment: false
agnostic_nms: false
classes: null
retina_masks: false
embed: null
show: false
save_frames: false
save_txt: false
save_conf: false
save_crop: false
show_labels: true
show_conf: true
show_boxes: true
line_width: null
format: torchscript
keras: false
optimize: false
int8: false
dynamic: false
simplify: true
opset: null
workspace: null
nms: false
lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 7.5
cls: 0.5
dfl: 1.5
pose: 12.0
kobj: 1.0
nbs: 64
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
bgr: 0.0
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0
copy_paste_mode: flip
auto_augment: randaugment
erasing: 0.4
cfg: null
tracker: botsort.yaml
save_dir: runs/detect/train3

View File

@ -0,0 +1,104 @@
task: detect
mode: train
model: /home/admin-root/haotian/rk3588/pytorch模型转rknn/models/yolov8m.pt
data: coco.yaml
epochs: 100
time: null
patience: 100
batch: 16
imgsz: 640
save: true
save_period: -1
cache: false
device: null
workers: 8
project: null
name: train4
exist_ok: false
pretrained: true
optimizer: auto
verbose: true
seed: 0
deterministic: true
single_cls: false
rect: false
cos_lr: false
close_mosaic: 10
resume: false
amp: true
fraction: 1.0
profile: false
freeze: null
multi_scale: false
overlap_mask: true
mask_ratio: 4
dropout: 0.0
val: true
split: val
save_json: false
conf: null
iou: 0.7
max_det: 300
half: false
dnn: false
plots: true
source: null
vid_stride: 1
stream_buffer: false
visualize: false
augment: false
agnostic_nms: false
classes: null
retina_masks: false
embed: null
show: false
save_frames: false
save_txt: false
save_conf: false
save_crop: false
show_labels: true
show_conf: true
show_boxes: true
line_width: null
format: torchscript
keras: false
optimize: false
int8: false
dynamic: false
simplify: true
opset: null
workspace: null
nms: false
lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 7.5
cls: 0.5
dfl: 1.5
pose: 12.0
kobj: 1.0
nbs: 64
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
bgr: 0.0
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0
copy_paste_mode: flip
auto_augment: randaugment
erasing: 0.4
cfg: null
tracker: botsort.yaml
save_dir: runs/detect/train4

BIN
tflite_mobilenet_v1_0.npy Normal file

Binary file not shown.