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

# + colab={"base_uri": "https://localhost:8080/"} id="FhHJcvK8QV4Y" outputId="5366ea86-b175-42fd-87a7-2eaa460d24c3"
import tensorflow as tf
print(tf.__version__)

# + [markdown] id="Z8KAKynxQV4e"
# # Saving and loading models

# + [markdown] id="08vh_EHeQV4f"
#  ## Coding tutorials
#  #### [1. Saving and loading model weights](#coding_tutorial_1)
#  #### [2. Model saving criteria](#coding_tutorial_2)
#  #### [3. Saving the entire model](#coding_tutorial_3)
#  #### [4. Loading pre-trained Keras models](#coding_tutorial_4)
#  #### [5. Tensorflow Hub modules](#coding_tutorial_5)

# + [markdown] id="psJljMPDQV4g"
# ***
# <a id="coding_tutorial_1"></a>
# ## Saving and loading model weights

# + [markdown] id="I1vw5dC7QV4h"
# #### Load and inspect CIFAR-10 dataset

# + [markdown] id="WmRAfrLnQV4h"
# The CIFAR-10 dataset consists of, in total, 60000 color images, each with one of 10 labels: airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck. For an introduction and a download, see [this link](https://www.cs.toronto.edu/~kriz/cifar.html).

# + colab={"base_uri": "https://localhost:8080/"} id="tYaEDcT3QV4j" outputId="4dbc371e-c62c-4755-bd18-53f807aff6d8"
# Import the CIFAR-10 dataset and rescale the pixel values

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0

# Use smaller subset -- speeds things up
x_train = x_train[:10000]
y_train = y_train[:10000]
x_test = x_test[:1000]
y_test = y_test[:1000]

# + colab={"base_uri": "https://localhost:8080/", "height": 78} id="2NFSQBPtQV4n" outputId="c1cd2a12-aa6b-4d0e-9ed0-f9f7fadda6f6"
# Plot the first 10 CIFAR-10 images

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 10, figsize=(10, 1))
for i in range(10):
    ax[i].set_axis_off()
    ax[i].imshow(x_train[i])


# + [markdown] id="8yXDKUOLQV4r"
# #### Introduce two useful functions

# + id="6vP4aH9fQV4s"
# Introduce function to test model accuracy

def get_test_accuracy(model, x_test, y_test):
    test_loss, test_acc = model.evaluate(x=x_test, y=y_test, verbose=0)
    print('accuracy: {acc:0.3f}'.format(acc=test_acc))


# + id="ecZD5RluQV4w"
# Introduce function that creates a new instance of a simple CNN

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

def get_new_model():
    model = Sequential([
        Conv2D(filters=16, input_shape=(32, 32, 3), kernel_size=(3, 3), 
               activation='relu', name='conv_1'),
        Conv2D(filters=8, kernel_size=(3, 3), activation='relu', name='conv_2'),
        MaxPooling2D(pool_size=(4, 4), name='pool_1'),
        Flatten(name='flatten'),
        Dense(units=32, activation='relu', name='dense_1'),
        Dense(units=10, activation='softmax', name='dense_2')
    ])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model


# + [markdown] id="_4L6ZINMQV40"
# #### Create simple convolutional neural network classifier

# + colab={"base_uri": "https://localhost:8080/"} id="gy8aByLEQV41" outputId="520ad78c-7591-44bb-ec95-276aa439cf83"
# Create an instance of the model and show model summary

model = get_new_model()
model.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="D0cUFG--QV44" outputId="51ca8a41-3a6b-483d-e49b-076f55aeab45"
# Test accuracy of the untrained model, around 10% (random)

get_test_accuracy(model, x_test, y_test)


# + colab={"base_uri": "https://localhost:8080/"} id="rp36ATF81mRB" outputId="a694657f-992e-48ea-a8fc-e0aec817ef08"

get_test_accuracy(model, x_test, y_test)

# + [markdown] id="BKs65rmrQV47"
# #### Train model with checkpoints

# + id="80y5otJhQV48"
from tensorflow.keras.callbacks import ModelCheckpoint

# + id="7jHrMg2gQV4_"
# Create Tensorflow checkpoint object
filepath = 'checkpoint/model'
checkpoint = ModelCheckpoint(filepath,
                             monitor = 'val_accuracy',
                             save_best_only = True,
                             mode = 'max')

# + colab={"base_uri": "https://localhost:8080/"} id="whLx85o8QV5C" outputId="77be383f-369b-4772-c902-9d33d42775c1"
# Fit model, with simple checkpoint which saves (and overwrites) model weights every epoch

history = model.fit(x = x_train, y = y_train, validation_split=0.1, epochs = 10, callbacks = [checkpoint])

# + id="D209jNsjQV5E"
# Have a look at what the checkpoint creates



# + id="h-pNaG0SQV5H"
# Evaluate the performance of the trained model



# + [markdown] id="WUfpiZPbQV5J"
# #### Create new model, load weights

# + id="lTea2mN3QV5L"
# Create a new instance of the (initialised) model, accuracy around 10% again

model1 = get_new_model()

# + colab={"base_uri": "https://localhost:8080/"} id="jek222rgQV5O" outputId="c4a74cbc-0e4d-430a-b469-5f7dfebef64f"
# Load weights -- accuracy is the same as the trained model
from tensorflow.keras.models import load_model
model2 = load_model(filepath)

model2.summary()
get_test_accuracy(model2, x_test, y_test)

# + [markdown] id="z1IlV3jlQV5R"
# #### Clear directory

# + id="x8rPJLyHQV5R"
# ! rm -r model_checkpoints

# + [markdown] id="hAytIX3jQV5U"
# ***
# <a id="coding_tutorial_2"></a>
# ## Model saving criteria

# + [markdown] id="nybq0atkQV5U"
# #### Create more customised checkpoint

# + id="C_cRCxneQV5V"
from tensorflow.keras.callbacks import ModelCheckpoint

# + id="241WckCvQV5X"
# Create Tensorflow checkpoint object with epoch and batch details

checkpoint_5000_path = 'model_checkpoints_5000/checkpoint_{epoch:03d}_{batch:04d}'

checkpoint_5000 = ModelCheckpoint(filepath = checkpoint_5000_path,
                                 save_weights_only = True,
                                 save_freq = 5000,
                                 verbose = 1)

checkpoint_5000

# + id="ZKzsgIMjQV5b"
# Create and fit model with checkpoint

model = get_new_model()

model.fit(x_train, y_train, validation_data = (x_test, y_test), epochs = 3, batch_size = 10, callbacks = [checkpoint_5000])

# + id="wc7zjxp2QV5d"
# Have a look at what the checkpoint creates



# + [markdown] id="_pme6veJQV5f"
# #### Work with model saving criteria

# + id="CjMa5HOOQV5g"
# Use tiny training and test set -- will overfit!

x_train = x_train[:100]
y_train = y_train[:100]
x_test = x_test[:100]
y_test = y_test[:100]

# + id="sxawGcUDQV5i"
# Create a new instance of untrained model

model = get_new_model()

# + id="OpnNVn2sQV5l"
# Create Tensorflow checkpoint object which monitors the validation accuracy
from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint_best_path = 'model_best_checkpoints/checkpoint'

checkpoint_best = ModelCheckpoint(filepath = checkpoint_best_path,
                                 save_best_only = True,
                                 save_weights_only = True,
                                  save_freq = 'epoch',
                                  verbose = 1,
                                  monitor = 'val_accuracy')

# + id="Zu0lO1m_QV5o"
# Fit the model and save only the weights with the highest validation accuracy

history = model.fit(x = x_train,
                   y=y_train,
                   epochs=50,
                   validation_data=(x_test,y_test),
                   batch_size=10,
                   callbacks = [checkpoint_best],
                   verbose=1)

# + id="85DHia85QV5r"
# Plot training and testing curves

import pandas as pd

df = pd.DataFrame(history.history)
df.plot(y=['accuracy', 'val_accuracy'])

# + id="MKsAfSmfQV5v"
# Inspect the checkpoint directory



# + id="Xrx5i7bcQV5y"
# Create a new model with the saved weights

new_model = get_new_model()
new_model.load_weights(checkpoint_best_path)
# -

get_test_accuracy(model, x_test, y_test)
get_test_accuracy(new_model, x_test, y_test)

# + [markdown] id="bNF93Fu_QV52"
# #### Clear directory

# + id="SerdaweWQV53"
# ! rm -r model_checkpoints_5000 model_checkpoints_best

# + [markdown] id="p2dNGwLzQV55"
# ***
# <a id="coding_tutorial_3"></a>
# ## Saving the entire model

# + [markdown] id="e0pP6oW1QV57"
# #### Create checkpoint that saves whole model, not just weights

# + id="S3ROR9KfQV58"
from tensorflow.keras.callbacks import ModelCheckpoint

# + id="VvacO7lDQV5-"
# Create Tensorflow checkpoint object



# + id="RhmnWVElQV6A"
# Create and fit model with checkpoint



# + [markdown] id="-CGRXB0rQV6C"
# #### Inspect what the checkpoint has created

# + id="Wtl3cHhLQV6D"
# Have a look at what the checkpoint creates



# + id="7okHv-mqQV6F"
# Enter variables directory



# + id="53_Xuy0pQV6J"
# Get the model's test accuracy



# + [markdown] id="4lc0c5GhQV6R"
# #### Create new model from scratch

# + id="NsakDucMQV6S"
# Delete model



# + id="YvAxxOnHQV6U"
from tensorflow.keras.models import load_model

# + id="LXDBpAg3QV6X"
# Reload model from scratch



# + [markdown] id="T0-NSEAlQV6c"
# #### Use the .h5 format to save model

# + id="nOKto4IkQV6c"
# Save the model in .h5 format



# + id="7C0vfEnWQV6e"
# Inspect .h5 file



# + id="G2uU17ozQV6g"
# Delete model



# + id="B9dtUhsjQV6i"
# Reload model from scratch



# + [markdown] id="-ca32hu5QV6l"
# #### Clear directory

# + id="ftRw7KksQV6m"
# ! rm -r model_checkpoints
# ! rm my_model.h5

# + [markdown] id="CIrP42vlQV6p"
# ***
# <a id="coding_tutorial_4"></a>
# ## Loading pre-trained Keras models

# + [markdown] id="dJIEw9d_QV6q"
# #### Import and build Keras ResNet50 model
#
# Today we'll be using the ResNet50 model designed by a team at Microsoft Research, available through Keras applications. Please see the description on the [Keras applications page](https://keras.io/applications/#resnet) for details. If you continue using it, please cite it properly! The paper it comes from is:
#
# <NAME>, <NAME>, <NAME>, <NAME>. "Deep Residual Learning for Image Recognition", 2015.
#
# In the coding tutorial on Coursera, this model is loaded directly from disk. On Colab, you will load the model using the Keras API.

# + id="i9GUZVHiQV6q"
from tensorflow.keras.applications import ResNet50
model = ResNet50(weights='imagenet')

# + [markdown] id="kRej5bQdQV6v"
# #### Import and preprocess 3 sample images

# + id="N7_mAS06ROOG"
# Retrieve the image files

# !wget -q -O lemon.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1JSgQ9qgi9nO9t2aGEk-zA6lzYNUT9vZJ"
# !wget -q -O viaduct.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1sQzMKmyCR5Tur19lP3n1IIlEMG_o6Mct"
# !wget -q -O water_tower.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1cPAQD1O6mAiMbg0fmG5HIk8OuO_BSC6J"

# + id="bZ8MTLLqQV6w"
# Import 3 sample ImageNet images

from tensorflow.keras.preprocessing.image import load_img

lemon_img = load_img('lemon.jpg', target_size=(224, 224))
viaduct_img = load_img('viaduct.jpg', target_size=(224, 224))
water_tower_img = load_img('water_tower.jpg', target_size=(224, 224))

# + [markdown] id="_aK33Lj-QV6y"
# #### Use ResNet50 model to classify images

# + id="XQXTBltYQV6z"
# Useful function: presents top 5 predictions and probabilities

from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import pandas as pd

def get_top_5_predictions(img):
    x = img_to_array(img)[np.newaxis, ...]
    x = preprocess_input(x)
    preds = decode_predictions(model.predict(x), top=5)
    top_preds = pd.DataFrame(columns=['prediction', 'probability'],
                             index=np.arange(5)+1)
    for i in range(5):
        top_preds.loc[i+1, 'prediction'] = preds[0][i][1]
        top_preds.loc[i+1, 'probability'] = preds[0][i][2] 
    return top_preds

