[深度学习]--分类问题的排查错误的流程

原因复现:
原生的.pt 好使, 转化后的 CoreML不好使, 分类有问题。

yolov8 格式的支持情况

                   Format     Argument           Suffix    CPU    GPU
0                 PyTorch            -              .pt   True   True
1             TorchScript  torchscript     .torchscript   True   True
2                    ONNX         onnx            .onnx   True   True
3                OpenVINO     openvino  _openvino_model   True  False
4                TensorRT       engine          .engine  False   True
5                  CoreML       coreml       .mlpackage   True  False
6   TensorFlow SavedModel  saved_model     _saved_model   True   True
7     TensorFlow GraphDef           pb              .pb   True   True
8         TensorFlow Lite       tflite          .tflite   True  False
9     TensorFlow Edge TPU      edgetpu  _edgetpu.tflite   True  False
10          TensorFlow.js         tfjs       _web_model   True  False
11           PaddlePaddle       paddle    _paddle_model   True   True
12                   NCNN         ncnn      _ncnn_model   True   True

这里可以看到CoreML 只支持cpu, 尼玛tflite也是只支持cpu的



def test_coreml():
    detect_weight = '/home/justin/Desktop/code/python_project/Jersey-Number/runs/detect/train64/weights/best.pt'
    model_detect = YOLO(detect_weight)
    results = model_detect(source="/home/justin/Desktop/code/python_project/Jersey-Number/zr_yz.MP4",stream=True,classes=[3])

    class_weight = '/home/justin/Desktop/code/python_project/Jersey-Number/runs/classify/train7/weights/best.mlpackage'
    class_weight = '/home/justin/Desktop/code/python_project/Jersey-Number/runs/classify/train7/weights/best.mlpackage'
    model_class = YOLO(class_weight)
    # 要使用的字体
    fontFace = cv2.FONT_HERSHEY_SIMPLEX
    fontScale = 3
    thickness = 1
    img_count = 0

    for result in results:
        img_count+=1
        if img_count == 6:
            a = 1
        boxes = result.boxes  # Boxes object for bounding box outputs
        for box in boxes:
            cls = box.cls.item()
            conf = box.conf.item()
            if conf > 0.5:
                x1,y1,x2,y2 = box.xyxy.tolist()[0]
                x1,y1,x2,y2 = int(x1),int(y1),int(x2),int(y2)
                orig_img = result.orig_img
                # H,W = orig_img.orig_shape
                cv2.imwrite("/home/justin/Desktop/code/python_project/Jersey-Number/runs/imgs"+"{:06d}-raw.jpg".format(img_count),orig_img)
                cropped_image = orig_img[y1:y2,x1:x2]
                # res_number_class = model_class(cropped_image,save_txt=True,save=True)
                res_number_class = model_class(cropped_image, device = "cpu")
                cv2.rectangle(orig_img, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), 2) 
                for r in res_number_class:
                    if hasattr(r,"probs"):
                        if r.probs.top1conf.item() > 0.2:
                            class_name = r.names[r.probs.top1]
                            (width, height), bottom = cv2.getTextSize(class_name, fontFace, fontScale=fontScale, thickness=thickness)
                            cv2.putText(orig_img, class_name+" conf:"+str(r.probs.top1conf.item()), (x1 - width, y1-height), fontFace, fontScale, color=(0, 0, 255), thickness=thickness,
                                            lineType=cv2.LINE_AA)
                cv2.imwrite("/home/justin/Desktop/code/python_project/Jersey-Number/runs/imgs"+"{:06d}.jpg".format(img_count),orig_img)

报错的这句话值得看一眼:
sklearn不支持,tensorflow和torch没测试过,可能会有问题。 先跑跑再说吧

Loading /home/justin/Desktop/code/python_project/Jersey-Number/runs/classify/train7/weights/best.mlpackage for CoreML inference...
scikit-learn version 1.4.2 is not supported. Minimum required version: 0.17. Maximum required version: 1.1.2. Disabling scikit-learn conversion API.
TensorFlow version 2.13.1 has not been tested with coremltools. You may run into unexpected errors. TensorFlow 2.12.0 is the most recent version that has been tested.
Torch version 2.3.0+cu121 has not been tested with coremltools. You may run into unexpected errors. Torch 2.1.0 is the most recent version that has been tested.

所以还要降级,真是麻烦,tensorflow是因为要转android侧的模型。
这里要给个参数,来指定cpu复现
res_number_class = model_class(cropped_image, device = “cpu”)

这意思是不能用pytorch 跑了吗? @todo, 然后用pytorch 2.0的环境试一下看看怎么样?@todo,
核心诉求是要把coreml的模型加载起来,看看是不是存在一样的错误

Exception has occurred: Exception
Model prediction is only supported on macOS version 10.13 or later.
  File "/home/justin/Desktop/code/python_project/Jersey-Number/zr_yz.py", line 76, in test_coreml
    res_number_class = model_class(cropped_image, device = "cpu")
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/justin/Desktop/code/python_project/Jersey-Number/zr_yz.py", line 88, in <module>
    test_coreml()
Exception: Model prediction is only supported on macOS version 10.13 or later.
detect 参数
detect_conf = 0.5172230005264282
切割位置: x1,y1,x2,y2
1. 原始位置:[1648.0953369140625, 882.2176513671875, 1682.9732666015625, 980.842041015625]
2.强制转成int 为后面切出这个区域做准备(1648, 882, 1682, 980)

分类输出结果:

top1:64

top1conf:tensor(0.9994, device='cuda:0')

top5:[64, 53, 10, 0, 20]

top5conf:tensor([9.9943e-01, 4.8942e-04, 1.9284e-05, 1.8095e-05, 8.8464e-06], device='cuda:0')

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-17 14:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-17 14:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-17 14:36:02       87 阅读
  4. Python语言-面向对象

    2024-06-17 14:36:02       96 阅读

热门阅读

  1. vue页面带滚动条,打开新页面页面不置顶的问题

    2024-06-17 14:36:02       28 阅读
  2. postman工具的使用

    2024-06-17 14:36:02       33 阅读
  3. 计算机专业的未来展望

    2024-06-17 14:36:02       33 阅读
  4. 决策树算法介绍:原理与案例实现

    2024-06-17 14:36:02       38 阅读
  5. Python 爬虫 文本转语音 支持多种音色模型选择

    2024-06-17 14:36:02       29 阅读
  6. LAMP部署及应用

    2024-06-17 14:36:02       29 阅读
  7. Linux Centos7.5 开放指定端口

    2024-06-17 14:36:02       24 阅读
  8. 走的人多了,也便成了路(八)

    2024-06-17 14:36:02       34 阅读
  9. Redis Cluster 为什么不支持传统的事务模型

    2024-06-17 14:36:02       28 阅读
  10. Spring Boot 面试热点(三)

    2024-06-17 14:36:02       32 阅读
  11. Dockerfile制作能够ssh的ubuntu和centos7系统

    2024-06-17 14:36:02       31 阅读
  12. 代码随想三刷栈与队列篇

    2024-06-17 14:36:02       36 阅读
  13. 学习笔记——交通安全分析06

    2024-06-17 14:36:02       29 阅读
  14. PHP框架详解 - symfony框架

    2024-06-17 14:36:02       33 阅读