weixin_39827728
weixin_39827728
2020-12-29 21:01

cpu可以用,gpu出错,请问是什么原因

libtorch 1.0,cuda9.0,

torch::Tensor Darknet::forward(torch::Tensor x) { int module_count = module_list.size();

std::vector<torch::Tensor> outputs(module_count);

torch::Tensor result;
int write = 0;

for (int i = 0; i < module_count; i++)
{
    map<string, string> block = blocks[i+1];

    string layer_type = block["type"];

    cout << layer_type << endl;

    if (layer_type == "net")
        continue;

    if (layer_type == "convolutional" || layer_type == "upsample" || layer_type == "maxpool")
    {
        torch::nn::SequentialImpl *seq_imp = dynamic_cast<torch::nn::SequentialImpl *>(module_list[i].ptr().get());

        x = seq_imp->forward(x);//这一句出错

        outputs[i] = x;
    }
    else if (layer_type == "route")
    {
        int start = std::stoi(block["start"]);
        int end = std::stoi(block["end"]);

        if (start > 0) start = start - i;

        if (end == 0)
        {
            x = outputs[i + start];
        }
        else
        {
            if (end > 0) end = end - i;

            torch::Tensor map_1 = outputs[i + start];
            torch::Tensor map_2 = outputs[i + end];

            x = torch::cat({map_1, map_2}, 1);
        }

        outputs[i] = x;
    }
    else if (layer_type == "shortcut")
    {
        int from = std::stoi(block["from"]);
        x = outputs[i-1] + outputs[i+from];
        outputs[i] = x;
    }
    else if (layer_type == "yolo")
    {
        torch::nn::SequentialImpl *seq_imp = dynamic_cast<torch::nn::SequentialImpl *>(module_list[i].ptr().get());

        map<string, string> net_info = blocks[0];
        int inp_dim = get_int_from_cfg(net_info, "height", 0);
        int num_classes = get_int_from_cfg(block, "classes", 0);

        x = seq_imp->forward(x, inp_dim, num_classes, *_device);

        if (write == 0)
        {
            result = x;
            write = 1;
        }
        else
        {
            result = torch::cat({result,x}, 1);
        }

        outputs[i] = outputs[i-1];
    }
}
return result;

}

该提问来源于开源项目:walktree/libtorch-yolov3

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

11条回答

  • weixin_39863008 weixin_39863008 4月前

    检查CUDA 是否可用:

    
    if (torch::cuda::is_available() ) {        
          // CUDA 是否可用
    }
    

    另外,最新代码适配了 libtorch 1.3。

    点赞 评论 复制链接分享
  • weixin_39827728 weixin_39827728 4月前

    检查CUDA 是否可用: if (torch::cuda::is_available() ) {
    // CUDA 是否可用 }

    另外,最新代码适配了 libtorch 1.3。

    cuda可以用的。我打印输出时显示在cuda上。因为电脑原因用不了libtorch1.3.就在main.cpp里面的 auto output = net.forward(img_tensor); 这一句出错

    点赞 评论 复制链接分享
  • weixin_39863008 weixin_39863008 4月前

    根据报错日志自己调试下

    点赞 评论 复制链接分享
  • weixin_39704066 weixin_39704066 4月前

    [ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o [100%] Linking CXX executable example CMakeFiles/example.dir/example.cpp.o: In function main': example.cpp:(.text+0x108d): undefined reference tocv::imread(std::string const&, int)' collect2: error: ld returned 1 exit status CMakeFiles/example.dir/build.make:112: recipe for target 'example' failed make[2]: [example] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example.dir/all' failed make[1]: [CMakeFiles/example.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

    我的opencv 总是链接不到是什么原因

    点赞 评论 复制链接分享
  • weixin_39996742 weixin_39996742 4月前

    i met the same problem,error happens at at x = seq_imp->forward(x);

    点赞 评论 复制链接分享
  • weixin_39996742 weixin_39996742 4月前

    i met the same problem,error happens at at x = seq_imp->forward(x);

    did you solve it-chen3

    点赞 评论 复制链接分享
  • weixin_39752941 weixin_39752941 4月前

    [ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o [100%] Linking CXX executable example CMakeFiles/example.dir/example.cpp.o: In function main': example.cpp:(.text+0x108d): undefined reference tocv::imread(std::string const&, int)' collect2: error: ld returned 1 exit status CMakeFiles/example.dir/build.make:112: recipe for target 'example' failed make[2]: [example] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example.dir/all' failed make[1]: [CMakeFiles/example.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

    我的opencv 总是链接不到是什么原因

    你用的什么版本的libtorch

    点赞 评论 复制链接分享
  • weixin_39827728 weixin_39827728 4月前

    i met the same problem,error happens at at x = seq_imp->forward(x);

    did you solve it-chen3

    No ,I haven't.It seems like we met the same problem.

    点赞 评论 复制链接分享
  • weixin_40005454 weixin_40005454 4月前

    x = seq_imp->forward(x);//这一句出错 same problem.

    点赞 评论 复制链接分享
  • weixin_40005454 weixin_40005454 4月前

    我发现了这个问题,在gpu下,不能使用torch::cat(),torch::cat不能将gpu里面的数据拼在一起, 要先在cpu下cat,然后再传到gpu里面

    点赞 评论 复制链接分享
  • weixin_39517400 weixin_39517400 4月前

    我发现了这个问题,在gpu下,不能使用torch::cat(),torch::cat不能将gpu里面的数据拼在一起, 要先在cpu下cat,然后再传到gpu里面

    seq_imp->forward(x);请问你这个问题解决了吗 怎么解决的? 我笔记本上Ubuntu、windows10运行程序没有任何问题,另外一台台式机Ubuntu下运行也没问题,今天又换了台win10的台式结果forward这一步就异常退出了。

    点赞 评论 复制链接分享

相关推荐