Docker【部署 05】docker使用tensorflow-gpu安装及调用GPU踩坑记录

这篇具有很好参考价值的文章主要介绍了Docker【部署 05】docker使用tensorflow-gpu安装及调用GPU踩坑记录。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.安装tensorflow-gpu

Building wheels for collected packages: tensorflow-gpu
  Building wheel for tensorflow-gpu (setup.py): started
  Building wheel for tensorflow-gpu (setup.py): finished with status 'error'
  Running setup.py clean for tensorflow-gpu
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [18 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-i6frcfa8/tensorflow-gpu_2cea358528754cc596c541f9c2ce45ca/setup.py", line 37, in <module>
          raise Exception(TF_REMOVAL_WARNING)
      Exception:

      =========================================================
      The "tensorflow-gpu" package has been removed!

      Please install "tensorflow" instead.

      Other than the name, the two packages have been identical
      since TensorFlow 2.1, or roughly since Sep 2019. For more
      information, see: pypi.org/project/tensorflow-gpu
      =========================================================


      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for tensorflow-gpu
Failed to build tensorflow-gpu

Other than the name, the two packages have been identical since TensorFlow 2.1 也就是说安装2.1版本的已经自带GPU支持。

2.Docker使用GPU

不同型号的GPU及驱动版本有所区别,环境驱动及CUDA版本如下:

[root@localhost ~]# nvidia-smi
# 查询结果
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.27.04    Driver Version: 460.27.04    CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+

2.1 Could not find cuda drivers

# 报错
I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.

在Docker容器中的程序无法识别CUDA环境变量,可以尝试以下步骤来解决这个问题:

  1. 检查CUDA版本:首先,需要确认宿主机上已经正确安装了CUDA。在宿主机上运行nvcc --version命令来检查CUDA版本。
  2. 使用NVIDIA Docker镜像:NVIDIA提供了一些预先配置好的Docker镜像,这些镜像已经包含了CUDA和其他必要的库。可以使用这些镜像作为Dockerfile的基础镜像。
  3. 设置环境变量:在Dockerfile中,可以使用ENV指令来设置环境变量。例如,如果CUDA安装在/usr/local/cuda目录下,可以添加以下行到Dockerfile中:ENV PATH /usr/local/cuda/bin:$PATH
  4. 使用nvidia-docker:nvidia-docker是一个用于运行GPU加速的Docker容器的工具。

检测CUDA版本是必要的,由于使用的是导出的镜像文件,2和3的方法无法使用,最终使用-e进行环境变量设置:

# 添加cuda的环境变量
-e PATH=/usr/local/cuda-11.2/bin:$PATH -e LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH

# 启动命令
nvidia-docker run --name deepface --privileged=true --restart=always --net="host" -e PATH=/usr/local/cuda-11.2/bin:$PATH -e LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH -v /root/.deepface/weights/:/root/.deepface/weights/ -v /usr/local/cuda-11.2/:/usr/local/cuda-11.2/ -d deepface_image

2.2 was unable to find libcuda.so DSO

I tensorflow/compiler/xla/stream_executor/cuda/cuda_diagnostics.cc:168] retrieving CUDA diagnostic information for host: localhost.localdomain
I tensorflow/compiler/xla/stream_executor/cuda/cuda_diagnostics.cc:175] hostname: localhost.localdomain
I tensorflow/compiler/xla/stream_executor/cuda/cuda_diagnostics.cc:199] libcuda reported version is: NOT_FOUND: was unable to find libcuda.so DSO loaded into this program
I tensorflow/compiler/xla/stream_executor/cuda/cuda_diagnostics.cc:203] kernel reported version is: 460.27.4

在Linux环境下,Docker可以支持将宿主机上的目录挂载到容器里。这意味着,如果宿主机上的目录包含软链接,那么这些软链接也会被挂载到容器中。然而,需要注意的是,这些软链接指向的路径必须在Docker容器中是可访问的。也就是说,如果软链接指向的路径没有被挂载到Docker容器中,那么在容器中访问这个软链接可能会失败。
原文链接:https://blog.csdn.net/u013546508/article/details/88637434,当前环境下问题解决步骤:

# 1.查找 libcuda.so 文件位置
find / -name libcuda.so*
# 查找结果
/usr/lib/libcuda.so
/usr/lib/libcuda.so.1
/usr/lib/libcuda.so.460.27.04
/usr/lib64/libcuda.so
/usr/lib64/libcuda.so.1
/usr/lib64/libcuda.so.460.27.04

# 2.查看LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH
# 查询结果
/usr/local/cuda/lib64

