Introduction to Keras!
!
Vincent Lepetit!
1!
Software Libraries for Deep Learning!
All these libraries come with a Python interface:!
!
• Caffe: from Berkeley and now Facebook, plain text for model description.
Mostly for computer vision. See also Caffe2!
!
• Theano: from University of Montréal. Discontinued in September 2017.!
• Torch/PyTorch: from Facebook. !
• TensorFlow: from Google.!
• …!
• Keras: from Google, built on top on TensorFlow or Theano, making it easy to
build deep architectures.!
2!
Installing Keras!
Install:!
1. (on a Mac, MacPorts (or maybe HomeBrew));!
2. Python 2.7 or Python 3.3+;!
3. pip (utility software to install python libraries);!
4. TensorFlow;!
5. Keras;!
6. matplotlib (Python graphic library), h5py (Python library
for loading models), Pillow (replaces PIL, Python library
for loading and processing images);!
7. optionally jupyter!
3!
jupyter notebook!
4!
First Example:!
Two-Layer Network!
5!
A Two-Layer Network!
We will train a two-layer network to approximate a 2D
function F(u, v):! ✓ ◆
u
x=
v
v
u
FC FC
✓ ◆
u y = F (u, v)
x=
v
6!
Our Two-Layer Network!
The input is a 2D point x;!
The output is a scalar value y
FC FC
x y
x
Hidden layer:!
h1 = ReLU(W1 x + b1)
!
Output layer:!
h2 = W2 h1 + b2
7!
Loss function!
Training set:
(x_traini , y_traini = F(x_traini))
Hidden layer:!
h1 = ReLU(W1 x + b1)
!
Output layer:!
h2 = W2 h1 + b2
Ns
X
1
Loss = (h2 (x traini ) y traini )2
Ns i=1
8!
v
Generating Training Data!
u
-A +A
import numpy as np
def F(x1, x2):
return np.sin(np.pi * x1 / 2.0) * np.cos(np.pi * x2 / 4.0)
A = 2
nb_samples = 1000
X_train = np.random.uniform(-A, +A, (nb_samples, 2))
Y_train = np.vectorize(F)(X_train[:,0], X_train[:,1])
9!
Models!
In Keras, a deep architecture is called a model.!
!
A model can be an arbitrary graph of layers.!
!
For this first example, we can use a Sequential model.!
from keras.models import Sequential
model = Sequential()
10!
Defining the Network!
FC FC
Hidden layer: h1 = ReLU(W1 x + b1) x y
Output layer: h2 = W2 h1 + b2
from keras.layers import Dense, Activation
nb_neurons = 20
model.add(Dense(nb_neurons, input_shape=(2,)))
model.add(Activation('relu'))
model.add(Dense(1))
11!
Shortcut!
from keras.models import Sequential
from keras.layers import Dense, Activation
nb_neurons = 20
model = Sequential([
Dense(nb_neurons, input_shape=(2,)),
Activation('relu'),
Dense(1)])
12!
Defining the Optimization Method!
from keras.optimizers import SGD
sgd = SGD(lr=0.01,
decay=1e-6, momentum=0.9,
nesterov=True)
model.compile(loss='mean_squared_error',
optimizer=sgd)
13!
Running the Optimization!
model.fit(X_train, Y_train, epochs=10, batch_size=32)
Output:!
Epoch 1/10
1000/1000 [==============================] - 0s 490us/step - loss: 0.0487
Epoch 2/10
1000/1000 [==============================] - 0s 43us/step - loss: 0.0415
Epoch 3/10
1000/1000 [==============================] - 0s 49us/step - loss: 0.0345
Epoch 4/10
1000/1000 [==============================] - 0s 44us/step - loss: 0.0290
Epoch 5/10
1000/1000 [==============================] - 0s 52us/step - loss: 0.0235
Epoch 6/10
1000/1000 [==============================] - 0s 43us/step - loss: 0.0190
Epoch 7/10
1000/1000 [==============================] - 0s 45us/step - loss: 0.0154
Epoch 8/10
1000/1000 [==============================] - 0s 47us/step - loss: 0.0122
Epoch 9/10
1000/1000 [==============================] - 0s 50us/step - loss: 0.0098
Epoch 10/10
1000/1000 [==============================] - 0s 48us/step - loss: 0.0082
14!
Prediction!
x = [1.5, 0.5]
print(F(x[0], x[1]))
x = np.array(x).reshape(1, 2)
print(x)
print( model.predict(x) )
print( model.predict(x)[0][0] )
Output:!
0.6532814824381883
[[1.5, 0.5]]
[[0.5451795]]
0.5451795
15!
Visualization!
Width = 200
Height = 200
U = np.linspace(-A, +A, Width)
V = np.linspace(-A, +A, Height)
# Computes cartesian product between U and V:
UV = np.transpose([np.tile(U, len(V)), np.repeat(V, len(U))])
print(UV)
ys = model.predict(UV)
print(ys)
I = ys.reshape(Width, Height)
Output:!
[[-2. -2. ]
[-1.9798995 -2. ]
...
[ 1.95979899 2. ]
[ 2. 2. ]]
[[ 0.02076489]
[-0.00082633]
...
[-0.11296707]
[-0.12041384]]
16!
Visualization (2)!
I = ys.reshape(Width, Height)
import matplotlib.pyplot as plt
import matplotlib.cm as cm
#Make imageplotlib show the images inline
#in Jupyter notebooks:
%matplotlib inline
plt.imshow(I, cmap = cm.Greys)
Output:!
17!
Second Example:!
CNN for MNIST!
18!
MNIST!
19!
Loading the Dataset!
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print( X_train.shape )
Output:!
(60000, 28, 28)
20!
Visualizing one Sample!
import matplotlib.pyplot as plt
import matplotlib.cm as cm
%matplotlib inline
plt.imshow(X_train[0], cmap = cm.Greys)
Output:!
21!
Reformating the Input!
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_train /= 255
print(X_train.shape)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_test = X_test.astype('float32')
X_test /= 255
Output:!
(60000, 28, 28, 1)
22!
Reformating the Desired Ouput!
print(y_train.shape)
print(y_train[0:3])
from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train, 10)
print(y_train[0])
Output:!
(60000,)
[5 0 4]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
23!
Creating the Model!
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
input_shape=(28, 28, 1)))
print(model.output_shape)
Output:!
(None, 26, 26, 32)
24!
Creating the Model (2)!
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
input_shape=(28, 28, 1)))
print(model.output_shape)
model.add(MaxPooling2D(pool_size=(2, 2)))
print(model.output_shape)
from keras.layers import Dropout
model.add(Dropout(0.25))
print(model.output_shape)
Output:!
(None, 26, 26, 32)
(None, 13, 13, 32)
(None, 13, 13, 32)
25!
Creating the Model (3)!
from keras.layers import Flatten
model.add(Flatten())
print(model.output_shape)
from keras.layers import Dense
model.add(Dense(128, activation='relu'))
print(model.output_shape)
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
print(model.output_shape)
Output:!
(None, 5408)
(None, 128)
(None, 10)
26!
Optimization!
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train,
batch_size=32, epochs=10, verbose=1)
27!
Testing!
plt.imshow(X_test[0].reshape(28,28), cmap = cm.Greys)
print(model.predict(X_test[0].reshape(1, 28, 28, 1)))
Output:!
[[2.7737193e-10 2.5943342e-08 2.5428611e-07 3.6613658e-06 1.0967714e-10
7.6563078e-10 1.0837641e-14 9.9999535e-01 2.9037880e-08 7.2273968e-07]]
28!
Third Example:!
!
Using VGG to Recognize Objects!
in Images!
29!
Loading the VGG Model!
from keras.applications.vgg16 import VGG16
model = VGG16()
print(model.summary())
Output:!
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
etc.
30!
Loading the VGG Model!
from keras.preprocessing.image import load_img
# load an image from file
image = load_img('cat.jpg', target_size=(224, 224))
image
Output:!
31!
Converting the Image!
from keras.preprocessing.image import img_to_array
# convert the image pixels to a numpy array
image = img_to_array(image)
print(image.shape)
image = image.reshape((1, image.shape[0], image.shape[1],
image.shape[2]))
print(image.shape)
Output:!
(224, 224, 3)
(1, 224, 224, 3)
32!
Applying the Model!
y_pred = model.predict(image)
print(y_pred.shape)
from keras.applications.vgg16 import decode_predictions
# convert the probabilities to class labels
labels_pred = decode_predictions(y_pred)
print(labels_pred)
Output:!
(1, 1000)
[[('n02124075', 'Egyptian_cat', 0.5917598), ('n02123159', 'tiger_cat',
0.37640235), ('n02123045', 'tabby', 0.031016493), ('n02127052', 'lynx',
0.0006430329), ('n04589890', 'window_screen', 7.664625e-05)]]
33!