📑   Keras Cookbook

In [13]:
%%html
<style>
@import url('https://fonts.googleapis.com/css?family=Orbitron|Roboto');
body {background-color: gainsboro;} 
a {color: darkgreen; font-family: 'Roboto';} 
h1 {color: slategray; font-family: 'Orbitron'; text-shadow: 4px 4px 4px #aaa;} 
h2, h3 {color: darkslategray; font-family: 'Orbitron'; text-shadow: 4px 4px 4px #aaa;}
h4 {color: slategray; font-family: 'Roboto';}
span {text-shadow: 4px 4px 4px #ddd;}
div.input_prompt, div.output_area pre {color: slategray;}
div.output_prompt {color: green;}     
</style>
In [14]:
import numpy as np 
import pandas as pd
import os
import json

import scipy
import cv2
import seaborn
import time

import keras

from PIL import ImageFile
from IPython.display import SVG
from tqdm import tqdm
from skimage import data

import matplotlib.pylab as plt
from matplotlib import cm

plt.style.use('seaborn-whitegrid')
%matplotlib inline
In [15]:
from keras.datasets import mnist, cifar10, imdb, boston_housing
In [16]:
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_files  
In [17]:
from keras.preprocessing import sequence as keras_sequence
from keras.preprocessing import image as keras_image

from keras.utils import to_categorical, np_utils
from keras.utils.vis_utils import model_to_dot
In [18]:
from keras.models import Sequential, Model
from keras.models import load_model, model_from_json
In [19]:
from keras.layers import Conv1D, MaxPooling1D, GlobalAveragePooling1D
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.layers.embeddings import Embedding
from keras.layers.advanced_activations import PReLU
from keras.layers import Dense, LSTM, GlobalMaxPooling2D
from keras.layers import Activation, Flatten, Dropout
from keras.layers import Input, BatchNormalization
In [20]:
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input as resnet50_preprocess_input
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input as vgg16_preprocess_input
from keras.applications.vgg19 import VGG19
from keras.applications.vgg19 import preprocess_input as vgg19_preprocess_input
In [21]:
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_v3 import preprocess_input as inception_v3_preprocess_input
from keras.applications.xception import Xception
from keras.applications.xception import preprocess_input as xception_preprocess_input
In [23]:
def loss_plot(fit_history):
    plt.figure(figsize=(18, 4))

    plt.plot(fit_history.history['loss'], label = 'train')
    plt.plot(fit_history.history['val_loss'], label = 'test')

    plt.legend()
    plt.title('Loss Function');  
    
def mae_plot(fit_history):
    plt.figure(figsize=(18, 4))

    plt.plot(fit_history.history['mean_absolute_error'], label = 'train')
    plt.plot(fit_history.history['val_mean_absolute_error'], label = 'test')

    plt.legend()
    plt.title('Mean Absolute Error'); 

def acc_plot(fit_history):
    plt.figure(figsize=(18, 4))

    plt.plot(fit_history.history['acc'], label = 'train')
    plt.plot(fit_history.history['val_acc'], label = 'test')

    plt.legend()
    plt.title('Accuracy');     
In [24]:
def decode_predictions2(preds, fpath, top=5):
    global CLASS_INDEX
    if len(preds.shape) != 2 or preds.shape[1] != 1000:
        raise ValueError('`decode_predictions` expects '
                         'a batch of predictions '
                         '(i.e. a 2D array of shape (samples, 1000)). '
                         'Found array with shape: ' + str(preds.shape))
    if CLASS_INDEX is None:
        CLASS_INDEX = json.load(open(fpath))
    results = []
    for pred in preds:
        top_indices = pred.argsort()[-top:][::-1]
        result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices]
        results.append(result)
    return results

1. Backend

In [25]:
'''
/.keras/keras.json
{"image_data_format": "channels_last",
 "epsilon": 1e-07,
 "floatx": "float32",
 "backend": "tensorflow"}
'''
''''''
Out[25]:
''
In [26]:
# variants: "theano", "tensorflow", "cntk"
keras.backend.backend()
Out[26]:
'tensorflow'
In [27]:
keras.backend.image_dim_ordering()
Out[27]:
'tf'

2. Data

internal datasets

In [28]:
# 32x32 color images; 50,000 - the train set; 10,000 - the test set; labeled over 10 categories
(x_train1, y_train1), (x_test1, y_test1) = cifar10.load_data()
In [29]:
n = int(len(x_test1)/2)
x_valid1, y_valid1 = x_test1[:n], y_test1[:n]
x_test1, y_test1 = x_test1[n:], y_test1[n:]
print(x_train1.shape, x_valid1.shape, x_test1.shape)
print(y_train1.shape, y_valid1.shape, y_test1.shape)
(50000, 32, 32, 3) (5000, 32, 32, 3) (5000, 32, 32, 3)
(50000, 1) (5000, 1) (5000, 1)
In [30]:
print('Label: ', y_train1[1][0])
plt.figure(figsize=(3,3)); plt.xticks([]); plt.yticks([])
plt.imshow(x_train1[1]);
Label:  9
In [31]:
# 28x28 grayscale images; 55,000 - the train set; 10,000 - the test set; labeled over 10 categories
(x_train2, y_train2), (x_test2, y_test2) = mnist.load_data()
In [32]:
n = int(len(x_test2)/2)
x_valid2, y_valid2 = x_test2[:n], y_test2[:n]
x_test2, y_test2 = x_test2[n:], y_test2[n:]
print(x_train2.shape, x_valid2.shape, x_test2.shape)
print(y_train2.shape, y_valid2.shape, y_test2.shape)
(60000, 28, 28) (5000, 28, 28) (5000, 28, 28)
(60000,) (5000,) (5000,)
In [33]:
print('Label: ', y_train2[1])
plt.figure(figsize=(3,3)); plt.xticks([]); plt.yticks([])
plt.imshow(x_train2[1], cmap='tab10');
Label:  0
In [34]:
# 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative)
num_words, max_length, embedding_vector_length = 10000, 1000, 32
(x_train3, y_train3), (x_test3, y_test3) = imdb.load_data(path="imdb_full.pkl", num_words=num_words,
                                                          skip_top=0, maxlen=max_length, seed=113,
                                                          start_char=1, oov_char=2, index_from=3)
In [35]:
n = int(len(x_test3)/2)
x_valid3, y_valid3 = x_test3[:n], y_test3[:n]
x_test3, y_test3 = x_test3[n:], y_test3[n:]
print(x_train3.shape, x_valid3.shape, x_test3.shape)
print(y_train3.shape, y_valid3.shape, y_test3.shape)
(25000,) (12409,) (12410,)
(25000,) (12409,) (12410,)
In [36]:
print('Label: ', y_train3[1])
print('Sequence of word indexes: \n', x_train3[1])
Label:  0
Sequence of word indexes: 
 [1, 194, 1153, 194, 8255, 78, 228, 5, 6, 1463, 4369, 5012, 134, 26, 4, 715, 8, 118, 1634, 14, 394, 20, 13, 119, 954, 189, 102, 5, 207, 110, 3103, 21, 14, 69, 188, 8, 30, 23, 7, 4, 249, 126, 93, 4, 114, 9, 2300, 1523, 5, 647, 4, 116, 9, 35, 8163, 4, 229, 9, 340, 1322, 4, 118, 9, 4, 130, 4901, 19, 4, 1002, 5, 89, 29, 952, 46, 37, 4, 455, 9, 45, 43, 38, 1543, 1905, 398, 4, 1649, 26, 6853, 5, 163, 11, 3215, 2, 4, 1153, 9, 194, 775, 7, 8255, 2, 349, 2637, 148, 605, 2, 8003, 15, 123, 125, 68, 2, 6853, 15, 349, 165, 4362, 98, 5, 4, 228, 9, 43, 2, 1157, 15, 299, 120, 5, 120, 174, 11, 220, 175, 136, 50, 9, 4373, 228, 8255, 5, 2, 656, 245, 2350, 5, 4, 9837, 131, 152, 491, 18, 2, 32, 7464, 1212, 14, 9, 6, 371, 78, 22, 625, 64, 1382, 9, 8, 168, 145, 23, 4, 1690, 15, 16, 4, 1355, 5, 28, 6, 52, 154, 462, 33, 89, 78, 285, 16, 145, 95]
In [37]:
# 13 attributes of houses at different locations, targets are the median values of the houses at a location (in k$)
(x_train4, y_train4), (x_test4, y_test4) =  boston_housing.load_data()
In [38]:
n = int(len(x_test4)/2)
x_valid4, y_valid4 = x_test4[:n], y_test4[:n]
x_test4, y_test4 = x_test4[n:], y_test4[n:]
print(x_train4.shape, x_valid4.shape, x_test4.shape)
print(y_train4.shape, y_valid4.shape, y_test4.shape)
(404, 13) (51, 13) (51, 13)
(404,) (51,) (51,)
In [39]:
print('Target value: ', y_train4[1])
print("Features' values: \n", x_train4[1])
Target value:  42.3
Features' values: 
 [2.1770e-02 8.2500e+01 2.0300e+00 0.0000e+00 4.1500e-01 7.6100e+00
 1.5700e+01 6.2700e+00 2.0000e+00 3.4800e+02 1.4700e+01 3.9538e+02
 3.1100e+00]
In [40]:
plt.figure(figsize=(14, 3))
plt.hist(y_train4, bins=50, alpha=0.7);

artificial datasets

In [41]:
# Classification; the artificial set, labeled over 2 categories 
X5, Y5 = make_classification(n_samples=5000, n_features=2, n_redundant=0, n_informative=2)
x_train5, x_test5, y_train5, y_test5 = train_test_split(X5, Y5, test_size = 0.2, random_state = 1)
In [42]:
n = int(len(x_test5)/2)
x_valid5, y_valid5 = x_test5[:n], y_test5[:n]
x_test5, y_test5 = x_test5[n:], y_test5[n:]
print(x_train5.shape, x_valid5.shape, x_test5.shape)
print(y_train5.shape, y_valid5.shape, y_test5.shape)
(4000, 2) (500, 2) (500, 2)
(4000,) (500,) (500,)
In [43]:
print('Label: ', y_train5[1])
print('Features: \n', x_train5[1])
Label:  0
Features: 
 [-0.68760779 -0.88166915]
In [44]:
plt.figure(figsize=(14,3)) 
plt.scatter(X5[:, 0], X5[:, 1], marker='o', s=5, c=Y5, cmap='tab10');

external datasets

In [45]:
# 150x150 grayscale face images; labeled over 15 categories(persons)
yalefaces_paths = []
yalefaces_images = []
yalefaces_labels = []

for element in os.listdir("yale_faces"):
    if element != 'Readme.txt':
        yalefaces_paths.append(os.path.join("yalefaces", element))
    
for path in yalefaces_paths:
    image = data.imread(path, as_gray=True)
    yalefaces_images.append(image)
    
    label = int(os.path.split(path)[1].split(".")[0].replace("subject", "")) - 1
    yalefaces_labels.append(label) 
    
yalefaces_cut_images = []
face_detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
for i in range(len(yalefaces_images)):
    image = yalefaces_images[i]
    face = face_detector.detectMultiScale(image)
    x, y = face[0][:2]
    cut_image = image[y: y+150, x: x+150]
    yalefaces_cut_images.append(cut_image)    
In [46]:
yalefaces_labels = np.array(yalefaces_labels)
yalefaces_cut_images = np.array(yalefaces_cut_images) / 255

print('Label: ', yalefaces_labels[11])
plt.imshow(yalefaces_cut_images[11], cmap='bone');
Label:  1
In [47]:
x_train6, x_test6, y_train6, y_test6 = \
train_test_split(yalefaces_cut_images, yalefaces_labels, test_size = 0.2, random_state = 1)

n = int(len(x_test6)/2)
x_valid6, y_valid6 = x_test6[:n], y_test6[:n]
x_test6, y_test6 = x_test6[n:], y_test6[n:]
print(x_train6.shape, x_valid6.shape, x_test6.shape)
print(y_train6.shape, y_valid6.shape, y_test6.shape)
(132, 150, 150) (16, 150, 150) (17, 150, 150)
(132,) (16,) (17,)
In [48]:
# 210 128x128 flower color images; labeled over 10 categories
flowers = pd.read_csv("flower_images/flower_labels.csv")
flower_files = flowers['file']
flower_targets = flowers['label'].values
In [49]:
def path_to_tensor(img_path):
    img = keras_image.load_img("flower_images/"+img_path, target_size=(128, 128))
    x = keras_image.img_to_array(img)
    return np.expand_dims(x, axis=0)

def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors)

ImageFile.LOAD_TRUNCATED_IMAGES = True                 

flower_tensors = paths_to_tensor(flower_files) / 255;
100%|██████████| 210/210 [00:10<00:00, 20.40it/s]
In [50]:
x_train7, x_test7, y_train7, y_test7 = \
train_test_split(flower_tensors, flower_targets,  test_size = 0.1, random_state = 1)

n = int(len(x_test7)/2)
x_valid7, y_valid7 = x_test7[:n], y_test7[:n]
x_test7, y_test7 = x_test7[n:], y_test7[n:]
print(x_train7.shape, x_valid7.shape, x_test7.shape)
print(y_train7.shape, y_valid7.shape, y_test7.shape)
(189, 128, 128, 3) (10, 128, 128, 3) (11, 128, 128, 3)
(189,) (10,) (11,)
In [51]:
print('Label: ', flower_targets[1])
flower_image = cv2.imread('flower_images/'+flower_files[1])
rgb_flower_image = cv2.cvtColor(flower_image, cv2.COLOR_BGR2RGB)
plt.xticks([]); plt.yticks([])
plt.imshow(rgb_flower_image);
Label:  0

3. Preprocessing

In [52]:
# Sequence Padding
p_x_train3 = keras_sequence.pad_sequences(x_train3, maxlen=max_length)
p_x_valid3 = keras_sequence.pad_sequences(x_valid3, maxlen=max_length)
p_x_test3 = keras_sequence.pad_sequences(x_test3, maxlen=max_length)
In [53]:
# One-Hot Encoding
c_y_train1 = to_categorical(y_train1, 10)
c_y_valid1 = to_categorical(y_valid1, 10)
c_y_test1 = to_categorical(y_test1, 10)

c_y_train2 = to_categorical(y_train2, 10)
c_y_valid2 = to_categorical(y_valid2, 10)
c_y_test2 = to_categorical(y_test2, 10)

c_y_train6 = to_categorical(y_train6, 15)
c_y_valid6 = to_categorical(y_valid6, 15)
c_y_test6 = to_categorical(y_test6, 15)

c_y_train7 = to_categorical(y_train7, 10)
c_y_valid7 = to_categorical(y_valid7, 10)
c_y_test7 = to_categorical(y_test7, 10)

4. Basic Information

Optimizers:

  • 'adam', 'nadam',
  • 'adagrad', 'adadelta', 'adamax',
  • 'sgd', 'rmsprop',
  • TFOptimizer.

Loss functions:

  • 'mean_squared_error'('mse'), 'mean_absolute_error'('mae'),
  • 'mean_absolute_percentage_error', 'mean_squared_logarithmic_error',
  • 'squared_hinge', 'hinge', 'categorical_hinge',
  • 'categorical_crossentropy', 'sparse_categorical_crossentropy',
  • 'binary_crossentropy',
  • 'kullback_leibler_divergence',
  • 'poisson',
  • 'cosine_proximity',
  • 'logcosh'.

Metrics:

  • mae, mse, acc,
  • binary_accuracy,
  • categorical_accuracy, sparse_categorical_accuracy,
  • top_k_categorical_accuracy, sparse_top_k_categorical_accuracy.

Compilation

For a multi-class classification problem:

  • model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
  • model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

For a binary classification problem:

  • model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
  • model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])

For a mean squared error regression problem:

  • model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
  • model.compile(optimizer='sgd', loss='mse', metrics=['mae'])

Activation

Types:

  • 'elu', 'selu', 'relu', 'tanh', 'linear', 'softmax', 'softplus', 'softsign', 'sigmoid', 'hard_sigmoid'
  • LeakyReLU, PReLU, ELU, ThresholdedReLU

For a multi-class classification problem:

  • 'softmax'

For a binary classification problem:

  • 'sigmoid'

For a mean squared error regression problem:

  • 'linear'
Layers
  1. Core Layers

    • Keras Documentation. Core Layers
    • Input layers hold an input tensor (for example, the pixel values of the image with width 32, height 32, and 3 color channels).
    • Dense (fully connected) layers compute the class scores, resulting in volume of size. For example, the size [11] corresponds to class scores, such as 10 digits and 1 empty place. Each neuron in these layers are connected to all the numbers in the previous volume.
    • Activation applies an activation function to an output. This leaves the size of the volume unchanged.
    • Dropout layers consist in randomly setting a fraction rate of input units to 0 at each update during training time, which helps prevent overfitting. This leaves the size of the volume unchanged.
    • Reshape layers convert an output to a certain shape.
    • Flatten layers flatten the input and collapses it into the one-dimensional feature vector. This results in the volume such as [2, 30, 64] -> [3840].
    • etc.
  2. Convolutional Layers

    • Keras Documentation. Convolutional Layers
    • Convolutional Neural Networks. Fei-Fei Li & Andrej Karpathy & Justin Johnson
    • Convolutional Neural Networks. Mirella Lapata
    • Conv1D layers (temporal convolution) convolve the filter with the signal, i.e. “is sliding over the signal vector, computing dot products”. Here the filter is an integer, the dimensionality of the output space (i.e. the number output of filters in the convolution) and the kernel size is an integer, specifying the length of the 1D convolution window.
    • Conv2D layers (spatial convolution over images) compute the output of neurons that are connected to local regions in the input each computing a dot product between their weights and a small region they are connected to in the input volume. This results in the volume such as [64, 64, 1] -> [64, 64, 32] when it was used 32 filters.
    • Conv3D layers (spatial convolution over volumes).
    • SeparableConv2D, Conv2DTranspose.
    • etc.
  3. Recurrent Layers

    • SimpleRNN layers are fully-connected RNN. This leaves the size of the volume unchanged.
    • LSTM layers.
    • GRU layers.
  4. Pooling Layers

    • Convolutional Neural Networks. CS231
    • MaxPooling2D layers perform a downsampling operation along the spatial dimensions (width, height). Max-pooling partitions the input image into a set of non-overlapping rectangles and, for each such subregion, outputs the maximum value. This results in the volume such as [28, 28, 32] -> [14, 14, 32].
    • MaxPooling1D & MaxPooling3D is a max pooling operation for temporal (1D) data and spatial or spatio-temporal (3D) data respectively.
  5. Advanced Activations Layers

    • LeakyReLU.
      • $\mathbf {f(x) = alpha * x for x < 0, f(x) = x for x >= 0}$, alpha is a small gradient.
    • PReLU.
      • $\mathbf {f(x) = alpha * x for x < 0, f(x) = x for x >= 0}$, alpha is a learned array.
    • ELU.
      • $\mathbf {f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0}$, alpha is a small gradient.
    • ThresholdedReLU.
      • $\mathbf {f(x) = x for x > theta, f(x) = 0}$ otherwise.

etc.

5. Basic Examples

In [57]:
# The basic model for binary classification
basic_model = Sequential([Dense(16, input_dim=2),  Activation('relu'), Dense(1),  Activation('sigmoid')])
basic_model.compile(optimizer='adam',  loss='binary_crossentropy',  metrics=['accuracy'])
In [58]:
# Train 
basic_model.fit(x_train5, y_train5, 
                validation_data=(x_valid5, y_valid5), 
                epochs=100, batch_size=128, verbose=0)

# Predict classes
y_test5_predictions = basic_model.predict_classes(x_test5)
In [59]:
plt.figure(figsize=(14,2))
plt.scatter(range(100), y_test5[:100], s=100)
plt.scatter(range(100), y_test5_predictions[:100], s=25);
In [60]:
# Evaluate
basic_score = basic_model.evaluate(x_test5, y_test5)
basic_score
500/500 [==============================] - 0s 44us/step
Out[60]:
[0.1389314285516739, 0.9439999990463257]
In [61]:
# Model parameters
basic_model.input, basic_model.outputs
Out[61]:
(<tf.Tensor 'dense_1_input:0' shape=(?, 2) dtype=float32>,
 [<tf.Tensor 'activation_2/Sigmoid:0' shape=(?, 1) dtype=float32>])
In [62]:
basic_model.input_shape, basic_model.output_shape
Out[62]:
((None, 2), (None, 1))
In [63]:
basic_model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 16)                48        
_________________________________________________________________
activation_1 (Activation)    (None, 16)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 17        
_________________________________________________________________
activation_2 (Activation)    (None, 1)                 0         
=================================================================
Total params: 65
Trainable params: 65
Non-trainable params: 0
_________________________________________________________________
In [64]:
basic_model.get_config()
Out[64]:
[{'class_name': 'Dense',
  'config': {'activation': 'linear',
   'activity_regularizer': None,
   'batch_input_shape': (None, 2),
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'dtype': 'float32',
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'name': 'dense_1',
   'trainable': True,
   'units': 16,
   'use_bias': True}},
 {'class_name': 'Activation',
  'config': {'activation': 'relu', 'name': 'activation_1', 'trainable': True}},
 {'class_name': 'Dense',
  'config': {'activation': 'linear',
   'activity_regularizer': None,
   'bias_constraint': None,
   'bias_initializer': {'class_name': 'Zeros', 'config': {}},
   'bias_regularizer': None,
   'kernel_constraint': None,
   'kernel_initializer': {'class_name': 'VarianceScaling',
    'config': {'distribution': 'uniform',
     'mode': 'fan_avg',
     'scale': 1.0,
     'seed': None}},
   'kernel_regularizer': None,
   'name': 'dense_2',
   'trainable': True,
   'units': 1,
   'use_bias': True}},
 {'class_name': 'Activation',
  'config': {'activation': 'sigmoid',
   'name': 'activation_2',
   'trainable': True}}]
In [65]:
basic_model.get_weights() 
Out[65]:
[array([[ 0.09925822, -0.8250601 ,  1.0280185 , -0.78917974, -0.76693577,
          1.2097049 , -0.5525417 , -0.6076799 , -0.14964095,  0.14575952,
         -0.54850155, -0.32459787,  0.74387157, -0.42363188, -0.45986006,
         -0.8186099 ],
        [ 1.0927873 , -0.10296759, -1.0314506 , -0.11693878, -0.13376448,
         -1.1853591 , -0.11369531, -0.09200531,  0.02896296,  1.0351516 ,
          0.12109938, -0.1563327 , -1.0734129 , -0.07054693, -0.4914755 ,
         -0.07361535]], dtype=float32),
 array([ 0.19543295,  0.40398502, -0.12174819,  0.45671332,  0.43987665,
        -0.15403457,  0.4860427 ,  0.52776366,  0.31206858,  0.21048962,
         0.5965124 , -0.07407436, -0.09734194,  0.5791241 , -0.27770907,
         0.42895946], dtype=float32),
 array([[ 0.77585286],
        [-0.46512994],
        [ 0.7959009 ],
        [-0.37247956],
        [-0.6835275 ],
        [ 0.8069941 ],
        [-0.7256957 ],
        [-0.41119534],
        [-0.54048514],
        [ 0.7797967 ],
        [-0.71725374],
        [ 0.9512965 ],
        [ 1.0971694 ],
        [-0.5804297 ],
        [ 0.5187552 ],
        [-0.41334665]], dtype=float32),
 array([0.33555368], dtype=float32)]
