首先在此之前,你可能已经见到过各种和CUDA有关的错误,比如:
1、CUDA error: CUBLAS_STATUS_NOT_INITIALIZED when calling `cublasCreate(handle)`
2、RuntimeError: CUDA error: device-side assert triggered CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
这些不管能不能看懂,强烈建议先换成cpu再跑一遍,显示出真实的错误原因。
#device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
device = torch.device('cpu')
可见报错是因为加载参数过程中出现的错误,权重的size不匹配:
比如我这里是加载了一个resnet的预训练模型,但是从网上下载的时候把3D的模型下成了2D的,可以看到参数少了一个维度,重新下载之后就解决啦。