前言
TensorFlow 1.x仍然是一个成熟的深度学习框架,拥有广泛的应用和强大的社区支持。虽然TensorFlow 2.0已经发布,但很多用户对于其新特性还不熟悉,仍在使用老版本来完成自己的项目。此外,一些旧代码可能仅适用于TensorFlow 1.x版本,因此仍需要维护和更新。总之,TensorFlow 1.x仍然是一个重要的深度学习工具,在未来一段时间内将继续得到广泛的使用。
作为AI语言库的代表,TensorFlow是人工智能领域的重要工具之一,其使用方式比较复杂,安装时也可能会遇到各种问题。以下是一些可能遇到的困难及解决方法:
- 安装环境不兼容:TensorFlow需要特定版本的Python和相关依赖库,如果你没有正确安装这些组件,那么TensorFlow将无法正常工作。解决方法是确保你的Python版本在2.7.x或3.4+之间,并且安装了必要的依赖库。
- 缺失依赖库:除了Python以外,TensorFlow还需要许多其他库来运行。如果你缺少其中任何一个库,TensorFlow都无法正常工作。解决方法是安装所需的所有依赖库,并确保它们都被正确地配置。
- 安装失败:如果TensorFlow安装过程中出现错误,你需要仔细检查日志以找到问题所在。通常情况下,你可以通过重新运行安装程序来解决此问题。在重新运行安装程序之前,请确保删除之前安装的任何TensorFlow文件。
- 版本不匹配:如果你正在使用旧版本的TensorFlow,但尝试使用新代码,则可能会出现版本不匹配的问题。确保你正在使用与你的代码兼容的最新版本的TensorFlow。
因此,我们尝试用docker封装tensorflow1。
配置方法和问题
方法一,直接下载tensorflow/tensorflow镜像
docker pull tensorflow/tensorflow:1.15.5-gpu-py3
- 这个镜像是dockerhub上的,可以在Dockerfile构建时使用pip安装python库,但是使用apt安装时会报错,原因不明。
- 可以
docker exec -it bash
运行,也可以用pycharm构建docker解释器运行。 - 如果要和tensorlayer共用,要特别注意版本匹配问题
- opencv版本高了容易报找不到库的错,装3.4.8.29版本没有问题。
Dockerfile
# 针对srgan项目
FROM tensorflow/tensorflow:1.15.5-gpu-py3
LABEL author=xxx \
version=0 \
description='xxx'
RUN pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple \
&& pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple \
&& pip install easydict -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install tensorlayer==1.11.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install opencv-contrib-python==3.4.8.29 -i https://pypi.tuna.tsinghua.edu.cn/simple \
&& pip install opencv-contrib-python-headless==3.4.8.29 -i https://pypi.tuna.tsinghua.edu.cn/simple \
&& pip install opencv-python==3.4.8.29 -i https://pypi.tuna.tsinghua.edu.cn/simple
#opencv版本高了容易报找不到库的错,但是以下两个解决方法不能用
#RUN apt update && apt install -qqy libgl1
#RUN apt install -qqy libglib2.0-dev
方法二(推荐),用nvidia/cuda镜像从头安装
特别注意,cuda版本不能太高
- 拉取原始镜像
- 通过Dockerfile构建
- conda安装python3.6虚拟环境,
conda create -n xxx python=3.6
- 激活虚拟环境,
conda activate xxx
- pip安装
# 版本特别重要
pip install tensorflow-gpu==1.12.0
pip install tensorlayer==1.11.1
pip install opencv-python==3.4.8.29
注意
- python版本不能高
- cuda版本就选9.0,9.1都报错,提示就找9.0的库
- 注意降低numpy的版本,使tensorflow和tensorlayer都适用。
- 曾经试过tensorflow1.12.3,运行时有较长的时间卡住,改为1.12.0问题解决。
docker pull nvidia/cuda:9.0-cudnn7-devel-ubuntu16.04
Dockerfile
FROM nvidia/cuda:9.0-cudnn7-devel-ubuntu16.04
LABEL author=xxx \
version=0 \
description='xxx'
# # change resource 换清华源
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
# apt install
RUN apt-get clean && apt-get update --fix-missing && apt-get upgrade -y
RUN apt install -y wget curl openssh-server vim g++ zip sudo git net-tools iputils-ping \
&& apt-get autoclean \
&& apt-get autoremove
# ssh
# 修改ssh配置文件,文件位置/etc/ssh/sshd_config,添加允许所有主机连接,
RUN mkdir -p /var/run/sshd \
&& sed -i '1iPermitRootLogin yes' /etc/ssh/sshd_config
# 开放22端口
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
# 设置root密码
RUN echo "root:0"|chpasswd
# 安装miniconda
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O conda.sh \
&& /bin/bash conda.sh -b -p /opt/conda \
&& rm conda.sh
# && /opt/conda/bin/conda clean -tipsy
# root config
RUN mkdir ~/.pip \
&& echo '[global]\n\
index-url=https://pypi.tuna.tsinghua.edu.cn/simple\n\
[install]\n\
trusted-host=pypi.tuna.tsinghua.edu.cn\n'>>~/.pip/pip.conf
RUN echo '\n\
__conda_setup="$('/opt/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"\n\
if [ $? -eq 0 ]; then\n\
eval "$__conda_setup"\n\
else\n\
if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then\n\
. "/opt/conda/etc/profile.d/conda.sh"\n\
else\n\
export PATH="/opt/conda/bin:$PATH"\n\
fi\n\
fi\n\
unset __conda_setup\n'\
>> ~/.bashrc
# 配置cuda环境变量
RUN echo '\n\
export PATH=/usr/local/cuda/bin:$PATH\n\
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH\n'>> ~/.bashrc