In [66]:
# Save/reload models
# basic_model.save('basic_model.h5')
# basic_model = load_model('basic_model.h5')
In [67]:
# Save/reload weights
# basic_model.save_weights('basic_model_weights.h5')
# basic_model.load_weights('basic_model_weights.h5', by_name=False)
In [69]:
# Choose optimization
optimizer = keras.optimizers.rmsprop(lr=0.001, decay=1e-5)
optimizer = keras.optimizers.Nadam(lr=0.005, beta_1=0.99, beta_2=0.9999, 
                                   epsilon=None, schedule_decay=0.005)

basic_model = Sequential([Dense(16, input_dim=2),  Activation('relu'), Dense(1),  Activation('sigmoid')])
basic_model.compile(optimizer=optimizer,  loss='binary_crossentropy',  metrics=['accuracy'])
basic_model.fit(x_train5, y_train5, 
                validation_data=(x_valid5, y_valid5), 
                epochs=100, batch_size=128, verbose=0)

basic_score = basic_model.evaluate(x_test5, y_test5)
basic_score
500/500 [==============================] - 0s 33us/step
Out[69]:
[0.12681850445270537, 0.954]
In [71]:
# Improve activation

# encoder
inp = Input(shape=(2,))
act = keras.layers.LeakyReLU(alpha=0.4)
lay = Dense(16, name='encoder')(inp)
lay = act(lay)

# decoder
out = Dense(1, activation='sigmoid', name='decoder')(lay)

# model
basic_model = Model(inputs=inp, outputs=out, name='cae')
basic_model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

basic_model.fit(x_train5, y_train5, 
                validation_data=(x_valid5, y_valid5), 
                epochs=100, batch_size=128, verbose=0)

basic_score = basic_model.evaluate(x_test5, y_test5)
basic_score
500/500 [==============================] - 0s 33us/step
Out[71]:
[0.13024811893701554, 0.954]
In [72]:
# Use callbacks

early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)
checkpointer = keras.callbacks.ModelCheckpoint(filepath='weights.best.basic_model.hdf5', save_best_only=True)
lr_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, factor=0.5)

basic_model = Model(inputs=inp, outputs=out, name='cae')
basic_model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])

basic_model.fit(x_train5, y_train5, validation_data=(x_valid5, y_valid5),
                epochs=200, batch_size=128, verbose=0, 
                callbacks=[early_stopping, checkpointer,  lr_reduction])

basic_model.load_weights('weights.best.basic_model.hdf5')
basic_score = basic_model.evaluate(x_test5, y_test5)
basic_score
500/500 [==============================] - 0s 37us/step
Out[72]:
[0.12721524745225907, 0.956]

6. Neural Networks

Multi-Layer Perceptrons

In [54]:
# Reshape image arrays
x_train6 = (x_train6).reshape(-1, 150*150)
x_valid6 = (x_valid6).reshape(-1, 150*150)
x_test6 = (x_test6).reshape(-1, 150*150)
In [56]:
# Multi-Class Classification
def model():
    model = Sequential()
    
    model.add(Dense(128, activation='relu', input_shape=(22500,)))
    model.add(Dropout(0.1))
    
    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.1))
    
    model.add(Dense(15, activation='softmax'))

    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model

model = model()
checkpointer = keras.callbacks.ModelCheckpoint(filepath='weights.best.model.hdf5', save_best_only=True)
lr_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train6, c_y_train6, validation_data=(x_valid6, c_y_valid6),
                    epochs=70, batch_size=64, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test6, c_y_test6)
test_score
Epoch 00035: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.

Epoch 00046: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.

Epoch 00051: ReduceLROnPlateau reducing learning rate to 0.0001250000059371814.
17/17 [==============================] - 0s 1ms/step
Out[56]:
[0.35443150997161865, 0.8823529481887817]
In [57]:
loss_plot(history)
acc_plot(history)
In [58]:
y_test6_predictions = model.predict_classes(x_test6)

plt.figure(figsize=(14,5))
plt.scatter(range(17), y_test6, s=100)
plt.scatter(range(17), y_test6_predictions, s=25);
In [62]:
# MLP: Multi-Class Classification
def model():
    model = Sequential()
    
    model.add(Dense(49, activation='relu', input_shape=(784,)))
    model.add(Dropout(0.1))
    
    model.add(Dense(8*784, activation='relu'))
    model.add(Dropout(0.1))
    
    model.add(Dense(10, activation='softmax'))

    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    return model

model = model()

early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss', patience=20)
checkpointer = keras.callbacks.ModelCheckpoint(filepath='weights.best.model.hdf5', save_best_only=True)
lr_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', verbose=2, patience=5, factor=0.5)

history = model.fit(x_train2.reshape(-1,784), c_y_train2, 
                    validation_data=(x_valid2.reshape(-1,784), c_y_valid2),
                    epochs=70, batch_size=256, verbose=0,
                    callbacks=[early_stopping, checkpointer,  lr_reduction]);
Epoch 00010: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.

Epoch 00015: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.

Epoch 00020: ReduceLROnPlateau reducing learning rate to 0.0001250000059371814.

Epoch 00030: ReduceLROnPlateau reducing learning rate to 6.25000029685907e-05.

Epoch 00046: ReduceLROnPlateau reducing learning rate to 3.125000148429535e-05.

Epoch 00052: ReduceLROnPlateau reducing learning rate to 1.5625000742147677e-05.

Epoch 00057: ReduceLROnPlateau reducing learning rate to 7.812500371073838e-06.

Epoch 00062: ReduceLROnPlateau reducing learning rate to 3.906250185536919e-06.

Epoch 00067: ReduceLROnPlateau reducing learning rate to 1.9531250927684596e-06.
In [63]:
loss_plot(history)
acc_plot(history)
In [64]:
model.load_weights('weights.best.model.hdf5')
y_test2_predictions = model.predict_classes(x_test2.reshape(-1,784))

plt.figure(figsize=(14,5))
plt.scatter(range(100), y_test2[:100], s=100)
plt.scatter(range(100), y_test2_predictions[:100], s=25)

test_score = model.evaluate(x_test2.reshape(-1,784), c_y_test2)
test_score
5000/5000 [==============================] - 0s 98us/step
Out[64]:
[5.141114578247071, 0.6808]
In [161]:
# Regression
def model():
    model = Sequential()
    
    model.add(Dense(52, activation='relu', input_shape=(13,)))    
    model.add(Dense(52, activation='relu')) 
    
    model.add(Dense(208, activation='relu'))
    model.add(Dense(208, activation='relu'))
    
    model.add(Dense(832, activation='relu'))
    
    model.add(Dense(1))

    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])     
    return model

model = model()
checkpointer = keras.callbacks.ModelCheckpoint(filepath='weights.best.model.hdf5', verbose=2, save_best_only=True)
lr_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train4, y_train4, validation_data=(x_valid4, y_valid4),
                    epochs=100, batch_size=16, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test4, y_test4)
test_score
Epoch 00001: val_loss improved from inf to 51.20755, saving model to weights.best.model.hdf5

Epoch 00002: val_loss did not improve from 51.20755

Epoch 00003: val_loss did not improve from 51.20755

Epoch 00004: val_loss did not improve from 51.20755

Epoch 00005: val_loss did not improve from 51.20755

Epoch 00006: val_loss did not improve from 51.20755

Epoch 00006: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.

Epoch 00007: val_loss improved from 51.20755 to 43.92056, saving model to weights.best.model.hdf5

Epoch 00008: val_loss improved from 43.92056 to 41.85145, saving model to weights.best.model.hdf5

Epoch 00009: val_loss did not improve from 41.85145

Epoch 00010: val_loss did not improve from 41.85145

Epoch 00011: val_loss did not improve from 41.85145

Epoch 00012: val_loss did not improve from 41.85145

Epoch 00013: val_loss did not improve from 41.85145

Epoch 00013: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.

Epoch 00014: val_loss did not improve from 41.85145

Epoch 00015: val_loss did not improve from 41.85145

Epoch 00016: val_loss improved from 41.85145 to 40.27671, saving model to weights.best.model.hdf5

Epoch 00017: val_loss did not improve from 40.27671

Epoch 00018: val_loss did not improve from 40.27671

Epoch 00019: val_loss improved from 40.27671 to 37.29227, saving model to weights.best.model.hdf5

Epoch 00020: val_loss did not improve from 37.29227

Epoch 00021: val_loss did not improve from 37.29227

Epoch 00022: val_loss did not improve from 37.29227

Epoch 00023: val_loss did not improve from 37.29227

Epoch 00024: val_loss improved from 37.29227 to 32.66993, saving model to weights.best.model.hdf5

Epoch 00025: val_loss did not improve from 32.66993

Epoch 00026: val_loss did not improve from 32.66993

Epoch 00027: val_loss did not improve from 32.66993

Epoch 00028: val_loss did not improve from 32.66993

Epoch 00029: val_loss did not improve from 32.66993

Epoch 00029: ReduceLROnPlateau reducing learning rate to 0.0001250000059371814.

Epoch 00030: val_loss did not improve from 32.66993

Epoch 00031: val_loss did not improve from 32.66993

Epoch 00032: val_loss did not improve from 32.66993

Epoch 00033: val_loss did not improve from 32.66993

Epoch 00034: val_loss did not improve from 32.66993

Epoch 00034: ReduceLROnPlateau reducing learning rate to 6.25000029685907e-05.

Epoch 00035: val_loss improved from 32.66993 to 31.74107, saving model to weights.best.model.hdf5

Epoch 00036: val_loss did not improve from 31.74107

Epoch 00037: val_loss did not improve from 31.74107

Epoch 00038: val_loss did not improve from 31.74107

Epoch 00039: val_loss improved from 31.74107 to 30.95767, saving model to weights.best.model.hdf5

Epoch 00040: val_loss did not improve from 30.95767

Epoch 00041: val_loss did not improve from 30.95767

Epoch 00042: val_loss did not improve from 30.95767

Epoch 00043: val_loss did not improve from 30.95767

Epoch 00044: val_loss improved from 30.95767 to 30.82275, saving model to weights.best.model.hdf5

Epoch 00045: val_loss did not improve from 30.82275

Epoch 00046: val_loss did not improve from 30.82275

Epoch 00047: val_loss did not improve from 30.82275

Epoch 00048: val_loss improved from 30.82275 to 30.05832, saving model to weights.best.model.hdf5

Epoch 00049: val_loss did not improve from 30.05832

Epoch 00050: val_loss did not improve from 30.05832

Epoch 00051: val_loss improved from 30.05832 to 30.01430, saving model to weights.best.model.hdf5

Epoch 00052: val_loss did not improve from 30.01430

Epoch 00053: val_loss did not improve from 30.01430

Epoch 00054: val_loss did not improve from 30.01430

Epoch 00055: val_loss did not improve from 30.01430

Epoch 00056: val_loss did not improve from 30.01430

Epoch 00056: ReduceLROnPlateau reducing learning rate to 3.125000148429535e-05.

