Created
May 28, 2019 15:43
-
-
Save netsatsawat/e77f98f4a90ed41c22ada99ee9e5fca9 to your computer and use it in GitHub Desktop.
Part 2 of the script to build the CNN architecture and callbacks function
This file contains hidden or 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
np.random.seed(SEED) | |
tf.random.set_random_seed(SEED) | |
model = Sequential() | |
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(IMG_ROWS, IMG_COLS, 1))) | |
model.add(Conv2D(32, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Conv2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(rate=0.25)) | |
model.add(Dense(NUM_CLASS, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', | |
optimizer='adam', | |
metrics=['accuracy']) | |
class auc_loss_history(Callback): | |
def __init__(self, training_data, validation_data): | |
self.x = training_data[0] | |
self.y = training_data[1] | |
self.x_val = validation_data[0] | |
self.y_val = validation_data[1] | |
self.auc = None | |
self.auc_val = None | |
self.losses = None | |
self.val_losses = None | |
def on_train_begin(self, logs={}): | |
self.auc = [] | |
self.auc_val = [] | |
self.losses = [] | |
self.val_losses = [] | |
def _auc_score(self, predictions, targets): | |
""" | |
Function to compute Compute AUC ROC Score | |
""" | |
return roc_auc_score(predictions, targets) | |
def on_train_end(self, logs={}): | |
return | |
def on_epoch_begin(self, epoch, logs={}): | |
return | |
def on_epoch_end(self, epoch, logs={}): | |
self.losses.append(logs.get('loss')) | |
self.val_losses.append(logs.get('val_loss')) | |
auc = self._auc_score(self.y, self.model.predict(self.x)) | |
auc_val = self._auc_score(self.y_val, self.model.predict(self.x_val)) | |
self.auc.append(auc) | |
self.auc_val.append(auc_val) | |
print('\rroc-auc: %s - roc-auc_val: %s' % \ | |
(str(round(auc, 4)),str(round(auc_val, 4))),end=100*' '+'\n') | |
return | |
def on_batch_begin(self, batch, logs={}): | |
return | |
def on_batch_end(self, batch, logs={}): | |
return | |
# define the callbacks | |
auc_loss_hist = auc_loss_history(training_data=(X_train, y_train), | |
validation_data=(X_val, y_val)) | |
checkpointer = ModelCheckpoint(filepath='../model/kmnist_weights.hdf5', verbose=1, | |
monitor='val_loss',save_best_only=True, mode='min') | |
early_stop = EarlyStopping(monitor='val_loss', patience=10, mode='min') | |
my_callbacks = [auc_loss_hist, checkpointer, early_stop] | |
# fit the model | |
hist = model.fit(X_train, y_train, | |
batch_size=128, | |
epochs=100, | |
verbose=2, | |
validation_data=(X_val, y_val), | |
callbacks=my_callbacks | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment