Skip to content

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

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

Open
Richard-chen3 opened this issue Jan 9, 2020 · 11 comments
Open

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

Richard-chen3 opened this issue Jan 9, 2020 · 11 comments

Comments

@Richard-chen3
Copy link

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
Copy link
Owner

walktree commented Jan 9, 2020

检查CUDA 是否可用:

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

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

@Richard-chen3
Copy link
Author

Richard-chen3 commented Jan 9, 2020

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

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

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

@walktree
Copy link
Owner

根据报错日志自己调试下

@xuming0629
Copy link

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

@zeta7337
Copy link

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

@zeta7337
Copy link

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

did you solve it@Richard-chen3

@ZhenpengChenCode
Copy link

[ 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

@Richard-chen3
Copy link
Author

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.

@bravestpeng
Copy link

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

@bravestpeng
Copy link

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

@zhangbinxlz
Copy link

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

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

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

7 participants