换了台本本需要重新配置,重新来过遇到好多问题,花了一天时间解决了,而且还发现自己还附带把之前安装时出现的两个问题解决掉了(CNMeM is disable,CuDNN not available)。下面上步骤:
一、需求的软件及环境:
1.Anaconda2:点击下载
2.VS2010:这个一定要这个版本,其他版本我不确定会不会出错!我开始用的12版的装了好几遍都不行!!
3.win10 64bit
二、卸载之前版本。
把之前单独安装的Python等统统卸载掉。因为Anaconda2里边包含了python。
三、安装Anaconda2。
这个炒鸡简单,安装目录我用的是的 D:\Anaconda2 。这个特别要注意:安装路径千万不要有空格!!!血的教训
四、安装MinGw。
其他教程讲在环境变量中添加 path D:\Anaconda2\MinGW\bin;D:\Anaconda2\MinGW\x86_64-w64-mingw32\lib; ,但是你会发现 D:\Anaconda2\ 下面根本没有MinGw这个目录,所以最好的方法就是用命令安装,不需要自己下载什么mingw-steup.exe等。
安装方法:
- 打开CMD(注意是windows命令提示符,并不是进入到python环境下,否则会提示语法错误,因为conda命令就是在windows下面执行的。);
- 输入conda install mingw libpython,然后回车,会出现安装进度,稍等片刻即可安装完毕。此时就有D:\Anaconda2\MinGw目录了。
五、配置环境变量。
- 编辑用户变量中的path变量(如果没有就新建一个,一般会有的),在后边追加D:\Anaconda2;D:\Anaconda2\Scripts; 不要漏掉分号,此处因为我的Anaconda的安装目录是D:\Anaconda2,此处需要根据自己的安装目录填写。
- 在用户变量中新建变量pythonpath,变量值为D:\Anaconda2\Lib\site-packages\theano; ,此处就是指明安装的theano的目录是哪,但是现在咱们还没有安装,所以不着急,先写完再说。
- 打开cmd,会看到窗口里边有个路径,我的是C:\Users\Administrator>,根据自己的路径,找到对应的目录,在该目录下新建一个文本文档.theanorc.txt (注意有两个“.”),编辑它,写入以下内容:
[global]
openmp=False
[blas]
ldflags=
[gcc]
cxxflags=-ID:\Anaconda2\MinGW
其中红体字部分是你安装的Anaconda的路径,一定不要弄错。否则找不到MinGw。
配置文件的命名一定要注意!有些朋友的电脑是刚装的,默认不显示常用后缀名。如果你的电脑不显示后缀名,那么你就要命名为.theanorc了。因为当你显示后缀了以后,就是.theanorc.txt了。很多朋友什么都配置好了,也不报错,仍然不可以使用gpu,很多原因就在于此,他们的配置文件名称为.theanorc.txt.txt - 最好重启下电脑(那就一定要重启!TVT)
六、安装Theano
不需要手动下载zip等压缩包,直接用命令安装最简单。
- 打开CMD,方法和安装MinGw一样,不要进入python。
- 输入pip install theano,回车后就是赏心悦目的下载进度条,这个很小,所以安装的比较快。
-
如果安装出现了pip命令不能识别的问题: Unable to create process using '""
-
用 python -m pip install theano来代替
-
- 在cmd中,输入python 进入到python环境下,然后先输入import theano回车,需要等一段时间。
- 继续输入theano.test()。又会输出好长一段信息,只要没有error就说明安装成功了。我安装时等了一段时间还在输出,我就ctrl+c退出了。(其实我发现,有部分error信息也没有关系,theano的功能也可以正常使用,包括theano.function(),所以如果有同学无论如何配置还是有error信息的话,可以暂时忽略掉,直接跑一段程序试一下,可以去测试一下卷积操作运算代码。
七、配置GPU加速(先按照本教程安装好theano)
首先检查电脑显卡是否支持GPU加速,如果不支持下面的1,2,3,4就不用看了!!!
这一步至关重要,在确定了电脑显卡支持GPU加速后,再进行后面【5】【6】步操作,不然费时费力而且还没有结果。就目前而言,似乎只有NVIDIA的GF8级别以上的显卡才能支持physx物理加速(即GPU加速,这里所说的GPU加速均默认是CUDA编程),ATI的显卡不支持。
如何确认自己电脑的GPU是否支持CUDA编程?在设备管理器中找到显示适配器(Display adapters),找到自己电脑的显卡型号,然后到http://www.nvidia.com/object/cuda_gpus.html列表(如图1所示)中进行比对,若在列表中则支持CUDA编程,否则就利用Theano框架所编写深度网络就只能在CPU上面运行了。
图1 支持CUDA编程的显卡型号
如果支持的话按照下面的步骤配置:- 要装一个vs2010,能支持大部分的cuda版本。 装vs2010的时候,可以只选择装C++语言就够。还是那句话,其他的版本可能出错你永远装不上!
- 下载cuda,如果你的Anaconda(python)是64位的,那就下载cuda64bit的;否则下载32bit的。其次,如果你的是笔记本,那就选择notebook,否则选择桌面版。下载地址:https://developer.nvidia.com/cuda-toolkit-archive
我下载的是cuda8.0 - 安装Cuda8.0,安装的时候,选择“自定义安装”,安装全部功能,还有要安装到默认位置最好,安装很简单,可能需要点时间。
- 安装完后,打开环境变量应该会多出来2个变量,CUDA_PATH_V8_0和CUDA_PATH.
-
打开cmd控制台命令行,输入命令nvcc –V回车(注意是大写V)就可以查看版本信息,如果安装正确会显示Cuda的版本号。
-
配置.theanorc.txt,如下:
[global]
openmp=False
device = gpu
floatX = float32
allow_input_downcast=True
[blas]
ldflags=
[gcc]
cxxflags=-ID:\Anaconda2\MinGW
[nvcc]
flags = -LD:\Anaconda2\libs #此处是Anaconda的路径
compiler_bindir = D:\Microsoft Visual Studio 11.0\VC\bin#此处一定要和你安装的VS的路径保持一致
fastmath = True -
然后就测试一下就行了,这样应该能安装成功。 代码如下:
from theano import function, config, shared, sandbox import theano.tensor as T import numpy import time vlen = 10 * 30 * 768 # 10 x #cores x # threads per core iters = 1000 rng = numpy.random.RandomState(22) x = shared(numpy.asarray(rng.rand(vlen), config.floatX)) f = function([], T.exp(x)) print (f.maker.fgraph.toposort()) t0 = time.time() for i in range(iters): r = f() t1 = time.time() print ('Looping %d times took' % iters, t1 - t0, 'seconds') print ('Result is', r) if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]): print ('Used the cpu') else: print ('Used the gpu')
有的人说不影响使用,但是我的情况确实是不能使用!!所以我跋山涉水历经千辛万苦终于解决了这个问题了!方法如下:
- 修改配置.theanorc.txt
加上这一条:
[lib]
cnmem = 1
注意:网上有的配置文件中没有[lib]这个块,后面导入theano时会出现CNMeM is disabled提示。
如果你有了这个还是出现CNMeM is disabled,那么只要把cnmem = 1后面的1改小一下就可以了,我改成0.8就可以了
最终的.theanorc.txt文件是:
[global]
openmp=False
device = gpu
floatX = float32
allow_input_downcast=True
[lib]
cnmem = 0.8
[blas]
ldflags=
[gcc]
cxxflags=-IC:\Anaconda2\MinGW
[nvcc]
flags = -LC:\Anaconda2\libs
compiler_bindir = D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
fastmath = True
-
安装cudnn
到网上搜索cudnn下载下来(官网下载好像要注册才行。我的是从CSDN下载下来的)。将下载来的文件解压,解压出cuda文件夹,里面包含3个文件夹。将设三个文件夹替换掉系统里面的对应文件,进行覆盖替换即可。C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
覆盖掉后面导入theano时会就不会出现CuDNN not available提示了。
最后是这样的Using gpu device 0: GeForce GTX 965M (CNMeM is enabled with initial size: 80.0% of memory, cuDNN 5105)
完美解决!
九、深度学习框架Keras
- 打开CMD,方法和安装MinGw一样,不要进入python。
- 输入pip install theano,回车后就是赏心悦目的下载进度条。
同样pip命令识别不了,用的 python -m pip install keras代替
注:在Anaconda Prompt中是识别pip命令的,上述两个pip命令也可以直接在这里面装,效果是一样的。
测试keras程序:
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
batch_size = 128
nb_classes = 10
nb_epoch = 12
# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
因为我用的是theano,而keras默认使用tensorflow。切换方法有英文资料
Switching from one backend to another
If you have run Keras at least once, you will find the Keras configuration file at:
~/.keras/keras.json
If it isn't there, you can create it.
The default configuration file looks like this:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
Simply change the field backend to either "theano" or "tensorflow", and Keras will use the new configuration next time you run any Keras code.
简单说就是把~/.keras/keras.json文件改成如下:
{
"image_dim_ordering": "th",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}
如果找不到这个文件:打开cmd,会看到窗口里边有个路径,我的是C:\Users\xuke,根据自己的路径,找到对应的目录,在该目录下应该会有,没有的话就创建一个就好了。