文章目录
- 问题ImportError: DLL load failed:
- 我的报错:from torchvision import _C as C ImportError: DLL load failed: The specified module could not be found.
- 近似报错1:RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA versions.
- 近似报错2:from torch._C import * ImportError: DLL load failed: The specified module could not be found.
- 问题error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio"
- 待补充
问题ImportError: DLL load failed:
我的报错:from torchvision import _C as C ImportError: DLL load failed: The specified module could not be found.
11:30-21:00
当前环境:
CPU: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
当初在my_cpu_py3虚拟环境下安装PyTorch、Torchvision的命令conda install pytorch-cpu torchvision-cpu -c pytorch
python v3.7.4
pytorch v1.3.0
torchvision v0.4.1
>>> import sys; print('Python %s on %s' % (sys.version, sys.platform))
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] on win32
# \lesson_08_03\detection_demo.py代码片段摘录
# 1. load data & model
input_image = Image.open(path_img).convert("RGB")
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
略
# 4. forward
input_list = [img_chw]
with torch.no_grad():
tic = time.time()
print("input img tensor shape:{}".format(input_list[0].shape))
output_list = model(input_list)
略
运行\lesson_08_03\detection_demo.py,出现以下报错信息:
D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\python.exe "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\pydevconsole.py" --mode=client --port=8055
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['D:\\PytorchWorkSpace\\MyCondaProject\\MyCondaProject001_hello', 'D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello'])
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.8.0
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] on win32
runfile('D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello/lesson_08_03/detection_demo.py', wdir='D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello/lesson_08_03')
input img tensor shape:torch.Size([3, 624, 1270])
Traceback (most recent call last):
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-e185e3a99ac2>", line 1, in <module>
runfile('D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello/lesson_08_03/detection_demo.py', wdir='D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello/lesson_08_03')
File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/PytorchWorkSpace/MyCondaProject/MyCondaProject001_hello/lesson_08_03/detection_demo.py", line 69, in <module>
output_list = model(input_list)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\models\detection\generalized_rcnn.py", line 51, in forward
proposals, proposal_losses = self.rpn(images, features, targets)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torch\nn\modules\module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\models\detection\rpn.py", line 411, in forward
boxes, scores = self.filter_proposals(proposals, objectness, images.image_sizes, num_anchors_per_level)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\models\detection\rpn.py", line 336, in filter_proposals
keep = box_ops.batched_nms(boxes, scores, lvl, self.nms_thresh)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\ops\boxes.py", line 72, in batched_nms
keep = nms(boxes_for_nms, scores, iou_threshold)
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\ops\boxes.py", line 32, in nms
_C = _lazy_import()
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\extension.py", line 12, in _lazy_import
from torchvision import _C as C
File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed: The specified module could not be found. 找不到指定的模块。
通过在\lesson_08_03\detection_demo.py的output_list = model(input_list)这行代码处设置调试断点进行debug,程序运行跳转到
File “D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\extension.py”, line 12, in _lazy_import
from torchvision import _C as C
便终止了。
# D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\ops\boxes.py
import torch
from torchvision.extension import _lazy_import
def nms(boxes, scores, iou_threshold):
"""
Performs non-maximum suppression (NMS) on the boxes according
to their intersection-over-union (IoU).
NMS iteratively removes lower scoring boxes which have an
IoU greater than iou_threshold with another (higher scoring)
box.
Parameters
----------
boxes : Tensor[N, 4])
boxes to perform NMS on. They
are expected to be in (x1, y1, x2, y2) format
scores : Tensor[N]
scores for each one of the boxes
iou_threshold : float
discards all overlapping
boxes with IoU < iou_threshold
Returns
-------
keep : Tensor
int64 tensor with the indices
of the elements that have been kept
by NMS, sorted in decreasing order of scores
"""
_C = _lazy_import()
return _C.nms(boxes, scores, iou_threshold)
def batched_nms(boxes, scores, idxs, iou_threshold):
略
# D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\extension.py
_C = None
def _lazy_import():
"""
Make sure that CUDA versions match between the pytorch install and torchvision install
"""
global _C
if _C is not None:
return _C
import torch
from torchvision import _C as C
_C = C
if hasattr(_C, "CUDA_VERSION") and torch.version.cuda is not None:
tv_version = str(_C.CUDA_VERSION)
if int(tv_version) < 10000:
tv_major = int(tv_version[0])
tv_minor = int(tv_version[2])
else:
tv_major = int(tv_version[0:2])
tv_minor = int(tv_version[3])
t_version = torch.version.cuda
t_version = t_version.split('.')
t_major = int(t_version[0])
t_minor = int(t_version[1])
if t_major != tv_major or t_minor != tv_minor:
raise RuntimeError("Detected that PyTorch and torchvision were compiled with different CUDA versions. "
"PyTorch has CUDA Version={}.{} and torchvision has CUDA Version={}.{}. "
"Please reinstall the torchvision that matches your PyTorch install."
.format(t_major, t_minor, tv_major, tv_minor))
return _C
直接在 Python Console 中键入from torchvision import _C as C
,便会出现相同的报错信息;
>>> from torchvision import _C as C
Traceback (most recent call last):
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-ad371959dad5>", line 1, in <module>
from torchvision import _C as C
File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed: 找不到指定的模块。
https://github.com/pytorch/vision/issues/1018
https://github.com/pytorch/vision/issues/1062
根据@peterjc123的答复,pytorch 1.2.0 and torchvision 0.4.0 及其以上的版本应该不会出现这种报错信息;因此,虽然我的报错信息与 issue #1018 相同,但是我这边的问题可能是出在别的地方。
受"\lib\site-packages\torchvision\extension.py"文件中的"Detected that PyTorch and torchvision were compiled with different CUDA versions. “这一句话启示,感觉我这边的问题应该与cpu, cuda也有关系,便去 Files :: Anaconda Cloud 比较了win-64/torchvision-0.4.1-py37_cpu.tar.bz2和win-64/torchvision-0.4.1-py37_cu92.tar.bz2中的几个文件,发现两个安装包中的”\lib\site-packages\torchvision\ops\boxes.py"和"\lib\site-packages\torchvision\extension.py"文件都相同,只是_C.cp37-win_amd64.pyd文件不同。
![](https://i-blog.csdnimg.cn/blog_migrate/d3fc1850e59b7b606e9b4d83c42f7400.jpeg#pic_center)
据此判断,问题应该出在\lesson_08_03\detection_demo.py中的model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)无法在cpu环境下运行,而这是由于程序在from torchvision import _C as C
处发生中断所导致的。因此,如果 pytorch and torchvision 的后续版本对"\lib\site-packages\torchvision\extension.py"文件作了更改,model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)应该就可以在cpu环境下运吧!
解决方案:对 pytorch and torchvision 进行版本更新
为此,键入命令conda update pytorch torchvision
对 pytorch and torchvision 进行版本更新:
python v3.7.4
pytorch v1.5.0
torchvision v0.6.0
>>> import sys; print('Python %s on %s' % (sys.version, sys.platform))
Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] on win32
把 pytorch and torchvision 更新至 pytorch v1.5.0 and torchvision v0.6.0 版本后,"\lib\site-packages\torchvision\ops\boxes.py"和"\lib\site-packages\torchvision\extension.py"等文件都变了,extension.py中没有了from torchvision import _C as C语句。
再次直接在 Python Console 中键入from torchvision import _C as C
,出现的报错信息是:
>>> from torchvision import _C as C
Traceback (most recent call last):
File "D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-ad371959dad5>", line 1, in <module>
from torchvision import _C as C
File "D:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
SystemError: initialization of _C failed without raising an exception
但再次运行\lesson_08_03\detection_demo.py,无报错信息出现,问题解决。
此外可以发现,pytorch v1.5.0 and torchvision v0.6.0 版本对"\lib\site-packages\torchvision\ops\boxes.py"和"\lib\site-packages\torchvision\extension.py"文件均作了更改。
# D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\ops\boxes.py
import torch
from torch.jit.annotations import Tuple
from torch import Tensor
import torchvision
def nms(boxes, scores, iou_threshold):
# type: (Tensor, Tensor, float)
"""
Performs non-maximum suppression (NMS) on the boxes according
to their intersection-over-union (IoU).
NMS iteratively removes lower scoring boxes which have an
IoU greater than iou_threshold with another (higher scoring)
box.
Parameters
----------
boxes : Tensor[N, 4])
boxes to perform NMS on. They
are expected to be in (x1, y1, x2, y2) format
scores : Tensor[N]
scores for each one of the boxes
iou_threshold : float
discards all overlapping
boxes with IoU > iou_threshold
Returns
-------
keep : Tensor
int64 tensor with the indices
of the elements that have been kept
by NMS, sorted in decreasing order of scores
"""
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
def batched_nms(boxes, scores, idxs, iou_threshold):
略
# D:\OtherProgramFiles\Anaconda3\envs\my_cpu_py3\lib\site-packages\torchvision\extension.py
_HAS_OPS = False
def _register_extensions():
import os
import importlib
import torch
# load the custom_op_library and register the custom ops
lib_dir = os.path.dirname(__file__)
loader_details = (
importlib.machinery.ExtensionFileLoader,
importlib.machinery.EXTENSION_SUFFIXES
)
extfinder = importlib.machinery.FileFinder(lib_dir, loader_details)
ext_specs = extfinder.find_spec("_C")
if ext_specs is None:
raise ImportError
torch.ops.load_library(ext_specs.origin)
try:
_register_extensions()
_HAS_OPS = True
except (ImportError, OSError):
pass
def _check_cuda_version():
"""
Make sure that CUDA versions match between the pytorch install and torchvision install
"""
if not _HAS_OPS:
return -1
import torch
_version = torch.ops.torchvision._cuda_version()
if _version != -1 and torch.version.cuda is not None:
tv_version = str(_version)
if int(tv_version) < 10000:
tv_major = int(tv_version[0])
tv_minor = int(tv_version[2])
else:
tv_major = int(tv_version[0:2])
tv_minor = int(tv_version[3])
t_version = torch.version.cuda
t_version = t_version.split('.')
t_major = int(t_version[0])
t_minor = int(t_version[1])
if t_major != tv_major or t_minor != tv_minor:
raise RuntimeError("Detected that PyTorch and torchvision were compiled with different CUDA versions. "
"PyTorch has CUDA Version={}.{} and torchvision has CUDA Version={}.{}. "
"Please reinstall the torchvision that matches your PyTorch install."
.format(t_major, t_minor, tv_major, tv_minor))
return _version
_check_cuda_version()
近似报错1:RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA versions.
RuntimeError: Detected that PyTorch and torchvision were compiled with different CUDA versions. PyTorch has CUDA Version=9.0 and torchvision has CUDA Version=10.0. Please reinstall the torchvision that matches your PyTorch install.
解决方案:
I recommend you to install new pytorch and torchvision together from official sources.
If you use anaconda, then run this
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
Or use pip / pip3 when you use python package manager
pip3 install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-linux_x86_64.whl
pip3 install https://download.pytorch.org/whl/cu100/torchvision-0.3.0-cp36-cp36m-linux_x86_64.whl
Take a look at this official link for pytorch installation
近似报错2:from torch._C import * ImportError: DLL load failed: The specified module could not be found.
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
from torch._C import *
ImportError: DLL load failed: The operating system cannot run %1.
解决方案:
问题error: Microsoft Visual C++ 14.0 is required. Get it with “Build Tools for Visual Studio”
问题描述: 安装pycocotools时出现此问题
时间戳: 20200826
(my_gpu_py3) E:\WorkSpace\Pytorch_WorkSpace\OpenSourcePlatform\cocoapi-master\PythonAPI>python setup.py build_ext install
running build_ext
cythoning pycocotools/_mask.pyx to pycocotools\_mask.c
E:\OtherProgramFiles\Anaconda3\envs\my_gpu_py3\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). Thi
s will change in a later release! File: E:\WorkSpace\Pytorch_WorkSpace\OpenSourcePlatform\cocoapi-master\PythonAPI\pycocotools\_mask.pyx
tree = Parsing.p_module(s, pxd, full_module_name)
building 'pycocotools._mask' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
(my_gpu_py3) E:\WorkSpace\Pytorch_WorkSpace\OpenSourcePlatform\cocoapi-master\PythonAPI>
解决方案: 安装Microsoft Visual C++运行库
虽然可以安装VS2015完整版,但很占硬盘空间。这里通过Microsoft Visual C++ Build Tools_v14.0.25420.1.exe工具安装Microsoft Visual C++运行库。
但在安装运行Microsoft Visual C++ Build Tools_v14.0.25420.1.exe工具时,出现了“安装包丢失或损坏 \n 请提供搜索包的位置C:\Users\username\下载\packages\VisualC_D14\VC_Tools.X86.Nat\VC_Tools.X86.Nat.msi
C:\Users\username\下载\packages\VisualC_D14\VC_Tools.X64.Base\VC_Tools.X64.Base.msi”问题,导致安装被迫中途终止。
若出现VS2015报“安装包丢失或损坏”问题,则原因为:microsoft root certificate authority 2010、microsoft root certificate authority 2011证书未安装,导致文件校验未通过,下载并安装这两个证书即可。链接:https://pan.baidu.com/s/1fbIWjuczPFTtZ_hm9aYuaw 提取码:8swi。
Microsoft Visual C++ 14.0 is required 的解决方案_tszupup的博客 20181030
如何下载visual c++ build tools的离线完整版_u013085021的博客 20200826
待补充
待补充