Keras的使用方法
一种high-level的python API。
cv-tricks教程篇
Keras 可以作为tensorflow和Theano的前端接口。
The power of being able to run the same code with different back-end is a great reason for choosing Keras.
Install
sudo pip install h5py #Used for save the network
sudo pip install numpy scipy pillow
sudo pip install keras
装好后,可以到配置文件~/.keras/keras.json里指定后台运行在Tensorflow还是Theano上。
{
"epsilon": 1e-07,
"floatx": "float32",
"image_data_format": "channels_last",
"backend": "tensorflow"
}
Tensorflow要用channels_last, 而Theano要用channels_first, 因为:
Note that, the value of image_data_format is “channels_last”, which is the correct value for Tensorflow. In Tensorflow, images are stored as Tensors/arrays of shape [height, width, channels] while in Theano the order is different [channels, height, width]. So, if you don’t have this parameter set correctly, your intermediate results will be very strange. For Theano, this value will be “channels_first”.
Fundamentals of Keras
两种方式可以创建模型:
1. Sequential models: 创建简单模型
2. Functional API: 创建复杂模型
Keras Sequential Model
使用这一种方法创建模型,首先需要创建一个顺序模型:
from keras.models import Sequential
model = Sequential()
定义网络结构的相关方法:Dense(fully connected layer),Activation,Conv2D,MaxPooling2D,Flatten,Dropout
from keras.layers import Dense, Activation,Conv2D,MaxPooling2D,Flatten,Dropout
model.add(Conv2D(64, (3, 3), activation='relu'))
#This adds a Convolutional layer with 64 filters of size 3 * 3 to the graph
model.add(MaxPooling2D(pool_size=(2, 2)))
#MaxPooling layer: Specify the type of layer and specify the pool size and you are done.
model.add(Dense(256, activation='relu'))
#Fully connected layer: It’s called Dense in Keras. Just specify the number of outputs and you are done.
model.add(Dropout(0.5))
model.add(Flatten())
网络的第一层需要读入数据,因此第一层网络需要额外指明数据的shape:
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))
# Here the shape is 224 * 224 * 3.
网络的损失函数和优化方式。使用model.compile()函数。
For example, in this line below we are asking the network to use the ‘rmsprop’ optimizer to change weights in such a way that the loss ‘binary_crossentropy’ is minimized at each iteration.
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
If you want to specify stochastic gradient descent and you want to choose proper initialization and other hyperparameters:
from keras.optimizers import SGD
.......
......
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
使用fit函数数据输入。
model.fit(x_train, y_train, batch_size=32, epochs=10,validation_data=(x_val, y_val))
# epochs: maximum numbers of epochs you want training to go on.
最后是使用evaluate函数进行模型测试。
score = model.evaluate(x_test, y_test, batch_size=32)
Sequence Model解决一个数值回归问题
import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
trX = np.linspace(-1, 1, 101)
trY = 3 * trX + np.random.randn(*trX.shape) * 0.33
model = Sequential()
#model.add(Dense(input_dim=1, output_dim=1, init='uniform', activation='linear'))
model.add(Dense(input_dim=1, units=1, kernel_initializer='uniform', activation='linear'))
model.compile(optimizer='sgd', loss='mse')
weights = model.layers[0].get_weights()
w_init = weights[0][0][0]
b_init = weights[1][0]
print('Linear regression model is initialized with weight w: %.2f, b: %.2f' % (w_init, b_init))
# Linear regression model is initialized with weight w: -0.04, b: 0.00
model.fit(trX, trY, epochs=200, verbose=1)
weights = model.layers[0].get_weights()
w_final = weights[0][0][0]
b_final = weights[1][0]
print('Linear regression model is trained to have final weight w: %.2f, b: %.2f' % (w_final, b_final))
# Linear regression model is trained to have final weight w: 2.98, b: 0.09
如果不使用Keras的话,tensorflow的代码应该这样写:
import tensorflow as tf
import numpy as np
trainX = np.linspace(-1, 1, 101)
trainY = 3 * trainX + np.random.randn(*trainX.shape) * 0.33
X = tf.placeholder("float")
Y = tf.placeholder("float")
w = tf.Variable(0.0, name="weights")
init = tf.global_variables_initializer()
print( "Your Tensorflow version is "+ str(tf.__version__) +".")
print("If you Tensorflow version is < 0.11, you will face error in tf.multiply function. Check code comment" )
y_model = tf.multiply(X, w)
# This was tf.mul for older versions
cost = (tf.pow(Y-y_model, 2))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
with tf.Session() as sess:
sess.run(init)
for i in range(100):
for (x, y) in zip(trainX, trainY):
sess.run(train_op, feed_dict={X: x, Y: y})
print(sess.run(w))
简单对比一下可以发现:创建placeholder, session和变量及其初始化上面都可以省略,确实方便了一些。如果是创建卷积神经网络,则会方便更多。
保存网络和加载
这里使用的数据形式是HDF5 Binary format。保存方法:
model.save_weights("my_model.h5")
加载方法:
model.load_weights('my_model_weights.h5')