Epoch 00057: val_loss improved from 30.01430 to 29.96632, saving model to weights.best.model.hdf5

Epoch 00058: val_loss did not improve from 29.96632

Epoch 00059: val_loss did not improve from 29.96632

Epoch 00060: val_loss did not improve from 29.96632

Epoch 00061: val_loss did not improve from 29.96632

Epoch 00062: val_loss did not improve from 29.96632

Epoch 00062: ReduceLROnPlateau reducing learning rate to 1.5625000742147677e-05.

Epoch 00063: val_loss did not improve from 29.96632

Epoch 00064: val_loss did not improve from 29.96632

Epoch 00065: val_loss did not improve from 29.96632

Epoch 00066: val_loss did not improve from 29.96632

Epoch 00067: val_loss did not improve from 29.96632

Epoch 00067: ReduceLROnPlateau reducing learning rate to 7.812500371073838e-06.

Epoch 00068: val_loss improved from 29.96632 to 29.95711, saving model to weights.best.model.hdf5

Epoch 00069: val_loss did not improve from 29.95711

Epoch 00070: val_loss did not improve from 29.95711

Epoch 00071: val_loss did not improve from 29.95711

Epoch 00072: val_loss did not improve from 29.95711

Epoch 00073: val_loss did not improve from 29.95711

Epoch 00073: ReduceLROnPlateau reducing learning rate to 3.906250185536919e-06.

Epoch 00074: val_loss did not improve from 29.95711

Epoch 00075: val_loss improved from 29.95711 to 29.94242, saving model to weights.best.model.hdf5

Epoch 00076: val_loss improved from 29.94242 to 29.92641, saving model to weights.best.model.hdf5

Epoch 00077: val_loss improved from 29.92641 to 29.80197, saving model to weights.best.model.hdf5

Epoch 00078: val_loss did not improve from 29.80197

Epoch 00079: val_loss did not improve from 29.80197

Epoch 00080: val_loss did not improve from 29.80197

Epoch 00081: val_loss improved from 29.80197 to 29.78994, saving model to weights.best.model.hdf5

Epoch 00082: val_loss did not improve from 29.78994

Epoch 00083: val_loss did not improve from 29.78994

Epoch 00084: val_loss improved from 29.78994 to 29.78937, saving model to weights.best.model.hdf5

Epoch 00085: val_loss did not improve from 29.78937

Epoch 00086: val_loss did not improve from 29.78937

Epoch 00087: val_loss did not improve from 29.78937

Epoch 00088: val_loss did not improve from 29.78937

Epoch 00089: val_loss did not improve from 29.78937

Epoch 00089: ReduceLROnPlateau reducing learning rate to 1.9531250927684596e-06.

Epoch 00090: val_loss did not improve from 29.78937

Epoch 00091: val_loss did not improve from 29.78937

Epoch 00092: val_loss did not improve from 29.78937

Epoch 00093: val_loss did not improve from 29.78937

Epoch 00094: val_loss did not improve from 29.78937

Epoch 00094: ReduceLROnPlateau reducing learning rate to 9.765625463842298e-07.

Epoch 00095: val_loss did not improve from 29.78937

Epoch 00096: val_loss did not improve from 29.78937

Epoch 00097: val_loss did not improve from 29.78937

Epoch 00098: val_loss did not improve from 29.78937

Epoch 00099: val_loss did not improve from 29.78937

Epoch 00099: ReduceLROnPlateau reducing learning rate to 4.882812731921149e-07.

Epoch 00100: val_loss did not improve from 29.78937
51/51 [==============================] - 0s 114us/step
Out[161]:
[34.604219848034425, 4.243978883705887]
In [162]:
loss_plot(history)
mae_plot(history)
In [163]:
y_test4_predictions = model.predict(x_test4)
plt.figure(figsize=(14,3))
plt.plot(range(len(y_test4)), y_test4, '-o', label = 'real data')
plt.plot(range(len(y_test4)), y_test4_predictions, '-o', label = 'predictions');

Convolutional Neural Networks (CNN)

In [65]:
# CNN: Binary Classification
def model():
    model = Sequential()
    
    model.add(Conv1D(filters=8, kernel_size=5, padding='same', 
                     activation='relu', input_shape=(2,1)))
    model.add(Conv1D(filters=8, kernel_size=5, 
                     padding='same', activation='relu'))
    
    model.add(MaxPooling1D(pool_size=2))   
    model.add(Dropout(0.1))  

    model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])   
    return model

model = model()
checkpointer = keras.callbacks.ModelCheckpoint(filepath='weights.best.model.hdf5', save_best_only=True)
lr_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train5.reshape(-1, 2, 1), y_train5.reshape(-1, 1, 1), 
                    validation_data=(x_valid5.reshape(-1, 2, 1), y_valid5.reshape(-1, 1, 1)),
                    epochs=20, batch_size=64, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test5.reshape(-1, 2, 1), y_test5.reshape(-1, 1, 1))
test_score
Epoch 00016: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.
500/500 [==============================] - 0s 44us/step
Out[65]:
[0.14828426146507262, 0.9459999990463257]
In [66]:
y_test5_predictions = model.predict_classes(x_test5.reshape(-1, 2, 1))
plt.figure(figsize=(14,2))
plt.scatter(range(100), y_test5[:100], s=100)
plt.scatter(range(100), y_test5_predictions[:100], s=25);
In [67]:
# VGG-like CNN: Multi-Class Classification
def model():
    model = Sequential()

    model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train1.shape[1:]))
    model.add(Activation('relu'))
    model.add(Conv2D(32, (3, 3)))
    model.add(Activation('relu'))
    
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(96, (3, 3), padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(96, (3, 3)))
    model.add(Activation('relu'))
    
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(GlobalMaxPooling2D())
    
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])    
    return model
In [68]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5',  save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train1, c_y_train1, validation_data=(x_valid1, c_y_valid1),
                    epochs=30, batch_size=128, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test1, c_y_test1)
test_score
5000/5000 [==============================] - 14s 3ms/step
Out[68]:
[0.821739066696167, 0.7358]
In [69]:
loss_plot(history)
acc_plot(history)
In [70]:
steps, epochs = 1000, 5
data_generator = keras_image.ImageDataGenerator(zoom_range=0.2, horizontal_flip=True)
dg_history = \
model.fit_generator(data_generator.flow(x_train1, c_y_train1, batch_size=128),
                    steps_per_epoch = steps, epochs = epochs, verbose=2, 
                    validation_data = (x_valid1, c_y_valid1),
                    callbacks=[checkpointer, lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test1, c_y_test1)
test_score
Epoch 1/5
 - 1123s - loss: 0.9626 - acc: 0.6683 - val_loss: 0.8603 - val_acc: 0.7036
Epoch 2/5
 - 890s - loss: 0.9466 - acc: 0.6730 - val_loss: 0.8477 - val_acc: 0.7118
Epoch 3/5
 - 884s - loss: 0.9260 - acc: 0.6815 - val_loss: 0.9020 - val_acc: 0.6934
Epoch 4/5
 - 1101s - loss: 0.9109 - acc: 0.6866 - val_loss: 0.8628 - val_acc: 0.7014
Epoch 5/5
 - 1016s - loss: 0.9033 - acc: 0.6885 - val_loss: 0.7767 - val_acc: 0.7380
5000/5000 [==============================] - 12s 2ms/step
Out[70]:
[0.7820099027633667, 0.738]
In [71]:
loss_plot(dg_history)
acc_plot(dg_history)
In [72]:
y_valid1_predictions = model.predict_classes(x_valid1)
y_test1_predictions = model.predict_classes(x_test1)
In [73]:
plt.figure(1, figsize=(18, 5))
plt.subplot(121)
plt.scatter(range(100), y_valid1[:100], s=100)
plt.scatter(range(100), y_valid1_predictions[:100], s=25)
plt.title("Validation Predictions")
plt.subplot(122)
plt.scatter(range(100), y_test1[:100], s=100)
plt.scatter(range(100), y_test1_predictions[:100], s=25)
plt.title("Test Predictions");
In [74]:
# CNN: Regression
def model():
    model = Sequential()
    
    model.add(Conv1D(52, 5, padding='valid', activation='relu', input_shape=(13,1)))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Dropout(0.2))

    model.add(Conv1D(208, 3, padding='valid', activation='relu'))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Dropout(0.2))
    
    model.add(Flatten())

    model.add(Dense(1024, kernel_initializer='normal', activation='relu'))
    model.add(Dropout(0.4))

    model.add(Dense(1, kernel_initializer='normal'))
    
    model.compile(loss='mse', optimizer='rmsprop', metrics=['mae'])
    return model    
In [75]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5', verbose=0, save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=10, verbose=2, factor=0.95)

history = model.fit(x_train4.reshape(-1, 13, 1),  y_train4, 
                    validation_data=(x_valid4.reshape(-1, 13, 1),  y_valid4),
                    epochs=300, batch_size=16, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test4.reshape(-1, 13, 1),  y_test4)
test_score
Epoch 00013: ReduceLROnPlateau reducing learning rate to 0.0009500000451225787.

Epoch 00023: ReduceLROnPlateau reducing learning rate to 0.0009025000152178108.

Epoch 00043: ReduceLROnPlateau reducing learning rate to 0.0008573750033974647.

Epoch 00082: ReduceLROnPlateau reducing learning rate to 0.0008145062311086804.

Epoch 00099: ReduceLROnPlateau reducing learning rate to 0.0007737808919046074.

Epoch 00109: ReduceLROnPlateau reducing learning rate to 0.000735091819660738.

Epoch 00119: ReduceLROnPlateau reducing learning rate to 0.0006983372120885178.

Epoch 00137: ReduceLROnPlateau reducing learning rate to 0.0006634203542489559.

Epoch 00147: ReduceLROnPlateau reducing learning rate to 0.0006302493420662358.

Epoch 00162: ReduceLROnPlateau reducing learning rate to 0.0005987368611386045.

Epoch 00172: ReduceLROnPlateau reducing learning rate to 0.0005688000208465382.

Epoch 00182: ReduceLROnPlateau reducing learning rate to 0.0005403600225690752.

Epoch 00192: ReduceLROnPlateau reducing learning rate to 0.0005133419937919825.

Epoch 00202: ReduceLROnPlateau reducing learning rate to 0.0004876748775132.

Epoch 00212: ReduceLROnPlateau reducing learning rate to 0.00046329112810781223.

Epoch 00222: ReduceLROnPlateau reducing learning rate to 0.00044012657308485355.

Epoch 00232: ReduceLROnPlateau reducing learning rate to 0.00041812024719547477.

Epoch 00242: ReduceLROnPlateau reducing learning rate to 0.00039721422654110934.

Epoch 00252: ReduceLROnPlateau reducing learning rate to 0.00037735351797891776.

Epoch 00262: ReduceLROnPlateau reducing learning rate to 0.00035848583793267607.

Epoch 00272: ReduceLROnPlateau reducing learning rate to 0.00034056155709549785.

Epoch 00282: ReduceLROnPlateau reducing learning rate to 0.00032353347924072293.

Epoch 00292: ReduceLROnPlateau reducing learning rate to 0.00030735681357327847.
51/51 [==============================] - 0s 135us/step
Out[75]:
[33.0717866934982, 4.104080508737003]
In [76]:
loss_plot(history)
mae_plot(history)
In [78]:
y_test4_predictions = model.predict(x_test4.reshape(-1, 13, 1))
plt.figure(figsize=(14,3))
plt.plot(range(len(y_test4)), y_test4, '-o', label = 'real data')
plt.plot(range(len(y_test4)), y_test4_predictions, '-o', label = 'predictions')
plt.legend();

Recurrent Neural Networks (RNN)

In [84]:
# RNN: Binary Classification
def model():
    model = Sequential()

    model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2, input_shape=(1, 2)))
    model.add(Dense(1, activation='sigmoid'))

    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])     
    
    return model 
In [85]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5',  save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train5.reshape(-1, 1, 2), y_train5, 
                    validation_data=(x_valid5.reshape(-1, 1, 2), y_valid5),
                    epochs=20, batch_size=128, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test5.reshape(-1, 1, 2), y_test5)
test_score
500/500 [==============================] - 0s 61us/step
Out[85]:
[0.1947311773300171, 0.9319999990463257]
In [86]:
loss_plot(history)
acc_plot(history)
In [87]:
y_test5_predictions = model.predict_classes(x_test5.reshape(-1, 1, 2))
plt.figure(figsize=(14,2))
plt.scatter(range(100), y_test5[:100], s=100)
plt.scatter(range(100), y_test5_predictions[:100], s=25);
In [88]:
# RNN: Multi-Class Classification
def model():
    model = Sequential()

    model.add(LSTM(112, return_sequences=True, input_shape=(1,784)))
    
    model.add(LSTM(112, return_sequences=True)) 
    model.add(LSTM(112))  
    
    model.add(Dense(10, activation='softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='nadam', metrics=['accuracy'])    
    return model 
In [89]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5',  save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = model.fit(x_train2.reshape(-1, 1, 784), c_y_train2, 
                    validation_data=(x_valid2.reshape(-1, 1, 784), c_y_valid2),
                    epochs=10, batch_size=128, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test2.reshape(-1, 1, 784), c_y_test2)
test_score
5000/5000 [==============================] - 1s 207us/step
Out[89]:
[0.2204880813241005, 0.9296]
In [90]:
loss_plot(history)
acc_plot(history)
In [92]:
y_test2_predictions = model.predict_classes(x_test2.reshape(-1, 1, 784))
plt.figure(figsize=(14, 5))
plt.scatter(range(100), y_test2[:100], s=100)
plt.scatter(range(100), y_test2_predictions[:100], s=25);
In [94]:
# RNN: Regression
def model():
    model = Sequential()
    
    model.add(LSTM(52, return_sequences=True, input_shape=(1, 13)))
    model.add(LSTM(208, return_sequences=False))   
    
    model.add(Dense(1))

    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])  
    return model
In [95]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5', verbose=0, save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=10, verbose=2, factor=0.95)

history = model.fit(x_train4.reshape(-1, 1, 13),  y_train4, 
                    validation_data=(x_valid4.reshape(-1, 1, 13),  y_valid4),
                    epochs=400, batch_size=16, verbose=0,
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(x_test4.reshape(-1, 1, 13),  y_test4)
test_score
Epoch 00097: ReduceLROnPlateau reducing learning rate to 0.0009500000451225787.

Epoch 00107: ReduceLROnPlateau reducing learning rate to 0.0009025000152178108.

Epoch 00136: ReduceLROnPlateau reducing learning rate to 0.0008573750033974647.

Epoch 00146: ReduceLROnPlateau reducing learning rate to 0.0008145062311086804.

Epoch 00160: ReduceLROnPlateau reducing learning rate to 0.0007737808919046074.

Epoch 00170: ReduceLROnPlateau reducing learning rate to 0.000735091819660738.

Epoch 00188: ReduceLROnPlateau reducing learning rate to 0.0006983372120885178.

Epoch 00198: ReduceLROnPlateau reducing learning rate to 0.0006634203542489559.

Epoch 00208: ReduceLROnPlateau reducing learning rate to 0.0006302493420662358.

Epoch 00218: ReduceLROnPlateau reducing learning rate to 0.0005987368611386045.

Epoch 00228: ReduceLROnPlateau reducing learning rate to 0.0005688000208465382.

Epoch 00238: ReduceLROnPlateau reducing learning rate to 0.0005403600225690752.

Epoch 00248: ReduceLROnPlateau reducing learning rate to 0.0005133419937919825.

Epoch 00258: ReduceLROnPlateau reducing learning rate to 0.0004876748775132.

Epoch 00268: ReduceLROnPlateau reducing learning rate to 0.00046329112810781223.

Epoch 00278: ReduceLROnPlateau reducing learning rate to 0.00044012657308485355.

Epoch 00288: ReduceLROnPlateau reducing learning rate to 0.00041812024719547477.

Epoch 00298: ReduceLROnPlateau reducing learning rate to 0.00039721422654110934.

Epoch 00308: ReduceLROnPlateau reducing learning rate to 0.00037735351797891776.

Epoch 00318: ReduceLROnPlateau reducing learning rate to 0.00035848583793267607.

Epoch 00328: ReduceLROnPlateau reducing learning rate to 0.00034056155709549785.

Epoch 00338: ReduceLROnPlateau reducing learning rate to 0.00032353347924072293.

Epoch 00348: ReduceLROnPlateau reducing learning rate to 0.00030735681357327847.

Epoch 00358: ReduceLROnPlateau reducing learning rate to 0.00029198898118920624.

Epoch 00368: ReduceLROnPlateau reducing learning rate to 0.00027738953212974593.

Epoch 00378: ReduceLROnPlateau reducing learning rate to 0.0002635200624354184.

Epoch 00388: ReduceLROnPlateau reducing learning rate to 0.0002503440482541919.

Epoch 00398: ReduceLROnPlateau reducing learning rate to 0.00023782684584148226.
51/51 [==============================] - 0s 152us/step
Out[95]:
[37.02598796171301, 4.492552588967716]
In [96]:
loss_plot(history)
mae_plot(history)
In [97]:
y_test4_predictions = model.predict(x_test4.reshape(-1, 1, 13))
plt.figure(figsize=(14,3))
plt.plot(range(len(y_test4)), y_test4, '-o', label = 'real data')
plt.plot(range(len(y_test4)), y_test4_predictions, '-o', label = 'predictions')
plt.legend();

Combined Neural Networks

In [98]:
# CNN & LSTM For Sequence Classification
def model():
    model = Sequential()

    model.add(Embedding(num_words, embedding_vector_length, input_length=max_length))

    model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
    model.add(MaxPooling1D(pool_size=2))
    
    model.add(LSTM(32))
    
    model.add(Dense(1, activation='sigmoid'))

    model.compile(loss='binary_crossentropy', optimizer='nadam', metrics=['accuracy'])    
    return model
In [99]:
model = model()
checkpointer = keras.callbacks\
.ModelCheckpoint(filepath='weights.best.model.hdf5', verbose=0, save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=10, verbose=2, factor=0.5)

history = model.fit(p_x_train3,  y_train3,  epochs=3,  batch_size=128,
                    validation_data=(p_x_valid3,  y_valid3),
                    callbacks=[checkpointer,  lr_reduction])

model.load_weights('weights.best.model.hdf5')
test_score = model.evaluate(p_x_test3,  y_test3)
test_score
Train on 25000 samples, validate on 12409 samples
Epoch 1/3
25000/25000 [==============================] - 222s 9ms/step - loss: 0.4281 - acc: 0.7884 - val_loss: 0.3321 - val_acc: 0.8610
Epoch 2/3
25000/25000 [==============================] - 182s 7ms/step - loss: 0.2195 - acc: 0.9174 - val_loss: 0.3582 - val_acc: 0.8732
Epoch 3/3
25000/25000 [==============================] - 204s 8ms/step - loss: 0.1566 - acc: 0.9443 - val_loss: 0.3816 - val_acc: 0.8636
12410/12410 [==============================] - 47s 4ms/step
Out[99]:
[0.3233053187321118, 0.8648670426690703]
In [100]:
loss_plot(history)
acc_plot(history)

7. Applications

In [102]:
resnet50_model = ResNet50(weights='imagenet')
# resnet50_model_json = resnet50_model.to_json()

# with open("resnet50.json", "w") as json_file:
#     json_file.write(resnet50_model_json)

# json_file = open('resnet50.json', 'r')
# resnet50_model_json = json_file.read()
# json_file.close()

# resnet50_model = model_from_json(resnet50_model_json)
In [103]:
# Classify imagenet_class_index.json classes with ResNet50
cat_image = keras_image.load_img('cat.png', target_size=(224, 224))
CLASS_INDEX = None

x = keras_image.img_to_array(cat_image)
x = np.expand_dims(x, axis=0)
x = resnet50_preprocess_input(x)

cat_predictions = resnet50_model.predict(x)
print('Predictions: \n', decode_predictions2(cat_predictions, 'imagenet_class_index.json')[0])

cv_cat_image = cv2.imread('cat.png')
rgb_cat_image = cv2.cvtColor(cv_cat_image, cv2.COLOR_BGR2RGB)
plt.imshow(rgb_cat_image);
Predictions: 
 [('n02123394', 'Persian_cat', 0.5807487), ('n02120079', 'Arctic_fox', 0.08761675), ('n02120505', 'grey_fox', 0.044340517), ('n02123045', 'tabby', 0.041976497), ('n02119022', 'red_fox', 0.032001775)]
In [104]:
# Extract image features with VGG16

# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/
# vgg16_weights_tf_dim_ordering_tf_kernels.h5
# vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5

vgg16_model = VGG16(weights='imagenet', include_top=False)
cat_image = keras_image.load_img('cat.png', target_size=(224, 224))

y = keras_image.img_to_array(cat_image)
y = np.expand_dims(y, axis=0)
y = vgg16_preprocess_input(y)

cat_features16 = vgg16_model.predict(y)
np.argmax(cat_features16)
Out[104]:
5406
In [106]:
# Create bottleneck features with VGG16
VGG16_model = VGG16(weights='imagenet', include_top=False)
bn_x_train1 = VGG16_model.predict(x_train1)
bn_x_valid1 = VGG16_model.predict(x_valid1)
bn_x_test1 = VGG16_model.predict(x_test1)
In [107]:
#np.save('bn_x_train1.npy', bn_x_train1.reshape(bn_x_train1.shape[0], bn_x_train1.shape[3]))
#np.save('bn_x_valid1.npy', bn_x_valid1.reshape(bn_x_valid1.shape[0], bn_x_valid1.shape[3]))
#np.save('bn_x_test1.npy', bn_x_test1.reshape(bn_x_test1.shape[0], bn_x_test1.shape[3]))
In [108]:
#bn_x_trainl = np.load('bn_x_train1.npy')
#bn_x_validl = np.load('bn_x_valid1.npy')
#bn_x_test1 = np.load('bn_x_test1.npy')
bn_x_train1 = bn_x_train1.reshape(bn_x_train1.shape[0], 1, 1, bn_x_train1.shape[3])
bn_x_valid1 = bn_x_valid1.reshape(bn_x_valid1.shape[0], 1, 1, bn_x_valid1.shape[3])
bn_x_testl = bn_x_test1.reshape(bn_x_test1.shape[0], 1, 1, bn_x_test1.shape[3])
In [113]:
def vgg16_add_model():
    model = Sequential()
    model.add(GlobalAveragePooling2D(input_shape=bn_x_train1.shape[1:]))

    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(384, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(10, activation='softmax'))
    
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
In [114]:
vgg16_add_model = vgg16_add_model()

checkpointer = keras.callbacks.\
ModelCheckpoint(filepath='weights.best.model.hdf5', verbose=2, save_best_only=True)
lr_reduction = keras.callbacks\
.ReduceLROnPlateau(monitor='val_loss', patience=10, verbose=2, factor=0.5)
In [115]:
vgg16_add_history = vgg16_add_model.fit(bn_x_train1, c_y_train1, 
                                        validation_data=(bn_x_valid1, c_y_valid1),
                                        epochs=50, batch_size=128, verbose=0, 
                                        callbacks=[checkpointer, lr_reduction]);
Epoch 00001: val_loss improved from inf to 2.01118, saving model to weights.best.model.hdf5

Epoch 00002: val_loss improved from 2.01118 to 1.60050, saving model to weights.best.model.hdf5

Epoch 00003: val_loss improved from 1.60050 to 1.45309, saving model to weights.best.model.hdf5

Epoch 00004: val_loss improved from 1.45309 to 1.37677, saving model to weights.best.model.hdf5

Epoch 00005: val_loss improved from 1.37677 to 1.32503, saving model to weights.best.model.hdf5

Epoch 00006: val_loss improved from 1.32503 to 1.29172, saving model to weights.best.model.hdf5

Epoch 00007: val_loss improved from 1.29172 to 1.25889, saving model to weights.best.model.hdf5

Epoch 00008: val_loss improved from 1.25889 to 1.22786, saving model to weights.best.model.hdf5

Epoch 00009: val_loss improved from 1.22786 to 1.21527, saving model to weights.best.model.hdf5

Epoch 00010: val_loss improved from 1.21527 to 1.19184, saving model to weights.best.model.hdf5

Epoch 00011: val_loss improved from 1.19184 to 1.18329, saving model to weights.best.model.hdf5

Epoch 00012: val_loss improved from 1.18329 to 1.17331, saving model to weights.best.model.hdf5

Epoch 00013: val_loss improved from 1.17331 to 1.15623, saving model to weights.best.model.hdf5

Epoch 00014: val_loss improved from 1.15623 to 1.15578, saving model to weights.best.model.hdf5

Epoch 00015: val_loss improved from 1.15578 to 1.14269, saving model to weights.best.model.hdf5

Epoch 00016: val_loss did not improve from 1.14269

Epoch 00017: val_loss did not improve from 1.14269

Epoch 00018: val_loss improved from 1.14269 to 1.13358, saving model to weights.best.model.hdf5

Epoch 00019: val_loss did not improve from 1.13358

Epoch 00020: val_loss did not improve from 1.13358

Epoch 00021: val_loss improved from 1.13358 to 1.12589, saving model to weights.best.model.hdf5

Epoch 00022: val_loss did not improve from 1.12589

Epoch 00023: val_loss did not improve from 1.12589

Epoch 00024: val_loss did not improve from 1.12589

Epoch 00025: val_loss did not improve from 1.12589

Epoch 00026: val_loss did not improve from 1.12589

Epoch 00027: val_loss did not improve from 1.12589

Epoch 00028: val_loss did not improve from 1.12589

Epoch 00029: val_loss did not improve from 1.12589

Epoch 00030: val_loss did not improve from 1.12589

Epoch 00031: val_loss did not improve from 1.12589

Epoch 00031: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.

Epoch 00032: val_loss did not improve from 1.12589

Epoch 00033: val_loss did not improve from 1.12589

Epoch 00034: val_loss did not improve from 1.12589

Epoch 00035: val_loss did not improve from 1.12589

Epoch 00036: val_loss did not improve from 1.12589

Epoch 00037: val_loss did not improve from 1.12589

Epoch 00038: val_loss did not improve from 1.12589

Epoch 00039: val_loss did not improve from 1.12589

Epoch 00040: val_loss did not improve from 1.12589

Epoch 00041: val_loss did not improve from 1.12589

Epoch 00041: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.

Epoch 00042: val_loss did not improve from 1.12589

Epoch 00043: val_loss did not improve from 1.12589

Epoch 00044: val_loss did not improve from 1.12589

Epoch 00045: val_loss did not improve from 1.12589

Epoch 00046: val_loss did not improve from 1.12589

Epoch 00047: val_loss did not improve from 1.12589

Epoch 00048: val_loss did not improve from 1.12589

Epoch 00049: val_loss did not improve from 1.12589

Epoch 00050: val_loss did not improve from 1.12589
In [116]:
loss_plot(vgg16_add_history)
acc_plot(vgg16_add_history)
In [117]:
vgg16_add_model.load_weights('weights.best.model.hdf5')
vgg16_add_predictions = \
[np.argmax(vgg16_add_model.predict(np.expand_dims(x, axis=0))) for x in bn_x_test1]
vgg16_add_test_accuracy = \
100*np.sum(np.array(vgg16_add_predictions)==np.argmax(c_y_test1, axis=1))/len(vgg16_add_predictions)
print('Test accuracy: %.4f%%' % vgg16_add_test_accuracy)
Test accuracy: 62.6800%
In [118]:
# Extract image features with VGG19

# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/
# vgg19_weights_tf_dim_ordering_tf_kernels.h5
# vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5

vgg19_model = VGG19(weights='imagenet')
cat_image = keras_image.load_img('cat.png', target_size=(224, 224))

z = keras_image.img_to_array(cat_image)
z = np.expand_dims(z, axis=0)
z = vgg19_preprocess_input(z)

cat_features19 = vgg19_model.predict(z)
np.argmax(cat_features19)
Out[118]:
281
In [122]:
# InceptionV3 for a new dataset

# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/
# inception_v3_weights_tf_dim_ordering_tf_kernels.h5
# inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5

iv3_base_model = InceptionV3(weights='imagenet', include_top=False)
x = iv3_base_model.output

x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)

y = Dense(10, activation='softmax')(x)

iv3_model = Model(inputs=iv3_base_model.input, outputs=y)
In [123]:
# Freeze InceptionV3 convolutional layers
for layer in iv3_base_model.layers:
    layer.trainable = False
    
iv3_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])    
In [124]:
# Train
steps, epochs = 189, 10
data_generator = \
keras_image.ImageDataGenerator(shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
checkpointer = \
keras.callbacks.ModelCheckpoint(filepath='weights.best.iv3_model.hdf5', verbose=2, save_best_only=True)
lr_reduction = \
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=2, factor=0.5)

history = iv3_model.fit_generator(data_generator.flow(x_train7, c_y_train7, batch_size=64), 
                                  steps_per_epoch = steps, epochs = epochs, 
                                  callbacks=[checkpointer, lr_reduction],
                                  validation_data = (x_valid7, c_y_valid7))
Epoch 1/10
189/189 [==============================] - 1038s 5s/step - loss: 0.4296 - acc: 0.8747 - val_loss: 4.2318 - val_acc: 0.5000

Epoch 00001: val_loss improved from inf to 4.23178, saving model to weights.best.iv3_model.hdf5
Epoch 2/10
189/189 [==============================] - 764s 4s/step - loss: 0.1375 - acc: 0.9602 - val_loss: 4.7080 - val_acc: 0.5000

Epoch 00002: val_loss did not improve from 4.23178
Epoch 3/10
189/189 [==============================] - 838s 4s/step - loss: 0.1075 - acc: 0.9668 - val_loss: 6.6460 - val_acc: 0.3000

Epoch 00003: val_loss did not improve from 4.23178
Epoch 4/10
189/189 [==============================] - 925s 5s/step - loss: 0.0873 - acc: 0.9720 - val_loss: 5.4881 - val_acc: 0.4000

Epoch 00004: val_loss did not improve from 4.23178
Epoch 5/10
189/189 [==============================] - 917s 5s/step - loss: 0.0689 - acc: 0.9788 - val_loss: 4.7920 - val_acc: 0.5000

Epoch 00005: val_loss did not improve from 4.23178
Epoch 6/10
189/189 [==============================] - 884s 5s/step - loss: 0.0553 - acc: 0.9823 - val_loss: 5.9515 - val_acc: 0.3000

Epoch 00006: val_loss did not improve from 4.23178

Epoch 00006: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.
Epoch 7/10
189/189 [==============================] - 713s 4s/step - loss: 0.0357 - acc: 0.9892 - val_loss: 4.9521 - val_acc: 0.5000

Epoch 00007: val_loss did not improve from 4.23178
Epoch 8/10
189/189 [==============================] - 704s 4s/step - loss: 0.0307 - acc: 0.9918 - val_loss: 5.0020 - val_acc: 0.5000

Epoch 00008: val_loss did not improve from 4.23178
Epoch 9/10
189/189 [==============================] - 708s 4s/step - loss: 0.0276 - acc: 0.9927 - val_loss: 7.4874 - val_acc: 0.4000

Epoch 00009: val_loss did not improve from 4.23178
Epoch 10/10
189/189 [==============================] - 702s 4s/step - loss: 0.0302 - acc: 0.9912 - val_loss: 8.2101 - val_acc: 0.3000

Epoch 00010: val_loss did not improve from 4.23178
In [126]:
loss_plot(history)
acc_plot(history)
In [127]:
for i, layer in enumerate(iv3_base_model.layers[173:]):
    print(i, layer.name)
0 batch_normalization_146
1 batch_normalization_151
2 activation_200
3 activation_205
4 conv2d_151
5 conv2d_156
6 batch_normalization_147
7 batch_normalization_152
8 activation_201
9 activation_206
10 average_pooling2d_15
11 conv2d_149
12 conv2d_152
13 conv2d_157
14 conv2d_158
15 batch_normalization_145
16 batch_normalization_148
17 batch_normalization_153
18 batch_normalization_154
19 activation_199
20 activation_202
21 activation_207
22 activation_208
23 mixed6
24 conv2d_163
25 batch_normalization_159
26 activation_213
27 conv2d_164
28 batch_normalization_160
29 activation_214
30 conv2d_160
31 conv2d_165
32 batch_normalization_156
33 batch_normalization_161
34 activation_210
35 activation_215
36 conv2d_161
37 conv2d_166
38 batch_normalization_157
39 batch_normalization_162
40 activation_211
41 activation_216
42 average_pooling2d_16
43 conv2d_159
44 conv2d_162
45 conv2d_167
46 conv2d_168
47 batch_normalization_155
48 batch_normalization_158
49 batch_normalization_163
50 batch_normalization_164
51 activation_209
52 activation_212
53 activation_217
54 activation_218
55 mixed7
56 conv2d_171
57 batch_normalization_167
58 activation_221
59 conv2d_172
60 batch_normalization_168
61 activation_222
62 conv2d_169
63 conv2d_173
64 batch_normalization_165
65 batch_normalization_169
66 activation_219
67 activation_223
68 conv2d_170
69 conv2d_174
70 batch_normalization_166
71 batch_normalization_170
72 activation_220
73 activation_224
74 max_pooling2d_11
75 mixed8
76 conv2d_179
77 batch_normalization_175
78 activation_229
79 conv2d_176
80 conv2d_180
81 batch_normalization_172
82 batch_normalization_176
83 activation_226
84 activation_230
85 conv2d_177
86 conv2d_178
87 conv2d_181
88 conv2d_182
89 average_pooling2d_17
90 conv2d_175
91 batch_normalization_173
92 batch_normalization_174
93 batch_normalization_177
94 batch_normalization_178
95 conv2d_183
96 batch_normalization_171
97 activation_227
98 activation_228
99 activation_231
100 activation_232
101 batch_normalization_179
102 activation_225
103 mixed9_0
104 concatenate_3
105 activation_233
106 mixed9
107 conv2d_188
108 batch_normalization_184
109 activation_238
110 conv2d_185
111 conv2d_189
112 batch_normalization_181
113 batch_normalization_185
114 activation_235
115 activation_239
116 conv2d_186
117 conv2d_187
118 conv2d_190
119 conv2d_191
120 average_pooling2d_18
121 conv2d_184
122 batch_normalization_182
123 batch_normalization_183
124 batch_normalization_186
125 batch_normalization_187
126 conv2d_192
127 batch_normalization_180
128 activation_236
129 activation_237
130 activation_240
131 activation_241
132 batch_normalization_188
133 activation_234
134 mixed9_1
135 concatenate_4
136 activation_242
137 mixed10
In [128]:
# Unfreeze InceptionV3 convolutional layers 
for layer in iv3_model.layers[:173]:
    layer.trainable = False
for layer in iv3_model.layers[173:]:
    layer.trainable = True
    
iv3_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])        
In [129]:
# Train
history = iv3_model.fit_generator(data_generator.flow(x_train7, c_y_train7, batch_size=64), 
                                  steps_per_epoch = steps, epochs = epochs, 
                                  callbacks=[checkpointer, lr_reduction],
                                  validation_data = (x_valid7, c_y_valid7))
Epoch 1/10
189/189 [==============================] - 1424s 8s/step - loss: 0.1454 - acc: 0.9822 - val_loss: 4.4839 - val_acc: 0.5000

Epoch 00001: val_loss did not improve from 4.23178
Epoch 2/10
189/189 [==============================] - 1373s 7s/step - loss: 0.0105 - acc: 0.9975 - val_loss: 1.5130 - val_acc: 0.7000

Epoch 00002: val_loss improved from 4.23178 to 1.51305, saving model to weights.best.iv3_model.hdf5
Epoch 3/10
189/189 [==============================] - 1246s 7s/step - loss: 0.0087 - acc: 0.9977 - val_loss: 1.1497 - val_acc: 0.7000

Epoch 00003: val_loss improved from 1.51305 to 1.14969, saving model to weights.best.iv3_model.hdf5
Epoch 4/10
189/189 [==============================] - 1221s 6s/step - loss: 0.0088 - acc: 0.9981 - val_loss: 2.2679 - val_acc: 0.8000

Epoch 00004: val_loss did not improve from 1.14969
Epoch 5/10
189/189 [==============================] - 1228s 6s/step - loss: 0.0044 - acc: 0.9992 - val_loss: 7.6884 - val_acc: 0.3000

Epoch 00005: val_loss did not improve from 1.14969
Epoch 6/10
189/189 [==============================] - 1228s 6s/step - loss: 0.0182 - acc: 0.9969 - val_loss: 1.6773 - val_acc: 0.9000

Epoch 00006: val_loss did not improve from 1.14969
Epoch 7/10
189/189 [==============================] - 1235s 7s/step - loss: 0.0052 - acc: 0.9987 - val_loss: 2.0594 - val_acc: 0.8000

Epoch 00007: val_loss did not improve from 1.14969
Epoch 8/10
189/189 [==============================] - 1253s 7s/step - loss: 0.0191 - acc: 0.9965 - val_loss: 3.1203 - val_acc: 0.6000

Epoch 00008: val_loss did not improve from 1.14969

Epoch 00008: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.
Epoch 9/10
189/189 [==============================] - 1243s 7s/step - loss: 0.0049 - acc: 0.9991 - val_loss: 2.7110 - val_acc: 0.7000

Epoch 00009: val_loss did not improve from 1.14969
Epoch 10/10
189/189 [==============================] - 1235s 7s/step - loss: 0.0059 - acc: 0.9995 - val_loss: 3.1312 - val_acc: 0.7000

Epoch 00010: val_loss did not improve from 1.14969
In [130]:
# Evaluate 
iv3_model.load_weights('weights.best.iv3_model.hdf5')
iv3_test_scores = iv3_model.evaluate(x_test7, c_y_test7)
print("Accuracy: %.2f%%" % (iv3_test_scores[1]*100))
11/11 [==============================] - 1s 110ms/step
Accuracy: 36.36%
In [40]:
# Xception 

# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/
# xception_weights_tf_dim_ordering_tf_kernels.h5
# xception_weights_tf_dim_ordering_tf_kernels_notop.h5

