• Keras is an excellent framework for deep learning, I am willing to write something about Keras in this blog. Adding with some example codes, I think we can understand the accessibility of Keras in details.

Why choose Keras?

KEras & TEnsorflow(KETE) combo rocks.

1. Perfect integration with Tensorflow

1
2
3
4
5
6
7
8
9
10
11
import tensorflow as tf
from keras import backend as k
from keras.layers import Dense
from keras.objectives import categorical_crossentropy

# this placeholder will contain our input digits, as flat vectors
img = tf.placeholder(tf.float32, shape=(None,784))
# Keras layers can be called on Tensorflow tensors:
# fully-connected layer with 128 units and ReLU activation
x = Dense(128, activation='relu')(img)
x = Dense(128, activation='relu')(x)

2. High-level abstraction

1
2
3
4
5
# for a multi-class classification problem
model.complile(optimiztaion='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)

3. Well-written document

website: https://keras.io/
description about all functions in Keras and how to use them.

Keras Working Pipeling

It contains four main steps.

1. Model definition

1
2
3
4
5
6
7
8
from keras.model import Sequential
model = Sequential()

from keras.layers import Dense, Activation
model.add(Dense(output_dim=64,input_dim=100))
model.add(Activation('relu'))
model.add(Dense(output_dim=10))
model.add(Activation('softmax'))

2. Model compilation

specify learning rate, loss function, metrics

1
2
3
4
model.compile(loss='categorical_crossentropy',optimizer='sgd',metircs=['accuracy']

from keras.optimizers import SGD
model.compile(loss='categorical_crossentropy',optimizer=SGD(lr=0.01,momentum=0.9,nesterov=True))

3. Training

1
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)

4. Prediction and Evaluation

Evaluate your performance in one line

1
loss_and_metrics = model.evaluate(X_test, Y_text, batch_size=32)

OR generate predictions on new data

1
2
classes = model.predict_classes(X_text, batch_size=32)
proba = model.predict_proba(X_text, batch_size)

Keras Utilities

1. Preprocessing

Keras Preprocessing provides useful data augmentation methods for Sequence, Text and Image Data

Keras.preprocessing.ImageDataGenerator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150,150),
batch_size=32,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150,150),
batch_size=32,
class_mode='binary')

model.fit_generator(
train_generator,
samples_per_epoch=2000,
nb_epoch=50,
validation_data=validation_generator,
nb_val_samples=800)

2. Application

Keras applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction and finetuning.

Extract features with VGG16

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg import preprocess_input
import numpy as np

model = VGG16(weights='imagenet',include_top=False)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224,224))
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)
x = preprocess_input(x)

features = model.predict(x)

Keras Examples

1. A simple implementation of ANN for MINST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import random
import numpy as np
import keras
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import RMSprop
from keras.utils import np_utils
from keras import backend as K

K.set_image_dim_ordering('tf') # always check you are useing the correct image dimension

import matplotlib.pyplot as plt
%matplotlib inline

# Data preprocessing
# set the full path to mnist.pkl.gz
# point this to the data folder inside the repository
path_to_dataset = "./mnist.pkl.gz"

#load the dataset
(X_train,y_train),(X_test,y_test) = mnist.load_data(path_to_dataset)
print(X_train.shape,y_train.shape)
print(X_test.shape,y_test.shape)

X_train = X_train.reshape(60000,784)
X_test = X_test.reshape(10000,784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /=255
print(X_train.shape[0],'training samples')
print(X_test.reshape[0],'testing samples')

# specify class labels
batch_size = 128
nb_classes = 10
nb_epoch = 10

# convert class vectors to binary class matrics for softmax lsyer
Y_train = keras.utils.np_utils.to_categorical(y_train,nb_classes)
Y_test = keras.utils.np_utils.to_categorical(y_test,nb_classes)
# for example 6's label is now(0,0,0,0,0,1,0,0,0,0)
print Y_train.shape

# Model definition
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))

model.summary() # will contain the detailed info of each layer

# Model Compliation
model.compile(loss='categorical_crossentropy',
optimizer = RMSprop(),
metrics = ['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test,Y_test)

# Evaluation and prediction
score = model.evaluate(X_test, Y_test, verbose=0)
print ('Test scores:', score[0])
print ('Test accuracy:', score[1])

X_test_0 = X_test[0,:].reshape(1,784)
Y_test_0 = Y_test[0,:]
plt.imshow(X_test_0.reshape([28,28]))

pred = model.predict(X_test_0[:])
print('Label of testing samples:', np.argmax(Y_test_0))
print('Output of the softmax layer:', pred[0])
print('Network prediction:', np.argmax(pred[0])) # for the output is a probability distribution

2. CNN networks for classifying cats and dogs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Input, Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, ZeroPadding2D
from keras import optimizers

# dimentions of our images
img_width, img_height = 150, 150

train_data_dir = '.../data/dogs_cats/keras_eg/train'
validation_data_dir = '.../data/dogs_cats/keras_eg/validation'
nb_train_samples = 2000
nb_validation_samples = 800
nb_epoch = 50

# Before playing with models, let's have a look at how data augmentation is done with ImageDataGenerator
from keras.preprocessing.image import ImageDataGenerator, array_to_img,img_to_array, load_img
from PIL import Image
import matplotlib.pyplot as plt
datagen = ImageDataGenerator(
rotation_range=40,
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('.../data/dogs_cats/keras_eg/train/cats/cat.0.jpg') # this is a PIL image
plt.imshow(img)
plt.axis('off')
plt.title('original image')
plt.show()
x = img_to_array(img) # this is a Numpy array with shape(150,150,3)
x2 = x.reshape((1,)+x.shape) # this is a Numpy array with shape(1,150,150,3)

# the .flow() command below generates batches of randomly transformed images
# and saves the results to './data_augment' directory
i = 0
fig = plt.figure()
fig.suptitle('Augmented Images')
for batch in datagen.flow(x2,batch_size=1,
save_to_dir='.../data_augment', save_prefix='cat', save_format='jpg'):
i+=1
if i > 9:
break # otherwise the generator would loop indefinitely

temp = batch.reshape(x.shape)
plt.subplot(3,3,i)
plt.imshow(temp.astype('uint8'))
plt.axis('off')
plt.show()

# Continue with real data
# this is the augmentation configration 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 scaling
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width,img_height),
batch_size=32,
class_mode='binary'
)
validation_generator = test_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width,img_height),
batch_size=32,
class_mode='binary'
)

# Training a custom CNN from scrach
model = Sequential()
model.add(Convolution2D(32,3,3,input_shape=(img_width,img_height,3)))
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)))

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'])
model.summary()

# fine tuning using pre_trained VGG16
# use pretrained VGG16 as a feature extractor
from keras.applications.vgg16 import VGG16
from keras.layers import Input
from keras.models import Model
import numpy as np
input_tensor = Input((img_width,img_height,3))

model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor)

datagen = ImageDataGenerator(rescale=1./255)
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width,img_height),
batch_size=32,
class_mode=None,
shuffle=False
)
bottleneck_features_train = model.predict_generator(generator,nb_train_samples)
np.save(open('bottleneck_features_train.npy','w'),bottleneck_features_train)
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width,img_height),
batch_size=32,
class_mode=None,
shuffle=False
)

bottleneck_features_validation = model.predict_generator(generator,nb_validation_samples)
np.save(open('bottleneck_features_validation.npy','w'),bottleneck_features_validation)

train_data = np.load(open('bottleneck_features_train.npy'))
train_labels = np.array([0]*(nb_train_samples/2) + [1]*[nb_train_samples/2])

validation_data = np.load(open('bottleneck_features_validation.npy'))
validation_labels = np.array([0]*(nb_validation_samples/2) + [1]*[nb_validation_samples/2])

model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))

model.compile(optimizer='rmsprop',loss='binary_crossentropy',merics=['accuracy'])
model.fit(train_data,train_labels,
nb_epoch=nb_epoch,batch_size=32,
validation_data=(validation_data,validation_labels))
model.save_weights('top_mmodel_weights.h5')

# Use the weights of pretrained VGG16 as initialization
input_tensor = Input((img_width,img_height,3))
model = VGG16(include_top=False, weights='imagenet',input_tensor=input_tensor)
vgg_input = model.input
vgg_output = model.output
top_model = Flatten()(vgg_output)
top_model = Dense(256,activation='relu')(top_model)
top_model = Dropout(0.5)(top_model)
top_model = Dense(1, activation='sigmoid')(top_model)

model = Model(vgg_input,top_model)
model.compile(optimizer='sgd',loss='binary_crossentropy',metrics=['accuracy'])

model.fit_generator(
train_generator,
samples_per_epoch = nb_train_samples,
nb_epoch = nb_epoch,
validation_data = validation_generator,
nb_val_samples = nb_validation_samples
)

*This is some codes and summary of deep learning using keras. This is from videos on website https://v.qq.com/x/page/w0380o88vju.html. Thanks for sharing!