Callback
Callbacks are used to check whether the model is improving with each epoch or not. If not then what are the necessary steps to be taken like ReduceLROnPlateau decreasing the learning rate further? Even then if model performance is not improving then training will be stopped by EarlyStopping. We can also define some custom callbacks to stop training in between if the desired results have been obtained early.
Python3
from keras.callbacks import EarlyStopping, ReduceLROnPlateau class myCallback(tf.keras.callbacks.Callback): def on_epoch_end( self , epoch, logs = {}): if logs.get( 'val_auc' ) > 0.99 : print ('\n Validation accuracy has reached upto 90 % \ so, stopping further training.') self .model.stop_training = True es = EarlyStopping(patience = 3 , monitor = 'val_auc' , restore_best_weights = True ) lr = ReduceLROnPlateau(monitor = 'val_loss' , patience = 2 , factor = 0.5 , verbose = 1 ) |
Now we will train our model:
Python3
history = model.fit(train_ds, validation_data = val_ds, epochs = 50 , verbose = 1 , callbacks = [es, lr, myCallback()]) |
Output:
Let’s visualize the training and validation accuracy with each epoch.
Python3
history_df = pd.DataFrame(history.history) history_df.loc[:, [ 'loss' , 'val_loss' ]].plot() history_df.loc[:, [ 'auc' , 'val_auc' ]].plot() plt.show() |
Output:
From the above graphs, we can observe that the model has overfitted the training data as the difference between the training and validation AUC score is quite observable.
Dog Breed Classification using Transfer Learning
In this article, we will learn how to build a classifier using the Transfer Learning technique which can classify among different breeds of dogs. This project has been developed using collab and the dataset has been taken from Kaggle whose link has been provided as well.
Contact Us