Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从.onnx转成.mnn之后不能训练 #2862

Open
teslanow opened this issue May 10, 2024 · 0 comments
Open

从.onnx转成.mnn之后不能训练 #2862

teslanow opened this issue May 10, 2024 · 0 comments

Comments

@teslanow
Copy link

平台(如果交叉编译请再附上交叉编译目标平台):

Platform(Include target platform as well if cross-compiling):

Ubuntu20.04

Github版本:

Github Version:

2.3.0

编译方式:

Compiling Method

cmake编译

请在这里粘贴cmake参数或使用的cmake脚本路径以及完整输出
Paste cmake arguments or path of the build script used here as well as the full log of the cmake proess here or pastebin

cmake .. DCMAKE_BUILD_TYPE=Debug

问题

首先将pytorch预训练模型转为.onnx,然后用MNNConvert转为.mnn。python代码如下:

import torch
import torchvision.transforms as transforms
from torchvision.models import mobilenet_v2
# 假设你已经加载了预训练模型
model = mobilenet_v2(pretrained=True)

# 保存模型
# torch.save(model.state_dict(), 'mobilenetv2_imagenet.pth')

import torch.onnx

# 输入一个示例输入,用于模型导出
dummy_input = torch.randn(1, 3, 224, 224)

# 导出模型为ONNX
torch.onnx.export(model, dummy_input, 'mobilenetv2_imagenet.onnx', export_params=True,
                  opset_version=11,  # 可能需要根据你的环境调整
                  do_constant_folding=True,
                  input_names = ['input'], output_names = ['output'],
                  dynamic_axes={'input' : {0 : 'batch_size'},    # 动态batch_size
                                'output' : {0 : 'batch_size'}})

然后MNNConvert命令如下:
../MNN-test/build/MNNConvert -f ONNX --modelFile mobilenetv2_imagenet.onnx --MNNModel mobilenetv2_imagenet_for_training.mnn --bizCode AliNNTest --forTraining 使用该模型放在官方训练demo MobilenetV2PostTrain这个类中训练后报错,表示在onforward第75个算子出错。即MNN_ASSERT(nullptr != tempInputs[i]);`

std::vector<VARP> PipelineModule::onForward(const std::vector<VARP>& inputs) {
    std::vector<VARP> mStack(mStackSize);
    for (int i = 0; i < mInitVars.size(); ++i) {
        mStack[i + mInputSize] = mInitVars[i];
    }
    MNN_ASSERT(mInputSize == inputs.size());
    for (int i = 0; i < mInputSize; ++i) {
        mStack[i] = inputs[i];
    }
    for (int index = 0; index < mSubModules.size(); ++index) {
        auto& m = mSubModules[index];
        std::vector<VARP> tempInputs(std::get<1>(m).size());
        for (int i = 0; i < tempInputs.size(); ++i) {
            auto stackInput = std::get<1>(m)[i];
            tempInputs[i] = mStack[stackInput];
            MNN_ASSERT(nullptr != tempInputs[i]);
        }
        std::vector<VARP> tempOutputs = std::get<0>(m)->onForward(tempInputs);
        if(tempOutputs.size() != std::get<2>(m).size()) {
            // Execute has error
            return {};
        }
        for (int i = 0; i < tempOutputs.size(); ++i) {
            mStack[std::get<2>(m)[i]] = tempOutputs[i];
            MNN_ASSERT(nullptr != tempOutputs[i]);
        }
    }

这个算子是StridedSlice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant