Brief presentation about keras framework. The propose of this presentation is to give some ideas about how it works and its main functionalities. In addition, is also shown a function to create different models from a config file.
1. Keras: Deep Learning library for Theano and TensorFlow
Keras: Deep Learning library for Theano and TensorFlow
Alison Marczewski
UFMG
April 28, 2017
Alison Marczewski | UFMG | April 28, 2017 1 / 19
2. Keras: Deep Learning library for Theano and TensorFlow
Introduction
Fast prototyping
Support combination of convolutional and recurrent networks
CPU and GPU
Alison Marczewski | UFMG | April 28, 2017 2 / 19
3. Keras: Deep Learning library for Theano and TensorFlow
Guiding principles
User friendliness
Modularity
Easy extensibility
Work with Python
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential([
Dense(32, input_shape=(784,)),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
Alison Marczewski | UFMG | April 28, 2017 3 / 19
4. Keras: Deep Learning library for Theano and TensorFlow
Core Layers
Dense(units, activation=None, use_bias=True) # Fully connected
Activation(activation) # linear, softmax, tanh...
Dropout(rate) # set values to zero
Flatten() # masks into vector
Reshape(target_shape)
Permute(dims) # Permute((2, 1), input_shape=(10, 64)) --> (64, 10)
RepeatVector(n) # RepeatVector(3, input_shape=(32,)) --> (3, 32)
ActivityRegularization(l1=0.0, l2=0.0)
# You can create your Layer
Alison Marczewski | UFMG | April 28, 2017 4 / 19
5. Keras: Deep Learning library for Theano and TensorFlow
Layer wrappers
from keras.layers.wrappers import *
# This wrapper allows to apply a layer to every temporal slice of an input.
TimeDistributed(layer)
# Sample
model = Sequential()
model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
# now model.output_shape == (None, 10, 8)
#Bidirectional wrapper for RNNs.
Bidirectional(layer, merge_mode='concat', weights=None)
Alison Marczewski | UFMG | April 28, 2017 5 / 19
6. Keras: Deep Learning library for Theano and TensorFlow
Preprocessings
For sequences: pad sequences, skipgrams...
For text: text to word, one hot, tokenizer
For image: ImageDataGenerator
Alison Marczewski | UFMG | April 28, 2017 6 / 19
7. Keras: Deep Learning library for Theano and TensorFlow
Compilation and Training
#--> Loss for a multi-class classification problem: 'categorical_crossentropy'
#--> Loss for a binary classification problem: 'binary_crossentropy'
#--> Loss for a mean squared error regression problem: 'mse'
#--> Metrics: binary_accuracy, categorical_accuracy...
# For custom metrics
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
# optimizer: SGD, RMSprop, Adagrad, Adadelta, Adam, Adamax, Nadam, TFOptimizer
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
# training model
model.fit(x_train, y_train, epochs=5, batch_size=32,
callbacks = [CALLBACK_LIST])
Alison Marczewski | UFMG | April 28, 2017 7 / 19
8. Keras: Deep Learning library for Theano and TensorFlow
Callbacks
from keras.callbacks import *
ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False,
save_weights_only=False)
EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0,
mode='auto')
LearningRateScheduler(schedule) #schedule: a function that takes an epoch
#index as input (integer, indexed from 0) and returns a new learning
#rate as output (float).
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0,
epsilon=0.0001, min_lr=0)
CSVLogger(filename, separator=',', append=False)
# You can create your callback
Alison Marczewski | UFMG | April 28, 2017 8 / 19
9. Keras: Deep Learning library for Theano and TensorFlow
Constraints
Functions from the constraints module allow setting constraints on
network parameters during optimization
max norm(max value=2, axis=0)
non neg
unit norm
from keras.constraints import maxnorm
# example
model.add(Dense(64, kernel_constraint=max_norm(2.)))
#--> kernel_constraint: for the main weights matrix
#--> bias_constraint: for the bias
Alison Marczewski | UFMG | April 28, 2017 9 / 19
10. Keras: Deep Learning library for Theano and TensorFlow
Utils
from keras.utils import *
# Conv a class vector to binary class matrix
# e.g: for use with categorical_crossentropy
to_categorical(y, num_classes=None)
plot_model(model, to_file='model.png', show_shapes=False,
show_layer_names=True)
Alison Marczewski | UFMG | April 28, 2017 10 / 19
11. Keras: Deep Learning library for Theano and TensorFlow
Applications
Some models are available for image classification with weights trained on
ImageNet: Xception, VGG16, VGG19, ResNet50, InceptionV3
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
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)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
print('Predicted:', decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225),
# (u'n01871265', u'tusker', 0.1122357),
# (u'n02504458', u'African_elephant', 0.061040461)]
Alison Marczewski | UFMG | April 28, 2017 11 / 19
12. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
Alison Marczewski | UFMG | April 28, 2017 12 / 19
13. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
Alison Marczewski | UFMG | April 28, 2017 12 / 19
14. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
models/configs
Alison Marczewski | UFMG | April 28, 2017 12 / 19
15. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
models/configs: for each data preprocessing, there are one or more
configs
Alison Marczewski | UFMG | April 28, 2017 12 / 19
16. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
module for loading dataset according to some parameters
models/configs: for each data preprocessing, there are one or more
configs
Alison Marczewski | UFMG | April 28, 2017 12 / 19
17. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
module for loading dataset according to some parameters
models/configs: for each data preprocessing, there are one or more
configs
module for creating a model according to parameters
Alison Marczewski | UFMG | April 28, 2017 12 / 19
18. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
module for loading dataset according to some parameters
models/configs: for each data preprocessing, there are one or more
configs
module for creating a model according to parameters
For each experiment: one data preprocessing with 1 or more
models/configs
Alison Marczewski | UFMG | April 28, 2017 12 / 19
19. Keras: Deep Learning library for Theano and TensorFlow
Working with several experiments/configs
What are the main differences among experiments?
data preprocessing
module for loading dataset according to some parameters
models/configs: for each data preprocessing, there are one or more
configs
module for creating a model according to parameters
For each experiment: one data preprocessing with 1 or more
models/configs
Instead of writing a python code for each one, loading a file with these
parameters
Alison Marczewski | UFMG | April 28, 2017 12 / 19
21. Keras: Deep Learning library for Theano and TensorFlow
Executing
if __name__ == '__main__':
exec(open(sys.argv[1]).read(), globals())
sys.stdout = Logger('experiments/{0}.log'.format(experiment['name']))
... = load_dataset(experiment['dataset'])
input_shape = X_train.shape[1:]
for config in experiment['configs']:
model = create_model(input_shape, config['layers'], experiment['classes'])
model.compile(loss='categorical_crossentropy',
optimizer=experiment['general'].get('optimizer', 'SGD'),
metrics=['accuracy'])
print(model.summary())
model.fit(...)
...
plot_model(model, '%s-%s'%(experiment['name'], config['name']))
Alison Marczewski | UFMG | April 28, 2017 14 / 19
22. Keras: Deep Learning library for Theano and TensorFlow
Create model I
def create_model(input_shape, layers, classes):
layers.append({'name': 'dense_final', 'output_dim': len(classes)})
layers.append({'name': 'activation_classifier',
'activation': 'softmax'})
model = Sequential()
for l in layers:
if len(model.layers) == 0:
l['input_shape'] = input_shape
print("layer:", l)
model.add(create_layer(**l))
return model
Alison Marczewski | UFMG | April 28, 2017 15 / 19
23. Keras: Deep Learning library for Theano and TensorFlow
Create model II
# Basic example
def create_layer(**kwargs):
name = kwargs.get('name')
if name.startswith('convolution'):
layer = Convolution1D(**kwargs)
elif name.startswith('maxpooling'):
layer = MaxPooling1D(**kwargs)
elif name.startswith('dropout'):
layer = Dropout(**kwargs)
elif name.startswith('flatten'):
layer = Flatten(**kwargs)
elif name.startswith('dense'):
layer = Dense(**kwargs)
elif name.startswith('activation'):
layer = Activation(**kwargs)
else:
raise "layer not supported yet", name
return layer
Alison Marczewski | UFMG | April 28, 2017 16 / 19
24. Keras: Deep Learning library for Theano and TensorFlow
Plot Model
Just for example
Alison Marczewski | UFMG | April 28, 2017 17 / 19
25. Keras: Deep Learning library for Theano and TensorFlow
Questions?
Alison Marczewski | UFMG | April 28, 2017 18 / 19
26. Keras: Deep Learning library for Theano and TensorFlow
Keras reference
https://keras.io
Alison Marczewski | UFMG | April 28, 2017 19 / 19