Last active
May 9, 2017 18:52
-
-
Save apacha/f52935d1225ccbb21d66fd5f4011d387 to your computer and use it in GitHub Desktop.
Load model failes in Keras for this model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
from time import time | |
import keras | |
import numpy as np | |
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras.layers import Activation, BatchNormalization, Convolution2D, Dense, Dropout, Flatten, MaxPooling2D | |
from keras.models import Sequential | |
from keras.optimizers import SGD | |
from keras.regularizers import l2 | |
best_model_path = "test.h5" | |
def add_convolution(classifier, filters, kernel_size, 0.0001, strides=(1, 1), input_shape=None): | |
if input_shape is None: | |
classifier.add(Convolution2D(filters, kernel_size, strides=strides, padding='same', | |
kernel_regularizer=l2(0.0001))) | |
else: | |
classifier.add( | |
Convolution2D(filters, kernel_size, padding='same', kernel_regularizer=l2(0.0001), | |
input_shape=input_shape)) | |
classifier.add(BatchNormalization()) | |
classifier.add(Activation('relu')) | |
classifier = Sequential() | |
add_convolution(classifier, 16, 3, 0.0001, input_shape=data_shape) | |
add_convolution(classifier, 16, 3, 0.0001) | |
classifier.add(MaxPooling2D()) | |
add_convolution(classifier, 32, 3, 0.0001) | |
add_convolution(classifier, 32, 3, 0.0001) | |
classifier.add(MaxPooling2D()) | |
add_convolution(classifier, 64, 3, 0.0001) | |
add_convolution(classifier, 64, 3, 0.0001) | |
add_convolution(classifier, 64, 3, 0.0001) | |
classifier.add(MaxPooling2D()) | |
add_convolution(classifier, 128, 3, 0.0001) | |
add_convolution(classifier, 128, 3, 0.0001) | |
add_convolution(classifier, 128, 3, 0.0001) | |
classifier.add(MaxPooling2D()) | |
add_convolution(classifier, 192, 3, 0.0001) | |
add_convolution(classifier, 192, 3, 0.0001) | |
add_convolution(classifier, 192, 3, 0.0001) | |
add_convolution(classifier, 192, 3, 0.0001) | |
classifier.add(MaxPooling2D()) | |
classifier.add(Flatten()) # Flatten | |
classifier.add(Dropout(0.5)) | |
classifier.add(Dense(units=2, kernel_regularizer=l2(0.0001))) | |
classifier.add(Activation('softmax', name="output_node")) | |
stochastic_gradient_descent = SGD(lr=0.0001, momentum=0.9, nesterov=True) | |
classifier.compile(stochastic_gradient_descent, loss="categorical_crossentropy", metrics=["accuracy"]) | |
model_checkpoint = ModelCheckpoint(best_model_path, monitor="val_acc", save_best_only=True, verbose=1) | |
history = model.fit_generator( | |
generator=training_data_generator, | |
steps_per_epoch=training_steps_per_epoch, | |
epochs=training_configuration.number_of_epochs, | |
callbacks=[model_checkpoint], | |
validation_data=validation_data_generator, | |
validation_steps=validation_steps_per_epoch | |
) | |
# For some models, loading the model directly does not work, but loading the weights does | |
# (see https://github.com/fchollet/keras/issues/4044#issuecomment-254921595) | |
best_model = keras.models.load_model(best_model_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment