MMDeploy SDK 使用記録 (ncnn)

ホーム: mmpose/projects/rtmpose at main · open-mmlab/mmpose · GitHub

        MMDeploy は、OpenMMLab のアルゴリズムをさまざまなデバイスやプラットフォームにより簡単にデプロイするのに役立つ一連のツールを提供します。現在、MMDeploy は PyTorch モデルを ONNX や TorchScript などのデバイスに依存しない IR モデルに変換できます。ONNX モデルを推論バックエンド モデルに変換することもできます。この 2 つを組み合わせることで、エンドツーエンドのモデル変換、つまりトレーニング側から運用側へのデプロイをワンクリックで行うことが可能になります。

1. モデル変換

        MMDeploy SDK では、Python と C++ の両方の推論 API が提供されています。SDK を使用するには、モデルの変換中に必要な情報をダンプする必要があります。モデル変換コマンドに --dump-info を追加するだけです。(MMDeploy SDK は、Python および C++ 推論 API を提供します) 、推論に API を使用する前に、PC 上の対応するファイルを変換する必要があります (pth->onnx、pth->ncnn...)。

        rtm の hand_detection モデルを onnx 出力に変換すると、出力後のフォルダーは次のようになります。 tensorrt をインストールしていないため、end2end.engine ファイルがありません。ncnn デプロイを行います。

|----{work-dir}
     |----end2end.onnx    # ONNX model
     |----end2end.bin     # ncnn model
     |----end2end.param   # ncnn model
     |----pipeline.json   #
     |----deploy.json     # json files for the SDK
     |----detail.json     #

2. モデル SDK の推論

1. モデルの位置と入力を定義します

DEFINE_string(det_model, "../model/rtm/rtmdet", "Decection model directory");
DEFINE_string(image, "../images/test.bmp", "Input image path");
DEFINE_string(device, "cpu", R"(Device name, e.g. "cpu", "cuda")");
DEFINE_double(det_thr, .5, "Detection score threshold");

2. 推論を実行して予測の信頼度を取得し、その後のキー ポイント検出の入力として bbox 内の手のひらをインターセプトします。

    // hand detection
    mmdeploy::Detector detector(mmdeploy::Model{FLAGS_det_model}, mmdeploy::Device{FLAGS_device});

    mmdeploy::Detector::Result dets = detector.Apply(img);

    // get the max socre detection
    mmdeploy_detection_t target_det = dets[0];
    std::cout << "detection max score: " << target_det.score << std::endl;
    if (target_det.score < FLAGS_det_thr) {
        std::cout << "failed detect hand." << std::endl;
        return 0;
    }
    std::cout << "successfully detect hand." << std::endl;

    // crop detection result
    cv::Point2f tl(target_det.bbox.left, target_det.bbox.top);
    cv::Point2f br(target_det.bbox.right, target_det.bbox.bottom);
    cv::Mat det_crop = img(cv::Rect(tl, br)).clone();
    cv::imwrite("../images/det_crop.bmp", det_crop);

 3.ビジュアルボックス

    // visualize detection
    utils::Visualize v;
    auto det_sess = v.get_session(img);
    det_sess.add_det(target_det.bbox, target_det.label_id, target_det.score, target_det.mask, 0);
    cv::imwrite("../images/det_out.bmp", det_sess.get());

 4. 手のキーポイントを予測するには、同様に、トリミングによって取得された手の画像を rtm-pose の入力として使用できます。

    // pose detection
    mmdeploy::PoseDetector pose_detector(mmdeploy::Model{FLAGS_pose_model}, mmdeploy::Device{FLAGS_device});
    mmdeploy::PoseDetector::Result pose_dets = pose_detector.Apply(det_crop);
    mmdeploy_pose_detection_t target_pose = pose_dets[0];

        視覚化:

    v.set_skeleton(utils::Skeleton::get("coco-wholebody-hand"));
    auto pose_sess = v.get_session(det_crop);
    pose_sess.add_pose(target_pose.point, target_pose.score, target_pose.length, 0.);
    cv::imwrite("../images/pose_out.bmp", pose_sess.get());

 

おすすめ

転載: blog.csdn.net/weixin_44855366/article/details/131287853