Assignment 2.3.1 Transfer Learning
Assignment 2.3.1 Transfer Learning
1 Transfer Learning
https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/Exercises/Exercise
%207%20-%20Transfer%20Learning/Exercise%207%20-%20Question.ipynb
# ATTENTION: Please do not alter any of the provided code in the exercise. Only add your own code where
indicated
# ATTENTION: Please do not add or remove any cells in the exercise. The grader will check specific cells
based on the cell position.
# ATTENTION: Please use the provided epoch values when training.
path_inception = f"{getcwd()}/../tmp2/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"
# Create an instance of the inception model from the local pre-trained weights
local_weights_file = path_inception
pre_trained_model.load_weights(local_weights_file)
# Expected Output:
# ('last layer output shape: ', (None, 7, 7, 768))
last layer output shape: (None, 7, 7, 768)
In [16]:
# Define a Callback class that stops training once accuracy reaches 97.0%
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('acc')>0.97):
print("\nReached 97.0% accuracy so cancelling training!")
self.model.stop_training = True
In [17]:
model.compile(optimizer = RMSprop(lr=0.0001),
loss = 'binary_crossentropy', # Your Code Here,
metrics = ['acc']) # Your Code Here
model.summary()
import os
import zipfile
import shutil
shutil.rmtree('/tmp')
local_zip = path_horse_or_human
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/training')
zip_ref.close()
local_zip = path_validation_horse_or_human
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/validation')
zip_ref.close()
In [19]:
# Expected Output:
# 500
# 527
# 128
# 128
# Expected Output:
# Found 1027 images belonging to 2 classes.
# Found 256 images belonging to 2 classes.
# Run this and see how many epochs it should take before the callback
# fires, and stops training at 97% accuracy
%matplotlib inline
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.show()
Submission Instructions
In [ ]:
%%javascript
<!-- Save the notebook -->
IPython.notebook.save_checkpoint();
In [ ]:
%%javascript
IPython.notebook.session.delete();
window.onbeforeunload = null
setTimeout(function() { window.close(); }, 1000);