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

模型加载参数问题 #2

Closed
SunZengyi opened this issue Mar 15, 2021 · 17 comments
Closed

模型加载参数问题 #2

SunZengyi opened this issue Mar 15, 2021 · 17 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@SunZengyi
Copy link

大佬,看你的博客收获巨多,谢谢大佬!
我自己用C++搭的模型加载python训练的参数pth文件一直报错,求教!

@AllentDan
Copy link
Owner

是通过trace生成的torchScript文件吗?torch.save保存的是字典文件,不一样哟。可以贴个错误内容

@SunZengyi
Copy link
Author

解决了,今天对照你写的第五章的内容仔细对照,就是保存文件的问题,感谢!

@AllentDan AllentDan added help wanted Extra attention is needed question Further information is requested labels Mar 16, 2021
@AllentDan
Copy link
Owner

那就好,不客气😄

@SunZengyi
Copy link
Author

大佬,请问如何在libtorch中实现tensor部分相加
比如pytorch中是这样的scores[indices] += a
用libtorch scores.index({indieces})的话是深拷贝,怎样取出浅拷贝的值然后加上对应的a呢

@AllentDan
Copy link
Owner

如果你的掩码indieces中true是矩阵块,完全可以用其他操作如narrow完成。

如果不是,你试试index_put_操作,libtorch它的两个函数接口,分别接受放置常数和张量。组合index_put_和index就可以实现你需要了。

	auto c = torch::randn({ 3,4 });
	auto mask = torch::zeros({ 3,4 });
	mask[0][0] = 1;
	mask[0][2] = 1;
	std::cout << c;
	std::cout << c.index({ mask.to(torch::kBool) });
	std::cout << c.index_put_({ mask.to(torch::kBool) }, c.index({ mask.to(torch::kBool) })+1.5);
	std::cout << c;

另外小建议哈,libtorch中尽量避免使用+=,在前后向传播的时候可能会出错

@SunZengyi
Copy link
Author

index_put_完美解决,谢谢大佬!

@SunZengyi
Copy link
Author

大佬,我又遇到问题了,再次求助
这次是模型转GPU的问题,在CPU上能跑通,GPU上报错
我看了看貌似是我将整个网络to(device)了,但是网络中我自己搭建的卷积层中的参数和部分网络依旧是CPU,我单独转GPU了也没用,大佬知道这是咋回事吗

@AllentDan
Copy link
Owner

看了我的第一章部署吗?里面有个情况就是无法使用GPU,你看看对不对。不行你catch一下错误贴一下

@SunZengyi
Copy link
Author

我好像知道为啥了,我先模型转的GPU后load参数,就又变回CPU了

@AllentDan
Copy link
Owner

有个nanquantile函数,你留个微信吧,我拉个群好了

@SunZengyi
Copy link
Author

太好了,感谢大佬! szy525525

@staring-s
Copy link

大佬,我是将训练好的pth文件转换为pt文件,使用python调用pt文件分割的图像效果很好,但是在使用c++调用,效果差了很多,是我在对forward的结果处理的问题吗?

@AllentDan
Copy link
Owner

@staring-s, 更可能是结果输入前的预处理问题,当然具体原因可以调试排查。

@staring-s
Copy link

我预处理的操作是:先转换为rgb,resize,转换为tensor,最后归一化。

@AllentDan
Copy link
Owner

@staring-s最好自己check一下模型的输入是否一致,不保证opencv或者其他算子库实现方式与python一致

@staring-s
Copy link

好的,我去看看,谢谢大佬!

@staring-s
Copy link

啊,大佬牛批,问题解决了,我又遇到新问题了。
test = np.random.randint(0, 256, (256, 3), dtype=np.uint8)[out]
其中out为(1024,2048),执行之后test就是(1024, 2048, 3),也就是一张图片,单独看np.random.randint(0, 256, (256, 3), dtype=np.uint8),是生成数值范围为0-255的(256,3),后面再加一个[out],不是太理解,大佬能解答一下吗?

@yuanhs1996 yuanhs1996 mentioned this issue Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants