Win7+theano+CUDA+Keras血泪史

来源:互联网 发布:js添加一行表格数据 编辑:程序博客网 时间:2024/05/18 00:17

之前弄过caffe,但是caffe用来做自己的数据太矫情和麻烦了,据说Keras简单点。现在按http://blog.csdn.net/niuwei22007/article/details/49045909配置,在自习室的台式机上,根据http://blog.csdn.net/niuwei22007/article/details/47684673装了VS2015 然后装Anaconda装的时候好慢啊。。。。这样就装好MinGw了。接着就是theano输入theano.test()后报了点错

http://blog.csdn.net/niuwei22007/article/details/47684673说可以忽略 我就忽略了。

这样就OK了

然后看了下学校自习室的电脑 竟然不是NVIDIA的  装不了CUDA 于是又只能在公司的电脑上重来一次了。公司电脑上是VS2013 我根据自己的显卡型号装的CUDA7.5可是竟然提示错了??于是我又把原来的CUDA 卸载了重装一次。。。心是崩溃的。。。但重装后还是这样  于是跳过这一步 按照http://blog.csdn.net/m624197265/article/details/45700619运行particle.sln的实例  因为我没找到.exe  所以运行了这个 目录是在C:\Program Files\NVIDIA Corporation\Installer2\CUDASamples_7.5.{F073AFAC-37AB-4C61-85C7-302041E69D36}\5_Simulations\particles下   和作者说的目录不一样  我运行后  证明是可以运行的  那应该就是CUDA可以了吧

然后我没有继续按照这个博客手动配置环境变量 因为之前已经有CUDA-PATH了 就直接进行第7步:然后

可是右下角出现的是use cpu  不是gpu!!!!!!!!!!!!!!我的心是拒绝看到的。。。。。。天哪心里一万匹马奔腾而过。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

重来吧按照http://blog.csdn.net/xuhang0910/article/details/47906949这个人的重新安装!

CUDA7.5傻瓜式安装 后nvcc和-V之间有空格!!!!之前我输入的都是nvcc-V难怪错了 注意空格!按照他的一直安装到这步 开始出错了。。。??

我又尝试了很多人写的安装教程 然而。。。又搞了一天 体会了哀莫大于心死的感觉。。。把之前的所有卸载 添加的环境变量也删除。。。电脑干净。从头配置  按照http://blog.csdn.net/hjimce/article/details/46654229这个人的:

一路配置到一直下去。。。可是结果呢还是CPU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我已经疯了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。import theano检查下

出来的是这个提示!!!!我已经疯了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

昨天是我第一次加班 加班到公司只剩我一人到晚上八点。。。一转身 外面天都黑了。。。就为了这个东西。。。又饿又累又困又烦躁。。。连我自己都被自己感动了。。。

现在是北京时间我终于装成功了。。。我要祭奠这个伟大的时刻  括号里的信息可以不管  import theano终于出来显卡型号了   激动得我手都颤抖了。。。连忙马上打开 spyder那个例子看 终于出来gpu了!!!!!!!!!!!!!!!!!!!!!!!!!!看现在的时间比之前cpu的时间少了N多有没有!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!我要去拿卫生纸擦眼泪了,,,激动到我痛哭流涕!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

但这是我自己瞎弄的  我也不知道自己刚刚具体改了什么使得它成功了   我只能说我参考了http://www.cnblogs.com/zhang-pengcheng/p/4553672.html设置这个环境变量  但加代码不知道在哪里加  所以我没加  还有http://blog.csdn.net/hjimce/article/details/46654229我按照他的运行到第4步时候  我没在dos下装VCForPython.msi 因为老是不行  所以我是自己手动装的  而且把stdint.h放在你说的目录下 但我的C:\Program Files (x86)\Common Files\Microsoft目录下没有Visual C++ for Python\9.0\ 我只在C盘找到:C:\用户\admin\AppData\Local\Pragrams\Common\Microsoft\Visual C++ for Python\9.0\include然后我就将stdint.h放在这里了。。。然后关机重启  它就成功了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

在此,给Win7+theano+CUDA+VS的苦行僧一个忠告:

        每次设置完环境变量记得重启

        多找几个教程试  也许像我一样在快疯了时 莫名其妙就成功了

        不要轻易放弃 哪怕你已经觉得自己要挂了  哪怕搞了N天快崩溃了

        我最终有很大一部分是按照http://blog.csdn.net/hjimce/article/details/46654229他的来配置的 跳过了dos下装VCForPython 自己手动装  当然还结合了http://www.cnblogs.com/zhang-pengcheng/p/4553672.html这个人设置环境变量的这里  


不说了  去擦眼泪。。。血泪史啊。。。

最后就是Keras的安装了  只要上面正确了这个就很简单了  直接在cmd下  输入pip install keras 就行了

接下来按照http://blog.csdn.net/zhoubl668/article/details/45559955测试手写数字mnist


把准确率显示出来:

看 最后一次迭代后 准确率97.56%  。。。。。。。。。。。。。。。。

至此 所有已经安装完毕。我还要去哭一会儿。。。。。。。。为了庆祝 决定给自己买一只心水很久的口红。。。