# 3.将64位的libcuda.so.460.27.04复制到LD_LIBRARY_PATH路径下【libcuda.so和libcuda.so.1都是软连接】
cp /usr/lib64/libcuda.so.460.27.04 /usr/local/cuda-11.2/lib64/

# 4.创建软连接
ln -s libcuda.so.460.27.04 libcuda.so.1
ln -s libcuda.so.1 libcuda.so

2.3 Could not find TensorRT&&Cannot dlopen some GPU libraries

I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
W tensorflow/core/common_runtime/gpu/gpu_device.cc:1960] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...

这个问题实际上是Docker镜像文件未安装TensorRT导致的,可以在Dockerfile里添加安装命令后重新构建镜像:

RUN pip install tensorrt -i https://pypi.tuna.tsinghua.edu.cn/simple

以下操作不推荐,进入容器进行安装:

# 1.查询容器ID
docker ps

# 2.在running状态进入容器
docker exec -it ContainerID /bin/bash

# 3.安装软件
pip install tensorrt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 4.提交新的镜像【可以将新的镜像导出使用】
docker commit ContainerID imageName:version

安装后的现象:

root@localhost:/app# python
Python 3.8.18 (default, Sep 20 2023, 11:41:31)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

# 使用tensorflow报错
>>> import tensorflow as tf
2023-10-09 10:15:55.482545: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-10-09 10:15:56.498608: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

# 先导入tensorrt后使用tensorflow看我用
>>> import tensorrt as tr
>>> import tensorflow as tf
>>> tf.test.is_gpu_available()
WARNING:tensorflow:From <stdin>:1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2023-10-09 10:16:41.452672: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1639] Created device /device:GPU:0 with 11389 MB memory:  -> device: 0, name: Tesla T4, pci bus id: 0000:2f:00.0, compute capability: 7.5
True

尝试解决,在容器启动要执行的py文件内加入以下代码,我将以下代码加入到app.py文件内:

import tensorrt as tr
import tensorflow as tf

if __name__ == "__main__":
    available = tf.config.list_physical_devices('GPU')
    print(f"available:{available}")

加入代码后的文件为:

# 3rd parth dependencies
import tensorrt as tr
import tensorflow as tf
from flask import Flask
from routes import blueprint

def create_app():
    available = tf.config.list_physical_devices('GPU')
    print(f"available:{available}")
    app = Flask(__name__)
    app.register_blueprint(blueprint)
    return app

启动容器:

nvidia-docker run --name deepface --privileged=true --restart=always --net="host" -e PATH=/usr/local/cuda-11.2/bin:$PATH -e LD_LIBRARY_PATH=/usr/local/cuda-11.2/lib64:$LD_LIBRARY_PATH -v /root/.deepface/weights/:/root/.deepface/weights/ -v /usr/local/cuda-11.2/:/usr/local/cuda-11.2/ -v /opt/xinan-facesearch-service-public/deepface/api/app.py:/app/app.py -d deepface_image

2.4 Could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED

E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:437] Could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED
E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:441] Memory usage: 1100742656 bytes free, 15843721216 bytes total.
E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:451] Possibly insufficient driver version: 460.27.4
W tensorflow/core/framework/op_kernel.cc:1828] OP_REQUIRES failed at conv_ops_impl.h:770 : UNIMPLEMENTED: DNN library is not found.

未安装cuDNN导致的问题,安装即可。

2.5 CuDNN library needs to have matching major version and equal or higher minor version

安装版本跟编译项目的版本不匹配,调整版本后成功使用GPU。文章来源地址https://www.toymoban.com/news/detail-713328.html

E tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:425] Loaded runtime CuDNN library: 8.1.1 but source was compiled with: 8.6.0.  CuDNN library needs to have matching major version and equal or higher minor version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.

