centos7下安装TensorFlow1.15 for Python2.7

前言:

  • TensorFlow作为一个现在来说比较成熟的机器学习框架还是比较强大的,但安装该框架对很多同学来说是比较困难的,大部分都是借助anaconda这个仓库管理工具来完成安装的(操作系统版本,Python版本,TensorFlow版本又是错综复杂的关系),不过这又出现了一个问题,anaconda又不太好安装,并且在Windows和linux下anaconda都有极大的不同(Windows需要安装一些类库,linux可能需要提高gcc版本以及操作系统内核版本),因为Python的相关源包基本都在国外的网站,对于国内来说,又是一个考验,必须要pip本地化才可以(说人话就是需要配置阿里云等等国内的镜像站),这又是一个不友好的地方。综上三点,这些都给TensorFlow的快速安装部署提出了比较大的挑战。
  • 基于以上问题,我们需要找出一个比较合适合理的快速安装TensorFlow框架的方法。现在就以基于centos7操作系统实现快速的可离线化安装TensorFlow 到Python2.7版本内的一个经典例子。

实验目标:

在centos7下的系统自带的Python2.7环境下快速部署一个可用的TensorFlow CPU类框架。

环境简介:

  • vm虚拟机搭建的最小化安装的一个centos7操作系统,该操作系统配置了以ISO安装包搭建的本地仓库
[root@centos1 ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
  • gcc6临时环境

gcc临时环境是使用的devtoolset-6

[root@centos1 ~]# ls /opt/rh/devtoolset-6/
enable  root


 

  • 编译安装所需的环境

编译安装环境指的是如下软件:

yum install gcc gcc-c++ zlib zlib-devel openssl openssl-devel cmake make python-devel -y
pip install Cython protobuf-c

 

  • 可连接互联网,pip可用

pip和setuptools以及wheel都是升级到最高版本。

[root@centos1 piprpm]# pip -V
pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7)
[root@centos1 piprpm]# pip list |grep setup
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
setuptools           44.1.1
[root@centos1 piprpm]# pip list |grep wheel
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
wheel                0.36.2

正式安装:

一,

gcc6激活和编译环境配置好,也就是上面的2 3 条。

二,

首先安装grpcio环境,此环境是TensorFlow的强依赖,并且该依赖需要gcc高版本支持,因此,选择使用gcc6快速搭建一个临时的gcc环境。安装命令为,编译wheel文件时间比较长,5到10分钟吧,虽然有报错,但echo $? 表示一切正常:

pip install grpcio-1.41.1.tar.gz


[root@centos1 ~]# pip install grpcio-1.41.1.tar.gz 
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Processing ./grpcio-1.41.1.tar.gz
Requirement already satisfied: six>=1.5.2 in /usr/lib/python2.7/site-packages (from grpcio==1.41.1) (1.16.0)
Requirement already satisfied: enum34>=1.0.4 in /usr/lib/python2.7/site-packages (from grpcio==1.41.1) (1.1.10)
Requirement already satisfied: futures>=2.2.0 in /usr/lib/python2.7/site-packages (from grpcio==1.41.1) (3.3.0)
Building wheels for collected packages: grpcio
  Building wheel for grpcio (setup.py) ... done
  Created wheel for grpcio: filename=grpcio-1.41.1-cp27-cp27mu-linux_x86_64.whl size=38482998 sha256=e637390884bd9c13bc474e0f60bc7da001c9f9e0095ee8d1b36a5a9c457a0ff8
  Stored in directory: /root/.cache/pip/wheels/1e/36/b9/7da52420708426cf222f34a63fad12d5acd0805350a75f0a2b
Successfully built grpcio
Installing collected packages: grpcio
  Attempting uninstall: grpcio
    Found existing installation: grpcio 1.41.1
    Uninstalling grpcio-1.41.1:
      Successfully uninstalled grpcio-1.41.1
ERROR: pip's legacy dependency resolver does not consider dependency conflicts when selecting packages. This behaviour is the source of the following dependency conflicts.
tensorflow 1.15.0 requires astor>=0.6.0, which is not installed.
tensorflow 1.15.0 requires backports.weakref>=1.0rc1; python_version < "3.4", which is not installed.
tensorflow 1.15.0 requires functools32>=3.2.3, which is not installed.
tensorflow 1.15.0 requires gast==0.2.2, which is not installed.
tensorflow 1.15.0 requires google-pasta>=0.1.6, which is not installed.
tensorflow 1.15.0 requires keras-applications>=1.0.8, which is not installed.
tensorflow 1.15.0 requires keras-preprocessing>=1.0.5, which is not installed.
tensorflow 1.15.0 requires mock>=2.0.0, which is not installed.
tensorflow 1.15.0 requires opt-einsum>=2.3.2, which is not installed.
tensorflow 1.15.0 requires tensorboard<1.16.0,>=1.15.0, which is not installed.
tensorflow 1.15.0 requires tensorflow-estimator==1.15.1, which is not installed.
tensorflow 1.15.0 requires termcolor>=1.1.0, which is not installed.
Successfully installed grpcio-1.41.1
[root@centos1 ~]# echo $?
0

此命令pip会自动解包并编译出一个whl文件,这个grpcio-1.41.1.tar.gz安装完成后需要重启服务器,重置gcc环境。

三,

pip install protobuf==3.15.4

TensorFlow将会使用到该包,但该包结合Python2.7环境来看,不能太高了,因此,该包指定了版本,如果使用高版本,测试的时候是会报错的哦。

四,

pip install tensorflow==1.15.0

pip install tensorflow==1.15.0
[root@centos1 ~]# pip install tensorflow==1.15.0
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: tensorflow==1.15.0 in /usr/lib64/python2.7/site-packages (1.15.0)
Collecting mock>=2.0.0
  Downloading http://mirrors.aliyun.com/pypi/packages/05/d2/f94e68be6b17f46d2c353564da56e6fb89ef09faeeff3313a046cb810ca9/mock-3.0.5-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: absl-py>=0.7.0 in /usr/lib/python2.7/site-packages (from tensorflow==1.15.0) (0.15.0)
Collecting backports.weakref>=1.0rc1; python_version < "3.4"
  Downloading http://mirrors.aliyun.com/pypi/packages/88/ec/f598b633c3d5ffe267aaada57d961c94fdfa183c5c3ebda2b6d151943db6/backports.weakref-1.0.post1-py2.py3-none-any.whl (5.2 kB)
Collecting keras-applications>=1.0.8
  Downloading http://mirrors.aliyun.com/pypi/packages/21/56/4bcec5a8d9503a87e58e814c4e32ac2b32c37c685672c30bc8c54c6e478a/Keras_Applications-1.0.8.tar.gz (289 kB)
     |████████████████████████████████| 289 kB 19.0 MB/s 
Requirement already satisfied: grpcio>=1.8.6 in /usr/lib64/python2.7/site-packages (from tensorflow==1.15.0) (1.41.1)
Requirement already satisfied: wrapt>=1.11.1 in /usr/lib64/python2.7/site-packages (from tensorflow==1.15.0) (1.14.1)
Collecting opt-einsum>=2.3.2
  Downloading http://mirrors.aliyun.com/pypi/packages/f6/d6/44792ec668bcda7d91913c75237314e688f70415ab2acd7172c845f0b24f/opt_einsum-2.3.2.tar.gz (59 kB)
     |████████████████████████████████| 59 kB 50.2 MB/s 
Collecting google-pasta>=0.1.6
  Downloading http://mirrors.aliyun.com/pypi/packages/59/22/38238cd9b83dd8a857abd7c907b8fe68ceff1611ab3ca5f0e80a5e025956/google_pasta-0.2.0-py2-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 46.8 MB/s 
Requirement already satisfied: protobuf>=3.6.1 in /usr/lib64/python2.7/site-packages (from tensorflow==1.15.0) (3.15.4)
Requirement already satisfied: numpy<2.0,>=1.16.0 in /usr/lib64/python2.7/site-packages (from tensorflow==1.15.0) (1.16.6)
Collecting tensorboard<1.16.0,>=1.15.0
  Downloading http://mirrors.aliyun.com/pypi/packages/d0/93/32ed4e1421885d3ca6bad68ff53d5310580e0ba30b1bd7d36c174a744207/tensorboard-1.15.0-py2-none-any.whl (3.8 MB)
     |████████████████████████████████| 3.8 MB 866 kB/s 
Requirement already satisfied: enum34>=1.1.6; python_version < "3.4" in /usr/lib/python2.7/site-packages (from tensorflow==1.15.0) (1.1.10)
Collecting gast==0.2.2
  Downloading http://mirrors.aliyun.com/pypi/packages/4e/35/11749bf99b2d4e3cceb4d55ca22590b0d7c2c62b9de38ac4a4a7f4687421/gast-0.2.2.tar.gz (10 kB)
Requirement already satisfied: six>=1.10.0 in /usr/lib/python2.7/site-packages (from tensorflow==1.15.0) (1.16.0)
Requirement already satisfied: wheel in /usr/lib/python2.7/site-packages (from tensorflow==1.15.0) (0.37.1)
Collecting keras-preprocessing>=1.0.5
  Downloading http://mirrors.aliyun.com/pypi/packages/79/4c/7c3275a01e12ef9368a892926ab932b33bb13d55794881e3573482b378a7/Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 16.7 MB/s 
Collecting termcolor>=1.1.0
  Downloading http://mirrors.aliyun.com/pypi/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz (3.9 kB)
Collecting tensorflow-estimator==1.15.1
  Downloading http://mirrors.aliyun.com/pypi/packages/de/62/2ee9cd74c9fa2fa450877847ba560b260f5d0fb70ee0595203082dafcc9d/tensorflow_estimator-1.15.1-py2.py3-none-any.whl (503 kB)
     |████████████████████████████████| 503 kB 976 kB/s 
Collecting astor>=0.6.0
  Downloading http://mirrors.aliyun.com/pypi/packages/c3/88/97eef84f48fa04fbd6750e62dcceafba6c63c81b7ac1420856c8dcc0a3f9/astor-0.8.1-py2.py3-none-any.whl (27 kB)
Collecting functools32>=3.2.3
  Downloading http://mirrors.aliyun.com/pypi/packages/c5/60/6ac26ad05857c601308d8fb9e87fa36d0ebf889423f47c3502ef034365db/functools32-3.2.3-2.tar.gz (31 kB)
Collecting funcsigs>=1; python_version < "3.3"
  Downloading http://mirrors.aliyun.com/pypi/packages/69/cb/f5be453359271714c01b9bd06126eaf2e368f1fddfff30818754b5ac2328/funcsigs-1.0.2-py2.py3-none-any.whl (17 kB)
Requirement already satisfied: h5py in /usr/lib64/python2.7/site-packages (from keras-applications>=1.0.8->tensorflow==1.15.0) (2.10.0)
Requirement already satisfied: futures>=2.2.0; python_version < "3.2" in /usr/lib/python2.7/site-packages (from grpcio>=1.8.6->tensorflow==1.15.0) (3.3.0)
Collecting markdown>=2.6.8
  Downloading http://mirrors.aliyun.com/pypi/packages/c0/4e/fd492e91abdc2d2fcb70ef453064d980688762079397f779758e055f6575/Markdown-3.1.1-py2.py3-none-any.whl (87 kB)
     |████████████████████████████████| 87 kB 14.9 MB/s 
Requirement already satisfied: setuptools>=41.0.0 in /usr/lib/python2.7/site-packages (from tensorboard<1.16.0,>=1.15.0->tensorflow==1.15.0) (44.1.1)
Collecting werkzeug>=0.11.15
  Downloading http://mirrors.aliyun.com/pypi/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
     |████████████████████████████████| 298 kB 15.5 MB/s 
