Windows10 64Bit完美配置Theano并实现GPU加速并解决((CNMeM is disable,CuDNN not available))

原文转自:http://blog.csdn.net/voidfaceless/article/details/53711550

感谢原作者的辛勤工作!

---------------------------------------------------------------------------------------------------------------------------------------------





换了台本本需要重新配置,重新来过遇到好多问题,花了一天时间解决了,而且还发现自己还附带把之前安装时出现的两个问题解决掉了(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等。 
安装方法:

  1. 打开CMD(注意是windows命令提示符,并不是进入到python环境下,否则会提示语法错误,因为conda命令就是在windows下面执行的。);
  2. 输入conda install mingw libpython,然后回车,会出现安装进度,稍等片刻即可安装完毕。此时就有D:\Anaconda2\MinGw目录了。

五、配置环境变量。

  1. 编辑用户变量中的path变量(如果没有就新建一个,一般会有的),在后边追加D:\Anaconda2;D:\Anaconda2\Scripts; 不要漏掉分号,此处因为我的Anaconda的安装目录是D:\Anaconda2,此处需要根据自己的安装目录填写。
  2. 在用户变量中新建变量pythonpath,变量值为D:\Anaconda2\Lib\site-packages\theano; ,此处就是指明安装的theano的目录是哪,但是现在咱们还没有安装,所以不着急,先写完再说。
  3. 打开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
  4. 最好重启下电脑(那就一定要重启!TVT)

六、安装Theano
不需要手动下载zip等压缩包,直接用命令安装最简单。

    1. 打开CMD,方法和安装MinGw一样,不要进入python。
    2. 输入pip install theano回车后就是赏心悦目的下载进度条,这个很小,所以安装的比较快。
      • 如果安装出现了pip命令不能识别的问题: Unable to create process using '""
      • 用 python -m pip install theano来代替
    3. 在cmd中,输入python 进入到python环境下,然后先输入import theano回车,需要等一段时间。
    4. 继续输入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编程的显卡型号

如果支持的话按照下面的步骤配置:
  1. 要装一个vs2010,能支持大部分的cuda版本。 装vs2010的时候,可以只选择装C++语言就够。还是那句话,其他的版本可能出错你永远装不上!
  2. 下载cuda,如果你的Anaconda(python)是64位的,那就下载cuda64bit的;否则下载32bit的。其次,如果你的是笔记本,那就选择notebook,否则选择桌面版。下载地址:https://developer.nvidia.com/cuda-toolkit-archive
    我下载的是cuda8.0
  3. 安装Cuda8.0,安装的时候,选择“自定义安装”,安装全部功能,还有要安装到默认位置最好,安装很简单,可能需要点时间。
  4. 安装完后,打开环境变量应该会多出来2个变量,CUDA_PATH_V8_0和CUDA_PATH.
  5. 打开cmd控制台命令行,输入命令nvcc –V回车(注意是大写V)就可以查看版本信息,如果安装正确会显示Cuda的版本号。

  6. 置.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 

  7. 然后就测试一下就行了,这样应该能安装成功。 代码如下:

    [python]  view plain  copy
    1. from theano import function, config, shared, sandbox  
    2. import theano.tensor as T  
    3. import numpy  
    4. import time  
    5.   
    6. vlen = 10 * 30 * 768  # 10 x #cores x # threads per core  
    7. iters = 1000  
    8.   
    9. rng = numpy.random.RandomState(22)  
    10. x = shared(numpy.asarray(rng.rand(vlen), config.floatX))  
    11. f = function([], T.exp(x))  
    12. print (f.maker.fgraph.toposort())  
    13. t0 = time.time()  
    14. for i in range(iters):  
    15.     r = f()  
    16. t1 = time.time()  
    17. print ('Looping %d times took' % iters, t1 - t0, 'seconds')  
    18. print ('Result is', r)  
    19. if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):  
    20.     print ('Used the cpu')  
    21. else:  
    22.     print ('Used the gpu')  