今天又星期四了,马上又周末了,好happy。用Keras下的CNN实现一个二分类:

参考资料:

http://www.360doc.com/content/16/0714/09/1317564_575383829.shtml  介绍了Keras:Sequential模型下的一些方法 参数含义等

http://keras-cn.readthedocs.io/en/latest/blog/image_classification_using_very_little_data/    Keras中文文档介绍

http://keras-cn.readthedocs.io/en/latest/blog/image_classification_using_very_little_data/
http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
http://www.360doc.com/content/16/0714/09/1317564_575383829.shtml
http://backnode.github.io/tag/cnn.html
http://blog.csdn.net/tina_ttl/article/details/51034821
http://blog.csdn.net/eagelangel/article/details/50759993
http://blog.csdn.net/hjimce/article/details/49095199
http://blog.csdn.net/u012162613/article/details/45397033

http://dataunion.org/16787.html

http://blog.csdn.net/niuwei22007/article/details/49207187

http://old.sebug.net/paper/books/scipydoc/numpy_intro.html   Keras的一些对数据的处理

上面都介绍的是与Keras有关的   下面的这些也是我收藏的要看的:

http://blog.csdn.net/zouxy09/article/details/14222605 介绍机器学习的

http://www.csdn.net/article/2015-08-27/2825549    介绍卷积神经网络的

好了,还是看看我参考上面的 来对自己的图片分类  我的两类:kuang:160张  stone:160张  总320张用来做训练   另有valiation文件夹下kuang:32  stone:32张做验证    图片大小不统一   所以都先统一到300X300大小  图片都是3通道的:

"""
Created on Thu Jul 28 14:15:32 2016
http://keras-cn.readthedocs.io/en/latest/blog/image_classification_using_very_little_data/
http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
http://www.360doc.com/content/16/0714/09/1317564_575383829.shtml
http://backnode.github.io/tag/cnn.html
http://blog.csdn.net/tina_ttl/article/details/51034821
http://blog.csdn.net/eagelangel/article/details/50759993
http://blog.csdn.net/hjimce/article/details/49095199
http://blog.csdn.net/u012162613/article/details/45397033
@author: admin
"""
from keras.preprocessing.image import ImageDataGenerator,img_to_array, load_img
#from mydata import load_mydata
#import numpy as np
#from keras.utils import np_utils
#import random
#from keras.callbacks import EarlyStopping
#import fit_generator
datagen = ImageDataGenerator(rotation_range=0,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')
img = load_img('train/kuang/0.bmp')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1,save_to_dir='preview', save_prefix='kuang', save_format='bmp'):
    i += 1
    if i > 20:
        break 
###########################################
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
#第一个模型,一个很简单的3层卷积加上ReLU激活函数,再接max-pooling
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3,300,300)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# the model so far outputs 3D feature maps (height, width, features)
#then两个全连接网络,并以单个神经元和sigmoid激活,二分类使用binary_crossentropy作为损失函数
model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
##################################################################
#开始准备数据,使用.flow_from_directory()来从我们的图片中直接产生数据和标签
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
        'train',  # this is the target directory
        target_size=(300,300),  # all images will be resized to 150x150
        batch_size=32,
        class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
        'valiation',
        target_size=(300,300),
        batch_size=32,
        class_mode='binary')
##################################################################
#用这个生成器来训练网络
model.compile(loss="binary_crossentropy", optimizer="rmsprop",metrics=["accuracy"])
model.fit_generator(train_generator,samples_per_epoch=320,nb_epoch=5,validation_data=validation_generator,nb_val_samples=64)
#model.save_weights('first_try.h5')  # always save your weights after training or during training
########################################################################################################
#data, label = load_mydata()
#nb_class = 2
#data = np.reshape(data,(384,3,300,300))
#label = np_utils.to_categorical(label, nb_class)
##############################################################

#下面是之前自己想直接从数据fit 但报错  还是用上面的从图片fit  不报错
#model.compile(loss="binary_crossentropy", optimizer="rmsprop",metrics=["accuracy"])
#np.random.seed(1024)
#index = [i for i in range(len(data))]
#random.shuffle(index)
#data = data[index]
#label = label[index]
#(X_train,X_val) = (data[0:300],data[300:])
#(Y_train,Y_val) = (label[0:300],label[300:])
#使用early stopping返回最佳epoch对应的model
#early_stopping = EarlyStopping(monitor='val_loss', patience=1)
#model.fit(X_train, Y_train, batch_size=32,show_accuracy=True,validation_data=(X_val, Y_val),nb_epoch=5,callbacks=[early_stopping])
#cPickle.dump(model,open("./model.pkl","wb"))

结果:

果然是图片太少了吗 还是卷积层等参数不合理   准确率偏低   但目前不管了 二分类的模型是类似这样就行  不报错我就很感谢了   等我把深度学习 神经网络理论了解了再来调整。

又快周末了 欧耶  心情好 放几张我最喜欢的脑残动漫《元气少女缘结神》巴卫和奈奈生:

巴卫变成小孩的样子真是可爱死了。。。



2 0
原创粉丝点击