xce_base_model = Xception(include_top=True, weights='imagenet')
xce_base_model.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_5 (InputLayer)             (None, 299, 299, 3)   0                                            
____________________________________________________________________________________________________
block1_conv1 (Conv2D)            (None, 149, 149, 32)  864         input_5[0][0]                    
____________________________________________________________________________________________________
block1_conv1_bn (BatchNormalizat (None, 149, 149, 32)  128         block1_conv1[0][0]               
____________________________________________________________________________________________________
block1_conv1_act (Activation)    (None, 149, 149, 32)  0           block1_conv1_bn[0][0]            
____________________________________________________________________________________________________
block1_conv2 (Conv2D)            (None, 147, 147, 64)  18432       block1_conv1_act[0][0]           
____________________________________________________________________________________________________
block1_conv2_bn (BatchNormalizat (None, 147, 147, 64)  256         block1_conv2[0][0]               
____________________________________________________________________________________________________
block1_conv2_act (Activation)    (None, 147, 147, 64)  0           block1_conv2_bn[0][0]            
____________________________________________________________________________________________________
block2_sepconv1 (SeparableConv2D (None, 147, 147, 128) 8768        block1_conv2_act[0][0]           
____________________________________________________________________________________________________
block2_sepconv1_bn (BatchNormali (None, 147, 147, 128) 512         block2_sepconv1[0][0]            
____________________________________________________________________________________________________
block2_sepconv2_act (Activation) (None, 147, 147, 128) 0           block2_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block2_sepconv2 (SeparableConv2D (None, 147, 147, 128) 17536       block2_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block2_sepconv2_bn (BatchNormali (None, 147, 147, 128) 512         block2_sepconv2[0][0]            
____________________________________________________________________________________________________
conv2d_107 (Conv2D)              (None, 74, 74, 128)   8192        block1_conv2_act[0][0]           
____________________________________________________________________________________________________
block2_pool (MaxPooling2D)       (None, 74, 74, 128)   0           block2_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
batch_normalization_107 (BatchNo (None, 74, 74, 128)   512         conv2d_107[0][0]                 
____________________________________________________________________________________________________
add_37 (Add)                     (None, 74, 74, 128)   0           block2_pool[0][0]                
                                                                   batch_normalization_107[0][0]    
____________________________________________________________________________________________________
block3_sepconv1_act (Activation) (None, 74, 74, 128)   0           add_37[0][0]                     
____________________________________________________________________________________________________
block3_sepconv1 (SeparableConv2D (None, 74, 74, 256)   33920       block3_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block3_sepconv1_bn (BatchNormali (None, 74, 74, 256)   1024        block3_sepconv1[0][0]            
____________________________________________________________________________________________________
block3_sepconv2_act (Activation) (None, 74, 74, 256)   0           block3_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block3_sepconv2 (SeparableConv2D (None, 74, 74, 256)   67840       block3_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block3_sepconv2_bn (BatchNormali (None, 74, 74, 256)   1024        block3_sepconv2[0][0]            
____________________________________________________________________________________________________
conv2d_108 (Conv2D)              (None, 37, 37, 256)   32768       add_37[0][0]                     
____________________________________________________________________________________________________
block3_pool (MaxPooling2D)       (None, 37, 37, 256)   0           block3_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
batch_normalization_108 (BatchNo (None, 37, 37, 256)   1024        conv2d_108[0][0]                 
____________________________________________________________________________________________________
add_38 (Add)                     (None, 37, 37, 256)   0           block3_pool[0][0]                
                                                                   batch_normalization_108[0][0]    
____________________________________________________________________________________________________
block4_sepconv1_act (Activation) (None, 37, 37, 256)   0           add_38[0][0]                     
____________________________________________________________________________________________________
block4_sepconv1 (SeparableConv2D (None, 37, 37, 728)   188672      block4_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block4_sepconv1_bn (BatchNormali (None, 37, 37, 728)   2912        block4_sepconv1[0][0]            
____________________________________________________________________________________________________
block4_sepconv2_act (Activation) (None, 37, 37, 728)   0           block4_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block4_sepconv2 (SeparableConv2D (None, 37, 37, 728)   536536      block4_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block4_sepconv2_bn (BatchNormali (None, 37, 37, 728)   2912        block4_sepconv2[0][0]            
____________________________________________________________________________________________________
conv2d_109 (Conv2D)              (None, 19, 19, 728)   186368      add_38[0][0]                     
____________________________________________________________________________________________________
block4_pool (MaxPooling2D)       (None, 19, 19, 728)   0           block4_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
batch_normalization_109 (BatchNo (None, 19, 19, 728)   2912        conv2d_109[0][0]                 
____________________________________________________________________________________________________
add_39 (Add)                     (None, 19, 19, 728)   0           block4_pool[0][0]                
                                                                   batch_normalization_109[0][0]    
____________________________________________________________________________________________________
block5_sepconv1_act (Activation) (None, 19, 19, 728)   0           add_39[0][0]                     
____________________________________________________________________________________________________
block5_sepconv1 (SeparableConv2D (None, 19, 19, 728)   536536      block5_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block5_sepconv1_bn (BatchNormali (None, 19, 19, 728)   2912        block5_sepconv1[0][0]            
____________________________________________________________________________________________________
block5_sepconv2_act (Activation) (None, 19, 19, 728)   0           block5_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block5_sepconv2 (SeparableConv2D (None, 19, 19, 728)   536536      block5_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block5_sepconv2_bn (BatchNormali (None, 19, 19, 728)   2912        block5_sepconv2[0][0]            
____________________________________________________________________________________________________
block5_sepconv3_act (Activation) (None, 19, 19, 728)   0           block5_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
block5_sepconv3 (SeparableConv2D (None, 19, 19, 728)   536536      block5_sepconv3_act[0][0]        
____________________________________________________________________________________________________
block5_sepconv3_bn (BatchNormali (None, 19, 19, 728)   2912        block5_sepconv3[0][0]            
____________________________________________________________________________________________________
add_40 (Add)                     (None, 19, 19, 728)   0           block5_sepconv3_bn[0][0]         
                                                                   add_39[0][0]                     
____________________________________________________________________________________________________
block6_sepconv1_act (Activation) (None, 19, 19, 728)   0           add_40[0][0]                     
____________________________________________________________________________________________________
block6_sepconv1 (SeparableConv2D (None, 19, 19, 728)   536536      block6_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block6_sepconv1_bn (BatchNormali (None, 19, 19, 728)   2912        block6_sepconv1[0][0]            
____________________________________________________________________________________________________
block6_sepconv2_act (Activation) (None, 19, 19, 728)   0           block6_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block6_sepconv2 (SeparableConv2D (None, 19, 19, 728)   536536      block6_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block6_sepconv2_bn (BatchNormali (None, 19, 19, 728)   2912        block6_sepconv2[0][0]            
____________________________________________________________________________________________________
block6_sepconv3_act (Activation) (None, 19, 19, 728)   0           block6_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
block6_sepconv3 (SeparableConv2D (None, 19, 19, 728)   536536      block6_sepconv3_act[0][0]        
____________________________________________________________________________________________________
block6_sepconv3_bn (BatchNormali (None, 19, 19, 728)   2912        block6_sepconv3[0][0]            
____________________________________________________________________________________________________
add_41 (Add)                     (None, 19, 19, 728)   0           block6_sepconv3_bn[0][0]         
                                                                   add_40[0][0]                     
____________________________________________________________________________________________________
block7_sepconv1_act (Activation) (None, 19, 19, 728)   0           add_41[0][0]                     
____________________________________________________________________________________________________
block7_sepconv1 (SeparableConv2D (None, 19, 19, 728)   536536      block7_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block7_sepconv1_bn (BatchNormali (None, 19, 19, 728)   2912        block7_sepconv1[0][0]            
____________________________________________________________________________________________________
block7_sepconv2_act (Activation) (None, 19, 19, 728)   0           block7_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block7_sepconv2 (SeparableConv2D (None, 19, 19, 728)   536536      block7_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block7_sepconv2_bn (BatchNormali (None, 19, 19, 728)   2912        block7_sepconv2[0][0]            
____________________________________________________________________________________________________
block7_sepconv3_act (Activation) (None, 19, 19, 728)   0           block7_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
block7_sepconv3 (SeparableConv2D (None, 19, 19, 728)   536536      block7_sepconv3_act[0][0]        
____________________________________________________________________________________________________
block7_sepconv3_bn (BatchNormali (None, 19, 19, 728)   2912        block7_sepconv3[0][0]            
____________________________________________________________________________________________________
add_42 (Add)                     (None, 19, 19, 728)   0           block7_sepconv3_bn[0][0]         
                                                                   add_41[0][0]                     
____________________________________________________________________________________________________
block8_sepconv1_act (Activation) (None, 19, 19, 728)   0           add_42[0][0]                     
____________________________________________________________________________________________________
block8_sepconv1 (SeparableConv2D (None, 19, 19, 728)   536536      block8_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block8_sepconv1_bn (BatchNormali (None, 19, 19, 728)   2912        block8_sepconv1[0][0]            
____________________________________________________________________________________________________
block8_sepconv2_act (Activation) (None, 19, 19, 728)   0           block8_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block8_sepconv2 (SeparableConv2D (None, 19, 19, 728)   536536      block8_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block8_sepconv2_bn (BatchNormali (None, 19, 19, 728)   2912        block8_sepconv2[0][0]            
____________________________________________________________________________________________________
block8_sepconv3_act (Activation) (None, 19, 19, 728)   0           block8_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
block8_sepconv3 (SeparableConv2D (None, 19, 19, 728)   536536      block8_sepconv3_act[0][0]        
____________________________________________________________________________________________________
block8_sepconv3_bn (BatchNormali (None, 19, 19, 728)   2912        block8_sepconv3[0][0]            
____________________________________________________________________________________________________
add_43 (Add)                     (None, 19, 19, 728)   0           block8_sepconv3_bn[0][0]         
                                                                   add_42[0][0]                     
____________________________________________________________________________________________________
block9_sepconv1_act (Activation) (None, 19, 19, 728)   0           add_43[0][0]                     
____________________________________________________________________________________________________
block9_sepconv1 (SeparableConv2D (None, 19, 19, 728)   536536      block9_sepconv1_act[0][0]        
____________________________________________________________________________________________________
block9_sepconv1_bn (BatchNormali (None, 19, 19, 728)   2912        block9_sepconv1[0][0]            
____________________________________________________________________________________________________
block9_sepconv2_act (Activation) (None, 19, 19, 728)   0           block9_sepconv1_bn[0][0]         
____________________________________________________________________________________________________
block9_sepconv2 (SeparableConv2D (None, 19, 19, 728)   536536      block9_sepconv2_act[0][0]        
____________________________________________________________________________________________________
block9_sepconv2_bn (BatchNormali (None, 19, 19, 728)   2912        block9_sepconv2[0][0]            
____________________________________________________________________________________________________
block9_sepconv3_act (Activation) (None, 19, 19, 728)   0           block9_sepconv2_bn[0][0]         
____________________________________________________________________________________________________
block9_sepconv3 (SeparableConv2D (None, 19, 19, 728)   536536      block9_sepconv3_act[0][0]        
____________________________________________________________________________________________________
block9_sepconv3_bn (BatchNormali (None, 19, 19, 728)   2912        block9_sepconv3[0][0]            
____________________________________________________________________________________________________
add_44 (Add)                     (None, 19, 19, 728)   0           block9_sepconv3_bn[0][0]         
                                                                   add_43[0][0]                     
____________________________________________________________________________________________________
block10_sepconv1_act (Activation (None, 19, 19, 728)   0           add_44[0][0]                     
____________________________________________________________________________________________________
block10_sepconv1 (SeparableConv2 (None, 19, 19, 728)   536536      block10_sepconv1_act[0][0]       
____________________________________________________________________________________________________
block10_sepconv1_bn (BatchNormal (None, 19, 19, 728)   2912        block10_sepconv1[0][0]           
____________________________________________________________________________________________________
block10_sepconv2_act (Activation (None, 19, 19, 728)   0           block10_sepconv1_bn[0][0]        
____________________________________________________________________________________________________
block10_sepconv2 (SeparableConv2 (None, 19, 19, 728)   536536      block10_sepconv2_act[0][0]       
____________________________________________________________________________________________________
block10_sepconv2_bn (BatchNormal (None, 19, 19, 728)   2912        block10_sepconv2[0][0]           
____________________________________________________________________________________________________
block10_sepconv3_act (Activation (None, 19, 19, 728)   0           block10_sepconv2_bn[0][0]        
____________________________________________________________________________________________________
block10_sepconv3 (SeparableConv2 (None, 19, 19, 728)   536536      block10_sepconv3_act[0][0]       
____________________________________________________________________________________________________
block10_sepconv3_bn (BatchNormal (None, 19, 19, 728)   2912        block10_sepconv3[0][0]           
____________________________________________________________________________________________________
add_45 (Add)                     (None, 19, 19, 728)   0           block10_sepconv3_bn[0][0]        
                                                                   add_44[0][0]                     
____________________________________________________________________________________________________
block11_sepconv1_act (Activation (None, 19, 19, 728)   0           add_45[0][0]                     
____________________________________________________________________________________________________
block11_sepconv1 (SeparableConv2 (None, 19, 19, 728)   536536      block11_sepconv1_act[0][0]       
____________________________________________________________________________________________________
block11_sepconv1_bn (BatchNormal (None, 19, 19, 728)   2912        block11_sepconv1[0][0]           
____________________________________________________________________________________________________
block11_sepconv2_act (Activation (None, 19, 19, 728)   0           block11_sepconv1_bn[0][0]        
____________________________________________________________________________________________________
block11_sepconv2 (SeparableConv2 (None, 19, 19, 728)   536536      block11_sepconv2_act[0][0]       
____________________________________________________________________________________________________
block11_sepconv2_bn (BatchNormal (None, 19, 19, 728)   2912        block11_sepconv2[0][0]           
____________________________________________________________________________________________________
block11_sepconv3_act (Activation (None, 19, 19, 728)   0           block11_sepconv2_bn[0][0]        
____________________________________________________________________________________________________
block11_sepconv3 (SeparableConv2 (None, 19, 19, 728)   536536      block11_sepconv3_act[0][0]       
____________________________________________________________________________________________________
block11_sepconv3_bn (BatchNormal (None, 19, 19, 728)   2912        block11_sepconv3[0][0]           
____________________________________________________________________________________________________
add_46 (Add)                     (None, 19, 19, 728)   0           block11_sepconv3_bn[0][0]        
                                                                   add_45[0][0]                     
____________________________________________________________________________________________________
block12_sepconv1_act (Activation (None, 19, 19, 728)   0           add_46[0][0]                     
____________________________________________________________________________________________________
block12_sepconv1 (SeparableConv2 (None, 19, 19, 728)   536536      block12_sepconv1_act[0][0]       
____________________________________________________________________________________________________
block12_sepconv1_bn (BatchNormal (None, 19, 19, 728)   2912        block12_sepconv1[0][0]           
____________________________________________________________________________________________________
block12_sepconv2_act (Activation (None, 19, 19, 728)   0           block12_sepconv1_bn[0][0]        
____________________________________________________________________________________________________
block12_sepconv2 (SeparableConv2 (None, 19, 19, 728)   536536      block12_sepconv2_act[0][0]       
____________________________________________________________________________________________________
block12_sepconv2_bn (BatchNormal (None, 19, 19, 728)   2912        block12_sepconv2[0][0]           
____________________________________________________________________________________________________
block12_sepconv3_act (Activation (None, 19, 19, 728)   0           block12_sepconv2_bn[0][0]        
____________________________________________________________________________________________________
block12_sepconv3 (SeparableConv2 (None, 19, 19, 728)   536536      block12_sepconv3_act[0][0]       
____________________________________________________________________________________________________
block12_sepconv3_bn (BatchNormal (None, 19, 19, 728)   2912        block12_sepconv3[0][0]           
____________________________________________________________________________________________________
add_47 (Add)                     (None, 19, 19, 728)   0           block12_sepconv3_bn[0][0]        
                                                                   add_46[0][0]                     
____________________________________________________________________________________________________
block13_sepconv1_act (Activation (None, 19, 19, 728)   0           add_47[0][0]                     
____________________________________________________________________________________________________
block13_sepconv1 (SeparableConv2 (None, 19, 19, 728)   536536      block13_sepconv1_act[0][0]       
____________________________________________________________________________________________________
block13_sepconv1_bn (BatchNormal (None, 19, 19, 728)   2912        block13_sepconv1[0][0]           
____________________________________________________________________________________________________
block13_sepconv2_act (Activation (None, 19, 19, 728)   0           block13_sepconv1_bn[0][0]        
____________________________________________________________________________________________________
block13_sepconv2 (SeparableConv2 (None, 19, 19, 1024)  752024      block13_sepconv2_act[0][0]       
____________________________________________________________________________________________________
block13_sepconv2_bn (BatchNormal (None, 19, 19, 1024)  4096        block13_sepconv2[0][0]           
____________________________________________________________________________________________________
conv2d_110 (Conv2D)              (None, 10, 10, 1024)  745472      add_47[0][0]                     
____________________________________________________________________________________________________
block13_pool (MaxPooling2D)      (None, 10, 10, 1024)  0           block13_sepconv2_bn[0][0]        
____________________________________________________________________________________________________
batch_normalization_110 (BatchNo (None, 10, 10, 1024)  4096        conv2d_110[0][0]                 
____________________________________________________________________________________________________
add_48 (Add)                     (None, 10, 10, 1024)  0           block13_pool[0][0]               
                                                                   batch_normalization_110[0][0]    
____________________________________________________________________________________________________
block14_sepconv1 (SeparableConv2 (None, 10, 10, 1536)  1582080     add_48[0][0]                     
____________________________________________________________________________________________________
block14_sepconv1_bn (BatchNormal (None, 10, 10, 1536)  6144        block14_sepconv1[0][0]           
____________________________________________________________________________________________________
block14_sepconv1_act (Activation (None, 10, 10, 1536)  0           block14_sepconv1_bn[0][0]        
____________________________________________________________________________________________________
block14_sepconv2 (SeparableConv2 (None, 10, 10, 2048)  3159552     block14_sepconv1_act[0][0]       
____________________________________________________________________________________________________
block14_sepconv2_bn (BatchNormal (None, 10, 10, 2048)  8192        block14_sepconv2[0][0]           
____________________________________________________________________________________________________
block14_sepconv2_act (Activation (None, 10, 10, 2048)  0           block14_sepconv2_bn[0][0]        
____________________________________________________________________________________________________
avg_pool (GlobalAveragePooling2D (None, 2048)          0           block14_sepconv2_act[0][0]       
____________________________________________________________________________________________________
predictions (Dense)              (None, 1000)          2049000     avg_pool[0][0]                   
====================================================================================================
Total params: 22,910,480
Trainable params: 22,855,952
Non-trainable params: 54,528
____________________________________________________________________________________________________