PyTorch图像标注报错:CUDA Runtime Error(48)设备无可用内核镜像
Hey there! I get that you're new to PyTorch and trying to get the image captioning repo up and running—let's fix this error together.
First, let's break down what's going on:
核心错误分析:cuda runtime error (48)
This error usually means the PyTorch model (or the PyTorch library itself) was compiled for a GPU architecture that your device doesn't support. In plain terms, the code has instructions your GPU can't understand.
逐步解决方法
1. 确认你的GPU架构
First, check what GPU you have by running this in your terminal:
nvidia-smi
Note down your GPU model (e.g., NVIDIA GeForce RTX 3080, GTX 1060), then look up its CUDA Compute Capability (you can find this on NVIDIA's official specs for your GPU—for example, RTX 30 series uses 8.6, GTX 10 series uses 6.1).
2. 检查PyTorch的CUDA支持
Next, verify that your installed PyTorch version supports your GPU's compute capability. Run this in a Python shell:
import torch print(torch.cuda.get_device_capability())
This will output a tuple like (8, 6)—if this doesn't match your GPU's compute capability, your PyTorch installation isn't compatible with your hardware.
3. 重新安装匹配的PyTorch
Grab the installation command from the PyTorch official site that matches your CUDA version and GPU. For example, if you're using CUDA 11.8 and a GPU with compute capability 8.6, the pip command might look like:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
Adjust the command for conda if that's your package manager.
4. 修复模型加载(临时 workaround)
If you can't recompile/reinstall PyTorch right now, try loading the model onto CPU first, then moving it to your GPU. Open the eval.py script and find the line where the model is loaded—change it from:
model = torch.load(opt.model)
To:
model = torch.load(opt.model, map_location='cpu') model = model.cuda()
This will convert the model to run on your GPU, though it might be a bit slower than a native-compiled version.
5. 处理h5py警告
That h5py warning is just a compatibility issue between old h5py versions and Python 2.7 (which is no longer supported, by the way!). It doesn't break your code, but if you want to get rid of it:
- Upgrade h5py to the latest version compatible with Python 2.7:
pip install h5py==2.10.0 - Or, better yet, switch to Python 3.x—Python 2.7 is end-of-life, and most modern ML libraries (including newer PyTorch versions) don't support it anymore.
最后测试
Once you've made these changes, re-run your command:
CUDA_LAUNCH_BLOCKING=1 python eval.py --model model.pth --infos_path infos.pkl --image_folder blah --num_images 1
It should work without the CUDA error now!
内容的提问来源于stack exchange,提问作者Азат Султанов

