Skip to content

Commit

Permalink
new content and updates for test images datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
erinmgraham committed Oct 6, 2023
1 parent 385652e commit f69644e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
4 changes: 3 additions & 1 deletion episodes/05-evaluate-predict-cnn.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Is the CINIC-10 model a good test data set? Hint: Read the 'Details' and 'Constr

:::::::::::::::::::::::: solution

No! "The entirety of the original CIFAR-10 test set is within the above mentioned new test set."
No! "The entirety of the original CIFAR-10 test set is within the above mentioned new test set."

Make sure the images you use for test have not been used to train!

:::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down
55 changes: 40 additions & 15 deletions episodes/scripts/predict.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 15 16:27:34 2023
Evaluate a Convolutional Neural Network and Make Predictions (Classifications)
@author: jc140298
"""
from tensorflow import keras
import pandas as pd
import matplotlib.pyplot as plt

from tensorflow.keras.utils import load_img
from tensorflow.keras.utils import img_to_array
# load weights for your best model if not still in memory
model_dropout = keras.models.load_model('C:/Users/jc140298/Documents/QCIF/SwC/20230316_ML_AI/scripts/outputs_cinic10/model_dropout.h5')

# load a new image and prepare it to match cifar10 dataset
new_img_pil = load_img("01_Jabiru_TGS.JPG", target_size=(32,32)) # Image format
new_img_arr = img_to_array(new_img_pil) # convert to array for analysis
new_img_reshape = new_img_arr.reshape(1, 32, 32, 3) # reshape into single sample
new_img_float = new_img_reshape.astype('float64') / 255.0 # normalize
# recreate test_images from 'image-data.py' if not still in memory

# predict the classname
result = model.predict(new_img_float) # make prediction
print(result) # probability for each class
print(class_names[result.argmax()]) # class with highest probability
# check correct model is loaded
print('We are using ', model_dropout.name, '\n')

# check test image dataset is loaded
print('The test image dataset has the shape: ', test_images.shape)

# test set
y_pred = model.predict(test_images)
# predict the class labels
result = model_dropout.predict(test_images)

# use our current best model to predict probability of each class on new test set
predicted_prob = model_dropout.predict(test_images)

# create a dictionary to convert from string labels to numeric labels
label_str_int_map = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
#TODO

# convert probability predictions to table using class names for column names
prediction_df = pd.DataFrame(predicted_prob, columns=class_names)

# inspect
print(prediction_df.head())

# now find the maximum probability for each image
predicted_labels = predicted_prob.argmax(axis=1)

### Step 8. Measuring Performance

# plot the predicted versus the true class
plt.plot(label_str_int_map[test_labels], predicted_labels)
plt.xlabel('Test Class')
plt.ylabel('Predicted Class')
plt.xlim(0, 9)
plt.ylim(0, 9)
#plt.axline(xy1=(0,0), xy2=(9,9), linestyle='--')
plt.show()

0 comments on commit f69644e

Please sign in to comment.