Introduction To Keras!: Vincent Lepetit!
Introduction To Keras!: Vincent Lepetit!
!
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.!
• …!
2!
Installing Keras!
Install:!
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:
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
-A +A
import numpy as np
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.!
model = Sequential()
10!
Defining the Network!
FC FC
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!
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)
Output:!
17!
Second Example:!
CNN for MNIST!
18!
MNIST!
19!
Loading the Dataset!
from keras.datasets import mnist
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)
Output:!
(60000, 28, 28, 1)
22!
Reformating the Desired Ouput!
print(y_train.shape)
print(y_train[0:3])
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)
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)
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)
Output:!
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)
Output:!
(1, 1000)
33!