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

推理时动态Reshape试无法推理output shape的问题 #532

Closed
banbishan opened this issue Nov 10, 2020 · 6 comments
Closed

推理时动态Reshape试无法推理output shape的问题 #532

banbishan opened this issue Nov 10, 2020 · 6 comments

Comments

@banbishan
Copy link

使用动态Reshape推理报错,我看是源码里这一段报出的错误,但是dim0[i] == 1 || dim1[i] == 1 || dim1[i] == dim0[i]应该是满足true的,麻烦看下,谢谢

bool SupportBroadcast(DimsVector dim0, DimsVector dim1) {
    if (dim0.size() != dim1.size()) {
        EXPAND(dim0, dim1);
    }
    ASSERT(dim0.size() == dim1.size());

    for (int i = 0; i < dim0.size(); ++i) {
        // the "!" is key point
        if (!(dim0[i] == 1 || dim1[i] == 1 || dim1[i] == dim0[i])) {
            return false;
        }
    }
    return true;
}

报错如下:
image
推理代码如下:

    int x,y,chanels_in_file=3;
    unsigned char* im_bgr = stbi_load(imagepath,&x,&y,&chanels_in_file,0);
    void* sourcePixelscolor = (void*)im_bgr;
    
    TNN_NS::TNN tnn;
    TNN_NS::ModelConfig model_config;
    //proto文件内容存入proto_buffer
    auto proto_tnn = fdLoadFile(model_param);
    auto model_tnn = fdLoadFile(model);
    model_config.model_type = TNN_NS::MODEL_TYPE_TNN;
    model_config.params = {proto_tnn, model_tnn};

    tnn.Init(model_config);

    TNN_NS::NetworkConfig config;
    config.device_type = TNN_NS::DEVICE_OPENCL;
    TNN_NS::Status error;
    TNN_NS::MatConvertParam input_cvt_param;
    input_cvt_param.scale = {1.0 / (255 * 0.229), 1.0 / (255 * 0.224), 1.0 / (255 * 0.225), 0.0};
    input_cvt_param.bias  = {-0.485 / 0.229, -0.456 / 0.224, -0.406 / 0.225, 0.0};
    auto net_instance = tnn.CreateInst(config, error);

    // 输入输出和batch参数
     const std::string input_name = "input.1";

     // 修改输入mat shape
     TNN_NS::DimsVector input_dim = {1,3,x,y};
     std::shared_ptr<TNN_NS::Mat> input_mat(new TNN_NS::Mat(TNN_NS::DEVICE_ARM, TNN_NS::N8UC3, input_dim,sourcePixelscolor));

     // 修改输出mat shape
     //DimsVector output_dim = output_mat_map[output_name]->GetDims();
     TNN_NS::DimsVector output_dim = {1,7,x,y};
     std::shared_ptr<TNN_NS::Mat> output_mat(new TNN_NS::Mat(TNN_NS::DEVICE_ARM, TNN_NS::NCHW_FLOAT, output_dim));

     // reshape
     TNN_NS::InputShapesMap input_shape_map;
     input_shape_map[input_name] = input_dim;
     auto ret = net_instance->Reshape(input_shape_map);

    auto status = net_instance->SetInputMat(input_mat, input_cvt_param);
    if (status != TNN_NS::TNN_OK) { printf("convert input failed.\n"); return -1; }
    status = net_instance->ForwardAsync(nullptr);
    status = net_instance->GetOutputMat(output_mat);
@seanxcwang
Copy link
Collaborator

你可以试一下,不用动态reshape,直接用TNNTest测试你想要输入的大小,确定下试reshape的问题,还是这个尺寸本身就不行

@banbishan
Copy link
Author

你可以试一下,不用动态reshape,直接用TNNTest测试你想要输入的大小,确定下试reshape的问题,还是这个尺寸本身就不行

模型本身是卷积网络,应该是支持动态shape的,模型在https://github.com/Tencent/TNN/issues/480里有,TNNTest是指TNN/test/test.cc吗?

@banbishan
Copy link
Author

banbishan commented Nov 10, 2020

你可以试一下,不用动态reshape,直接用TNNTest测试你想要输入的大小,确定下试reshape的问题,还是这个尺寸本身就不行

我用自己的程序,不用reshape,直接用其他shape的图片是不行的,必须是模型固定的shape1 * 3 * 960 * 1280,但是这不就是reshape的功能吗?

@seanxcwang
Copy link
Collaborator

你可以试一下,不用动态reshape,直接用TNNTest测试你想要输入的大小,确定下试reshape的问题,还是这个尺寸本身就不行

模型本身是卷积网络,应该是支持动态shape的,模型在https://github.com/Tencent/TNN/issues/480里有,TNNTest是指TNN/test/test.cc吗?

对,就是test.cc,直接用scripts目录下的build_linux.sh,会编译出一个TNNTest,在linux环境下运行,可以指定参数为不同的shape

@banbishan
Copy link
Author

以指定参数为不同的shape

无法初始化layer 663,这是reshape的问题吗?
image

@banbishan
Copy link
Author

是网络的问题,感谢

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

2 participants