tensorflow keras入门,深度学习跑起来

来源:互联网 发布:ubuntu文件服务器搭建 编辑:程序博客网 时间:2024/03/29 09:43

keras官方中文文档

说实话安装tensorflow的cuda支持版本的时候还是看英文的官方教程比较好,中文的太久了,安装不成功,而且似乎只能通过源码安装,需要安装java,bazel等工具,然后是cuda的驱动和cudnn,官网英文链接,按这个装才没有错,然后就是安装keras,这个可以按照中文文档来 keras中文文档,安装过程每台电脑也不一样,自己谷歌解决把,。然后我安装完了之后还是有一个dot_parser库没有安装成功,不过运行没有影响,还有老师的泰坦x跑起来是真的快,速度亲测比至强的cpu快百倍。。。。深度学习爽的不行

下面是跑自己的数据集和测试,网上都没有,官网也只有fit,没有预测的代码

下面是训练的代码,安装中文网站的猫狗分类写的,用于测试自己的前车识别图片

'''This script goes along the blog post"Building powerful image classification models using very little data"from blog.keras.io.It uses data that can be downloaded at:https://www.kaggle.com/c/dogs-vs-cats/dataIn our setup, we:- created a data/ folder- created train/ and validation/ subfolders inside data/- created cats/ and dogs/ subfolders inside train/ and validation/- put the cat pictures index 0-999 in data/train/cats- put the cat pictures index 1000-1400 in data/validation/cats- put the dogs pictures index 12500-13499 in data/train/dogs- put the dog pictures index 13500-13900 in data/validation/dogsSo that we have 1000 training examples for each class, and 400 validation examples for each class.In summary, this is our directory structure:```data/    train/        veh/            1.jpg            2.jpg            ...        noveh/            1.jpg            2.jpg            ...    validation/        veh/            1.jpg            2.jpg            ...        noveh/            1.jpg            2.jpg            ...```'''from keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Activation, Dropout, Flatten, Densefrom keras import backend as Kimport h5py# dimensions of our images.img_width, img_height = 64, 64train_data_dir = 'data/train'validation_data_dir = 'data/validation'nb_train_samples = 2000nb_validation_samples = 1000epochs = 30batch_size = 32if K.image_data_format() == 'channels_first':    input_shape = (3, img_width, img_height)else:    input_shape = (img_width, img_height, 3)#################################################################################model = Sequential()model.add(Conv2D(32, (3, 3), input_shape=input_shape))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(32, (3, 3)))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Conv2D(64, (3, 3)))model.add(Activation('relu'))model.add(MaxPooling2D(pool_size=(2, 2)))model.add(Flatten())model.add(Dense(64))model.add(Activation('relu'))model.add(Dropout(0.5))model.add(Dense(1))model.add(Activation('sigmoid'))model.compile(loss='binary_crossentropy',              optimizer='rmsprop',              metrics=['accuracy'])#################################################################################################################################################################### from keras.optimizers import SGD# model = Sequential()# # input: 100x100 images with 3 channels -> (100, 100, 3) tensors.# # this applies 32 convolution filters of size 3x3 each.# model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))# model.add(Conv2D(32, (3, 3), activation='relu'))# model.add(MaxPooling2D(pool_size=(2, 2)))# model.add(Dropout(0.25))# model.add(Conv2D(64, (3, 3), activation='relu'))# model.add(Conv2D(64, (3, 3), activation='relu'))# model.add(MaxPooling2D(pool_size=(2, 2)))# model.add(Dropout(0.25))# model.add(Flatten())# model.add(Dense(256, activation='relu'))# model.add(Dropout(0.5))# model.add(Dense(1, activation='softmax'))# sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)# model.compile(loss='binary_crossentropy', optimizer=sgd)############################################################################################################################################################ model = Sequential()# model.add(Dense(64, input_dim=20, activation='relu'))# model.add(Dropout(0.5))# model.add(Dense(64, activation='relu'))# model.add(Dropout(0.5))# model.add(Dense(1, activation='sigmoid'))# model.compile(loss='binary_crossentropy',#               optimizer='rmsprop',#               metrics=['accuracy'])########################################################################## this is the augmentation configuration we will use for trainingtrain_datagen = ImageDataGenerator(    rescale=1. / 255,    shear_range=0.1,    zoom_range=0.1,    horizontal_flip=True)# this is the augmentation configuration we will use for testing:# only rescalingtest_datagen = ImageDataGenerator(rescale=1. / 255)train_generator = train_datagen.flow_from_directory(    train_data_dir,    target_size=(img_width, img_height),    batch_size=batch_size,    class_mode='binary')validation_generator = test_datagen.flow_from_directory(    validation_data_dir,    target_size=(img_width, img_height),    batch_size=batch_size,    class_mode='binary')model.fit_generator(    train_generator,    steps_per_epoch=nb_train_samples // batch_size,    epochs=epochs,    validation_data=validation_generator,    validation_steps=nb_validation_samples // batch_size)#model.save_weights('first_try.h5')model.save('first_try.h5')


注意最后可以只save_weight,但是在用的时候需要加载模型的框架,然后自己再载入模型weight,不方便,不如直接全部保留下来,然后是使用训练好的模型预测

也可以通过下面的方式保存模型

import h5py from keras.models import model_from_json  json_string = model.to_json()  open('my_model_architecture.json','w').write(json_string)  model.save_weights('my_model_weights.h5')  
加载模型

model = model_from_json(open('my_model_architecture.json').read())  model.load_weights('my_model_weights.h5')  


然后是加载模型进行预测

import kerasfrom keras.models import load_modelfrom keras.models import Sequentialfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_imgfrom keras import backend as Kimport cv2import numpy as np import h5pyimport os#model = Sequential()model = load_model('first_try.h5')num_test=1000img = load_img('pic/0.png')  # this is a PIL imagexx = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)#print xx#x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 64, 64)# print xx.shape# print K.image_data_format()batch_x = np.zeros((num_test,) + xx.shape, dtype=K.floatx())for i in range(num_test):    #path='pic/'+str(i)+'.png';    path='/home/cx/Desktop/tensorflow_test/the_first_keras/data/train/no_veh/'+str(i)+'.png'    #print path    img = load_img(path)  # this is a PIL image    x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)    #img2=array_to_img(img)    # cv2.imshow('test',x)    # cv2.waitKey(0)    batch_x[i] = x# for i in range(num_test):#     img = array_to_img(batch_x[i], scale=True)#     fname = '{prefix}_{index}_{hash}.{format}'.format(prefix='test',#                                                     index= i,#                                                     hash=np.random.randint(1e4),#                                                     format='png')#     img.save(os.path.join('pic2', fname))#classes = model.predict_proba(batch_x)classes = model.predict_classes(batch_x)count=0print '\n'for i in classes:    if (i==[1]):        count+=1print count#print classes


不出意外的话效果是不好的,,毕竟这个多层神经网络很简单,并不能很好的分类,下一篇我们用预训练的高级网络再来跑自己的数据集

0 0