说实话安装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/data
In 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/dogs
So 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 ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import h5py
# dimensions of our images.
img_width, img_height = 64, 64
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 1000
epochs = 30
batch_size = 32
if 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 training
train_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 rescaling
test_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 keras
from keras.models import load_model
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras import backend as K
import cv2
import numpy as np
import h5py
import os
#model = Sequential()
model = load_model('first_try.h5')
num_test=1000
img = load_img('pic/0.png') # this is a PIL image
xx = 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=0
print '\n'
for i in classes:
if (i==[1]):
count+=1
print count
#print classes
不出意外的话效果是不好的,,毕竟这个多层神经网络很简单,并不能很好的分类,下一篇我们用预训练的高级网络再来跑自己的数据集