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)
= tf.keras.utils.load_img("test_dogs/" + filename,
img =(img_height, img_width))
target_size
= tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # create a batch
img_array
= model.predict(img_array)
predictions = tf.nn.softmax(predictions[0])
score
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
format(class_names[np.argmax(score)], 100 * np.max(score))
.
)
def main():
= 180
img_height = 180
img_width = ['Basset Hound', 'Boxer', 'Chihuahua', 'English Cocker Spaniel', 'Great Pyrenees', 'Japanese Chin', 'Shiba Inu', 'Yorkshire Terrier']
class_names
= tf.keras.models.load_model('model.keras')
model
# test the model
test_model(img_height, img_width, class_names, model)
main()
Project 4
In this project, you will modeling and predicting dog breed data.
- Download the data files and set up your coding environment
- Run a CNN using tensorflow on the images found in the
dogs
folder - Save your model at the end of your code using
model.save("model.keras")
- The test images are found in the
test_dogs
file folder, test these images with themodel.keras
- The code to use the test images with is found below
- Submit your
model.keras
file to Gradescope - Gradescope is going to load your
model.keras
file
Test function: