train_generator = image_datagen.flow_from_directory(
'C:/output/train/',
class_mode="categorical",
seed=seed,
batch_size=batch_size,
target_size=(input_size, input_size),
color_mode='grayscale',
shuffle=True)
valid_generator = image_datagen.flow_from_directory(
'C:/output/valid/',
class_mode="categorical",
seed=seed,
batch_size=batch_size,
target_size=(input_size, input_size),
color_mode='grayscale',
shuffle=True)
# https://github.com/keras-team/keras/blob/master/keras/callbacks.py
class MyCheckPoint(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
loss = logs["loss"]
val_loss = logs["val_loss"]
fileName = "model.%02d_%0.5f_%0.5f.h5" % (epoch, loss, val_loss)
self.model.save(fileName)
#weight_saver = MyCheckPoint()
model = models.getVGGModel(num_classes)
#model = models. getStandardModel(input_size)
model.compile(optimizer=Adam(lr=1e-5, decay=1e-8), loss=keras.losses.categorical_crossentropy)
#model.load_weights("weights.26-1.48.h5")
weight_saver = ModelCheckpoint('weights.{epoch:02d}-{val_loss:.2f}.h5',save_best_only=True, save_weights_only=True)
hist = model.fit_generator(train_generator, validation_data=valid_generator, validation_steps=80, steps_per_epoch=400, epochs=200, callbacks=[weight_saver])
def getVGGModel(num_classes):
model = Sequential()
model.add(Dense(32, input_shape=(1, 224, 224)))
# Reshape((784,), input_shape=(1, 224, 224))
model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',input_shape=(1, 224, 224), data_format="channels_first"))
model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',data_format = 'channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),data_format = 'channels_first'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same', data_format = 'channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))
model.add(Flatten())
model.add(Dense(4096, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(4096, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation="softmax"))
return model