Saturday, March 17, 2018

How to save whole model of Keras (with learned weightts)

At first, we need to install Keras. To install Keras, see these articles: Use Keras on WindowsInstall Keras on LinuxInstall Keras and test deep learning.
And to make it work, you need to create a "animal" folder.


And "train" and "validate" folders in the "animal" folder.

In the "train" amd "validate" folders, create folders of categories of animal: "cat" and "dog".
In the validate folder

In the train folder

In the "cat" and "dog" folder, the photos of cat and dog.


You need to use different photos for photos of train and of validate.

Save these lines of code as "animal.py" and run it as "python3.5 aninal.py".  This code will save the whole model with learned weights. (The documentation of Keras about model.save()).
from __future__ import print_function

import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from keras.models import model_from_json
from keras.layers import Dense, Dropout, Activation, AlphaDropout
from keras.preprocessing.text import Tokenizer
import keras.backend.tensorflow_backend as KTF
import tensorflow as tf
import os.path


f_log = './log'
f_model = './'
weights_filename = 'animal_model.hdf5'
model_filename = weights_filename

batch_size = 32
epochs = 5
nb_validation_samples = 14000

old_session = KTF.get_session()
print('Building model...')
session = tf.Session('')
KTF.set_session(session)

if os.path.isfile(os.path.join(f_model,weights_filename)):
    print('\n\n\n\nSaved parameters found. I will use this file...')
    print(os.path.join(f_model,model_filename + '\n\n\n\n'))
    model_hdf5 = os.path.join(f_model, model_filename)
    model = keras.models.load_model(model_hdf5)
    model.summary()
else:
    print('\n\n\n\n' + os.path.join(f_model,weights_filename))
    print('Saved parameters Not found. Creating new model...\n\n\n\n')
    model = Sequential()
    model.add(Conv2D(32, 3, 3, input_shape=(200, 200, 3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(64, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(2))
    model.add(Activation('softmax'))

    model.summary()

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

train_datagen = ImageDataGenerator(
    rescale=1.0 / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1.0 / 255)

train_generator = train_datagen.flow_from_directory(
    'animals/train',
    target_size=(200, 200),
    batch_size=batch_size,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    'animals/validation',
    target_size=(200, 200),
    batch_size=batch_size,
    class_mode='categorical')

tb_cb = keras.callbacks.TensorBoard(log_dir=f_log, histogram_freq=0)
cp_cb = keras.callbacks.ModelCheckpoint(filepath = os.path.join(f_model,weights_filename), monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
cbks = [tb_cb, cp_cb]

history = model.fit_generator(
    train_generator,
    steps_per_epoch=np.ceil(nb_validation_samples/batch_size),
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=np.ceil(nb_validation_samples/batch_size),
    )

score = model.evaluate_generator(validation_generator, nb_validation_samples/batch_size)

print('')
print('Test score:', score[0])
print('Test accuracy:', score[1])
print('save weights')
model.save(os.path.join(f_model,weights_filename))
KTF.set_session(old_session)

The model file will be generated in the current directory