# + [markdown] id="_fAaFrecQV64"
# ##### Image 1: lemon

# + id="e3mc6ukbQV64"
# Display image



# + id="6ytQGmivQV67"
# Display top 5 predictions



# + [markdown] id="84aRsXzIQV69"
# ##### Image 2: viaduct

# + id="CIUQ4SouQV6-"
# Display image



# + id="tSy1QNxZQV7A"
# Display top 5 predictions



# + [markdown] id="TByZ1C2bQV7B"
# ##### Image 3: water tower

# + id="RhkcV3TtQV7C"
# Display image



# + id="pQBVCRH6QV7D"
# Display top 5 predictions



# + [markdown] id="w3k4XIb9QV7F"
# ***
# <a id="coding_tutorial_5"></a>
# ## Tensorflow Hub modules

# + [markdown] id="VX3o8DULQV7F"
# #### Import and build Tensorflow Hub MobileNet v1 model
#
# Today we'll be using Google's MobileNet v1 model, available on Tensorflow Hub. Please see the description on the [Tensorflow Hub page](https://tfhub.dev/google/imagenet/mobilenet_v1_050_160/classification/4) for details on it's architecture, how it's trained, and the reference. If you continue using it, please cite it properly! The paper it comes from is:
#
# <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>: "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications", 2017.
#
# In the coding tutorial on Coursera, this model is loaded directly from disk. On Colab, you will load the model from TensorFlow Hub.

# + id="3IKZUe3GQV7G"
import tensorflow_hub as hub

# + id="ZizaX_mqQV7K"
# Build Google's Mobilenet v1 model

module_url = "https://tfhub.dev/google/imagenet/mobilenet_v1_050_160/classification/4"
model = Sequential([hub.KerasLayer(module_url)])
model.build(input_shape=[None, 160, 160, 3])

# + [markdown] id="ASOUEQuyQV7N"
# #### Use MobileNet model to classify images

# + id="3d5jycF8Q6YH"
# Retrieve the image files

# !wget -q -O lemon.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1JSgQ9qgi9nO9t2aGEk-zA6lzYNUT9vZJ"
# !wget -q -O viaduct.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1sQzMKmyCR5Tur19lP3n1IIlEMG_o6Mct"
# !wget -q -O water_tower.jpg --no-check-certificate "https://docs.google.com/uc?export=download&id=1cPAQD1O6mAiMbg0fmG5HIk8OuO_BSC6J"

# + id="W7u6LUc9QV7N"
# Import and preprocess 3 sample ImageNet images

from tensorflow.keras.preprocessing.image import load_img

lemon_img = load_img("lemon.jpg", target_size=(160, 160))
viaduct_img = load_img("viaduct.jpg", target_size=(160, 160))
water_tower_img = load_img("water_tower.jpg", target_size=(160, 160))

# + id="0XLfrTUUQV7P"
# Read in categories text file

with open('data/imagenet_categories.txt') as txt_file:
    categories = txt_file.read().splitlines()

# + id="hfbCaoShQV7R"
# Useful function: presents top 5 predictions

import pandas as pd

def get_top_5_predictions(img):
    x = img_to_array(img)[np.newaxis, ...] / 255.0
    preds = model.predict(x)
    top_preds = pd.DataFrame(columns=['prediction'],
                             index=np.arange(5)+1)
    sorted_index = np.argsort(-preds[0])
    for i in range(5):
        ith_pred = categories[sorted_index[i]]
        top_preds.loc[i+1, 'prediction'] = ith_pred
            
    return top_preds

# + [markdown] id="5PQnzp0SQV7T"
# ##### Image 1: lemon

# + id="29pUkAKaQV7T"


# + id="7X9val1IQV7V"


# + [markdown] id="nq22XagQQV7Y"
# ##### Image 2: viaduct

# + id="pI0wl0ZxQV7Y"


# + id="XKr8sPJ9QV7a"


# + [markdown] id="rj_qOPFIQV7b"
# ##### Image 3: water tower

# + id="3wjj0sBrQV7b"


# + id="wB_nL9AAQV7d"


StarcoderdataJupyterScriptsDedupFiltered