# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] colab_type="text" id="W_tvPdyfA-BL"
# ##### Copyright 2019 The TensorFlow Authors.

# + cellView="form" colab={} colab_type="code" id="0O_LFhwSBCjm"
#@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.

# + [markdown] colab_type="text" id="9-3Pry4jh1-E"
# <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/l06c01_tensorflow_hub_and_transfer_learning.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
#   </td>
#   <td>
#     <a target="_blank" href="https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l06c01_tensorflow_hub_and_transfer_learning.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View source on GitHub</a>
#   </td>
# </table>

# + [markdown] colab_type="text" id="NxjpzKTvg_dd"
# # TensorFlow Hub and Transfer Learning

# + [markdown] colab_type="text" id="crU-iluJIEzw"
# [TensorFlow Hub](http://tensorflow.org/hub) is an online repository of already trained TensorFlow models that you can use.
# These models can either be used as is, or they can be used for Transfer Learning.
#
# Transfer learning is a process where you take an existing trained model, and extend it to do additional work. This involves leaving the bulk of the model unchanged, while adding and retraining the final layers, in order to get a different set of possible outputs.
#
# In this Colab we will do both.
#
# Here, you can see all the models available in [TensorFlow Module Hub](https://tfhub.dev/).
#
# ## Concepts that will be covered in this Colab
#
# 1. Use a TensorFlow Hub model for prediction.
# 2. Use a TensorFlow Hub model for Dogs vs. Cats dataset.
# 3. Do simple transfer learning with TensorFlow Hub.
#
# Before starting this Colab, you should reset the Colab environment by selecting `Runtime -> Reset all runtimes...` from menu above.

# + [markdown] colab_type="text" id="7RVsYZLEpEWs"
# # Imports
#
#

# + [markdown] colab_type="text" id="fL7DqCwbmfwi"
# This Colab will require us to use some things which are not yet in official releases of TensorFlow. So below, we're first installing a nightly version of TensorFlow as well as TensorFlow Hub.
#
# This will switch your installation of TensorFlow in Colab to this TensorFlow version. Once you are finished with this Colab, you should  switch batch to the latest stable release of TensorFlow by doing selecting `Runtime -> Reset all runtimes...` in the menus above. This will reset the Colab environment to its original state.

# + colab={} colab_type="code" id="e3BXzUGabcI9"
# !pip install tf-nightly-gpu
# !pip install "tensorflow_hub==0.4.0"
# !pip install -U tensorflow_datasets

# + [markdown] colab_type="text" id="ZUCEcRdhnyWn"
# Some normal imports we've seen before. The new one is importing tensorflow_hub which was installed above, and which this Colab will make heavy use of.

# + colab={} colab_type="code" id="OGNpmn43C0O6"
from __future__ import absolute_import, division, print_function, unicode_literals

import matplotlib.pylab as plt

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
tf.enable_eager_execution()

import tensorflow_hub as hub
import tensorflow_datasets as tfds

from tensorflow.keras import layers

# + [markdown] colab_type="text" id="s4YuF5HvpM1W"
# # Part 1: Use a TensorFlow Hub MobileNet for prediction

# + [markdown] colab_type="text" id="4Sh2sPc10V0b"
# In this part of the Colab, we'll take a trained model, load it into to Keras, and try it out.
#
# The model that we'll use is MobileNet v2 (but any model from [tf2 compatible image classifier url from tfhub.dev](https://tfhub.dev/s?q=tf2&module-type=image-classification) would work).

# + [markdown] colab_type="text" id="xEY_Ow5loN6q"
# ## Download the classifier
#
# Download the MobileNet model and create a Keras model from it.
# MobileNet is expecting images of 224 $\times$ 224 pixels, in 3 color channels (RGB).

# + colab={} colab_type="code" id="y_6bGjoPtzau"
CLASSIFIER_URL ="https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"
IMAGE_RES = 224

model = tf.keras.Sequential([
    hub.KerasLayer(CLASSIFIER_URL, input_shape=(IMAGE_RES, IMAGE_RES, 3))
])

# + [markdown] colab_type="text" id="pwZXaoV0uXp2"
# ## Run it on a single image

# + [markdown] colab_type="text" id="TQItP1i55-di"
# MobileNet has been trained on the ImageNet dataset. ImageNet has 1000 different output classes, and one of them is military uniforms.
# Let's get an image containing a military uniform that is not part of ImageNet, and see if our model can predict that it is a military uniform.

# + colab={} colab_type="code" id="w5wDjXNjuXGD"
import numpy as np
import PIL.Image as Image

grace_hopper = tf.keras.utils.get_file('image.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg')
grace_hopper = Image.open(grace_hopper).resize((IMAGE_RES, IMAGE_RES))
grace_hopper 

# + colab={} colab_type="code" id="BEmmBnGbLxPp"
grace_hopper = np.array(grace_hopper)/255.0
grace_hopper.shape

