Top Banner
Keras with Tensorflow back-end in R and Python Longhow Lam
46

Keras on tensorflow in R & Python

Jan 24, 2018

Download

Data & Analytics

Longhow Lam
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Keras on tensorflow in R & Python

Keras with Tensorflowback-end in R and Python

Longhow Lam

Page 2: Keras on tensorflow in R & Python

Agenda

• Introduction to neural networks &Deep learning

• Keras some examples• Train from scratch

• Use pretrained models

• Fine tune

Page 3: Keras on tensorflow in R & Python

Introduction to neural networks

Page 4: Keras on tensorflow in R & Python

Neural network NEURAL NETWORK FOR BEGINNERS, JUST LINEAR REGRESSION

f Y = f(X,w) = w1 + w2X2 + w3X3 + w4X41

X2

X3

X4w4

w3

w1

w2 Neural network compute node

f is the so-called activation function. This could be the logit function, but other choices are possible.

There are no hidden layers.

There are four weights w’s that have to be determined

Page 5: Keras on tensorflow in R & Python

Neural networks ONE HIDDEN LAYER, MATHEMATICAL FORMULATION

Age

Income

Region

Gender

X1

X2

X3

X4

Z1

Z2

Z3

f

X inputs Hidden layer z outputs

α1

β1

neural net prediction f = 𝑔 𝑇𝑌

𝑇𝑌 = 𝛽0𝑌 + 𝛽𝑌𝑇𝑍

𝑍𝑚 = 𝜎 𝛼0𝑚 + 𝛼𝑚𝑇 𝑋

The function σ is defined as:

𝜎(𝑥) =1

1+𝑒−𝑥

𝝈 is also called the activation function,

In case of regression the function g is the Identify function I

In case of a binary classifier, g is the softmax 𝑔 𝑇𝑌 =𝑒𝑇𝑌

𝑒𝑇𝑁+𝑒𝑇𝑌

The model weights w = (α , β) have to be estimated from the data

m = 1, ... ,M

number of nodes / neurons in the hidden layer

Page 6: Keras on tensorflow in R & Python

Neural networks

Back propagation algorithm

is just gradient descent in numerical optimization terms

Randomly choose small values for all wi’ s. For each data point (observation) i :

• Calculate the neural net prediction fi

• Calculate the error, for example for regression squared error (yi – fi)2

• Calculate the sum of all errors: E = Σ (yi – fi)2

Adjust weights w according to:

A run through all observations is called an epoch

Stop if error E is small enough.

Training the weights

𝑤𝑖𝑛𝑒𝑤 = 𝑤𝑖 + ∆𝑤𝑖

∆𝑤𝑖 = −𝛼𝜕𝐸

𝜕𝑤𝑖

Page 7: Keras on tensorflow in R & Python

Deep learning

Page 8: Keras on tensorflow in R & Python

Deep learning LOOSELY DEFINED:

NEURAL NET WORK WITH MORE THAN 2 HIDDEN LAYERS

Don’t use deep learning for ‘simple’ business analytics problems… it is really an overkill!

Keep it simple if you have ‘classical’ churn or response models: logistics regression, trees, or forests.

In this example all layers are fully connected (or also called dense layers)

Page 9: Keras on tensorflow in R & Python

Convolutional networksFor computer vision special structures are used.Usually not all layers fully connected.

We have so-called Convolutional layers and pooling layers.

Convolutional layer A, takes only from a local window inputs from previous layer

Pooling layer ‘max’, takes max value of a bunch of inputs

Page 10: Keras on tensorflow in R & Python

But pictures are arrays…. No problem

These blocks of numbers are called “tensors” in linear algebra terms.

Calculations on these tensors can be done very fast in parallel on GPU’s

Page 11: Keras on tensorflow in R & Python

Training imagesVGG19 deep learning networks structure

The model achieves 92.7% top-5 test accuracy in ImageNet , which is a dataset of over 14 million images belonging to 1000classes. 143.mln weights!

Target output: 1000 classes

Page 12: Keras on tensorflow in R & Python

KERAS on Tensorflow

Page 13: Keras on tensorflow in R & Python

Keras

• Keras is a high-level neural networks API, written in Python and capable of running on top of either TensorFlow or Theano.

• It was developed with a focus on enabling fast experimentation.

• Being able to go from idea to result with the least possible delay is key to doing good research.

• Specifying models in keras is at a higher level than tensorflow, but you still have lot’s of options

• There is now also an R interface (of course created by Rstudio… )

Page 14: Keras on tensorflow in R & Python

Simpel set-up “Architecture”

Tensorflow installed on a (linux) machineIdeally with lots of GPU’s

pip install keras

You’re good to go in Python (Jupyter notebooks)

install_github("rstudio/keras")

You’re good to go inR / RStudio

Page 15: Keras on tensorflow in R & Python

Training from scratch: MNIST example

MNIST data:

70.000 handwritten digits with a label (“0”, “1”,…,”9”)

Each image has a resolution of 28*28 pixels, so a 28 by 28 matrix

Page 16: Keras on tensorflow in R & Python

First a simple neural network in R

Treat image as a vector. It has length 784 (28by28), the number of pixels. One hidden layer (fully connected)

Pixel 3

Pixel 2

Pixel 1

Pixel 783

Pixel 784

neuron 1

neuron 256

Label 0

Label 9

Page 17: Keras on tensorflow in R & Python

First a simple neural network in R

N of neurons time for 50 epochs Test accuracy

5 39 s 0.8988

15 51 s 0.9486

25 44 s 0.9626

50 51 s 0.9741

