深度学习
开发环境准备
配置
- 硬件
- 主机:Dell XPS 15 9550
- CPU:i7-6700HQ @ 2.60GHz
- GPU 1:Intel(R) HD Graphics 530
- GPU 2:NVIDIA GeForce GTX 960M
- 内存:16GB
- 硬盘:265G SSD + 1TB 机械硬盘
- 软件
- 操作系统:Windows 10 教育版
CUDA
CUDA官网 下载地址
目标平台:
Windows + x86_64 + 10 + exe(local)
下载:
- Base Installer
- Patch 1 (Released Jan 25, 2018)
- Patch 2 (Released Feb 27, 2018)
- Patch 3 (Released Mar 5, 2018)
分别安装。
安装目录位于:C:\Program Files\NVIDIA GPU Computing Toolkit
cuDNN
cuDNN的全称为NVIDIA CUDA® Deep Neural Network library,是NVIDIA专门针对深度神经网络(Deep Neural Networks)中的基础操作而设计基于GPU的加速库。cuDNN为深度神经网络中的标准流程提供了高度优化的实现方式,例如convolution、pooling、normalization以及activation layers的前向以及后向过程。
基本上所有的深度学习框架都支持cuDNN这一加速工具,例如:Caffe、Caffe2、TensorFlow、Torch、Pytorch、Theano等。
Caffe可以通过修改Makefile.config中的相应选项来修改是否在编译Caffe的过程中编译cuDNN,如果没有编译cuDNN的话,执行一些基于Caffe这一深度学习框架的程序速度上要慢3-5倍(Caffe官网上说不差多少,明明差很多嘛)。Caffe对cuDNN的版本不是很严格,只要大于cuDNN 4就可以。
TensorFlow目前的版本r1.2,强行要求装cuDNN,而且对版本也有相应的限制(cuDNN 5.1)。不过就官方说明看,TensorFlow将在下一个版本r1.3中加入对cuDNN 6.0的支持。
Torch通过LuaJit可以自动检测目前系统中的cuDNN版本来进行相应的编译(如没有cuDNN,也可在进行Torch的安装)。
下载
下载地址
https://developer.nvidia.com/cudnn(需要注册或登录,然后还要填一个调查问卷)
下载后,解压到 CUDA 目录下即可。
TensorFlow
创建一个虚拟环境,然后安装TensorFlow1
2
3python -m virtualenv env_ml
env_ml\Scripts\activate.bat
pip install tensorflow
安装过程有点慢,需要一点耐心。
如果系统中安装了 CUDA (写此文档时,tensorflow 的版本为 1.6.0 ,需要CUDA的版本为 9.0),可以安装tensorflow的GPU版本:1
pip install tensorflow-gpu
此外,还需要安装一些可选的库(根据需要来)1
pip install scipy numpy matplotlib
Theano
注意:Theano需要使用 g++,在windows下,可能需要设置为其它 C++ 编译器。
安装,在python虚拟环境中:
···bash
pip install theano
···
Keras
安装,在python虚拟环境中:1
pip install keras
检查一下安装是否成功:1
2
3
4
5
6
7
8(python) λ python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>> print(keras.__version__)
2.1.5
>>>
PyTorch
目前PyTorch还不支持自动下载安装,需要下载文件 torch-0.3.0b0+591e73e-cp36-cp36m-win_amd64.whl
下载后,执行安装命令:1
pip install torch-0.3.0b0+591e73e-cp36-cp36m-win_amd64.whl
检查一下安装是否成功,顺便检查一下GPU(CUDA)是否可用:1
2
3>>> import torch
>>> print(torch.cuda.is_available())
True
scikit-learn
1 | pip install scikit-learn scikit-image |
摄像头和图像识别
安装 opencv
1 | pip install opencv-python |
安装 torchvision
1 | pip install torchvision |
附录
安装pyyaml时,出现错误(UnicodeDecodeError)
注意,在 win10 环境下,安装 Keras 或 PyTorch 时,会自动先安装pyyaml,此时,会出现错误(UnicodeDecodeError):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37(python) λ pip install keras
Collecting keras
Using cached Keras-2.1.5-py2.py3-none-any.whl
Requirement already satisfied: numpy>=1.9.1 in d:\home\ml\python\lib\site-packages (from keras)
Requirement already satisfied: scipy>=0.14 in d:\home\ml\python\lib\site-packages (from keras)
Collecting pyyaml (from keras)
Using cached PyYAML-3.12.tar.gz
Requirement already satisfied: six>=1.9.0 in d:\home\ml\python\lib\site-packages (from keras)
Building wheels for collected packages: pyyaml
Running setup.py bdist_wheel for pyyaml ... error
Failed building wheel for pyyaml
Running setup.py clean for pyyaml
Failed to build pyyaml
Installing collected packages: pyyaml, keras
Running setup.py install for pyyaml ... error
Exception:
Traceback (most recent call last):
File "d:\home\ml\python\lib\site-packages\pip\compat\__init__.py", line 73, in console_to_str
return s.decode(sys.__stdout__.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 72: invalid continuation byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\home\ml\python\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "d:\home\ml\python\lib\site-packages\pip\commands\install.py", line 342, in run
prefix=options.prefix_path,
File "d:\home\ml\python\lib\site-packages\pip\req\req_set.py", line 784, in install
**kwargs
File "d:\home\ml\python\lib\site-packages\pip\req\req_install.py", line 878, in install
spinner=spinner,
File "d:\home\ml\python\lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
line = console_to_str(proc.stdout.readline())
File "d:\home\ml\python\lib\site-packages\pip\compat\__init__.py", line 75, in console_to_str
return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 72: invalid continuation byte
找到pip的安装目录(在虚拟环境目录python\Lib\site-packages\pip\compat\)中的文件1
__init__.py
line 73 替换为:1
2#return s.decode(sys.__stdout__.encoding)
return s.decode(subprocess_encoding(), errors="replace")
并加入函数定义:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23def subprocess_encoding():
if WINDOWS:
if sys.version_info >= (3, 6):
return "oem"
# Prior to Python 3.6, sys.__stdout__ is opened
# with the OEM code page (at least in the console
# interpreter, which is what pip uses). This changed
# in Python 3.6 to be UTF8, but we don't care as we
# use the explicit "oem" encoding in that case via
# the code above
return sys.__stdout__.encoding
else:
import locale
# Note that the use of getpreferredencoding here calls
# setlocale, which isn't thread safe. This is OK, as we
# never call this code in a multi-threaded context.
# If thread safety was important, we could call
# getpreferredencoding(False), but there are apparently
# some systems where that will not give the correct answer.
#
# We fall back to UTF8 if locale can't provide an answer,
# as UTF8 is the most common encoding used nowadays.
return locale.getpreferredencoding() or "utf-8"
Keras切换不同的后端
Keras运行一次后,会在 home 目录下创建 .keras 目录来保存自己的配置信息。
要切换不同的后端,修改其中的配置文件 keras.json。
使用tensorflow作为后端:1
2
3
4
5
6{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}
使用 theano 作为后端:
···json
{
“floatx”: “float32”,
“epsilon”: 1e-07,
“backend”: “theano”,
“image_data_format”: “channels_last”
}`