Project 4

In this project, you will modeling and predicting dog breed data.

  1. Download the data files and set up your coding environment
  2. Run a CNN using tensorflow on the images found in the dogs folder
  3. Save your model at the end of your code using model.save("model.keras")
  4. The test images are found in the test_dogs file folder, test these images with the model.keras
  5. The code to use the test images with is found below
  6. Submit your model.keras file to Gradescope
  7. Gradescope is going to load your model.keras file

Link to Gradescope assignment

Test function:

import os
import numpy as np
import tensorflow as tf 


def test_model(img_height, img_width, class_names, model):
    print("********* Predicting dog breed for test images *********")
    for filename in os.listdir("test_dogs"):
        if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
                print(filename)
                img = tf.keras.utils.load_img("test_dogs/" + filename, 
                                        target_size=(img_height, img_width))

                img_array = tf.keras.utils.img_to_array(img)
                img_array = tf.expand_dims(img_array, 0) # create a batch

                predictions = model.predict(img_array)
                score = tf.nn.softmax(predictions[0])

                print(
                "This image most likely belongs to {} with a {:.2f} percent confidence."
                .format(class_names[np.argmax(score)], 100 * np.max(score))
                )

def main():
    img_height = 180
    img_width = 180
    class_names = ['Basset Hound', 'Boxer', 'Chihuahua', 'English Cocker Spaniel', 'Great Pyrenees', 'Japanese Chin', 'Shiba Inu', 'Yorkshire Terrier']

    model = tf.keras.models.load_model('model.keras')

    # test the model
    test_model(img_height, img_width, class_names, model)

main()