100 73 s 0.9751

256 125 s 0.9796

512 213 s 0.9825

1024 314 s 0.9830

Page 18: Keras on tensorflow in R & Python

2 dense (fully connected) layers

2 layer sec Test acc

64 *64 58 0.9705

128*128 88 0.9768

256*256 143 0.9797

512*512 349 0.9819

1024*1024 900 0.9835

Pixel 3

Pixel 2

Pixel 1

Pixel 783

Pixel 784

Label 0

Label 9

Page 19: Keras on tensorflow in R & Python

A more complex model in Python

Images are treated as matrices / arrays• Convolutional layers• Pooling layer• Dropouts• Dense last layer

Page 20: Keras on tensorflow in R & Python

Test loss: 0.028Test Accuracy: 0.9912

Run time 25 minutes

Page 21: Keras on tensorflow in R & Python

Now compare with GPU

Some extra steps:

1. Spin up: Microsoft NC6 machine: 1 X Tesla K80 GPU ($1.084/hr)

2. Install CUDA toolkit / install cuDNN

3. pip install tensorflow-gpu

Run same model as in previous slide: Now it takes 2.9 minutes

Page 22: Keras on tensorflow in R & Python

Example predictions some are wrong….

1 vs 9 7 vs 2 8 vs 8 0 vs 6

Page 23: Keras on tensorflow in R & Python

Tensorboard

TensorBoard is a visualization tool included with TensorFlow

It enables you to visualize dynamic graphs of your Keras training and test metrics, as well as activation histograms for the different layers in your model.

model %>% fit(

x_train, y_train,

batch_size = batch_size,

epochs = epochs,

verbose = 2,

callbacks = callback_tensorboard(

log_dir = "logs/run_1",

write_images = TRUE

),

validation_split = 0.2

)

Page 24: Keras on tensorflow in R & Python

Now open a shell and start tensorboard, providing the log directory

Page 25: Keras on tensorflow in R & Python

Pre trained neural networks

Page 26: Keras on tensorflow in R & Python

Using pre-trained models

Image classifiers have been trained on big GPU machines for weeks with millions of pictures on very large networks

Not many people do that from scratch. Instead, one can use pre-trained networks and start from there.

Page 27: Keras on tensorflow in R & Python

predict image class using pretrained models

Page 28: Keras on tensorflow in R & Python

RTL NIEUWS Images trough resnet and vgg16

Link to trellisJS app

Page 29: Keras on tensorflow in R & Python

Images from VideosUse ffmpeg: open source tool for video analyses Example call for Dutch series Family Kruys trailer

ffmpeg –i

"FAMILIE_KRUYS_TRAILER.mp4"

-s 600x400 –ss 00:00:05.000

-t 1200 -r 2.0

"FamKruys%03d.jpg"

Page 30: Keras on tensorflow in R & Python

And now tag them with vgg16

Page 31: Keras on tensorflow in R & Python

See my video analyser shiny app on github

Page 32: Keras on tensorflow in R & Python

Extract features using pre-trained models

Remove top layers for feature extraction

We have a 7*7*512 ‘feature’ tensor = 25.088 values

Page 33: Keras on tensorflow in R & Python

Only a few lines of R code

Page 34: Keras on tensorflow in R & Python

RTL NIEUWS Image similarity

1024 RTL Nieuws Sample pictures. Compute for each image the 25.088 feature values.

Calculate for each image the top 10 closest images, based on cosine similarity.

Little Shiny APP

Page 35: Keras on tensorflow in R & Python

Examples RTL Nieuws image similarities

Page 36: Keras on tensorflow in R & Python

Examples RTL Nieuws image similarities

Page 37: Keras on tensorflow in R & Python

Examples RTL Nieuws image similarities

Page 38: Keras on tensorflow in R & Python

Same can be done for Videoland ‘boxarts’

See little shiny app

Page 39: Keras on tensorflow in R & Python

The Brad Pitt similarity index

Page 40: Keras on tensorflow in R & Python

Take five Brad Pitt pictures

Run them trough the pre-trained vgg16 and extract feature vectors. This is a 5 by 25088 matrix

The brad Pit IndexTake other images, run them through the VGG16Calculate the distances with the five Brad Pitt pictures and average:

0.771195 0.802654 0.714752 0.792587 0.8291976 0.8096944 0.665990 0.9737212

Page 41: Keras on tensorflow in R & Python

0.6273 0.5908 0.8231 0.7711 0.8839 0.8975 0.6934 0.9659

Focusing on only the face!!

Page 42: Keras on tensorflow in R & Python

Transfer learning / Fine tune pre trained models

Page 43: Keras on tensorflow in R & Python

Transfer learning orfinetune pre-trained models

Train new image classifiers on limited training cases

• Get a pretrained model, say VGG16

• Remove existing top layers

• Add your own (fully) connected layer(s)

• Fix all the parameters except for your layers

• Use your (limited) samples as train cases to train the weights of your layers.

Page 44: Keras on tensorflow in R & Python

Python code examplebase_model = VGG16(weights='imagenet', include_top=False)

x = base_model.outputx = GlobalAveragePooling2D()(x)# let's add a fully-connected layerx = Dense(256, activation='relu')(x)

# and a logistic layer -- 2 classes dogs and catspredictions = Dense(2, activation='softmax')(x)

# this is the model we will trainmodel = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)# i.e. freeze all convolutional layersfor layer in base_model.layers:

layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics =['accuracy'])

Page 45: Keras on tensorflow in R & Python

Python code example

Page 46: Keras on tensorflow in R & Python

1000 cats and dogs example