# + [markdown] colab_type="text" id="0Ic8OEEo2b73"
# Remember, models always want a batch of images to process. So here, we add a batch dimension, and pass the image to the model for prediction.

# + colab={} colab_type="code" id="EMquyn29v8q3"
result = model.predict(grace_hopper[np.newaxis, ...])
result.shape

# + [markdown] colab_type="text" id="NKzjqENF6jDF"
# The result is a 1001 element vector of logits, rating the probability of each class for the image.
#
# So the top class ID can be found with argmax. But how can we know what class this actually is and in particular if that class ID in the ImageNet dataset denotes a military uniform or something else?

# + colab={} colab_type="code" id="rgXb44vt6goJ"
predicted_class = np.argmax(result[0], axis=-1)
predicted_class

# + [markdown] colab_type="text" id="YrxLMajMoxkf"
# ## Decode the predictions
#
# To see what our predicted_class is in the ImageNet dataset, download the ImageNet labels and fetch the row that the model predicted.

# + colab={} colab_type="code" id="ij6SrDxcxzry"
labels_path = tf.keras.utils.get_file('ImageNetLabels.txt','https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
imagenet_labels = np.array(open(labels_path).read().splitlines())

plt.imshow(grace_hopper)
plt.axis('off')
predicted_class_name = imagenet_labels[predicted_class]
_ = plt.title("Prediction: " + predicted_class_name.title())

# + [markdown] colab_type="text" id="a6TNYYAM4u2-"
# Bingo. Our model correctly predicted miliatry uniform!

# + [markdown] colab_type="text" id="amfzqn1Oo7Om"
# # Part 2: Use a TensorFlow Hub models for the Cats vs. Dogs dataset

# + [markdown] colab_type="text" id="K-nIpVJ94xrw"
# Now we'll use the full MobileNet model and see how it can perform on the Dogs vs. Cats dataset. 

# + [markdown] colab_type="text" id="Z93vvAdGxDMD"
# ## Dataset
#
# We can use TensorFlow Datasets to load the Dogs vs Cats dataset. 

# + colab={} colab_type="code" id="DrIUV3V0xDL_"
splits = tfds.Split.ALL.subsplit(weighted=(80, 20))

splits, info = tfds.load('cats_vs_dogs', with_info=True, as_supervised=True, split = splits)

(train_examples, validation_examples) = splits

num_examples = info.splits['train'].num_examples
num_classes = info.features['label'].num_classes

# + [markdown] colab_type="text" id="UlFZ_hwjCLgS"
# The images in the Dogs vs. Cats dataset are not all the same size.

# + colab={} colab_type="code" id="W4lDPkn2cpWZ"
for i, example_image in enumerate(train_examples.take(3)):
  print("Image {} shape: {}".format(i+1, example_image[0].shape))


# + [markdown] colab_type="text" id="mbgpD3E6gM2P"
# So we need to reformat all images to the resolution expected by MobileNet (224, 224)
#
# The `.repeat()` and `steps_per_epoch` here is not required, but saves ~15s per epoch, since the shuffle-buffer only has to cold-start once.

# + colab={} colab_type="code" id="we_ftzQxNf7e"
def format_image(image, label):
  image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES))/255.0
  return  image, label

BATCH_SIZE = 32

