libtorch-yolov3 icon indicating copy to clipboard operation
libtorch-yolov3 copied to clipboard

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

Open Richard-chen3 opened this issue 5 years ago • 11 comments

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;

}

Richard-chen3 avatar Jan 09 '20 09:01 Richard-chen3

检查CUDA 是否可用:

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

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

walktree avatar Jan 09 '20 09:01 walktree

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

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

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

Richard-chen3 avatar Jan 09 '20 09:01 Richard-chen3

根据报错日志自己调试下

walktree avatar Jan 10 '20 01:01 walktree

[ 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 to cv::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 总是链接不到是什么原因

xuming0629 avatar Feb 02 '20 14:02 xuming0629

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

zeta7337 avatar Mar 25 '20 06:03 zeta7337

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

did you solve it@Richard-chen3

zeta7337 avatar Mar 25 '20 06:03 zeta7337

[ 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 to cv::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

ZhenpengChenCode avatar Apr 02 '20 15:04 ZhenpengChenCode

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

did you solve it@Richard-chen3

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

Richard-chen3 avatar Apr 04 '20 12:04 Richard-chen3

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

bravestpeng avatar Apr 20 '20 06:04 bravestpeng

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

bravestpeng avatar Apr 20 '20 10:04 bravestpeng

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

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

zhangbinxlz avatar May 15 '20 19:05 zhangbinxlz