unet MNN

#include <MNN/Interpreter.hpp>
#include <MNN/ImageProcess.hpp>
#include <MNN/expr/ExprCreator.hpp>
#include <MNN/expr/Expr.hpp>
#include <MNN/expr/Executor.hpp>
#include <MNN/expr/Module.hpp>
#include <MNN/expr/Expr.hpp>

#include <opencv2/opencv.hpp>

class SemanticSegmentation {
public:
    SemanticSegmentation(const std::string& modelPath) {
        // 创建MNN网络
        m_interpreter = std::shared_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(modelPath.c_str()));

        // 创建MNN会话
        MNN::ScheduleConfig config;
        m_session = m_interpreter->createSession(config);

        // 获取输入tensor和输出tensor的信息
        m_inputInfo = m_interpreter->getInputInfo(0);
        m_outputInfo = m_interpreter->getOutputInfo(0);
    }

    ~SemanticSegmentation() {
        // 释放资源
        m_interpreter->releaseModel();
        m_interpreter->releaseSession(m_session);
    }

    std::vector<float> inference(const std::string& imagePath) {
        cv::Mat image = cv::imread(imagePath);
        cv::cvtColor(image, image, cv::COLOR_BGR2RGB);

        // 创建一个临时的MNN::CV::ImageProcess对象
        MNN::CV::ImageProcess process;
        process.setResizeDims({image.cols, image.rows});
        process.setNormalizationParam(1.0f / 255, {0.485f, 0.456f, 0.406f}, {0.229f, 0.224f, 0.225f});
        process.process(image.data, image.cols, image.rows);

        // 创建输入Tensor,并将数据拷贝到输入Tensor
        MNN::Tensor* inputTensor = m_interpreter->getSessionInput(m_session, nullptr);
        process.convertToTensor(inputTensor);

        // 运行推理
        m_interpreter->runSession(m_session);

        // 获取输出Tensor的数据指针和shape信息
        const MNN::Tensor* outputTensor = m_interpreter->getSessionOutput(m_session, nullptr);
        const std::vector<int> outputShape = outputTensor->shape();
        const int outputSize = outputShape[1] * outputShape[2];

        std::vector<float> outputData(outputSize, 0.0f);
        // 将数据拷贝到输出vector
        memcpy(outputData.data(), outputTensor->host<float>(), outputSize * sizeof(float));

        return outputData;
    }

private:
    std::shared_ptr<MNN::Interpreter> m_interpreter;
    MNN::Session* m_session;
    const MNN::Tensor* m_inputTensor;
    const MNN::Tensor* m_outputTensor;
};

int main() {
    const std::string modelPath = "path_to_model.mnn";
    const std::string imagePath = "path_to_input_image.jpg";

    SemanticSegmentation segmentation(modelPath);
    std::vector<float> output = segmentation.inference(imagePath);

    // 进行后处理,将输出转换为0-1掩模图
    // 对output进行处理,根据具体模型输出的数据格式进行解析和操作

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值