Docker使用Caffe和GPU方法及挂载方法

本文详细介绍了在 Docker 中解决 CUDA driver version is insufficient for CUDA runtime version 错误的方法,包括使用 nvidia-docker 替代 docker,以及如何正确挂载宿主机文件,确保容器能正常运行 GPU 加速应用如 Caffe。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到报错:Check failed: error == cudaSuccess (35 vs. 0) CUDA driver version is insufficient for CUDA runtime version

参考:

CUDA driver version is insufficient for CUDA runtime version - ONLY DOCKER

Ok I found the solution right after creating the issue:
Instead of sudo docker run -v /home/ubuntu/nvDocker/:/data/ -p 5000:5000 nvidia/digits:6.0-rc I have to use sudo nvidia-docker run -v /home/ubuntu/nvDocker/:/data/ -p 5000:5000 nvidia/digits:6.0-rc

Maybe master/docs/DockerInstall.md should be updated.

从一开始用Docker建立container开始,就要用nvidia-docker 代替 docker。

如果要加载本地宿主机上的文件,直接使用

nvidia-docker run -ti -v /home/dock/Downloads:/usr/Downloads bvlc/caffe:gpu 

会出现类似 

error while loading shared libraries: libboost_system.so.1.54.0

的错误(可参考官网:https://github.com/BVLC/caffe/tree/master/docker)。解决方法可以用数据卷的挂载方法。

 

解决方法:

1. 首先建立数据卷container(可参考下文挂载方法二)。

创建一个普通的容器。用--name给他指定了一个名(不指定的话会生成一个随机的名子)。:

nvidia-docker run -v /home/dock/Downloads:/usr/Downloads  --name dataVol ubuntu64 /bin/bash

其中的ubuntu64可以为任意的Image name.

2. 随后建立要使用的container:

创建一个新的容器,来使用这个数据卷。

nvidia-docker run -it --volumes-from dataVol bvlc/caffe:gpu /bin/bash

此容器可以正常使用caffe及GPU。

 

 

 

(选读)挂载方法:

以下内容转载自:https://blog.youkuaiyun.com/huludan/article/details/52641090

 

REF:

https://blog.youkuaiyun.com/huludan/article/details/52641090

https://my.oschina.net/piorcn/blog/324202

http://blog.youkuaiyun.com/yangzhenping/article/details/43667785

 

方法一:docker可以支持把一个宿主机上的目录挂载到镜像里。

docker run -it -v /home/dock/Downloads:/usr/Downloads ubuntu64 /bin/bash

 

通过-v参数,冒号前为宿主机目录,必须为绝对路径,冒号后为镜像内挂载的路径。

现在镜像内就可以共享宿主机里的文件了。

默认挂载的路径权限为读写。如果指定为只读可以用:ro

docker run -it -v /home/dock/Downloads:/usr/Downloads:ro ubuntu64 /bin/bash

 

方法二:docker还提供了一种高级的用法。叫数据卷。

数据卷:“其实就是一个正常的容器,专门用来提供数据卷供其它容器挂载的”。感觉像是由一个容器定义的一个数据挂载信息。其他的容器启动可以直接挂载数据卷容器中定义的挂载信息。

看示例:

docker run -v /home/dock/Downloads:/usr/Downloads  --name dataVol ubuntu64 /bin/bash

 

创建一个普通的容器。用--name给他指定了一个名(不指定的话会生成一个随机的名子)。

再创建一个新的容器,来使用这个数据卷。

docker run -it --volumes-from dataVol ubuntu64 /bin/bash

 

--volumes-from用来指定要从哪个数据卷来挂载数据。

 

如何在Docker容器内外互相拷贝数据?

 

 

从容器内拷贝文件到主机上

  1. docker cp <containerId>:/file/path/within/container /host/path/target  

从主机上拷贝文件到容器内


参考自:

http://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container


1.用-v挂载主机数据卷到容器内

  1. docker run -v /path/to/hostdir:/mnt $container  
  2. 在容器内拷贝  
  3. cp /mnt/sourcefile /path/to/destfile  

2.直接在主机上拷贝到容器物理存储系统
A. 获取容器名称或者id :

  1. $ docker ps  

B. 获取整个容器的id

  1. $ docker inspect -f   '{{.Id}}'  步骤A获取的名称或者id  

C. 在主机上拷贝文件:

  1. $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE   
  2. 或者  
  3. $ sudo cp path-file-host /var/lib/docker/devicemapper/mnt/123abc<<id>>/rootfs/root  

例子:

  1. $ docker ps  
  2.   
  3. CONTAINER ID      IMAGE    COMMAND       CREATED      STATUS       PORTS        NAMES  
  4.   
  5. d8e703d7e303   solidleon/ssh:latest      /usr/sbin/sshd -D                      cranky_pare  
  6.   
  7. $ docker inspect -f   '{{.Id}}' cranky_pare  
  8.   
  9. or   
  10. $ docker inspect -f   '{{.Id}}' d8e703d7e303  
  11.   
  12. d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5  
  13.   
  14. $ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5  

3.用输入输出符

  1. docker run -i ubuntu /bin/bash -c 'cat > /path/to/container/file' < /path/to/host/file/  


或者

  1. docker exec -it <container_id> bash -c 'cat > /path/to/container/file' < /path/to/host/file/  
由于我以下模型推理代码是在docker容器中执行的,所以不能弹出窗口显示推理结果,请你帮我改成本地保存的形式保存推理结果 import mmcv from mmcv.runner import load_checkpoint from mmdet.apis import inference_detector, show_result_pyplot from mmdet.models import build_detector import warnings warnings.filterwarnings("ignore", message="\"ImageToTensor\" pipeline is replaced by") def main(): # Choose to use a config and initialize the detector\n", config = 'kneron-mmdetection/configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco.py' # Setup a checkpoint file to load\n", checkpoint = 'checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth' # Set the device to be used for evaluation\n", device='cuda:0' # Load the config\n", config = mmcv.Config.fromfile(config) # Set pretrained to be None since we do not need pretrained model here\n", config.model.pretrained = None # Initialize the detector\n", model = build_detector(config.model) # Load checkpoint\n", checkpoint = load_checkpoint(model, checkpoint, map_location=device) # Set the classes of models for inference\n", model.CLASSES = checkpoint['meta']['CLASSES'] # We need to set the model's cfg for inference\n", model.cfg = config # Convert the model to GPU\n", model.to(device) # Convert the model into evaluation mode\n", model.eval() img = 'kneron-mmdetection/demo/demo.jpg' result = inference_detector(model, img) show_result_pyplot(model, img, result, score_thr = 0.3) if __name__ == '__main__': main()
03-08
<think>好的,我需要帮助用户将他们的模型推理代码从弹出窗口显示结果改为本地保存。首先,我得仔细阅读他们的代码,看看哪里需要修改。 用户当前的代码使用`show_result_pyplot`函数来显示结果,但这个函数会在窗口中弹出图像,而他们在Docker容器中运行,所以无法显示窗口。因此,需要找到替代的方法来保存图像而不是显示它。 在MMDetection库中,通常处理结果保存的函数是`mmcv.imwrite`或者`mmdet.apis.show_result`的保存版本。查阅文档,发现`show_result_pyplot`主要用于显示,而保存结果可能需要将带有检测框的图像数组写入文件。 所以步骤应该是: 1. 使用`mmdet.apis.show_result`生成带有标注的图像数组,但设置`show=False`避免显示。 2. 指定保存路径,比如在代码所在目录创建`outputs`文件夹。 3. 使用`mmcv.imwrite`将图像数组写入文件。 接下来需要修改代码中的`show_result_pyplot`部分。原函数可能返回图像数据,或者需要直接生成图像数据。需要确认参数是否正确,比如传入模型、图像路径、结果阈值,然后得到绘制后的图像数组。 然后,确保保存路径存在,可能需要用`os.makedirs`创建目录。最后用`mmcv.imwrite`保存图像。 另外,检查是否需要导入额外的模块,比如`os`来处理路径,或者确认`mmcv`是否有必要的写入函数。用户当前的代码已经导入`mmcv`,所以应该可以直接使用`mmcv.imwrite`。 最后,测试修改后的代码是否在Docker容器中能正确保存图像,而不会尝试弹出窗口。确保保存路径在容器内部是可写的,或者挂载到宿主机的目录。 总结修改步骤: - 替换`show_result_pyplot`为生成图像数组的函数。 - 创建输出目录。 - 保存图像到指定路径。</think>我帮你修改代码,将显示结果改为本地保存。主要改动点如下(分步骤说明): 步骤1:导入mmcv的imwrite方法 ```python import mmcv ``` 步骤2:修改结果展示部分代码(原代码最后几行) ```python # 原始代码(会弹出窗口) # show_result_pyplot(model, img, result, score_thr = 0.3) # 修改后代码(保存结果到本地) output_path = './outputs/detection_result.jpg' # 设置保存路径 result_img = show_result_pyplot(model, img, result, score_thr=0.3, show=False) # 关闭显示 mmcv.imwrite(result_img, output_path) # 保存到本地 print(f'检测结果已保存至:{output_path}') ``` 步骤3:完整修改后的代码 ```python import mmcv from mmcv.runner import load_checkpoint import os # 新增导入用于路径处理 from mmdet.apis import inference_detector, show_result_pyplot from mmdet.models import build_detector import warnings warnings.filterwarnings("ignore", message="\"ImageToTensor\" pipeline is replaced by") def main(): # 初始化配置 config = 'kneron-mmdetection/configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco.py' checkpoint = 'checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth' device='cuda:0' # 构建模型 config = mmcv.Config.fromfile(config) config.model.pretrained = None model = build_detector(config.model) checkpoint = load_checkpoint(model, checkpoint, map_location=device) model.CLASSES = checkpoint['meta']['CLASSES'] model.cfg = config model.to(device) model.eval() # 推理保存 img = 'kneron-mmdetection/demo/demo.jpg' result = inference_detector(model, img) # 创建输出目录 os.makedirs('./outputs', exist_ok=True) # 保存结果(核心修改部分) output_path = './outputs/detection_result.jpg' result_img = show_result_pyplot(model, img, result, score_thr=0.3, show=False) mmcv.imwrite(result_img, output_path) print(f'检测结果已保存至:{output_path}') if __name__ == '__main__': main() ``` 修改说明: 1. 通过设置`show=False`禁止弹出窗口 2. 使用`mmcv.imwrite()`代替图形界面展示 3. 添加了输出目录自动创建功能(`os.makedirs`) 4. 输出结果会保存在项目根目录下的outputs文件夹 5. 添加了保存路径的打印提示 扩展建议: - 如果要保存原始分辨率结果,可以使用: ```python result_img = mmcv.imread(img) # 原始图像 model.show_result(img, result, out_file=output_path) # 保持原始尺寸 ``` - 可以添加时间戳防止覆盖: ```python from datetime import datetime timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output_path = f'./outputs/result_{timestamp}.jpg' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值