八、仅仅这样的话,可能会出现using gpu device 0: Geforce GTX 965M(CNMeM is disable,CuDNN not available)这样的情况!
有的人说不影响使用,但是我的情况确实是不能使用!!所以我跋山涉水历经千辛万苦终于解决了这个问题了!方法如下:
  1. 修改配置.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 
  2. 安装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

  1. 打开CMD,方法和安装MinGw一样,不要进入python。
  2. 输入pip install theano,回车后就是赏心悦目的下载进度条。

  同样pip命令识别不了,用的 python -m pip install keras代替

    注:在Anaconda Prompt中是识别pip命令的,上述两个pip命令也可以直接在这里面装,效果是一样的。

测试keras程序:

[python]  view plain  copy
  1. '''''Trains a simple convnet on the MNIST dataset. 
  2. Gets to 99.25% test accuracy after 12 epochs 
  3. (there is still a lot of margin for parameter tuning). 
  4. 16 seconds per epoch on a GRID K520 GPU. 
  5. '''  
  6.   
  7. from __future__ import print_function  
  8. import numpy as np  
  9. np.random.seed(1337)  # for reproducibility  
  10.   
  11. from keras.datasets import mnist  
  12. from keras.models import Sequential  
  13. from keras.layers import Dense, Dropout, Activation, Flatten  
  14. from keras.layers import Convolution2D, MaxPooling2D  
  15. from keras.utils import np_utils  
  16.   
  17. batch_size = 128  
  18. nb_classes = 10  
  19. nb_epoch = 12  
  20.   
  21. # input image dimensions  
  22. img_rows, img_cols = 2828  
  23. # number of convolutional filters to use  
  24. nb_filters = 32  
  25. # size of pooling area for max pooling  
  26. nb_pool = 2  
  27. # convolution kernel size  
  28. nb_conv = 3  
  29.   
  30. # the data, shuffled and split between train and test sets  
  31. (X_train, y_train), (X_test, y_test) = mnist.load_data()  
  32.   
  33. X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)  
  34. X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)  
  35. X_train = X_train.astype('float32')  
  36. X_test = X_test.astype('float32')  
  37. X_train /= 255  
  38. X_test /= 255  
  39. print('X_train shape:', X_train.shape)  
  40. print(X_train.shape[0], 'train samples')  
  41. print(X_test.shape[0], 'test samples')  
  42.   
  43. # convert class vectors to binary class matrices  
  44. Y_train = np_utils.to_categorical(y_train, nb_classes)  
  45. Y_test = np_utils.to_categorical(y_test, nb_classes)  
  46.   
  47. model = Sequential()  
  48.   
  49. model.add(Convolution2D(nb_filters, nb_conv, nb_conv,  
  50.                         border_mode='valid',  
  51.                         input_shape=(1, img_rows, img_cols)))  
  52. model.add(Activation('relu'))  
  53. model.add(Convolution2D(nb_filters, nb_conv, nb_conv))  
  54. model.add(Activation('relu'))  
  55. model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))  
  56. model.add(Dropout(0.25))  
  57.   
  58. model.add(Flatten())  
  59. model.add(Dense(128))  
  60. model.add(Activation('relu'))  
  61. model.add(Dropout(0.5))  
  62. model.add(Dense(nb_classes))  
  63. model.add(Activation('softmax'))  
  64.   
  65. model.compile(loss='categorical_crossentropy',  
  66.               optimizer='adadelta',  
  67.               metrics=['accuracy'])  
  68.   
  69. model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,  
  70.           verbose=1, validation_data=(X_test, Y_test))  
  71. score = model.evaluate(X_test, Y_test, verbose=0)  
  72. print('Test score:', score[0])  
  73. 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,根据自己的路径,找到对应的目录,在该目录下应该会有,没有的话就创建一个就好了。

至此,你就可以开心的使用啦!!!
给两个教程学习一下:

十一、参考链接
3.http://www.cnblogs.com/zhaopengcheng/p/5992911.html
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值