train_batches      = train_examples.shuffle(num_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
validation_batches = validation_examples.map(format_image).batch(BATCH_SIZE).prefetch(1)

# + [markdown] colab_type="text" id="0gTN7M_GxDLx"
# ## Run the classifier on a batch of images

# + [markdown] colab_type="text" id="O3fvrZR8xDLv"
# Remember our `model` object is still the full MobileNet model trained on ImageNet, so it has 1000 possible output classes.
# ImageNet has a lot of dogs and cats in it, so let's see if it can predict the images in our Dogs vs. Cats dataset.
#

# + colab={} colab_type="code" id="kii_jWZYOn0B"
image_batch, label_batch = next(iter(train_batches.take(1)))
image_batch = image_batch.numpy()
label_batch = label_batch.numpy()

result_batch = model.predict(image_batch)

predicted_class_names = imagenet_labels[np.argmax(result_batch, axis=-1)]
predicted_class_names

# + [markdown] colab_type="text" id="QmvSWg9nxDLa"
# The labels seem to match names of Dogs and Cats. Let's now plot the images from our Dogs vs Cats dataset and put the ImageNet label next to them.

# + colab={} colab_type="code" id="IXTB22SpxDLP"
plt.figure(figsize=(10,9))
for n in range(30):
  plt.subplot(6,5,n+1)
  plt.imshow(image_batch[n])
  plt.title(predicted_class_names[n])
  plt.axis('off')
_ = plt.suptitle("ImageNet predictions")

# + [markdown] colab_type="text" id="JzV457OXreQP"
# # Part 3: Do simple transfer learning with TensorFlow Hub
#
# Let's now use TensorFlow Hub to do Transfer Learning.
#
# With transfer learning we reuse parts of an already trained model and change the final layer, or several layers, of the model, and then retrain those layers on our own dataset.
#
# In addition to complete models, TensorFlow Hub also distributes models without the last classification layer. These can be used to easily do transfer learning. We will continue using MobileNet v2 because in later parts of this course, we will take this model and deploy on a mobile device using [TensorFlow Lite](https://www.tensorflow.org/lite). Any [image feature vector url from tfhub.dev](https://tfhub.dev/s?module-type=image-feature-vector&q=tf2) would work here. 
#
# We'll also continue to use the Dogs vs Cats dataset, so we will be able to compare the performance of this model against the ones we created from scratch earlier.
#
# Note that we're calling the partial model from TensorFlow Hub (without the final classification layer) a `feature_extractor`. The reasoning for this term is that it will take the input all the way to a layer containing a number of features. So it has done the bulk of the work in identifying the content of an image, except for creating the final probability distribution. That is, it has extracted the features of the image.

# + colab={} colab_type="code" id="5wB030nezBwI"
URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
feature_extractor = hub.KerasLayer(URL,
                                   input_shape=(IMAGE_RES, IMAGE_RES,3))

# + [markdown] colab_type="text" id="pkSvAPvKOWg2"
# Let's run a batch of images through this, and see the final shape. 32 is the number of images, and 1280 is the number of neurons in the last layer of the partial model from TensorFlow Hub.

# + colab={} colab_type="code" id="Of7i-35F09ls"
feature_batch = feature_extractor(image_batch)
print(feature_batch.shape)

# + [markdown] colab_type="text" id="CtFmF7A5E4tk"
# Freeze the variables in the feature extractor layer, so that the training only modifies the final classifier layer.

# + colab={} colab_type="code" id="Jg5ar6rcE4H-"
feature_extractor.trainable = False

# + [markdown] colab_type="text" id="RPVeouTksO9q"
# ## Attach a classification head
#
# Now wrap the hub layer in a `tf.keras.Sequential` model, and add a new classification layer.

# + colab={} colab_type="code" id="mGcY27fY1q3Q"
model = tf.keras.Sequential([
  feature_extractor,
  layers.Dense(2, activation='softmax')
])

model.summary()

# + [markdown] colab_type="text" id="OHbXQqIquFxQ"
# ## Train the model
#
# We now train this model like any other, by first calling `compile` followed by `fit`.

# + colab={} colab_type="code" id="3n0Wb9ylKd8R"
model.compile(
  optimizer='adam', 
  loss='sparse_categorical_crossentropy',
  metrics=['accuracy'])

EPOCHS = 6
history = model.fit(train_batches,
                    epochs=EPOCHS,
                    validation_data=validation_batches)

# + [markdown] colab_type="text" id="76as-K8-vFQJ"
# You can see we get ~97% validation accuracy, which is absolutely awesome. This is a huge improvement over the model we created in the previous lesson, where we were able to get ~83% accuracy. The reason for this difference is that MobileNet was carefully designed over a long time by experts, then trained on a massive dataset (ImageNet).
#
# Although not equivalent to TensorFlow Hub, you can check out how to create MobileNet in Keras [here](https://github.com/keras-team/keras-applications/blob/master/keras_applications/mobilenet.py).
#
# Let's plot the training and validation accuracy/loss graphs.

# + colab={} colab_type="code" id="d28dhbFpr98b"
acc = history.history['acc']
val_acc = history.history['val_acc']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

# + [markdown] colab_type="text" id="5zmoDisGvNye"
# What is a bit curious here is that validation performance is better than training performance, right from the start to the end of execution.
#
# One reason for this is that validation performance is measured at the end of the epoch, but training performance is the average values across the epoch.
#
# The bigger reason though is that we're reusing a large part of MobileNet which is already trained on Dogs and Cats images. While doing training, the network is still performing image augmentation on the training images, but not on the validation dataset. This means the training images may be harder to classify compared to the normal images in the validation dataset.

# + [markdown] colab_type="text" id="kb__ZN8uFn-D"
# ## Check the predictions
#
# To redo the plot from before, first get the ordered list of class names.

# + colab={} colab_type="code" id="W_Zvg2i0fzJu"
class_names = np.array(info.features['label'].names)
class_names

# + [markdown] colab_type="text" id="4Olg6MsNGJTL"
# Run the image batch through the model and comvert the indices to class names.

# + colab={} colab_type="code" id="fCLVCpEjJ_VP"
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

# + [markdown] colab_type="text" id="CkGbZxl9GZs-"
# Let's look at the true labels and predicted ones.

# + colab={} colab_type="code" id="nL9IhOmGI5dJ"
print("Labels: ", label_batch)
print("Predicted labels: ", predicted_ids)

# + colab={} colab_type="code" id="wC_AYRJU9NQe"
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)")

StarcoderdataJupyterScriptsDedupFiltered