<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Copyright 2019 The TensorFlow Authors.
Step1:  <table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https
Step2:  Some normal imports we've seen before.
Step3:  Part 1
Step4:  The images in the Dogs vs. Cats dataset are not all the same size. So, we need to reformat all images to the resolution expected by MobileNet (224, 224)
Step5:  Part 2
Step6:  Freeze the variables in the feature extractor layer, so that the training only modifies the final classifier layer.
Step7:  Attach a classification head
Now wrap the hub layer in a tf.keras.Sequential model, and add a new classification layer.
Step8:  Train the model
We now train this model like any other, by first calling compile followed by fit.
Step9:  Check the predictions
Get the ordered list of class names.
Step10:  Run an image batch through the model and convert the indices to class names.
Step11:  Let's look at the true labels and predicted ones.
Step12:  Part 3
Step13:  You can later recreate the same model from this file, even if you no longer have access to the code that created the model.
This file includes
Step14:  We can check that the reloaded model and the previous model give the same result
Step15:  The difference in output should be zero
Step16:  As we can see, the reult is 0.0, which indicates that both models made the same predictions on the same batch of images.
Keep Training
Besides making predictions, we can also take our reloaded model and keep training it. To do this, you can just train the reloaded as usual, using the .fit method.
Step17:  Part 5
Step18:  Part 6
Step19:  Now, let's use the reloaded_sm (reloaded SavedModel) to make predictions on a batch of images.
Step20:  We can check that the reloaded SavedModel and the previous model give the same result.
Step21:  As we can see, the result is 0.0, which indicates that both models made the same predictions on the same batch of images.
Part 7
Step22:  Now, let's use the reloaded_sm)keras (reloaded Keras model from our SavedModel) to make predictions on a batch of images.
Step23:  We can check that the reloaded Keras model and the previous model give the same result.
Step24:  Part 8
Step25:  The zip file is saved in the current working directory. You can see what the current working directory is by running
Step26:  Once the file is zipped, you can download  it to your local disk.
<ASSISTANT_TASK:>
Python Code:
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Explanation: Copyright 2019 The TensorFlow Authors.
End of explanation
!pip install -U tensorflow_hub
!pip install -U tensorflow_datasets
Explanation: <table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l07c01_saving_and_loading_models.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
  </td>
  <td>
    <a target="_blank" href="https://github.com/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l07c01_saving_and_loading_models.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View source on GitHub</a>
  </td>
</table>
Saving and Loading Models
In this tutorial we will learn how we can take a trained model, save it, and then load it back to keep training it or use it to perform inference. In particular, we will use transfer learning to train a classifier to classify images of cats and dogs, just like we did in the previous lesson. We will then take our trained model and save it as an HDF5 file, which is the format used by Keras. We will then load this model, use it to perform predictions, and then continue to train the model. Finally, we will save our trained model as a TensorFlow SavedModel and then we will download it to a local disk, so that it can later be used for deployment in different platforms.
Concepts that will be covered in this Colab
Saving models in HDF5 format for Keras
Saving models in the TensorFlow SavedModel format
Loading models
Download models to Local Disk
Before starting this Colab, you should reset the Colab environment by selecting Runtime -&gt; Reset all runtimes... from menu above.
Imports
In this Colab we will use the TensorFlow 2.0 Beta version.
End of explanation
import time
import numpy as np
import matplotlib.pylab as plt
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
tfds.disable_progress_bar()
from tensorflow.keras import layers
Explanation: Some normal imports we've seen before.
End of explanation
(train_examples, validation_examples), info = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:]'],
    with_info=True,
    as_supervised=True,
)
Explanation: Part 1: Load the Cats vs. Dogs Dataset
We will use TensorFlow Datasets to load the Dogs vs Cats dataset.
End of explanation
def format_image(image, label):
  # `hub` image modules exepct their data normalized to the [0,1] range.
  image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))/255.0
  return  image, label
num_examples = info.splits['train'].num_examples
BATCH_SIZE = 32
IMAGE_RES = 224
train_batches      = train_examples.cache().shuffle(num_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
validation_batches = validation_examples.cache().map(format_image).batch(BATCH_SIZE).prefetch(1)
Explanation: The images in the Dogs vs. Cats dataset are not all the same size. So, we need to reformat all images to the resolution expected by MobileNet (224, 224)
End of explanation
URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor = hub.KerasLayer(URL,
                                   input_shape=(IMAGE_RES, IMAGE_RES,3))
Explanation: Part 2: Transfer Learning with TensorFlow Hub
We will now use TensorFlow Hub to do Transfer Learning.
End of explanation
feature_extractor.trainable = False
Explanation: Freeze the variables in the feature extractor layer, so that the training only modifies the final classifier layer.
End of explanation
model = tf.keras.Sequential([
  feature_extractor,
  layers.Dense(2)
])
model.summary()
Explanation: Attach a classification head
Now wrap the hub layer in a tf.keras.Sequential model, and add a new classification layer.
End of explanation
model.compile(
  optimizer='adam', 
  loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
  metrics=['accuracy'])
EPOCHS = 3
history = model.fit(train_batches,
                    epochs=EPOCHS,
                    validation_data=validation_batches)
Explanation: Train the model
We now train this model like any other, by first calling compile followed by fit.
End of explanation
class_names = np.array(info.features['label'].names)
class_names
Explanation: Check the predictions
Get the ordered list of class names.
End of explanation
image_batch, label_batch = next(iter(train_batches.take(1)))
image_batch = image_batch.numpy()
label_batch = label_batch.numpy()
predicted_batch = model.predict(image_batch)
predicted_batch = tf.squeeze(predicted_batch).numpy()
predicted_ids = np.argmax(predicted_batch, axis=-1)
predicted_class_names = class_names[predicted_ids]
predicted_class_names
Explanation: Run an image batch through the model and convert the indices to class names.
End of explanation
print("Labels: ", label_batch)
print("Predicted labels: ", predicted_ids)
plt.figure(figsize=(10,9))
for n in range(30):
  plt.subplot(6,5,n+1)
  plt.imshow(image_batch[n])
  color = "blue" if predicted_ids[n] == label_batch[n] else "red"
  plt.title(predicted_class_names[n].title(), color=color)
  plt.axis('off')
_ = plt.suptitle("Model predictions (blue: correct, red: incorrect)")
Explanation: Let's look at the true labels and predicted ones.
End of explanation
t = time.time()
export_path_keras = "./{}.h5".format(int(t))
print(export_path_keras)
model.save(export_path_keras)
!ls
Explanation: Part 3: Save as Keras .h5 model
Now that we've trained the model,  we can save it as an HDF5 file, which is the format used by Keras. Our HDF5 file will have the extension '.h5', and it's name will correpond to the current time stamp.
End of explanation
reloaded = tf.keras.models.load_model(
  export_path_keras, 
  # `custom_objects` tells keras how to load a `hub.KerasLayer`
  custom_objects={'KerasLayer': hub.KerasLayer})
reloaded.summary()
Explanation: You can later recreate the same model from this file, even if you no longer have access to the code that created the model.
This file includes:
The model's architecture
The model's weight values (which were learned during training)
The model's training config (what you passed to compile), if any
The optimizer and its state, if any (this enables you to restart training where you left off)
Part 4:  Load the Keras .h5 Model
We will now load the model we just saved into a new model called reloaded. We will need to provide the file path and the custom_objects parameter. This parameter tells keras how to load the hub.KerasLayer from the feature_extractor we used for transfer learning.
End of explanation
result_batch = model.predict(image_batch)
reloaded_result_batch = reloaded.predict(image_batch)
Explanation: We can check that the reloaded model and the previous model give the same result
End of explanation
(abs(result_batch - reloaded_result_batch)).max()
Explanation: The difference in output should be zero:
End of explanation
EPOCHS = 3
history = reloaded.fit(train_batches,
                    epochs=EPOCHS,
                    validation_data=validation_batches)
Explanation: As we can see, the reult is 0.0, which indicates that both models made the same predictions on the same batch of images.
Keep Training
Besides making predictions, we can also take our reloaded model and keep training it. To do this, you can just train the reloaded as usual, using the .fit method.
End of explanation
t = time.time()
export_path_sm = "./{}".format(int(t))
print(export_path_sm)
tf.saved_model.save(model, export_path_sm)
!ls {export_path_sm}
Explanation: Part 5: Export as SavedModel
You can also export a whole model to the TensorFlow SavedModel format. SavedModel is a standalone serialization format for Tensorflow objects, supported by TensorFlow serving as well as TensorFlow implementations other than Python. A SavedModel contains a complete TensorFlow program, including weights and computation. It does not require the original model building code to run, which makes it useful for sharing or deploying (with TFLite, TensorFlow.js, TensorFlow Serving, or TFHub).
The SavedModel files that were created contain:
A TensorFlow checkpoint containing the model weights.
A SavedModel proto containing the underlying Tensorflow graph. Separate graphs are saved for prediction (serving), train, and evaluation. If the model wasn't compiled before, then only the inference graph gets exported.
The model's architecture config, if available.
Let's save our original model as a TensorFlow SavedModel. To do this we will use the tf.saved_model.save() function. This functions takes in the model we want to save and the path to the folder where we want to save our model. 
This function will create a folder where you will find an assets folder, a variables folder, and the saved_model.pb file.
End of explanation
reloaded_sm = tf.saved_model.load(export_path_sm)
Explanation: Part 6: Load SavedModel
Now, let's load our SavedModel and use it to make predictions. We use the tf.saved_model.load() function to load our SavedModels. The object returned by tf.saved_model.load is 100% independent of the code that created it.
End of explanation
reload_sm_result_batch = reloaded_sm(image_batch, training=False).numpy()
Explanation: Now, let's use the reloaded_sm (reloaded SavedModel) to make predictions on a batch of images.
End of explanation
(abs(result_batch - reload_sm_result_batch)).max()
Explanation: We can check that the reloaded SavedModel and the previous model give the same result.
End of explanation
t = time.time()
export_path_sm = "./{}".format(int(t))
print(export_path_sm)
tf.saved_model.save(model, export_path_sm)
reload_sm_keras = tf.keras.models.load_model(
  export_path_sm,
  custom_objects={'KerasLayer': hub.KerasLayer})
reload_sm_keras.summary()
Explanation: As we can see, the result is 0.0, which indicates that both models made the same predictions on the same batch of images.
Part 7: Loading the SavedModel as a Keras Model
The object returned by tf.saved_model.load is not a Keras object (i.e. doesn't have .fit, .predict, .summary, etc. methods). Therefore, you can't simply take your reloaded_sm model and keep training it by running .fit. To be able to get back a full keras model from the Tensorflow SavedModel format we must use the tf.keras.models.load_model function. This function will work the same as before, except now we pass the path to the folder containing our SavedModel.
End of explanation
result_batch = model.predict(image_batch)
reload_sm_keras_result_batch = reload_sm_keras.predict(image_batch)
Explanation: Now, let's use the reloaded_sm)keras (reloaded Keras model from our SavedModel) to make predictions on a batch of images.
End of explanation
(abs(result_batch - reload_sm_keras_result_batch)).max()
Explanation: We can check that the reloaded Keras model and the previous model give the same result.
End of explanation
!zip -r model.zip {export_path_sm}
Explanation: Part 8:  Download your model
You can download the SavedModel to your local disk by creating a zip file. We wil use the -r (recursice) option to zip all subfolders.
End of explanation
!ls
Explanation: The zip file is saved in the current working directory. You can see what the current working directory is by running:
End of explanation
try:
  from google.colab import files
  files.download('./model.zip')
except ImportError:
  pass
Explanation: Once the file is zipped, you can download  it to your local disk.
End of explanation
<END_TASK>