1. 程式人生 > >Windows10(64bit)環境下Python3 + Tensorflow安裝

Windows10(64bit)環境下Python3 + Tensorflow安裝

  TensorFlow 簡介[1]
  TensorFlow™ 是一個開放原始碼軟體庫,用於進行高效能數值計算。藉助其靈活的架構,使用者可以輕鬆地將計算工作部署到多種平臺(CPU、GPU、TPU)和裝置(桌面裝置、伺服器叢集、移動裝置、邊緣裝置等)。TensorFlow™ 最初是由 Google Brain 團隊(隸屬於 Google 的 AI 部門)中的研究人員和工程師開發的,可為機器學習和深度學習提供強力支援,並且其靈活的數值計算核心廣泛應用於許多其他科學領域。
  
  檢查Python環境,及各種包的安裝情況:

D:\Python\Python36>pip list
DEPRECATION
: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. absl-py (0.4.0) astor (0.7.1) click (6.7) cycler (0.10.0) decorator (4.3.0) Flask (1.0.2) gast (0.2.0) grpcio
(1.14.1) itsdangerous (0.24) Jinja2 (2.10) kiwisolver (1.0.1) Markdown (2.6.11) MarkupSafe (1.0) matplotlib (2.2.2) networkx (2.1) numpy (1.14.2+mkl) opencv-python (3.4.2.17) pandas (0.23.1) Pillow (5.1.0) pip (9.0.1) protobuf (3.6.1) pymongo (3.7.0) pyparsing (2.2.0) python-dateutil (2.7.2) pytz (2018.4) PyWavelets
(0.5.2) scikit-image (0.13.1) scikit-learn (0.19.1) scipy (1.0.1) setuptools (28.8.0) six (1.11.0) tensorboard (1.10.0) tensorflow (1.10.0) termcolor (1.1.0) Werkzeug (0.14.1) wheel (0.31.1) xgboost (0.72) You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.

  其中,grpcio、tensorflow、absl、astor、gast、markdown、protobuf、tensorboard、、 termcolor、wheel等是本次操作安裝內容,其他的是以前安裝過,其中過程詳解如下作者實踐的連結供參考:
  1.使用Python Matplotlib繪圖並輸出影象到檔案中的實踐,涉及到的包有:six、cycler、pyparsing、python-dateutil 、kiwisolver、numpy、pytz、matplotlib
  2.Python科學計算初探——餘弦相似度,涉及到的包有:numpy、 scipy
  3.Python sklearn決策樹演算法實踐,涉及到的包有:scikit-learn
  4.使用Python科學計算包搭建CNN演算法實踐(1),涉及到的包有:scikit_image、decorator、networkx、Pillow、PyWavelets
  5.XGBoost演算法應用入門學習實踐,涉及到的包有:xgboost
  6.Python基於Flask開發Restful API實踐,涉及到的包有:Jinja、 MarkupSafe、 Werkzeug、 click、 flask、 itsdangerous
  7.Python開發中使用Mongo DB入門實踐,涉及到的包有:pymongo

  直接安裝Tensorflow時,出現報錯:

D:\Python\Python36>pip install tensorflow
Collecting tensorflow
......
FileNotFoundError: [WinError 2] 系統找不到指定的檔案。: 'd:\\python\\python36\\Lib\\site-packages\\grpc'

  可能原因是:
這裡寫圖片描述

取消保護,並單獨安裝GRPC,如果不取消保護,仍安裝失敗!

D:\Python\Python36>pip install grpcio
Collecting grpcio
......
Installing collected packages: grpcio
Successfully installed grpcio-1.14.1
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

  再次執行安裝Tensorflow,安裝成功:

D:\Python\Python36>pip install tensorflow
Collecting tensorflow
......
Installing collected packages: absl-py, gast, markdown, wheel, protobuf, tensorboard, termcolor, astor, tensorflow
  Running setup.py install for absl-py ... done
  Running setup.py install for gast ... done
  Running setup.py install for termcolor ... done
Successfully installed absl-py-0.4.0 astor-0.7.1 gast-0.2.0 markdown-2.6.11 protobuf-3.6.1 tensorboard-1.10.0 tensorflow-1.10.0 termcolor-1.1.0 wheel-0.31.1
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

  驗證Tensorflow安裝,在 Python 互動式 shell 中輸入以下幾行簡短的程式程式碼:

D:\Python\Python36>python
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello ,TensorFlow')
>>> sess = tf.Session()
2018-08-21 13:51:54.348867: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
>>> print(sess.run(hello))

  如果系統輸出以下內容,說明您可以開始編寫 TensorFlow 程式了:

b'Hello ,TensorFlow'

  OpenCV簡介[2]
  
  OpenCV是一個基於BSD許可(開源)發行的跨平臺計算機視覺庫,可以執行在Linux、Windows、Android和Mac OS作業系統上。它輕量級而且高效——由一系列 C 函式和少量 C++ 類構成,同時提供了Python、Ruby、MATLAB等語言的介面,實現了影象處理和計算機視覺方面的很多通用演算法。

  影象處理,需要Opencv,在此安裝,很簡單,操作如下:

D:\Python\Python36>pip install opencv-python
Collecting opencv-python
......
Installing collected packages: opencv-python
Successfully installed opencv-python-3.4.2.17
You are using pip version 9.0.1, however version 18.0 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.