本文介绍深度学习的环境搭建,该环境包含 keras,tensorflow,python,pip,cv2,numpy, jupyter 等,适合 Python 学习、深度学习、运行一段 Python 代码程序等
1. 安装 Miniconda
进入 Miniconda官网,根据操作系统,点击下载对应的 bash 文件。我的是 Mac 操作系统,选择如下图所示的 bash 文件
这个文件 50 多M,稍等下载完成。打开终端,进入命令行执行这个 bash 文件
$ bash Miniconda3-latest-MacOSX-x86_64.sh
安装时会显示使用条款,长按 “Enter” 继续阅读,不想继续阅读可以 按 “Q”, 退出阅读。之后提示是否同意条款,输入 yes
$ bash ./Miniconda3-latest-MacOSX-x86_64.sh
Welcome to Miniconda3 4.6.14
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
...
Do you accept the license terms? [yes|no]
[no] >>> yes
接下来会询问安装位置,按 Enter
Miniconda3 will now be installed into this location:
/Users/simon/miniconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/Users/simon/miniconda3] >>>
PREFIX=/Users/simon/miniconda3
installing: python-3.7.3-h359304d_0 ...
Python 3.7.3
installing: ca-certificates-2019.1.23-0 ...
installing: libcxxabi-4.0.1-hcfea43d_1 ...
installing: xz-5.2.4-h1de35cc_4 ...
installing: yaml-0.1.7-hc338f04_2 ...
installing: zlib-1.2.11-h1de35cc_3 ...
installing: libcxx-4.0.1-hcfea43d_1 ...
installing: openssl-1.1.1b-h1de35cc_1 ...
...
installing: wheel-0.33.1-py37_0 ...
installing: pip-19.0.3-py37_0 ...
installing: pyopenssl-19.0.0-py37_0 ...
installing: urllib3-1.24.1-py37_0 ...
installing: requests-2.21.0-py37_0 ...
installing: conda-4.6.14-py37_0 ...
installation finished.
接下来会询问是否初始化,输入 yes
Do you wish the installer to initialize Miniconda3
by running conda init? [yes|no]
[yes] >>> yes
no change /Users/simon/miniconda3/condabin/conda
no change /Users/simon/miniconda3/bin/conda
...
no change /Users/simon/miniconda3/lib/python3.7/site-packages/xonsh/conda.xsh
no change /Users/simon/miniconda3/etc/profile.d/conda.csh
no change /Users/simon/.bash_profile
No action taken.
If you'd prefer that conda's base environment not be activated on startup,
set the auto_activate_base parameter to false:
conda config --set auto_activate_base false
Thank you for installing Miniconda3!
至此,Miniconda3 就安装好了
conda 和 pip 默认使用国外站点来下载软件,我们可以配置国内镜像来加速下载
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
如果镜像不可用了,可以执行上条命令设置其他可用镜像。或取消设置,使用原来的国外站点即可
$ pip config unset global.index-url
2. 创建环境
在终端输入命令,稍等片刻,输入 y
$ conda create -n myenv python
Collecting package metadata: done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.6.14
latest version: 4.7.11
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /Users/simon/miniconda3/envs/myenv
added / updated specs:
- python
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2019.5.15 | 1 133 KB
certifi-2019.6.16 | py37_1 155 KB
openssl-1.1.1c | h1de35cc_1 3.4 MB
pip-19.2.2 | py37_0 1.9 MB
python-3.7.4 | h359304d_1 21.6 MB
setuptools-41.0.1 | py37_0 635 KB
sqlite-3.29.0 | ha441bb4_0 2.4 MB
wheel-0.33.4 | py37_0 39 KB
------------------------------------------------------------
Total: 30.2 MB
The following NEW packages will be INSTALLED:
ca-certificates pkgs/main/osx-64::ca-certificates-2019.5.15-1
certifi pkgs/main/osx-64::certifi-2019.6.16-py37_1
libcxx pkgs/main/osx-64::libcxx-4.0.1-hcfea43d_1
libcxxabi pkgs/main/osx-64::libcxxabi-4.0.1-hcfea43d_1
libedit pkgs/main/osx-64::libedit-3.1.20181209-hb402a30_0
libffi pkgs/main/osx-64::libffi-3.2.1-h475c297_4
ncurses pkgs/main/osx-64::ncurses-6.1-h0a44026_1
openssl pkgs/main/osx-64::openssl-1.1.1c-h1de35cc_1
pip pkgs/main/osx-64::pip-19.2.2-py37_0
python pkgs/main/osx-64::python-3.7.4-h359304d_1
readline pkgs/main/osx-64::readline-7.0-h1de35cc_5
setuptools pkgs/main/osx-64::setuptools-41.0.1-py37_0
sqlite pkgs/main/osx-64::sqlite-3.29.0-ha441bb4_0
tk pkgs/main/osx-64::tk-8.6.8-ha441bb4_0
wheel pkgs/main/osx-64::wheel-0.33.4-py37_0
xz pkgs/main/osx-64::xz-5.2.4-h1de35cc_4
zlib pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3
Proceed ([y]/n)? y
Downloading and Extracting Packages
pip-19.2.2 | 1.9 MB | ##################################### | 100%
python-3.7.4 | 21.6 MB | ##################################### | 100%
wheel-0.33.4 | 39 KB | ##################################### | 100%
openssl-1.1.1c | 3.4 MB | ##################################### | 100%
sqlite-3.29.0 | 2.4 MB | ##################################### | 100%
certifi-2019.6.16 | 155 KB | ##################################### | 100%
setuptools-41.0.1 | 635 KB | ##################################### | 100%
ca-certificates-2019 | 133 KB | ##################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate myenv
#
# To deactivate an active environment, use
#
# $ conda deactivate
这样我们创建好了一个名称为 myenv 的环境。这个环境已经包含 python,pip,sqlite 等包了。根据上图提示激活 myenv 环境,在终端输入命令,进入 myenv 环境
$conda activate myenv
(myenv) MacBook-Pro-6:~ simon$ conda deactivate
注意此时命令行前面有(myenv)
3. 安装需要的包(目前的环境已经安装好 pip,python ,sqlite, numpy 等)
安装 request
$ sudo pip install request
安装 cv2
注意 cv2 的包名为 opencv-python,其他大部分情况下直接 pip install XXX 即可,XXX为代码中 import 进来的包。如果不成功可以问 Google,度娘。
$ sudo pip install opencv-python
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting opencv-python
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8d/ff/13e77ee7ac431f831e20d81a6bf0214ca1cf550cf9b575e3213e14325c81/opencv_python-4.1.0.25-cp37-cp37m-macosx_10_7_x86_64.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (52.1MB)
|████████████████████████████████| 52.1MB 14.1MB/s
Requirement already satisfied: numpy>=1.14.5 in /Users/simon/miniconda3/envs/myenv/lib/python3.7/site-packages (from opencv-python) (1.17.1)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.1.0.25
查看已经安装的包
$ conda list
# packages in environment at /Users/simon/miniconda3/envs/myenv:
#
# Name Version Build Channel
ca-certificates 2019.5.15 1
certifi 2019.6.16 py37_1
get 2019.4.13 pypi_0 pypi
libcxx 4.0.1 hcfea43d_1
libcxxabi 4.0.1 hcfea43d_1
libedit 3.1.20181209 hb402a30_0
libffi 3.2.1 h475c297_4
ncurses 6.1 h0a44026_1
numpy 1.17.1 pypi_0 pypi
opencv-python 4.1.0.25 pypi_0 pypi
openssl 1.1.1c h1de35cc_1
pip 19.2.2 py37_0
post 2019.4.13 pypi_0 pypi
public 2019.4.13 pypi_0 pypi
python 3.7.4 h359304d_1
query-string 2019.4.13 pypi_0 pypi
readline 7.0 h1de35cc_5
request 2019.4.13 pypi_0 pypi
setuptools 41.0.1 py37_0
sqlite 3.29.0 ha441bb4_0
tk 8.6.8 ha441bb4_0
wheel 0.33.4 py37_0
xz 5.2.4 h1de35cc_4
zlib 1.2.11 h1de35cc_3
可以看到我们已经安装了 numpy,python,pip,sqliste 等,还有刚安装的 request 和 opencv-python 。
4. 安装深度学习环境(如果不需要深度学习可以跳过这一步)
安装 Keras
sudo pip install keras
安装 TensorFlow
$ sudo pip install tensorflow
安装 Python 科学套件,Numpy(已经安装,可以不安装)、Matplotlib 和 SciPy
$ sudo pip install numpy
$ sudo pip install matplotlib
$ sudo pip install scipy
安装 jupyter
$ sudo pip install jupyter
5. 迫不及待要使用了?让我们试试吧!
安装完成后,需要让conda生效。需要运行一次 source ~/.bash_profile 或重启命令行应用
$ source ~/.bash_profile
确保进入了 myenv 环境。如果没进入,输入命令,进入 myenv 环境
$conda activate myenv
使用 Python 命令行,终端输入命令 python
(myenv) simon$ python
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello wrold!")
hello wrold!
>>> a = 1
>>> b = 2
>>> a + b
3
退出 Python 命令行,输入exit()
>>> exit()
运行一个 Python 文件。我们先创建一个文件,命名为 hello.py,插入下面代码
# -*- coding: utf-8 -*-
print("hellor world!")
sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
print("1 - 10 的 和 为:" + str(sum))
运行这个文件
(myenv) $ python hello.py
hellor world!
1 - 10 的 和 为:55
运行一个深度学习例子,比如我的上篇文章 深度学习入门(二)训练并使用 Keras 模型
(myenv) python train.py --dataset dataset --model pokedex.model --labelbin lb.pickle
Using TensorFlow backend.
...
Epoch 1/100
19/19 [==============================] - 24s 1s/step - loss: 2.7443 - acc: 0.4880 - val_loss: 1.9026 - val_acc: 0.6494
Epoch 2/100
19/19 [==============================] - 22s 1s/step - loss: 1.2445 - acc: 0.6388 - val_loss: 1.9894 - val_acc: 0.6299
Epoch 3/100
19/19 [==============================] - 22s 1s/step - loss: 1.0905 - acc: 0.6419 - val_loss: 1.1154 - val_acc: 0.7403
Epoch 4/100
6/19 [========>.....................] - ETA: 14s - loss: 0.9375 - acc: 0.6667
运行一个 jupyter notebook 文件
在终端输入命令, jupyter notebook,启动 jupyter notebook。(执行此命令时,当前目录要在包含需要运行的 jupyter notebook 文件的目录)
(myenv) $ jupyter notebook
[I 14:26:40.774 NotebookApp] 启动notebooks 在本地路径: /Users/simon/AI/
[I 14:26:40.774 NotebookApp] 本程序运行在: http://localhost:8888/?token=c1e6ffcd4777553fa639431ab699ecc4f1b2c2856265df17
[I 14:26:40.775 NotebookApp] or http://127.0.0.1:8888/?token=c1e6ffcd4777553fa639431ab699ecc4f1b2c2856265df17
[I 14:26:40.775 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).
[C 14:26:40.786 NotebookApp]
To access the notebook, open this file in a browser:
file:///Users/simon/Library/Jupyter/runtime/nbserver-767-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=c1e6ffcd4777553fa639431ab699ecc4f1b2c2856265df17
or http://127.0.0.1:8888/?token=c1e6ffcd4777553fa639431ab699ecc4f1b2c2856265df17
将在浏览器打开 jupyter notebook 主页
选择一个 notebook 文件,(或点击右边新建按钮,可以新建一个 notebook 文件)
点击 Run 按钮,注意要从头开始按顺序运行每一个 notebook cell 里面的代码。运行结果如下
6. 退出当前环境
$ conda deactivate
7. 总结
通过以上步骤,我们已经安装好了 Python 环境,可以学习深度学习,Python 学习,运行一段 Python 代码等。
扫码关注公众号,欢迎大家关注交流