到了这里,关于Docker【部署 05】docker使用tensorflow-gpu安装及调用GPU踩坑记录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 安装tensorflow-gpu

    打开anaconda prompt,添加镜像源: 删除镜像源使用: 创建虚拟环境并安装tensorflow-gpu: 查看一下包的版本: python是3.6.2版本的 在下面的网站中查找对应版本: 在 Windows 环境中从源代码构建  |  TensorFlow (google.cn) 对应的最低tensorflow-gpu是1.2.0版本的 选择一个合适的2.0.0版本的 但

    2024年02月07日
    浏览(35)
  • tensorflow-GPU环境搭建

    最近在学习AI相关知识,需要使用 gpu 运行 tensorflow 代码,选择在腾讯云上搭建环境 安装 tensorflow 与 GPU 支持需要对齐以下软件的版本号(按依赖顺序排列): GPU 驱动版本号 CUDA 版本号 cudnn 版本号 tensorflow 版本号 由于 tensorflow 位于最下游,因此更新时机最晚,因此实际安装

    2024年01月17日
    浏览(35)
  • tensorflow-gpu卸载 (windows)

    在安装 Tensorflow-gpu 时,如果, Tensorflow-gpu 、 Python 、 cuda 、 cuDNN 版本关系不匹配很容易安装出错,要重新安装的话,要把之前装的卸载干净! tensorflow-cpu卸载, 激活进入虚拟环境,在这里卸载: 进入虚拟环境安装路径: Proceed(y/n)? y 删除之前创建的虚拟环境(例子为删除名

    2024年02月05日
    浏览(42)
  • Windows安装tensorflow-gpu(1050Ti,cuda11.6,cuDNN7.6.5,python3.6,tensorflow-gpu2.3.0)

    参考:https://blog.csdn.net/qq_43215538/article/details/123852028 首先查看本机GPU对应的cuda版本,如下图所示,本机cuda版本为11.6,后面选择的cuda版本不要超过这里的版本就好。 https://developer.nvidia.com/cuda-toolkit-archive选择相应的cudatoolkit版本下载,如这里选择版本一定要注意,因为cuda版本

    2023年04月08日
    浏览(37)
  • MacOS M2:配置Tensorflow-GPU版

    本文主要介绍MacOS 12.x系统下的Tensorflow配置。总的来说,配置Mac版本的Tensorflow只需要三步:第一步配置一个虚拟环境,建议选择miniconda;第二步创建conda环境;第三步安装Tensorflow。 1、安装 Miniconda 1.1 彻底删除anaconda (1)下载  anaconda-clean   用于删除相关配置文件 2)删除

    2024年04月13日
    浏览(66)
  • win10 安装 tensorflow-gpu 2.10.0

    系统配置 系统 win10 x64 显卡 GTX 1660 Ti CUDA 12.2 cudnn 8.9 查看版本对应: https://tensorflow.google.cn/install/source_windows#gpu Version Python version Compiler Build tools cuDNN CUDA tensorflow_gpu-2.10.0 3.7-3.10 MSVC 2019 Bazel 5.1.1 8.1 11.2 这里查看我 系统配置 我安装 python 3.10 和 tensorflow_gpu-2.10.0 安装 conda 安装

    2024年02月14日
    浏览(35)
  • tensorflow,tensorflow-gpu, CUDA, cuDNN,Python, numpy对应版本

    本机GPU是NVIDIA GeForce GTX 1650, 支持的最高版本CUDA是12.0。想要在Windows下使用GPU跑深度学习模型,需要使用tensorflow-gpu,其目前最高版本是2.6.0。所以其他依赖环境均按照tensorflow-gpu的需求来配置。 Tensorflow-gpu 2.6.0 (目前为止最高版本) Python 3.9.18 (能够支持tensorflow-GPU的最高Python版

    2024年04月17日
    浏览(67)
  • 解决CUDA 11.6版本对应的tensorflow-gpu版本问题

    个人电脑相关配置版本信息 (超级超级新的版本,以至于适配方面花了很长时间来搞) cuda  11.6 cudnn  8.9.0 python  3.10 对应安装的gpu版本 tensorflow-gpu  2.10.0 对应代码 具体怎么安装的我已经放在文章底部啦,改镜像源什么的也不多说~ 感谢参考嘿(-v- ---------------------------------

    2024年02月08日
    浏览(41)
  • [conda]tf_agents和tensorflow-gpu安装傻瓜式教程

    1.打开终端或Anaconda Prompt(Windows用户)。 2.输入以下命令创建新的Python环境:    其中, env_name 是您想要创建的环境名称, version 是您想要安装的Python版本号。例如,要创建名为 python37 的Python 3.7环境,请输入以下命令: 3.进入到要使用的环境去使用pip安装应用 进入后显示如

    2024年02月11日
    浏览(34)
  • tensorflow-gpu 2.3.0安装 及 相关对应版本库安装(Anaconda安装)

    目录 如需转载,请标明出处,谢谢。 一、安装tensorflow-gpu2.3.0 二、配置其他相关的库 很多人以为安装完tensorflow-gpu就是一切都结束了,但是殊不知,python中的很多库,比如numpy,matplotlib等库,就与我们的tensorflow的版本有对应 总结 对于anaconda的下载,网上的教程很多,而且很

    2024年02月02日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包