重现步骤
参考k230关于yolo的pt模型转kmodel案例链接:
https://developer.canaan-creative.com/k230_canmv/zh/dev/zh/example/ai/YOLO%E5%A4%A7%E4%BD%9C%E6%88%98.html
将标准的yolov5s.pt模型转为yolov5s.kmodel模型,然后使用以上链接的案例运行如下代码:
from libs.YOLO import YOLOv5
import os,sys,gc
import ulab.numpy as np
import image
# 从本地读入图片,并实现HWC转CHW
def read_img(img_path):
img_data = image.Image(img_path)
img_data_rgb888=img_data.to_rgb888()
img_hwc=img_data_rgb888.to_numpy_ref()
shape=img_hwc.shape
img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2]))
img_tmp_trans = img_tmp.transpose()
img_res=img_tmp_trans.copy()
img_return=img_res.reshape((shape[2],shape[0],shape[1]))
return img_return,img_data_rgb888
if __name__=="__main__":
# 测试图位于test_yolov5\test_images下
img_path="/data/test_apple.jpg"
kmodel_path="/data/yolov5s.kmodel"
labels = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"]
confidence_threshold = 0.5
model_input_size=[224,224]
img,img_ori=read_img(img_path)
rgb888p_size=[img.shape[2],img.shape[1]]
# 初始化YOLOv5实例
yolo=YOLOv5(task_type="classify",mode="image",kmodel_path=kmodel_path,labels=labels,rgb888p_size=rgb888p_size,model_input_size=model_input_size,conf_thresh=confidence_threshold,debug_mode=0)
yolo.config_preprocess()
try:
res=yolo.run(img)
yolo.draw_result(res,img_ori)
gc.collect()
except Exception as e:
sys.print_exception(e)
finally:
yolo.deinit()
期待结果和实际结果
YOLOV.py自带源码报错:IndexError: index is out of bounds
软硬件版本信息
CanMV-K230_LCKFB_micropython_v1.2.2-0-g4b8cae1_nncase_v2.9.0.img.gz
错误日志
Traceback (most recent call last):
File "", line 31, in
File "/sdcard/libs/AIBase.py", line 74, in run
File "/sdcard/libs/YOLO.py", line 83, in postprocess
IndexError: index is out of bounds
MPY: soft reboot
CanMV v1.2.2(based on Micropython e00a144) on 2024-12-18; k230_canmv_lckfb with K230
尝试解决过程
尝试设置的图片大小640x640导出onnx,kmodel或使用224x224大小导出onnx在转kmodel,并且修改MicroPython代码model_input_size=[224,224]匹配的图片尺寸,测试依然无效
补充材料
转换的模型yolov5s.kmodel:
https://pan.baidu.com/s/1w9Ch1YI37Srbk0mYmWX_PA?pwd=b3j6