Building wheels for collected packages: keras-applications, opt-einsum, gast, termcolor, functools32
  Building wheel for keras-applications (setup.py) ... done
  Created wheel for keras-applications: filename=Keras_Applications-1.0.8-py2-none-any.whl size=50702 sha256=195e629c6fcce6a10c32d59ca6127b6353df48aadd6ed98e4a0d63d8ecf0c03b
  Stored in directory: /root/.cache/pip/wheels/7e/0c/a4/cdb3f185b345935e5f69d487ec6f551e01a61933bba372805c
  Building wheel for opt-einsum (setup.py) ... done
  Created wheel for opt-einsum: filename=opt_einsum-2.3.2-py2-none-any.whl size=49881 sha256=ffbb42fd18186d309622bf29ada3ab29af0b05d1680acd80373c4f50b0a056bd
  Stored in directory: /root/.cache/pip/wheels/21/bf/00/1b61756abac89d1f165d66a8d4350454368c6bd1d7d5166e86
  Building wheel for gast (setup.py) ... done
  Created wheel for gast: filename=gast-0.2.2-py2-none-any.whl size=7539 sha256=25e061e8bc0d69304c5ac0edadfecb83958d779ba7dd622e01d6a7d890080315
  Stored in directory: /root/.cache/pip/wheels/3b/94/b9/73b4df847878d36d4fae46f4ffd1fc328857dbc0079da69e21
  Building wheel for termcolor (setup.py) ... done
  Created wheel for termcolor: filename=termcolor-1.1.0-py2-none-any.whl size=4831 sha256=e114760a2bb65cbcb14b9f0d47e7203d528b5d38c4737b4c73b4a1e3fc04a7f2
  Stored in directory: /root/.cache/pip/wheels/fe/9f/06/ca11b1a204459e78d843e2ff39a0614d1852e634e2bc2c7298
  Building wheel for functools32 (setup.py) ... done
  Created wheel for functools32: filename=functools32-3.2.3.post2-py2-none-any.whl size=14556 sha256=7c47d7566c009f0dd58673bc2835fe74fecca2bcc0b765188344b50c5610069a
  Stored in directory: /root/.cache/pip/wheels/b2/49/50/def56d039079add38ce0c9033e2da7e6b813856904862915e6
Successfully built keras-applications opt-einsum gast termcolor functools32
Installing collected packages: funcsigs, mock, backports.weakref, keras-applications, opt-einsum, google-pasta, markdown, werkzeug, tensorboard, gast, keras-preprocessing, termcolor, tensorflow-estimator, astor, functools32
Successfully installed astor-0.8.1 backports.weakref-1.0.post1 funcsigs-1.0.2 functools32-3.2.3.post2 gast-0.2.2 google-pasta-0.2.0 keras-applications-1.0.8 keras-preprocessing-1.1.2 markdown-3.1.1 mock-3.0.5 opt-einsum-2.3.2 tensorboard-1.15.0 tensorflow-estimator-1.15.1 termcolor-1.1.0 werkzeug-1.0.1
[root@centos1 ~]# echo $?
0

TensorFlow的版本不能超过1.15版本,否则将会报错如下:

from tensorflow.python.tools import module_util as _module_util ImportError:
找不到tools!!!!!!!!!

五,安装完毕后的验证:

vim test.py

import tensorflow as tf
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(cpus)

输出如下:

[root@centos1 ~]# python cpu.py 
2022-05-21 22:51:31.657312: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2022-05-21 22:51:31.657386: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303)
2022-05-21 22:51:31.657430: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (centos1): /proc/driver/nvidia/version does not exist
[PhysicalDevice(name=u'/physical_device:CPU:0', device_type=u'CPU')]

这里的报错说的是没有找到cuda模块,因为本次实验不是安装GPU版本(虚拟机,无独立显卡),因此,cuda计算框架是没有安装的,最后一行输出表明CPU版TensorFlow安装成功的。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2022年5月22日
下一篇 2022年5月22日

相关推荐