写在前面

一、探索过程和错误

错误一:E0135 namespace “torch::jit” 没有成员 “load”

解决方式:添加头文件

来源:https://blog.csdn.net/jacke121/article/details/93322398

错误二:E0020 未定义标识符 “CV_BGR2RGB”

解决方式:CV已经改为COLOR(在cvtcolor()函数中)

来源:https://zouzhongliang.com/index.php/2019/08/19/opencv-yansekongjianzhuanhuanhanshucvtcoloryunyong/

错误三:E0135 class “at::DeprecatedTypeProperties” 没有成员 “tensorFromBlob”

原因:该为旧版代码,新版需要更改:

  1. //auto img_tensor = torch::CPU(torch::kFloat32).tensorFromBlob(float_image.data, { 1, img_size, img_size, 3 }); //将cv::Mat转成tensor,大小为1,224,224,3
  2. //img_tensor = img_tensor.permute({ 0, 3, 1, 2 }); //调换顺序变为torch输入的格式 1,3,224,224
  3. ////img_tensor[0][0] = img_tensor[0][0].sub_(0.485).div_(0.229); //减去均值,除以标准差
  4. ////img_tensor[0][1] = img_tensor[0][1].sub_(0.456).div_(0.224);
  5. ////img_tensor[0][2] = img_tensor[0][2].sub_(0.406).div_(0.225);
  6. //新改的:
  7. auto img_tensor = torch::from_blob(image.data, { 1, img_size, img_size, 3 }).permute({ 0, 3, 1, 2 }).to(torch::kCUDA);
  8. auto img_var = torch::autograd::make_variable(img_tensor, false); //不需要梯度

来源:https://blog.csdn.net/jacke121/article/details/93487947

错误四:E0312 不存在用户定义的从 “const std::experimental::filesystem::v1::path” 到 “std::string” 的适当转换

错误五: E0349 没有与这些操作数匹配的 “!=” 运算符 操作数类型为: torch::jit::script::Module != std::nullptr_t

  1. torch::jit::script::Module module = torch::jit::load(model_path); //load model
  2. module.to(at::kCUDA); // put model to gpu
  3. assert(module != nullptr);

解决方案和原因:由于新版本更新,module已经不是指针,所以直接删去这句话即可

错误六: E0035 #error 指令: The header providing std::experimental::filesystem is deprecated by Microsoft and will be REMOVED. It is superseded by the C++17 header providing std::filesystem. You can define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING to acknowledge that you have received this warning.

解决方案和原因:将头文件#include 改为#include 即可

来源:https://www.cnblogs.com/qq2806933146xiaobai/p/12153852.html

错误七:namespace std 没有filesystem 成员

解决方案:更改属性为最新标准

image.png
来源:https://blog.csdn.net/weixin_39450145/article/details/104691189

延续错误:不存在’std :: filesystem :: path’到’std :: string’的转换

原因:在msvs编译器,Windows下,std::filesystem::path隐式转换为wstring。

错误八: E0266 “ACCESS_MASK” 不明确 Project2 C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winioctl.h 11122

错误原因和解决方案:与opencv的cv命名空间有冲突,取消using namespace cv,改为使用cv::function的方式就可以了。

错误八九的代码:

出处:https://blog.csdn.net/liminwang0311/article/details/79975174(已更改,见下面)

  1. #include <Windows.h>
  2. //将string转换成wstring
  3. wstring string2wstring(string str)
  4. {
  5. wstring result;
  6. //获取缓冲区大小,并申请空间,缓冲区大小按字符计算
  7. int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
  8. TCHAR* buffer = new TCHAR[len + 1];
  9. //多字节编码转换成宽字节编码
  10. MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
  11. buffer[len] = '\0'; //添加字符串结尾
  12. //删除缓冲区并返回值
  13. result.append(buffer);
  14. delete[] buffer;
  15. return result;
  16. }
  17. //将wstring转换成string
  18. string wstring2string(wstring wstr)
  19. {
  20. string result;
  21. //获取缓冲区大小,并申请空间,缓冲区大小事按字节计算的
  22. int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
  23. char* buffer = new char[len + 1];
  24. //宽字节编码转换成多字节编码
  25. WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
  26. buffer[len] = '\0';
  27. //删除缓冲区并返回值
  28. result.append(buffer);
  29. delete[] buffer;
  30. return result;
  31. }

错误九:E0266 “byte” 不明确 Project2 C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared\rpcndr.h 192

参考:http://www.mamicode.com/info-detail-191940.html,未验证,没有再使用上面的代码。
更改使用:http://www.cppblog.com/kenwell/archive/2008/05/21/50661.html

错误十(警告):C4996 ‘mbstowcs’: This function or variable may be unsafe. Consider using mbstowcs_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Project2 E:\C++\retinanet_c\Project2\Project2\main.cpp 48

或者:
错误 C4996 ‘std::is_pod‘: warning STL4025: std::is_pod and std::is_pod_v are deprecated in C++20. The std::is_trivially_copyable and/or std::is_standard_layout traits likely suit your use case. You can define _SILENCE_CXX20_IS_POD_DEPRECATION_WARNING or _SILENCE_ALL_CXX20_DEPRECATION_WARNINGS to acknowledge that you have received this warning. Project2 E:\C++\retinanet_c\libtorch\include\c10\core\impl\LocalTensorTypeSet.h 45

解决方案:屏蔽警告

  1. #pragram warning(disable : 4996)
  2. //写在main.cpp的最前面

或者查询后更改。
官方解释:https://docs.microsoft.com/zh-cn/cpp/preprocessor/warning?view=vs-2019

错误十一:C2871 “chrono”: 具有该名称的命名空间不存在 Project2 E:\C++\retinanet_c\Project2\Project2\main.cpp 16

解决方案:可以直接去掉using namespace chrono

错误十二:缺少.pdb文件:

类似这样:image.png
或者:未加载kernelbase.pdb

解决方案:1.已知.pdb位置,找到并复制到main.cpp文件同一目录下即可;

2.未知的,可以参考https://blog.csdn.net/u011251940/article/details/89521518,不一定有效

代码:

  1. torch::jit::script::Module module = torch::jit::load(model_path);

我的原因和有效解决方案:model_path的实际内容不正确,不是torch script生成的.pt文件,造成的,更改了一个torch script生成的.pt文件就解决了。

错误:没有cuda_runtime.h,
解决方案:项目属性加入一个inlcude目录:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include

错误:E1696 无法打开 源 文件 “NvInfer.h” Project2 E:\C++\retinanet_c\libtorch\include\torch\csrc\engine.h 28




https://github.com/jhlee93/PytorchCppLoad


ImportError: cannot import name ‘_obtain_input_shape’ from ‘keras.applications.imagenet_utils’
https://blog.csdn.net/lly1122334/article/details/88774171


h5py is running against HDF5 1.10.5 when it was built against 1.10.4
https://blog.csdn.net/u010094719/article/details/104571954/


ImportError: cannot import name ‘PILLOW_VERSION’ from ‘PIL’ (/home/user8/anaconda3/envs/FCOS/lib/pyt
https://blog.csdn.net/weixin_36670529/article/details/103961303


AttributeError: module ‘resnet’ has no attribute ‘ResNet’


OpenCV(4.1.1) Error: Assertion failed (!_src.empty()) in cv::cvtColor, file C:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\color.cpp, line 182
https://stackoverflow.com/questions/54490710/opencv-error-215assertion-failed-src-empty-in-function-cvcvtcolor




torchscript


RuntimeError: Expected 4-dimensional input for 4-dimensional weight 64 3 7 7
https://blog.csdn.net/weixin_38208741/article/details/97894292