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')
也可以通过下面的方式保存模型
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
- tensorflow keras入门,深度学习跑起来
- keras深度学习入门
- 深度学习之安装 TensorFlow Keras
- 【Tensorflow keras】轻量级深度学习框架 Keras简介
- 【Tensorflow keras】Keras:基于Theano和TensorFlow的深度学习库
- 深度学习环境搭建 ubuntu16+GTX1080+CUDA8+Theano+Tensorflow+keras
- Keras: 基于Theano和TensorFlow的快速深度学习库
- Ubuntu下Python深度学习TensorFlow+Theana+Keras配置文档
- win7 远程配置ubuntu14 python keras tensorflow 深度学习
- R已经可以进行深度学习!!!tensorflow&keras
- Windows+Anaconda+tensorflow+keras深度学习框架搭建
- 深度学习框架keras安装(后端基于Tensorflow/theano)
- 【深度学习】Anaconda+Tensorflow+Keras Win7 64位安装i
- 深度学习库大排名:TensorFlow、Keras、caffe稳居前三
- centos 7 下搭建 tensorflow+keras 深度学习环境
- 深度学习tensorflow入门-CNN
- 2、Tensorflow:TensorFlow深度学习入门(上)
- 3、Tensorflow:TensorFlow深度学习入门(下)
- 程序运行时内存分布
- 多线程(十八)AQS(四):CLH同步队列
- Android百分比布局Percent支持库
- Android本地Json配置文件实现笔记
- ParallaxViewPager:ViewPager的视差背景效果
- tensorflow keras入门,深度学习跑起来
- 出现 com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error 异常的原因及解决方法
- bzoj 1770 [Usaco2009 Nov]lights 燈
- RecyclerView:带header的grid
- RecyclerView:带header和footer的grid
- 自定义TabBar
- RecyclerView:带header&footer&加载更多
- 用户登录记住密码
- 【Linux网络编程】IO模型与服务器模型