<jupyter_start><jupyter_text>Now load the saved model and re-run the same frog-or-not tests Please make sure you have run the previous notebood and saved the model otherwise this notebook won't work.<jupyter_code>from tensorflow import keras

model = keras.models.load_model('saved_model2/frog_model')<jupyter_output><empty_output><jupyter_text>Done!<jupyter_code>model.summary()<jupyter_output>Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_3 (Conv2D)            (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 4, 4, 64)          36928     
_________________________________________________________________
flatten_1 (Flatten)          (None, 1024)              0         
______________________________________________________[...]<jupyter_text>Test with outside images Load the images and display them<jupyter_code>from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
from pathlib import Path

img_dir = Path('internet_images')

internet_frog = Image.open(img_dir / Path('frog.jpg'))
internet_merlion = Image.open(img_dir / Path('merlion.jpg'))
internet_kermit = Image.open(img_dir / Path('kermit.jpg'))

plt.figure(figsize=(15,15))
plt.subplot(1,3,1)
plt.imshow(internet_frog)
plt.subplot(1,3,2)
plt.imshow(internet_merlion)
plt.subplot(1,3,3)
plt.imshow(internet_kermit)
plt.show()<jupyter_output><empty_output><jupyter_text>Convert images to fit the input requirements of the AI and create shortcut functionsWe re-use the previous code here<jupyter_code>import numpy as np

internet_frog_small = internet_frog.resize((32,32))
internet_merlion_small = internet_merlion.resize((32,32))
internet_kermit_small = internet_kermit.resize((32,32))


new_imgs = np.stack([np.asarray(internet_frog_small),
                     np.asarray(internet_merlion_small),
                     np.asarray(internet_kermit_small)],
                    axis=0)

def normalise(in_img):
    # Assume 0 to max.
    out_img = in_img / in_img.max()
    return out_img


def infer_img(img):

    for i, im in enumerate(img):
        plt.figure(figsize=(4, 2))

        plt.xticks([])
        plt.yticks([])
        plt.imshow(im, cmap=plt.cm.binary)
        plt.show()

        output = model.predict(img[i:i+1])
        if output[0,0] > output[0,1]:
            is_frog = False
        else:
            is_frog = True

        print(f'Inference: {output}: Is frog? {is_frog}')


out = infer_img(normalise(new_imgs))<jupyter_output><empty_output><jupyter_text>That's great, but I want to test it against a different image from google... OK- can.  We need to make the pipeline clear.<jupyter_code># Replace this with whatever image link you like
img_address = 'https://media.nature.com/w700/magazine-assets/d41586-017-03864-w/d41586-017-03864-w_15072998.jpg'

import requests
test_imgfile = Path('frog_downloads') / Path('test_image.png')

# -------- DOWNLOAD IMAGE
response = requests.get(img_address)

# -------- SAVE IMAGE
file = open(test_imgfile, "wb")
file.write(response.content)
file.close()

# ----------  DISPLAY IMAGE
test_img = Image.open(test_imgfile)
plt.imshow(test_img)
plt.show()

# ----------  RESIZE IMAGE
new_size = (32, 32)
test_img = test_img.resize(new_size)

# ----------  FORMAT DATA
new_imgs = np.stack([np.asarray(test_img),], axis=0)

# -----------  INFER WITH AI
out = infer_img(normalise(new_imgs))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12788676434070026, 'fraction_numerical': 0.034765333995530175, 'mean_word_length': 3.8471720818291217, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '34867', 'license': 'MIT', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1255, 'n_words': 271, 'path': '03_Train_a_CNN/Part 2 - Load Frog Model.ipynb', 'repo_name': 'olinickalls/R4-Teaching', 'size': '4027'}