Begin-with-Keras
- 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 | import tensorflow as tf |
2. High-level abstraction
1 | # for a multi-class classification problem |
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 | from keras.model import Sequential |
2. Model compilation
specify learning rate, loss function, metrics1
2
3
4model.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 line1
loss_and_metrics = model.evaluate(X_test, Y_text, batch_size=32)
OR generate predictions on new data1
2classes = 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.ImageDataGenerator1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26train_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 VGG161
2
3
4
5
6
7
8
9
10
11
12
13
14from 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 | import random |
2. CNN networks for classifying cats and dogs
1 | from keras.preprocessing.image import ImageDataGenerator |
*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!